7.3 Geographic data packages
A multitude of R packages have been developed for accessing geographic data, some of which are presented in Table 7.1.These provide interfaces to one or more spatial libraries or geoportals and aim to make data access even quicker from the command line.
Package | Description |
---|---|
getlandsat | Provides access to Landsat 8 data. |
osmdata | Download and import of OpenStreetMap data. |
raster | getData() imports administrative, elevation, WorldClim data. |
rnaturalearth | Access to Natural Earth vector and raster data. |
rnoaa | Imports National Oceanic and Atmospheric Administration (NOAA) climate data. |
rWBclimate | Access World Bank climate data. |
It should be emphasised that Table 7.1 represents only a small number of available geographic data packages.Other notable packages include GSODR, which provides Global Summary Daily Weather Data in R (see the package’s README for an overview of weather data sources);tidycensus and tigris, which provide socio-demographic vector data for the USA; and hddtools, which provides access to a range of hydrological datasets.
Each data package has its own syntax for accessing data.This diversity is demonstrated in the subsequent code chunks, which show how to get data using three packages from Table 7.1.Country borders are often useful and these can be accessed with the ne_countries()
function from the rnaturalearth package as follows:
library(rnaturalearth)
usa = ne_countries(country = "United States of America") # United States borders
class(usa)
#> [1] "SpatialPolygonsDataFrame"
#> attr(,"package")
#> [1] "sp"
# alternative way of accessing the data, with raster::getData()
# getData("GADM", country = "USA", level = 0)
By default rnaturalearth returns objects of class Spatial
.The result can be converted into an sf
objects with st_as_sf()
as follows:
usa_sf = st_as_sf(usa)
A second example downloads a series of rasters containing global monthly precipitation sums with spatial resolution of ten minutes.The result is a multilayer object of class RasterStack
.
library(raster)
worldclim_prec = getData(name = "worldclim", var = "prec", res = 10)
class(worldclim_prec)
#> [1] "RasterStack"
#> attr(,"package")
#> [1] "raster"
A third example uses the osmdata package (Padgham et al. 2018) to find parks from the OpenStreetMap (OSM) database.As illustrated in the code-chunk below, queries begin with the function opq()
(short for OpenStreetMap query), the first argument of which is bounding box, or text string representing a bounding box (the city of Leeds in this case).The result is passed to a function for selecting which OSM elements we’re interested in (parks in this case), represented by key-value pairs. Next, they are passed to the function osmdata_sf()
which does the work of downloading the data and converting it into a list of sf
objects (see vignette('osmdata')
for further details):
library(osmdata)
parks = opq(bbox = "leeds uk") %>%
add_osm_feature(key = "leisure", value = "park") %>%
osmdata_sf()
OpenStreetMap is a vast global database of crowd-sourced data and it is growing daily.Although the quality is not as spatially consistent as many official datasets, OSM data have many advantages: they are globally available free of charge and using crowd-source data can encourage ‘citizen science’ and contributions back to the digital commons.Further examples of osmdata in action are provided in Chapters 9, 12 and 13.
Sometimes, packages come with inbuilt datasets.These can be accessed in four ways: by attaching the package (if the package uses ‘lazy loading’ as spData does), with data(dataset)
, by referring to the dataset with pkg::dataset
or with system.file()
to access raw data files.The following code chunk illustrates the latter two options using the world
(already loaded by attaching its parent package with library(spData)
):31
world2 = spData::world
world3 = st_read(system.file("shapes/world.gpkg", package = "spData"))