geojsonio vignette

Scott Chamberlain

2019-10-29

geojsonio converts geographic data to geojson and topojson formats. Nothing else. We hope to do this one job very well, and handle all reasonable use cases.

Functions in this package are organized first around what you’re working with or want to get, geojson or topojson, then convert to or read from various formats:

Each of the above functions have methods for various objects/classes, including numeric, data.frame, list, SpatialPolygons, SpatialLines, SpatialPoints, etc.

Additional functions:

Install

Install rgdal - in case you can’t get it installed from binary , here’s what works on a Mac (change to the version of rgdal and GDAL you have).

install.packages("http://cran.r-project.org/src/contrib/rgdal_1.1-3.tar.gz", repos = NULL, type="source", configure.args = "--with-gdal-config=/Library/Frameworks/GDAL.framework/Versions/1.11/unix/bin/gdal-config --with-proj-include=/Library/Frameworks/PROJ.framework/unix/include --with-proj-lib=/Library/Frameworks/PROJ.framework/unix/lib")

Stable version from CRAN

install.packages("geojsonio")

Development version from GitHub

devtools::install_github("ropensci/geojsonio")
library("geojsonio")

GeoJSON

Convert various formats to geojson

From a numeric vector of length 2

as json

as a list

From a data.frame

as json

as a list

From SpatialPolygons class

to json

to a list

From SpatialPoints class

to json

to a list

Write geojson

Read geojson

Topojson

To JSON

topojson_json(c(-99.74,32.45))

To a list

library(sp)
x <- c(1,2,3,4,5)
y <- c(3,2,5,1,4)
s <- SpatialPoints(cbind(x,y))
topojson_list(s)

Read from a file

file <- system.file("examples", "us_states.topojson", package = "geojsonio")
out <- topojson_read(file, verbose = FALSE)
summary(out)

Read from a URL

url <- "https://raw.githubusercontent.com/shawnbot/d3-cartogram/master/data/us-states.topojson"
out <- topojson_read(url, verbose = FALSE)

Or use as.location() first

(loc <- as.location(file))
out <- topojson_read(loc, verbose = FALSE)