Learn R Programming

⚠️There's a newer version (0.8.6) of this package.Take me there.

title: "MazamaSpatialUtils" pagetitle: MazamaSpatialUtils

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 by MazamaScience to regularize our work with spatial data. The sp, rgdal and maptools packages have made it much easier to work with spatial data found in shapefiles. 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 defacto 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 defacto 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 we can use in various projects. Along with three built-in datasets, this package provides convert~() functions for other spatial datasets that we currently use. These convert functions all follow the same recipe:

  • download spatial data in shapefile format into a standard directory
  • convert shapefile data into a sp SpatialPolygonsDataFrame
  • modify the dataframe in the @data slot 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 (>= 3.1.0) and RStudio so make sure you have those installed first.

Users can use the devtools package to install the latest version of the package which may have new features that are not yet available on CRAN:

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

Spatial Datasets

Package Datasets

The package comes with the following simplified spatial spatial datasets:

 * 276K	data/SimpleCountries.RData
 * 2.1M	data/SimpleCountriesEEZ.RData
 * 1.1M	data/SimpleTimezones.RData

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

Core Datasets

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

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

# Tell the package about this location
setSpatialDataDir('~/Data/Spatial')

# Install core spatial data
installSpatialData()

Datasets included in the core set include:

 * 2.1M EEZCountries.RData
 *  15M NaturalEarthAdm1.RData
 *  61M OSMTimezones.RData
 * 3.0M	OSMTimezones_05.RData
 * 3.6M TMWorldBorders.RData
 *  48MTerrestrialEcoregions.RData
 * 3.5M TerrestrialEcoregions_05.RData
 * 7.5M USCensus115thCongress.RData
 *  17M USCensusCounties.RData
 * 4.6M USCensusStates.RData
 * 1.2M USIndianLands.RData
 *  17M WorldTimezones.RData

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

Additional Datasets

Mazama Science regularly generates new datasets that adhere to package standards. These can be download manually from http://data.mazamascience.com/MazamaSpatialUtils/Spatial/. As of Jan 10, 2019, the full list of available datasets includes:

 * 24K	CA_AirBasins_01.RData
 * 44K	CA_AirBasins_02.RData
 * 100K	CA_AirBasins_05.RData
 * 2.1M	CA_AirBasins.RData
 * 2.2M	EEZCountries.RData
 * 404K	GACC_05.RData
 * 7.0M	GACC.RData
 * 15M	NaturalEarthAdm1.RData
 * 3.1M	OSMTimezones_05.RData
 * 62M	OSMTimezones.RData
 * 3.6M	TerrestrialEcoregions_05.RData
 * 49M	TerrestrialEcoregions.RData
 * 3.7M	TMWorldBorders.RData
 * 7.6M	USCensus115thCongress.RData
 * 564K	USCensusCBSA_01.RData
 * 944K	USCensusCBSA_02.RData
 * 2.0M	USCensusCBSA_05.RData
 * 34M	USCensusCBSA.RData
 * 2.3M	USCensusCounties.RData
 * 3.5M	USCensusStates.RData
 * 1.2M	USIndianLands.RData
 * 769M	WBDHU10.RData
 * 1.5G	WBDHU12.RData
 * 424K	WBDHU2_01.RData
 * 840K	WBDHU2_02.RData
 * 38M	WBDHU2.RData
 * 1.1M	WBDHU4_01.RData
 * 2.2M	WBDHU4_02.RData
 * 108M	WBDHU4.RData
 * 1.4M	WBDHU6_01.RData
 * 2.8M	WBDHU6_02.RData
 * 137M	WBDHU6.RData
 * 295M	WBDHU8.RData
 * 18M	WorldTimezones.RData

Examples

Vignettes

The package vignette 'Introduction to MazamaSpatialUtils' has numerous examples.

Demos

There are three demos associated with the package:

demo(package = 'MazamaSpatialUtils')

Shiny App

There is also an exampe R Shiny app which uses the WBDHU# datasets combines two large datasets:

  • location data from the National Bridge Inventory
  • shapefiles from the Watershed Boundary Dataset

The app allows you to aggregate point location data by watershed to create summary values associated with each watershed. It also demonstrates the need to enable caching in a shiny app when plots take a long time to generate.

library(MazamaSpatialUtils)
setSpatialDataDir('~/Data/Spatial')
runExample()

Mapshaper

Instructions for installing the javascript mapshaper utility and using it to simplify large shapefiles are found in the localMapshaper/ directory.


This project is supported by Mazama Science.

Copy Link

Version

Install

install.packages('MazamaSpatialUtils')

Monthly Downloads

783

Version

0.7.6

License

GPL-2

Issues

Pull Requests

Stars

Forks

Maintainer

Jonathan Callahan

Last Published

September 15th, 2021

Functions in MazamaSpatialUtils (0.7.6)

convertEEZCountries

Convert Exclusive Economic Zones countries shapefile
dissolve

Aggregate shapes in a SpatialPolygonsDataFrame
convertTMWorldBorders

Convert World Borders Shapefile
countryToCode

Convert country names to country codes
convertStateLegislativeDistricts

Convert US state legislative districts shapefile
getHUC

Return HUCs at specified locations
getCountryName

Return country names at specified locations
convertEPARegions

Convert EPA Region shapefiles
getVariable

Return SPDF variable at specified locations
getUSCounty

Return US county name at specified locations
convertLayer

Convert Shapefile Layer to Spatial Polygon Dataframe
convertNWSFireZones

Convert NWS Public Forecast Zones Shapefile
convertNaturalEarthAdm1

Convert Level 1 (State) Borders Shapefile
convertUSCensusCounties

Convert US county borders shapefile
US_countyCodes

Dataframe of US state codes
convertUSCensusStates

Convert US Census State Shapefile
convertOSMTimezones

Convert OSM Timezone Shapefile
getCountry

Return country names at specified locations
convertGACC

Convert Geographic Area Coordination Center shapefile
US_stateConversion

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

Convert US Census Urban Areas shapefiles
convertGADM

Convert Global Administrative Areas (GADM) SPDF
getCountryCode

Return country ISO codes at specified locations
getHUCName

Return HUC names at specified locations
convertWikipediaTimezoneTable

Convert Wikipedia Timezone Table to Dataframe
getPolygonID

Get polygonID from SPDF of interest
codeToState

Convert state codes to state nnames
convertTMWorldBordersSimple

Convert (Simple) World Borders Shapefile
convertCARBAirBasins

Convert California Air Resources Board basin shapefiles
convertWorldTimezones

Convert Timezone Shapefile
getStateCode

Return state ISO codes at specified locations
installSpatialData

Install spatial datasets
getState

Return state names at specified locations
iso3ToIso2

Convert from ISO3 to ISO2 country codes
convertTerrestrialEcoregions

Convert Terrestrial Ecoregion Shapefile
loadSpatialData

Load spatial datasets
convertWBDHUC

Convert USGS hydrologic unit shapefiles
convertUSIndianLands

Convert Indian Lands Shapefile
getStateName

Return state names at specified locations
codeToCountry

Convert country codes to country names
getTimezone

Return Olson timezones at specified locations
convertUSCensusCBSA

Convert US Core Based Statistical Areas Shapefile
convertWeatherZones

Convert NWS Public Forecast Zones Shapefile.
getSpatialData

Return spatial data associated with a set of locations
getSpatialDataDir

Get package data directory
installedSpatialData

List locally installed spatial datasets
iso2ToIso3

Convert from ISO2 to ISO3 country codes
subsetHUC

Subset pre-formatted HUC files into smaller groupings.
convertUSCensusCongress

Convert US congressional districts shapefile
organizePolygons

Organize ungrouped polygons
summarizeByPolygon

Summarize values by polygon
simplify

Simplify SpatialPolygonsDataFrame
installSpatialData_0.6

Install version 0.6 spatial datasets
%>%

Pipe operator
removeSpatialDataDir

Remove package data directory
stateToCode

Convert state names to state codes
setSpatialDataDir

Set package data directory
SimpleCountriesEEZ

Simplified spatial dataset of EEZ/country combined boundaries.
US_countyConversion

Conversion functions for US county names and FIPS codes.
MazamaSpatialUtils

Mazama Science spatial data and utility functions.
US_stateCodes

Dataframe of US state codes
CONUS

CONUS state codes
US_52

US state codes
SimpleTimezones

Simplified spatial dataset of world timezones.
SimpleCountries

Simplified spatial dataset of country boundaries.
SpatialDataDir

Directory for spatial data