8.2 Geographic Data Packages
Many R packages provide convenient interfaces to spatial libraries or geoportals
Package | Description |
---|---|
osmdata |
small OpenStreetMap datasets. |
osmextract |
large OpenStreetMap datasets. |
geodata |
Download and import imports administrative, elevation, WorldClim data. |
rnaturalearth |
ropensci Natural Earth |
rnoaa |
(NOAA) climate data |
tidycensus |
US Demographics |
tigris |
US maps |
cancensus |
Canada Demographics |
eurostat |
EU |
giscoR |
EU |
idbr |
International Databases |
bcdata |
Province of British Columbia |
geobr |
Brazil |
RCzechia |
Czechia |
GSODR |
Global Summary Weather Data |
Each data package has its own syntax for accessing data. See the package vignettes, and in many cases, textbooks.
8.2.1 Example 1: rnaturalearth
This package recently changed maintainers and must be updated for dependencies:
library(rnaturalearth)
<- ne_countries(country = "United States of America") # United States borders
usa class(usa)
[1] "SpatialPolygonsDataFrame"
attr(,"package")
[1] "sp"
By default rnaturalearth
returns objects of class Spatial*
. The result can be converted into an sf objects with st_as_sf()
.
8.2.2 Example 2: geodata
downloads a series of rasters containing global monthly precipitation sums with spatial resolution of ten minutes (~18.5km at the equator)
library(geodata)
<- worldclim_global("prec", res = 10, path = tempdir())
worldclim_prec class(worldclim_prec)
[1] "SpatRaster"
attr(,"package")
[1] "terra"
8.2.3 Example 3: osmdata
OpenStreetMap is a vast global database of crowd-sourced data
library(osmdata)
<- opq(bbox = "chicago il") |>
parks add_osm_feature(key = "leisure", value = "park") |>
osmdata_sf()
A limitation with the osmdata package is that it is rate limited. Use osmextract
to cache .pbf files.
The data source and wider OSM ecosystems have many advantages:
they provide datasets that are available globally, free of charge, and constantly improving thanks to an army of volunteers.
Using OSM encourages ‘citizen science’ and contributions back to the digital commons
8.2.4 Built-in datasets
these four methods make the world
data shapefile available
library(spData) # loads all spData datasets
data(world, package = "spData")
= spData::world # retrieves world from spData
world2 = read_sf(system.file("shapes/world.gpkg", package = "spData")) # retrieves world from spData world3
8.2.5 Geocoding
Another way to obtain spatial information is to transform a description of a location, usually an address, into its coordinates.
Send a query to an online service and getting the location as a result.
Recommends tidygeocoder
for the variety of services available with a consistent interface.
Also allows performing the opposite process,reverse geocoding to get a set of information (name, address, etc.) based on coordinates.