1

I have to shapefiles 2 Simple feature collections in R (one for districts and one for regions) and I need to combine them into a single GeoJSON FeatureCollection in R with different property names (such that I can filter the data layers later in a MapBox map, see https://stackoverflow.com/a/74615216/10624798). For example, one layer might give the population per district and the other the population per region. This seems related to: How to make a GeometryCollection in GeoJSON with a single point + polygon?

This is what I have so far

library(sf)
library(dplyr)
library(sfheaders)
library(geojsonio)

path_to_data <- "Ghana_261"

sf_use_s2(FALSE)

districts <-
  read_sf(path_to_data) %>% 
  select(DIST_NAME = Name, REGION = Pcode) %>% 
  st_make_valid()  %>% 
  sf_remove_holes()

regions <-
  districts %>%  
  group_by(REGION) %>% 
  summarise() %>%
  sf_remove_holes()

plot(regions)

# how do I combine these into a single geojson?
districts_json <- geojson_json(districts)
regions_json <- geojson_json(regions)

Data can be downloaded here: https://drive.google.com/file/d/1fOzxnA_vaOJi97EVPAMlquegAaoCW2Ow/view?usp=sharing

L Smeets
  • 888
  • 4
  • 17

1 Answers1

0

Combining two {sf} objects to one can be done easily with dplyr::bind_rows() - you are building on the facts that sf objects are modified data frames, and data frames can be (subject to some assumptions) row bound.

The "some assumptions" are:

  • both objects have to have exactly the same column structure
  • both objects have to have exactly the same Coordinate Reference System

As your use case calls for building both from a single source (with a different level of spatial aggregation) the CRS should not cause a problem.

As for the column structure I suggest having as few columns as possible, ideally only an id (+ implicit geometry column).

For an example in action consider this piece of code, built on the trusty old nc.shp that ships with {sf} package.

library(sf)
library(dplyr)


shape <- st_read(system.file("shape/nc.shp", package="sf")) # included with sf package

# 100 rows for all counties
districts <- shape %>% 
  select(id = NAME) # geometry is seleted implicitly

# 1 row for entire NC state
regions <- shape %>% 
  group_by(id = "NC state") %>% # this will be new name
  summarise()

# merge two objects to one - 101 rows with multipolygon geometry
output <- districts %>% 
  bind_rows(regions)

# save result as a geojson
st_write(output, "output.geojson")
Jindra Lacko
  • 7,814
  • 3
  • 22
  • 44