NOT_CRAN <- identical(tolower(Sys.getenv("NOT_CRAN")), "true") knitr::opts_chunk$set( collapse = TRUE, comment = "#>", warning = FALSE, message = FALSE, fig.width = 10, fig.path = "../man/figures/" ) library("knitr") hook_output <- knitr::knit_hooks$get("output") knitr::knit_hooks$set(output = function(x, options) { lines <- options$output.lines if (is.null(lines)) { return(hook_output(x, options)) # pass to default hook } x <- unlist(strsplit(x, "\n")) more <- "..." if (length(lines)==1) { # first n lines if (length(x) > lines) { # truncate the output, but add .... x <- c(head(x, lines), more) } } else { x <- c(if (abs(lines[1])>1) more else NULL, x[lines], if (length(x)>lines[abs(length(lines))]) more else NULL ) } # paste these lines together x <- paste(c(x, ""), collapse = "\n") hook_output(x, options) })
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:
geojson_list()
/topojson_list()
- convert to GeoJSON/TopoJSON as R list formatgeojson_json()
/topojson_json()
- convert to GeoJSON/TopoJSON as JSONgeojson_sp()
- convert output of geojson_list()
or geojson_json()
to spatial objectsgeojson_read()
/topojson_read()
- read a GeoJSON/TopoJSON file from file path or URLgeojson_write()
/topojson_write()
- write a GeoJSON/TopoJSON file locallyEach of the above functions have methods for various objects/classes, including numeric
, data.frame
, list
, SpatialPolygons
, SpatialLines
, SpatialPoints
, etc.
Additional functions:
map_gist()
- push up a geojson or topojson file as a GitHub gist (renders as an interactive map) - See the maps with geojsonio vignette.map_leaf()
- create a local interactive map with the leaflet
package - See the maps with geojsonio vignette.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
remotes::install_github("ropensci/geojsonio")
library("geojsonio")
From a numeric
vector of length 2
as json
geojson_json(c(32.45, -99.74))
as a list
geojson_list(c(32.45, -99.74))
From a data.frame
as json
library('maps') data(us.cities) geojson_json(us.cities[1:2, ], lat = 'lat', lon = 'long')
as a list
geojson_list(us.cities[1:2, ], lat = 'lat', lon = 'long')
From SpatialPolygons
class
library('sp') poly1 <- Polygons(list(Polygon(cbind(c(-100,-90,-85,-100), c(40,50,45,40)))), "1") poly2 <- Polygons(list(Polygon(cbind(c(-90,-80,-75,-90), c(30,40,35,30)))), "2") sp_poly <- SpatialPolygons(list(poly1, poly2), 1:2)
to json
geojson_json(sp_poly)
to a list
geojson_list(sp_poly)
From SpatialPoints
class
x <- c(1, 2, 3, 4, 5) y <- c(3, 2, 5, 1, 4) s <- SpatialPoints(cbind(x, y))
to json
geojson_json(s)
to a list
geojson_list(s)
library('maps') data(us.cities) geojson_write(us.cities[1:2, ], lat = 'lat', lon = 'long')
library("sp") file <- system.file("examples", "california.geojson", package = "geojsonio") out <- geojson_read(file, what = "sp") plot(out)
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) summary(out)
Read from a URL
url <- "https://raw.githubusercontent.com/shawnbot/d3-cartogram/master/data/us-states.topojson" out <- topojson_read(url)
Or use as.location()
first
(loc <- as.location(file)) out <- topojson_read(loc)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.