[英]R geo: from points to SpatialPolygonsDataFrame
I am trying to create a SpatialPolygonDataFrame
starting from a data.frame with points, but I am not sure about the methodologies. 我正在尝试从带点的
SpatialPolygonDataFrame
创建一个SpatialPolygonDataFrame
,但是我不确定方法。 I succeeded in obtaining a list of spatial polygons, but failed to convert them in a SpatialPolygonsDataframe
. 我成功获取了空间多边形的列表,但未能在
SpatialPolygonsDataframe
其转换。
Here my data structure: 这是我的数据结构:
coordinates parcours_id tm_cid date_mesure tm_type tm_mnc tm_dbm
34268 (2.670962, 48.48172) 24680199 2017-07-04 01:38:36 lte 1 -113
34269 (2.670522, 48.48981) 24680199 2017-07-04 01:38:44 lte 1 -117
34270 (2.668994, 48.49398) 24657926 2017-07-04 01:38:59 lte 1 -116
34271 (2.668994, 48.49398) 24657926 2017-07-04 01:39:05 lte 1 -116
34272 (2.668069, 48.49653) 24657926 2017-07-04 01:39:17 lte 1 -116
34273 (2.667893, 48.5005) 24657926 2017-07-04 01:39:29 lte 1 -108
I want to create polygons by tm_cid. 我想通过tm_cid创建多边形。 I have tried to follow
?SpatialPolygons
我尝试遵循
?SpatialPolygons
polygons=c()
for (value in unique(mesures.sp$tm_cid)){
sub=subset(mesures.sp,mesures.sp$tm_cid==value)
x=Polygon(sub@coords)
polygons=c(polygons,x)
}
polygons=as.list(polygons)
#polygons my srl (list with Polygon Class Objects)
ID=unique(mesures.sp$tm_cid)
all_poly=c()
for(value in 1:length(polygons)){
poly=Polygons(polygons[value], ID[value])
all_poly=c(all_poly,poly)}
all_poly=as.list(all_poly)
#all_poly is my sr (list of object of class SpatialPolygons-class, shouldn't be a list but I failed to do otherwise)
spatPoly=c()
for(value in 1:length(polygons)){
spol=SpatialPolygons(all_poly[value])
spatPoly=c(spatPoly,spol)}
ID=as.data.frame(ID)
spdf=c()
for(value in 1:length(polygons)){
data=ID[value,1]
df=SpatialPolygonsDataFrame(spatPoly[[value]],data=data)
spdf=c(spdf,df)
}
I am getting this error: 我收到此错误:
Error in if (length(Sr@polygons) != nrow(data)) stop(paste("Object length mismatch:\\n ", : argument is of length zero
if(length(Sr @ polygons)!= nrow(data))stop(paste(“ Object length mismatch:\\ n”,:参数长度为零时出错
Any idea? 任何想法?
require(rgeos)
require(rgdal)
require(maptools)
(mesure.sp is a SpatialPontDataFrame
) (mesure.sp是
SpatialPontDataFrame
)
polygons=c()
for (value in unique(mesures.sp$tm_cid)){
sub=subset(mesures.sp,mesures.sp$tm_cid==value)
x=Polygon(sub@coords)
polygons=c(polygons,x)
}
polygons=as.list(polygons)
#polygons my srl (list with Polygon Class Objects)
ID=unique(mesures.sp$tm_cid)
all_poly=c()
for(value in 1:length(polygons)){
poly=Polygons(polygons[value], ID[value])
all_poly=c(all_poly,poly)}
all_poly=as.list(all_poly)
#all_poly is my sr (list of object of class SpatialPolygons-class, shouldn't be a list but I failed to do otherwise)
spatPoly=c()
for(value in 1:length(polygons)){
spol=SpatialPolygons(all_poly[value])
spol@proj4string=CRS("+proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0")
spatPoly=c(spatPoly,spol)}
spoly=spatPoly[[1]]
for(value in 2:length(spatPoly)){
spoly=c(spoly,spatPoly[[value]])
}
spoly=as.list(spoly)
spdf=c()
for(value in 1:length(polygons)){
data=as.data.frame(ID[value])
row.names(data)=data$ID
df=SpatialPolygonsDataFrame(spatPoly[[value]],data=data)
spdf=c(spdf,df)
}
final_spdf=spdf[[1]]
for(value in 2:length(spdf)){
final_spdf=spRbind(final_spdf,spdf[[value]])}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.