Learn R Programming

A dedicated Slack channel has been created for announcements, support and to help build a community of practice around this open source package. You may request an invitation to join from jonathan.callahan@dri.com.

MazamaSpatialUtils

A suite of conversion functions to create internally standardized
spatial polygons data frames. Utility functions use these data sets to
return values such as country, state, time zone, watershed, etc. associated
with a set of longitude/latitude pairs. (They also make cool maps.)

Background

The MazamaSpatialUtils package was created to regularize work with spatial data. Many sources of shapefile data are available and can be used to make beautiful maps in R. Unfortunately, the data attached to these datasets, even when fairly complete, often lacks standardized identifiers such as the ISO 3166-1 alpha-2 encodings for countries. Maddeningly, even when these ISO codes are used, the dataframe column in which they are stored does not have a standardized name. It may be called "ISO" or "ISO2" or "alpha" or "COUNTRY" or any of a dozen other names we have seen.

While many mapping packages provide "natural" naming of countries, those who wish to develop operational, GIS-like systems need something that is both standardized and language-independent. The ISO 3166-1 alpha-2 encodings have emerged as the de facto standard for this sort of work. In similar fashion, ISO 3166-2 alpha-2 encodings are available for the next administrative level down -- state/province/oblast, etc. For time zones, the de facto standard is the set of Olson time zones used in all UNIX systems.

The main goal of this package is to create an internally standardized set of spatial data that can be used in various projects. Along with three built-in datasets, this package provides convert~() functions for other spatial datasets of interest. These convert functions all follow the same recipe:

  • download spatial data into a standard directory
  • convert spatial data into a sf simple features data frame
  • modify the dataframe so that it adheres to package internal standards

Other datasets can be added following the same procedure.

The 'package internal standards' are very simple.

  1. Every spatial dataset must contain the following columns:
  • polygonID -- unique identifier for each polygon
  • countryCode -- country at centroid of polygon (ISO 3166-1 alpha-2)
  1. Spatial datasets with time zone data must contain the following column:
  • timezone -- Olson timezone
  1. Spatial datasets at scales smaller than the nation-state should contain the following column:
  • stateCode -- 'state' at centroid of polygon (ISO 3166-2 alpha-2)

If other columns contain these data, those columns must be renamed or duplicated with the internally standardized name. This simple level of consistency makes it possible to generate maps for any data that is ISO encoded. It also makes it possible to create functions that return the country, state or time zone associated with a set of locations.

Installation

This package is designed to be used with R (>= 4.0) and RStudio so make sure you have those installed first.

Installation from CRAN is standard:

install.packages("MazamaSpatialUtils")

Or you can use the devtools package to install the latest version from GitHub:

devtools::install_github('mazamascience/MazamaSpatialUtils', build_vignettes=TRUE)

Spatial Datasets

Package Datasets

The package comes with the following simplified spatial spatial datasets:

 * 367K	data/SimpleCountries.rda
 * 1.3M	data/SimpleCountriesEEZ.rda
 * 1.4M	data/SimpleTimezones.rda

These datasets allow you to work with low-resolution country outlines and time zones.

Additional Datasets

Additional datasets are available at http://data.mazamascience.com/MazamaSpatialUtils/Spatial_0.8/ and can be loaded with the following commands:

# Create a location where spatial datasets will be stored
dir.create('~/Data/Spatial_0.8', recursive = TRUE)

# Tell the package about this location
setSpatialDataDir('~/Data/Spatial_0.8')

# Install spatial datasets by name
installSpatialData("EPARegions")

You can review your currently installed datasets by running:

installedSpatialData()

Further details about each dataset are provided in the source code of the associated convert~() function. Datasets appearing with, e.g., _05 are simplified datasets whose polygons retain only 5% of the vertices in the full resolution dataset.

Examples

Demos

There are three demos associated with the package:

demo(package = 'MazamaSpatialUtils')

Development of this R package has been supported with funding from the following institutions:

Questions regarding further development of the package or inclusion of additional datasets should be directed to jonathan.callahan@dri.edu.

Copy Link

Version

Install

install.packages('MazamaSpatialUtils')

Monthly Downloads

568

Version

0.8.6

License

GPL-2

Issues

Pull Requests

Stars

Forks

Last Published

September 6th, 2023

Functions in MazamaSpatialUtils (0.8.6)

convertEPARegions

Convert EPA Region shapefiles
countryToCode

Convert country names to country codes
convertLayer

Convert shapefile layer to simple features dataframe
loadSpatialData

Load spatial datasets
getCountryName

Return country names at specified locations
getSpatialData

Return spatial data associated with a set of locations
convertNaturalEarthAdm1

Convert Level 1 (state) borders shapefile
dissolve

Aggregate shapes in a simple features data frame
getStateName

Return state names at specified locations
convertTMWorldBorders

Convert world borders shapefile
getHUC

Return HUCs at specified locations
getTimezone

Return Olson timezones at specified locations
convertOSMTimezones

Convert OSM timezone shapefile
simplify

Simplify simple features data frame
convertWBDHUC

Convert USGS hydrologic unit geodatabase
convertWikipediaTimezoneTable

Convert Wikipedia timezone table to dataframe
.removeSpatialDataDir

Remove package data directory
convertUSCensusCounties

Convert US county borders shapefile
iso3ToIso2

Convert from ISO3 to ISO2 country codes
convertGACC

Convert Geographic Area Coordination Center shapefile
convertUSCensusCBSA

Convert US Core Based Statistical Areas shapefile
getStateCode

Return state ISO codes at specified locations
convertWeatherZones

Convert NWS Public Forecast Zones Shapefile.
getState

Return state names at specified locations
convertUSCensusStates

Convert US Census state shapefile
simplifyAndSave

Save simplified versions of a spatial features dataframe
getUSCounty

Return US county name at specified locations
getPolygonID

Get polygonID from SFDF of interest
convertUSCensusCongress

Convert US congressional districts shapefile
%>%

Pipe operator
getHUCName

Return HUC names at specified locations
getCountryCode

Return country ISO codes at specified locations
getVariable

Return SFDF variable at specified locations
getCountry

Return country names at specified locations
stateToCode

Convert state names to state codes
getSpatialDataDir

Get package data directory
installSpatialData

Install spatial datasets
iso2ToIso3

Convert from ISO2 to ISO3 country codes
summarizeByPolygon

Summarize values by polygon
installedSpatialData

List locally installed spatial datasets
setSpatialDataDir

Set package data directory
CONUS

CONUS state codes
US_countyConversion

Conversion functions for US county names and FIPS codes.
MazamaSpatialUtils

Mazama Science spatial data and utility functions.
US_stateConversion

Conversion functions for US state names, codes and FIPS codes.
codeToCountry

Convert country codes to country names
US_52

US state codes
SimpleTimezones

Simplified spatial dataset of world timezones.
SimpleCountries

Simplified spatial dataset of country boundaries.
US_countyCodes

Dataframe of US county codes
US_stateCodes

Dataframe of US state codes
SimpleCountriesEEZ

Simplified spatial dataset of EEZ/country combined boundaries.
codeToState

Convert state codes to state nnames
SpatialDataDir

Directory for spatial data
convertEEZCountries

Convert Exclusive Economic Zones countries shapefile