R geo:从点到SpatialPolygonsDataFrame

时间:2017-07-06 11:15:37

标签: r spatial geo

我正在尝试从带有点的data.frame开始创建SpatialPolygonDataFrame,但我不确定这些方法。我成功获得了空间多边形列表,但未能在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(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)
    }

我收到此错误:

  

if(length(Sr @ polygons)!= nrow(data))中的错误停止(粘贴("对象)   长度不匹配:\ n",:参数长度为零

有什么想法吗?

1 个答案:

答案 0 :(得分: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(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]])}