如何组合两个具有相同 CRS 和非重叠区域的 sf 对象?
How to combine two sf objects with the same CRS and non-overlapping area?
假设我有两个 sf
对象,它们具有相同的坐标参考系统和非重叠区域,如何正确地将这两个 sf
对象组合成一个 sf
对象?
这是一个例子。
# Load packages
library(tidyverse)
library(sf)
# Load the example sf object
nc <- st_read(system.file("shape/nc.shp", package="sf"))
# print the first three rows of nc in the console
head(nc, 3)
# Simple feature collection with 3 features and 14 fields
# geometry type: MULTIPOLYGON
# dimension: XY
# bbox: xmin: -81.74107 ymin: 36.23388 xmax: -80.43531 ymax: 36.58965
# epsg (SRID): 4267
# proj4string: +proj=longlat +datum=NAD27 +no_defs
# AREA PERIMETER CNTY_ CNTY_ID NAME FIPS FIPSNO CRESS_ID BIR74 SID74 NWBIR74 BIR79 SID79 NWBIR79 geometry
# 1 0.114 1.442 1825 1825 Ashe 37009 37009 5 1091 1 10 1364 0 19 MULTIPOLYGON (((-81.47276 3...
# 2 0.061 1.231 1827 1827 Alleghany 37005 37005 3 487 0 10 542 3 12 MULTIPOLYGON (((-81.23989 3...
# 3 0.143 1.630 1828 1828 Surry 37171 37171 86 3188 5 208 3616 6 260 MULTIPOLYGON (((-80.45634 3...
nc
是一个sf
对象,也是一个数据框。 dplyr
或 tidyverse
中的大多数函数在 sf
对象上运行良好。下面,我使用 slice
函数将 nc
对象拆分为 nc1
和 nc2
.
# Split the nc object to two sf objects
nc1 <- nc %>% slice(1:50)
nc2 <- nc %>% slice(51:100)
# Print the crs of nc, nc1, and nc2
st_crs(nc)
# Coordinate Reference System:
# EPSG: 4267
# proj4string: "+proj=longlat +datum=NAD27 +no_defs"
st_crs(nc1)
# Coordinate Reference System:
# EPSG: 4267
# proj4string: "+proj=longlat +datum=NAD27 +no_defs"
st_crs(nc2)
# Coordinate Reference System:
# EPSG: 4267
# proj4string: "+proj=longlat +datum=NAD27 +no_defs"
可以看到,nc
、nc1
、nc2
都是同一个坐标系,nc1
和nc1
之间没有重叠区域nc2
.
由于 sf
对象是 data frames
,我首先想到的是组合两个 data frames
是使用 dplyr
包中的 bind_rows
。然而,bind_rows
给了我一个警告。
# Combine nc1 and nc2 with bind_rows
nc_combine <- bind_rows(nc1, nc2)
# Warning messages:
# 1: In bind_rows_(x, .id) :
# Vectorizing 'sfc_MULTIPOLYGON' elements may not preserve their attributes
# 2: In bind_rows_(x, .id) :
# Vectorizing 'sfc_MULTIPOLYGON' elements may not preserve their attributes
新对象 nc_combine
仍然是一个 sf
对象和一个 data frame
,具有与 nc
相同的行号和列号。但是,我无法访问nc_combine
中的任何信息,坐标参考信息似乎也不见了。
class(nc_combine)
# [1] "sf" "data.frame"
dim(nc_combine)
# [1] 100 15
nc_combine
# Error in .subset2(x, i, exact = exact) :
# attempt to select less than one element in get1index
换句话说,我的问题是,如何结合nc1
和nc2
重新创建nc
对象?
这个 rbind
函数自 oooh 1990 年以来一直在 R 中:
> rbind(nc1,nc2)
Simple feature collection with 100 features and 14 fields
geometry type: MULTIPOLYGON
dimension: XY
bbox: xmin: -84.32385 ymin: 33.88199 xmax: -75.45698 ymax: 36.58965
epsg (SRID): 4267
proj4string: +proj=longlat +datum=NAD27 +no_defs
First 10 features:
[etc]
我不知道为什么 bind_rows
不起作用。
假设我有两个 sf
对象,它们具有相同的坐标参考系统和非重叠区域,如何正确地将这两个 sf
对象组合成一个 sf
对象?
这是一个例子。
# Load packages
library(tidyverse)
library(sf)
# Load the example sf object
nc <- st_read(system.file("shape/nc.shp", package="sf"))
# print the first three rows of nc in the console
head(nc, 3)
# Simple feature collection with 3 features and 14 fields
# geometry type: MULTIPOLYGON
# dimension: XY
# bbox: xmin: -81.74107 ymin: 36.23388 xmax: -80.43531 ymax: 36.58965
# epsg (SRID): 4267
# proj4string: +proj=longlat +datum=NAD27 +no_defs
# AREA PERIMETER CNTY_ CNTY_ID NAME FIPS FIPSNO CRESS_ID BIR74 SID74 NWBIR74 BIR79 SID79 NWBIR79 geometry
# 1 0.114 1.442 1825 1825 Ashe 37009 37009 5 1091 1 10 1364 0 19 MULTIPOLYGON (((-81.47276 3...
# 2 0.061 1.231 1827 1827 Alleghany 37005 37005 3 487 0 10 542 3 12 MULTIPOLYGON (((-81.23989 3...
# 3 0.143 1.630 1828 1828 Surry 37171 37171 86 3188 5 208 3616 6 260 MULTIPOLYGON (((-80.45634 3...
nc
是一个sf
对象,也是一个数据框。 dplyr
或 tidyverse
中的大多数函数在 sf
对象上运行良好。下面,我使用 slice
函数将 nc
对象拆分为 nc1
和 nc2
.
# Split the nc object to two sf objects
nc1 <- nc %>% slice(1:50)
nc2 <- nc %>% slice(51:100)
# Print the crs of nc, nc1, and nc2
st_crs(nc)
# Coordinate Reference System:
# EPSG: 4267
# proj4string: "+proj=longlat +datum=NAD27 +no_defs"
st_crs(nc1)
# Coordinate Reference System:
# EPSG: 4267
# proj4string: "+proj=longlat +datum=NAD27 +no_defs"
st_crs(nc2)
# Coordinate Reference System:
# EPSG: 4267
# proj4string: "+proj=longlat +datum=NAD27 +no_defs"
可以看到,nc
、nc1
、nc2
都是同一个坐标系,nc1
和nc1
之间没有重叠区域nc2
.
由于 sf
对象是 data frames
,我首先想到的是组合两个 data frames
是使用 dplyr
包中的 bind_rows
。然而,bind_rows
给了我一个警告。
# Combine nc1 and nc2 with bind_rows
nc_combine <- bind_rows(nc1, nc2)
# Warning messages:
# 1: In bind_rows_(x, .id) :
# Vectorizing 'sfc_MULTIPOLYGON' elements may not preserve their attributes
# 2: In bind_rows_(x, .id) :
# Vectorizing 'sfc_MULTIPOLYGON' elements may not preserve their attributes
新对象 nc_combine
仍然是一个 sf
对象和一个 data frame
,具有与 nc
相同的行号和列号。但是,我无法访问nc_combine
中的任何信息,坐标参考信息似乎也不见了。
class(nc_combine)
# [1] "sf" "data.frame"
dim(nc_combine)
# [1] 100 15
nc_combine
# Error in .subset2(x, i, exact = exact) :
# attempt to select less than one element in get1index
换句话说,我的问题是,如何结合nc1
和nc2
重新创建nc
对象?
这个 rbind
函数自 oooh 1990 年以来一直在 R 中:
> rbind(nc1,nc2)
Simple feature collection with 100 features and 14 fields
geometry type: MULTIPOLYGON
dimension: XY
bbox: xmin: -84.32385 ymin: 33.88199 xmax: -75.45698 ymax: 36.58965
epsg (SRID): 4267
proj4string: +proj=longlat +datum=NAD27 +no_defs
First 10 features:
[etc]
我不知道为什么 bind_rows
不起作用。