在 rgeos 中简化多边形并在 SpatialPolygonsDataFrame 中维护数据
Simplifying polygons in rgeos and maintaining data in SpatialPolygonsDataFrame
背景
我对使用 gSimplify
function available through the rgeos
包简化多边形很感兴趣。
可重现的例子
可以使用以下代码生成可重现的示例:
# Data sourcing -----------------------------------------------------------
# Download an read US state shapefiles
tmp_shps <- tempfile()
tmp_dir <- tempdir()
download.file(
"http://www2.census.gov/geo/tiger/GENZ2014/shp/cb_2014_us_state_20m.zip",
tmp_shps
)
unzip(tmp_shps, exdir = tmp_dir)
# Libs
require(rgdal)
require(rgeos)
# Read
us_shps <- readOGR(dsn = tmp_dir, layer = "cb_2014_us_state_20m")
# Simplified --------------------------------------------------------------
# Simplifiy
us_shps_smpl <- gSimplify(spgeom = us_shps,
tol = 200,
topologyPreserve = TRUE)
预览
par(mfrow = c(2,1))
plot(us_shps_smpl, main = "Simplified")
plot(us_shps, main = "Original")
问题
除了简化多边形之外,gSimplify
函数更改了结果对象的 classes:
>> class(us_shps)
[1] "SpatialPolygonsDataFrame"
attr(,"package")
[1] "sp"
>> class(us_shps_smpl)
[1] "SpatialPolygons"
attr(,"package")
[1] "sp"
>> names(us_shps)
[1] "STATEFP" "STATENS" "AFFGEOID" "GEOID" "STUSPS" "NAME" "LSAD" "ALAND" "AWATER"
>> names(us_shps_smpl)
[1] "0" "1" "2" "3" "4" "5" "6" "7" "8" "9" "10" "11" "12" "13" "14" "15" "16" "17" "18" "19"
[21] "20" "21" "22" "23" "24" "25" "26" "27" "28" "29" "30" "31" "32" "33" "34" "35" "36" "37" "38" "39"
[41] "40" "41" "42" "43" "44" "45" "46" "47" "48" "49" "50" "51"
问题
如何安全地重新附加原始对象中最初可用的数据并将生成的 SpatialPolygons
对象转换为 SpatialPolygonsDataFrame
我认为一种方法只涉及 attaching data frame;但这取决于元素的顺序不变。还有其他更好的方法吗(最好保留初始对象class)?
sf
包完全基于数据框,因此其几何操作始终保留附加到每个要素的数据。该包还没有赶上 R 中的所有标准空间包,但是当您需要更多功能时,可以很容易地在 sf
和 sp
对象之间来回切换。
此处,st_simplify()
完成工作,但您需要先投影多边形:
library(sf)
# Download and read example data
tmp_shps <- tempfile()
tmp_dir <- tempdir()
download.file(
"http://www2.census.gov/geo/tiger/GENZ2014/shp/cb_2014_us_state_20m.zip",
tmp_shps
)
unzip(tmp_shps, exdir = tmp_dir)
us_shps <- st_read(paste(tmp_dir, "cb_2014_us_state_20m.shp", sep = "/"))
# st_simplify needs a projected CRS
us_shps_merc <- st_transform(us_shps, 3857)
simple_us_merc <- st_simplify(us_shps_merc)
# Change back to original CRS
simple_us <- st_transform(simple_us_merc, st_crs(us_shps))
# Change to sp object, if you like
simple_us_sp <- as(st_zm(simple_us), "Spatial")
背景
我对使用 gSimplify
function available through the rgeos
包简化多边形很感兴趣。
可重现的例子
可以使用以下代码生成可重现的示例:
# Data sourcing -----------------------------------------------------------
# Download an read US state shapefiles
tmp_shps <- tempfile()
tmp_dir <- tempdir()
download.file(
"http://www2.census.gov/geo/tiger/GENZ2014/shp/cb_2014_us_state_20m.zip",
tmp_shps
)
unzip(tmp_shps, exdir = tmp_dir)
# Libs
require(rgdal)
require(rgeos)
# Read
us_shps <- readOGR(dsn = tmp_dir, layer = "cb_2014_us_state_20m")
# Simplified --------------------------------------------------------------
# Simplifiy
us_shps_smpl <- gSimplify(spgeom = us_shps,
tol = 200,
topologyPreserve = TRUE)
预览
par(mfrow = c(2,1))
plot(us_shps_smpl, main = "Simplified")
plot(us_shps, main = "Original")
问题
除了简化多边形之外,gSimplify
函数更改了结果对象的 classes:
>> class(us_shps)
[1] "SpatialPolygonsDataFrame"
attr(,"package")
[1] "sp"
>> class(us_shps_smpl)
[1] "SpatialPolygons"
attr(,"package")
[1] "sp"
>> names(us_shps)
[1] "STATEFP" "STATENS" "AFFGEOID" "GEOID" "STUSPS" "NAME" "LSAD" "ALAND" "AWATER"
>> names(us_shps_smpl)
[1] "0" "1" "2" "3" "4" "5" "6" "7" "8" "9" "10" "11" "12" "13" "14" "15" "16" "17" "18" "19"
[21] "20" "21" "22" "23" "24" "25" "26" "27" "28" "29" "30" "31" "32" "33" "34" "35" "36" "37" "38" "39"
[41] "40" "41" "42" "43" "44" "45" "46" "47" "48" "49" "50" "51"
问题
如何安全地重新附加原始对象中最初可用的数据并将生成的
SpatialPolygons
对象转换为SpatialPolygonsDataFrame
我认为一种方法只涉及 attaching data frame;但这取决于元素的顺序不变。还有其他更好的方法吗(最好保留初始对象class)?
sf
包完全基于数据框,因此其几何操作始终保留附加到每个要素的数据。该包还没有赶上 R 中的所有标准空间包,但是当您需要更多功能时,可以很容易地在 sf
和 sp
对象之间来回切换。
此处,st_simplify()
完成工作,但您需要先投影多边形:
library(sf)
# Download and read example data
tmp_shps <- tempfile()
tmp_dir <- tempdir()
download.file(
"http://www2.census.gov/geo/tiger/GENZ2014/shp/cb_2014_us_state_20m.zip",
tmp_shps
)
unzip(tmp_shps, exdir = tmp_dir)
us_shps <- st_read(paste(tmp_dir, "cb_2014_us_state_20m.shp", sep = "/"))
# st_simplify needs a projected CRS
us_shps_merc <- st_transform(us_shps, 3857)
simple_us_merc <- st_simplify(us_shps_merc)
# Change back to original CRS
simple_us <- st_transform(simple_us_merc, st_crs(us_shps))
# Change to sp object, if you like
simple_us_sp <- as(st_zm(simple_us), "Spatial")