Reading Shapefiles in R using the GeoJSON API
Introduction
In this article, we will explore how to read shapefiles directly from a GeoJSON API in R. This approach eliminates the need to download shapefiles and reduces storage requirements. We will use the sf package, which provides an interface for working with simple features (SF) data.
Background
The sf package is part of the R Studio ecosystem and provides a convenient way to work with SF data. SF data is a type of vector data that represents geometric shapes such as points, lines, and polygons. The st_read function from the sf package allows us to read shapefiles directly from URLs.
Prerequisites
Before we begin, make sure you have R installed on your system. You will also need to install the sf package using the following command:
install.packages("sf")
Loading the Required Libraries and Data
First, we load the required libraries, including the sf package.
library(sf)
Next, we load the GeoJSON data from the specified URL using the st_read function.
data <- st_read('https://opendata.arcgis.com/datasets/01fd6b2d7600446d8af768005992f76a_3.geojson')
Quick View of the Data
To quickly view the data, we can use the mapview::mapview function.
mapview::mapview(data)
This will display a map of the shapefile data.
Reading Shapefiles from the GeoJSON API
Now that we have loaded the GeoJSON data, we can read it directly into an SF object using the st_read function. This approach eliminates the need to download shapefiles and reduces storage requirements.
data <- st_read('https://opendata.arcgis.com/datasets/01fd6b2d7600446d8af768005992f76a_3.geojson')
Fortifying the Shapefile
If the GeoJSON data is not already in a suitable format for SF processing, we may need to fortify it using the fortify function from the sf package.
shp <- fortify(data)
This step may be necessary depending on the structure of the GeoJSON data.
Creating a Map
Once we have read the shapefile and possibly fortified it, we can create a map using the ggplot2 package.
library(ggplot2)
NUTS_map <- ggplot() +
geom_polygon(data = shp,
aes(x = long, y = lat, group = group),
color = 'lightsteelblue', fill = 'lightcyan', size = .9) +
coord_fixed(1.7) + theme_void()
This code creates a map of the shapefile data.
Example Use Case
Here’s an example use case for reading shapefiles from the GeoJSON API:
# Load libraries
library(sf)
library(ggplot2)
# Load GeoJSON data
data <- st_read('https://opendata.arcgis.com/datasets/01fd6b2d7600446d8af768005992f76a_3.geojson')
# Fortify the shapefile (if necessary)
shp <- fortify(data)
# Create a map
NUTS_map <- ggplot() +
geom_polygon(data = shp,
aes(x = long, y = lat, group = group),
color = 'lightsteelblue', fill = 'lightcyan', size = .9) +
coord_fixed(1.7) + theme_void()
# Display the map
mapview::mapview(NUTS_map)
This code reads the shapefile from the GeoJSON API, fortifies it (if necessary), creates a map using ggplot2, and displays the map.
Conclusion
In this article, we explored how to read shapefiles directly from a GeoJSON API in R. We used the sf package to load the shapefile data and possibly fortified it using the fortify function. Finally, we created a map using the ggplot2 package. This approach eliminates the need to download shapefiles and reduces storage requirements.
Last modified on 2023-08-09