R - 将SpatialLines转换为唯一的SpatialPolygon

时间:2017-11-14 09:30:14

标签: r line polygon spatial

我有一个我想要转换为SpatialPolygons对象的SpatialLines对象。更确切地说,我的目标是将SpatiaLines(以下示例中的DataFrame)转换为单个多边形。

我找到的唯一解决方案是使用PBSmapping包。但我对这个包的PolySet概念并不十分舒服,结果并不完全令人满意。

所以,我正在寻找一种经典的方式'包(sprgeosrgdal)及其类。

这是我的代码:

library(sp) ; library(rgdal)
library(rgeos) ; library(maptools)

my_directory <- "" # fill with your own directory

# Load the data
my_map <- readOGR(dsn = paste0(my_directory, "Departements"),
                  layer = "LIMITE_DEPARTEMENT")

borders_map <- my_map [my_map $NATURE %in% c("Fronti\xe8re internationale",
                                             "Limite c\xf4ti\xe8re"), ]

class(borders_map) # a SpatialLinesDataFrame in this example

# Convert SpatialLinesDataFrame into SpatialPolygons
library(PBSmapping)

var_temp <- SpatialLines2PolySet(borders_map)
sp_try <- PolySet2SpatialPolygons(var_temp)

proj4string(sp_try) <- proj4string(borders_map)

class(sp_try)

plot(sp_try)

文件SPH&#34; Departements&#34;在这儿 : https://drive.google.com/file/d/1wksMnkufQPdOfSbhWoZbPKMqlcLcll_j

结果(特别是我不理解&#34;两个&#34;多边形的叠加):

ThePlot

1 个答案:

答案 0 :(得分:1)

我在DEPARTMENT数据集中读到,它是SpatialPolygon对象。之后,我将所有县合并为一个独特的Polygon。

my_map <- readOGR(dsn = paste0(my_directory, "Departements"),
                  layer = "DEPARTEMENT")
border <- unionSpatialPolygons(my_map, IDs=rep(1, 96))
plot(border)
str(border,2)

 Formal class 'SpatialPolygons' [package "sp"] with 4 slots
  ..@ polygons   :List of 1
  ..@ plotOrder  : int 1
  ..@ bbox       : num [1:2, 1:2] 99226 6049647 1242375 7110524
  .. ..- attr(*, "dimnames")=List of 2
  ..@ proj4string:Formal class 'CRS' [package "sp"] with 1 slot