r - 在 R 中提取形状文件对象的质心?

标签 r geospatial shapefile centroid

我有一个形状文件,上传到以下路径:

https://drive.google.com/open?id=0B1ITb_7lHh1EUFVfVWc4ekRfSnc

我使用“shapefiles”包中的“read.shapefiles”函数导入了数据:

landuse<- read.shapefile("landuse")

我现在需要提取 landuse 对象中所有形状的经/纬度质心并将其添加到 landuse$dbf 数据框

我尝试了两件事:

lu_df<-coordinates(landuse)
lu_df<-SpatialPoints(landuse)

两者都给了我以下错误:

Error in coordinates(as.data.frame(obj)) : 
  error in evaluating the argument 'obj' in selecting a method for function 'coordinates': Error in data.frame(record = 1L, content.length = 80L, shape.type = 5L,  : 
  arguments imply differing number of rows: 1, 4, 7

我不确定如何进行。

最佳答案

首先,我建议对空间数据使用 rgdal 包。 readOGRwriteOGR 函数提供了很好的 Shapefile 处理(如投影的输入和输出等)。

更新: 由于这不适用于 @Shaz(请参阅注释中的错误),因此使用了 maptools 函数 readShapePoly()。另见 this post .

针对您的问题:rgeos 包提供了一个函数 gCentroid(),它可以计算多边形的质心。解决方案看起来像这样:

setwd("/path/to/your/shapefile/")

library(maptools)
library(rgeos)
landuse <- readShapePoly("landuse") 

centr <- gCentroid(landuse, byid = TRUE)

# create SpatialPointsDataFrame to export via writeOGR
# positive side effect: All data from landuse@data joined to centr@data
centr <- SpatialPointsDataFrame(centr, data= landuse@data) 

writeOGR(centr, ".", "landuse_centroids", driver = "ESRI Shapefile")

关于r - 在 R 中提取形状文件对象的质心?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36673576/

相关文章:

r - 如何使用 Selenium 单击此按钮

重复 data.frame 的行 N 次

c# - 我可以覆盖 Entity Framework 查询生成器吗?

openlayers - 使用 Geoserver 和 Openlayers 的墨卡托投影世界地图

r - 在 R 中使用 contour() 制作 shapefile

r facet_wrap 未与 geom_point 正确分组

sql - 将 R randomForest 对象保存到 SQL 数据库

hadoop - 使用 s3 的 spark 加载 json 时 FS 错误

mongodb - Mongo从 CSV 中的独立纬度和经度列导入位置

python - 如何使用 python 查找 ESRI shapefile 中特定纬度/经度的信息?