【问题标题】:Export R plot to shapefile将 R 绘图导出到 shapefile
【发布时间】:2011-05-17 22:41:27
【问题描述】:

我对 R 相当陌生,但对 ArcView 不熟悉。我正在绘制一些双模式数据,并希望将绘图转换为 shapefile。具体来说,如果可能的话,我想转换顶点和边,以便在 ArcView 中显示相同的图以及属性。

我已经安装了“shapefiles”包,并且看到了 convert.to.shapefile 命令,但是帮助中没有提到如何将 XY 坐标分配给顶点。

谢谢,

提姆

【问题讨论】:

  • 您的数据目前是什么格式的?示例文件或dput() 将帮助我们回答您的问题。
  • 听起来你想要“线条”,但也许是“多边形”?请至少发布您描述的情节的图像,以及您运行生成它的代码。
  • @gsk3, @mdsumner 数据最初是一个包含两列的 CSV 文件。第一列是一个数字字段,具有代表一个人的唯一数字。第二列是表示事件的字符字段。将其转换为矩阵,然后是网络后,生成的图是由线连接的顶点,这就是我希望 shapefile 表示的内容。我无法对有关附加示例的帮助文件做出正面或反面,否则我会向您发送该图的 PDF。
  • 包含绘图的最简单方法是输出为 png 并在编辑帖子时附加图像。包含小对象的最简单方法是使用dput。包含大型对象的最简单方法是将它们上传到保管箱或某些 FTP 站点并链接到它们。

标签: r gis spatial


【解决方案1】:

好的,我在这里做了几个假设,但我阅读了这个问题,因为您希望将空间坐标分配给二分图并将顶点和边导出为点 shapefile 和折线以在 ArcGIS 中使用。

这个解决方案有点笨拙,但会生成坐标限制 xmin、ymin 和 xmax、ymax 为 -0.5 和 +0.5 的 shapefile。由您决定图形布局算法(例如 Kamada-Kawai),并根据 @gsk3 的建议将 shapefile 投影到所需的坐标系中。可以在创建 points.data 和 edge.data 数据框的位置添加顶点和边的附加属性。

library(igraph)
library(shapefiles)

# Create dummy incidence matrix
inc <- matrix(sample(0:1, 15, repl=TRUE), 3, 5)
colnames(inc) <- c(1:5) # Person ID
rownames(inc) <- letters[1:3] # Event 

# Create bipartite graph
g.bipartite <- graph.incidence(inc, mode="in", add.names=TRUE)

# Plot figure to get xy coordinates for vertices
tk <- tkplot(g.bipartite, canvas.width=500, canvas.height=500)
tkcoords <- tkplot.getcoords(1, norm=TRUE) # Get coordinates of nodes centered on 0 with +/-0.5 for max and min values

# Create point shapefile for nodes
n.points <- nrow(tkcoords)
points.attr <- data.frame(Id=1:n.points, X=tkcoords[,1], Y=tkcoords[,2])
points.data <- data.frame(Id=points.attr$Id, Name=paste("Vertex", 1:n.points, sep=""))
points.shp <- convert.to.shapefile(points.attr, points.data, "Id", 1)
write.shapefile(points.shp, "~/Desktop/points", arcgis=TRUE)


# Create polylines for edges in this example from incidence matrix
n.edges <- sum(inc) # number of edges based on incidence matrix
Id <- rep(1:n.edges,each=2) # Generate Id number for edges.
From.nodes <- g.bipartite[[4]]+1 # Get position of "From" vertices in incidence matrix
To.nodes <- g.bipartite[[3]]-max(From.nodes)+1 # Get position of "To" vertices in incidence matrix

# Generate index where position alternates between "From.node" to "To.node"
node.index <- matrix(t(matrix(c(From.nodes, To.nodes), ncol=2))) 

edge.attr <- data.frame(Id, X=tkcoords[node.index, 1], Y=tkcoords[node.index, 2])
edge.data <- data.frame(Id=1:n.edges, Name=paste("Edge", 1:n.edges, sep=""))
edge.shp <- convert.to.shapefile(edge.attr, edge.data, "Id", 3)
write.shapefile(edge.shp, "~/Desktop/edges", arcgis=TRUE)

希望这会有所帮助。

【讨论】:

【解决方案2】:

我将根据您的数据是什么样子的疯狂猜测来尝试一下。

基本上,您需要将数据强制转换为 data.frame,其中两列包含 x 和 y 坐标(或纬度/经度等)。

library(sp)
data(meuse.grid)
class(meuse.grid)
coordinates(meuse.grid) <- ~x+y
class(meuse.grid)

一旦你将它作为 SpatialPointsDataFrame,sp 提供了一些不错的功能,包括导出 shapefile:

writePointsShape(meuse.grid,"/home/myfiles/wherever/myshape.shp")

相关帮助文件示例来自:

  • 坐标
  • SpatialPointsDataFrame
  • 读取ShapePoints

至少在几年前,当我最后一次使用 sp 时,投影非常好,而将投影信息写入 shapefile 非常糟糕。所以最好保持坐标不变,并手动告诉 Arc 它是什么投影。或者使用writeOGR 而不是writePointsShape

【讨论】:

    猜你喜欢
    • 2012-12-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-28
    • 1970-01-01
    • 1970-01-01
    • 2015-03-21
    相关资源
    最近更新 更多