How to combine two sf objects with the same CRS and non-overlapping area?
Asked Answered
B

1

5

Assuming I have two sf objects with the same coordinate reference system and non-overlapping area, how to correctly combine these two sf objects to create one sf objects?

Here is an example.

# 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...

The nc is an sf object and also a data frame. Most of the functions from the dplyr or tidyverse work well on the sf object. Below, I used the slice function to split the nc object to nc1 and 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"

As you can see, nc, nc1, and nc2 all have the same coordinate-reference system, and there are no overlapping area between nc1 and nc2.

Since sf objects are data frames, my first thought to combine two data frames is to use bind_rows from the dplyr package. Howver, bind_rows gave me an warning.

# 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

The new object, nc_combine, is still an sf object and a data frame with the same row and column number as nc. However, I cannnot access any information in nc_combine, and the coordinate-reference information seems to be gone.

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

In other words, my question is, how to combine nc1 and nc2 to re-create the nc object?

Brod answered 23/3, 2019 at 17:20 Comment(0)
B
14

There's this rbind function which has been in R since oooh 1990-something:

> 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]

I don't know why bind_rows doesn't work.

Biforked answered 23/3, 2019 at 18:5 Comment(2)
Yes, I should have tested rbind first. Thanks.Brod
What do I do when rbind doesn't work because the two sfs have different number of columns?Misogyny

© 2022 - 2024 — McMap. All rights reserved.