我有一个形状文件,上传到以下路径:
https://drive.google.com/open?id=0B1ITb_7lHh1EUFVfVWc4ekRfSnc https://drive.google.com/open?id=0B1ITb_7lHh1EUFVfVWc4ekRfSnc
我使用“shapefiles”包中的“read.shapefiles”函数导入数据:
landuse<- read.shapefile("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
空间数据包。这readOGR
and writeOGR
函数提供了很好的 Shapefile 处理(如投影的输入和输出等)。
UPDATE:
由于这不适用于@Shaz(请参阅评论中的错误),maptools
功能readShapePoly()
被利用了。另请参阅这个帖子 https://stackoverflow.com/a/36674077/3250126.
针对您的问题: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")
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)