Learn R Programming

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

MazamaSpatialUtils

A suite of conversion scripts to create internally standardized spatial
polygons dataframes. Utility scripts use these datasets to return values such
as country, state, timezone, 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 beautfiul 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 aviailable for the next administrative level down – state/province/oblast, etc.. For timezones, the defacto standard is the set of Olson timezones 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. Every spatial dataset will have at least one of the following, consistently named colums of data:

  • polygonID – unique identifier associated with each polygon
  • countryCode – ISO 3166-1 alpha-2
  • stateCode – ISO 3166-2 alpha-2
  • timezone – Olson timezone

If another column contains this data, that column must be renamed or duplicated with the internally standardized name. This simple level of consistency makes it posisble to generate maps for any data that is ISO encoded. It also makes it possible to create functions that return the country, state or timezone 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 will want to install the devtools package to have access to latest versions of some packages that are not yet available on CRAN.

The following packages should be installed with devtools by typing the following at the RStudio console:

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

Data

Pre-generated .RData versions of the following standardized shapefiles are available at http://mazamascience.com/RData/Spatial/:

 * 2.1M	EEZCountries.RData
 *  15M	NaturalEarthAdm1.RData
 *  61M	OSMTimezones.RData
 * 3.0M	OSMTimezones_05.RData
 * 3.6M	TMWorldBorders.RData
 *  48M	TerrestrialEcoregions.RData
 * 3.5M	TerrestrialEcoregions_05.RData
 * 7.5M	USCensus115thCongress.RData
 * 2.2M	USCensusCounties.RData
 * 3.4M	USCensusStates.RData
 * 1.2M	USIndianLands.RData
 * 768M	WBDHU10.RData
 * 1.4G	WBDHU12.RData
 *  38M	WBDHU2.RData
 * 424K	WBDHU2_01.RData
 * 840K	WBDHU2_02.RData
 * 107M	WBDHU4.RData
 * 1.1M	WBDHU4_01.RData
 * 2.2M	WBDHU4_02.RData
 * 136M	WBDHU6.RData
 * 1.4M	WBDHU6_01.RData
 * 2.8M	WBDHU6_02.RData
 * 294M	WBDHU8.RData
 *  17M	WorldTimezones.RData

Examples

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


This project is supported by Mazama Science.

Copy Link

Version

Install

install.packages('MazamaSpatialUtils')

Monthly Downloads

864

Version

0.5.4

License

GPL-2

Maintainer

Jonathan Callahan

Last Published

September 18th, 2018

Functions in MazamaSpatialUtils (0.5.4)

MazamaSpatialUtils

Mazama Science spatial data and utility functions.
convertTerrestrialEcoregions

Convert Terrestrial Ecoregion Shapefile
convertIndianLands

Convert Indian Lands Shapefile
convertISOCodeTable

Convert ISO country code Table to Dataframe
convertTMWorldBordersSimple

Convert (Simple) World Borders Shapefile
convertUSCensusCongress

Convert US Congressional Disctricts Shapefile
convertUSCensusCounties

Convert US County Borders Shapefile
getSpatialData

Return Spatial Data Associated with a Set of Locations
getSpatialDataDir

Get Package Data Directory
iso3ToIso2

Convert Betweenrom ISO3 to ISO2 Country Codes
convertUSCensusCBSA

Convert US Core Based Statistical Areas Shapefile
getHUCName

Return HUC Names at Specified Locations
getPolygonID

Get PolygonID from SPDF of Interest
SimpleCountriesEEZ

World Country EEZ Polygons
SimpleTimezones

World Timezone Polygons
convertOSMTimezones

Convert OSM Timezone Shapefile
convertSimpleCountries

Convert (Simple) World Borders Shapefile
organizePolygons

Organize Ungrouped Polygons
convertWBDHUC

Convert USGS Hydrologic Unit Shapefiles
convertUSCensusStates

Convert US Census State Shapefile
getState

Return State Names at Specified Locations
setSpatialDataDir

Set Package Data Directory
getStateCode

Return State ISO Codes at Specified Locations
installSpatialData

Install Spatial Datasets
iso2ToIso3

Convert From ISO2 to ISO3 Country Codes
loadSpatialData

Load Spatial Datasets
convertGADM

Convert Global Administrative Areas (GADM) Shapefile
convertHMSSmoke

Convert NOAA Hazard Mapping System Smoke Shapefiles
convertSimpleCountriesEEZ

Convert (Simple) World Borders Shapefile
codeToState

Convert State Codes to State Names
convertSimpleTimezones

Convert SimpleTimezones Shapefile
summarizeByPolygon

Summarize values by polygon
convertEEZCountries

Convert Exclusive Economic Zones Countries Shapefile
convertWikipediaTimezoneTable

Convert Wikipedia Timezone Table to Dataframe
convertWorldTimezones

Convert Timezone Shapefile
convertWorldEEZ

Convert World Exclusive Economic Zones Boundaries Shapefile
countryToCode

Convert Country Names to Country Codes
getUSCounty

Return US County Name at Specified Locations
getCountry

Return Country Names at Specified Locations
SpatialDataDir

Directory for Spatial Data
codeToCountry

Convert Country Codes to Country Names
convertLayer

Convert Shapefile Layer to Spatial Polygon Dataframe
convertNaturalEarthAdm1

Convert Level 1 (State) Borders Shapefile
getCountryName

Return Country Names at Specified Locations
getVariable

Return SpatialDataframe Variable at Specified Locations
stateToCode

Convert State Names to State Codes
getCountryCode

Return Country ISO Codes at Specified Locations
subsetHUC

Subset pre-formatted HUC files into smaller groupings.
getHUC

Return HUCs at Specified Locations
getStateName

Return State Names at Specified Locations
getTimezone

Return Olson Timezones at Specified Locations
convertTMWorldBorders

Convert World Borders Shapefile
SimpleCountries

World Country Polygons