2017-07-06 106 views
0

我想創建一個SpatialPolygonDataFrame帶點的data.frame,但我不確定方法。我成功地獲得了空間多邊形列表,但未能在SpatialPolygonsDataframe中轉換它們。R幾何:從點到SpatialPolygonsDataFrame

這裏我的數據結構:

   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 

我想創建由tm_cid多邊形。我曾試圖按照?SpatialPolygons

polygons=c() 
    for (value in unique(mesures.sp$tm_cid)){ 
     sub=subset(mesures.sp,mesures.sp$tm_cid==value) 
     x=Polygon([email protected]) 
     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) 
    } 

我收到此錯誤:

Error in if (length([email protected]) != nrow(data)) stop(paste("Object length mismatch:\n ", : argument is of length zero

任何想法?

+0

添加你的答案作爲一個單獨的答案,而不是編輯的帖子!這樣你就可以幫助別人解決同樣的問題。 –

+0

我不認爲在編輯之前壓制答案並不好。我的電腦崩潰了,我來到這裏是因爲我失去了我所做的,而且我必須重做。我明白你的觀點,但下次請不要在評論被添加之前壓制答案。你這樣做的方式,可以完成沒有人得到答案。 – Oolis

+0

所有的修改都保存了,你可以在這裏查看https://stackoverflow.com/posts/44947371/revisions –

回答

0
require(rgeos) 
require(rgdal) 
require(maptools) 

(mesure.sp是SpatialPontDataFrame

polygons=c() 
    for (value in unique(mesures.sp$tm_cid)){ 
     sub=subset(mesures.sp,mesures.sp$tm_cid==value) 
     x=Polygon([email protected]) 
     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]) 
     [email protected]=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]])}