ropensci/geojsonio

Name: geojsonio

Owner: rOpenSci

Description: Convert many data formats to & from GeoJSON & TopoJSON

Created: 2014-04-01 17:20:23.0

Updated: 2018-01-02 00:10:12.0

Pushed: 2017-11-10 17:53:50.0

Homepage:

Size: 4131

Language: R

GitHub Committers

UserMost Recent Commit# Commits

Other Committers

UserEmailMost Recent Commit# Commits

README

geojsonio

Build Status codecov.io rstudio mirror downloads cran version

Convert various data formats to GeoJSON or TopoJSON

This package is a utility to convert 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:

*json Info
Install

A note about installing rgdal and rgeos - these two packages are built on top of C libraries, and their installation often causes trouble for Mac and Linux users because no binaries are provided on CRAN for those platforms. Other dependencies in geojsonio should install easily automatically when you install geojsonio. Change to the version of rgdal and GDAL you have):

Mac

Install GDAL on the command line first, e.g., using homebrew

 install gdal

Then install rgdal and rgeos

all.packages("rgdal", 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")
all.packages("rgeos", type = "source")

Linux

Get deps first

 apt-get install libgdal1-dev libgdal-dev libgeos-c1 libproj-dev

Then install rgdal and rgeos

all.packages("rgdal", type = "source")
all.packages("rgeos", type = "source")

Install geojsonio

Stable version from CRAN

all.packages("geojsonio")

Or development version from GitHub

all.packages("devtools")
ools::install_github("ropensci/geojsonio")

ary("geojsonio")
GeoJSON
Convert various formats to GeoJSON

From a numeric vector of length 2, as json or list

son_json(c(32.45, -99.74))
FeatureCollection> 
 type:  FeatureCollection 
 no. features:  1 
 features (1st 5):  Point

son_list(c(32.45, -99.74))
type
1] "FeatureCollection"

features
features[[1]]
features[[1]]$type
1] "Feature"

features[[1]]$geometry
features[[1]]$geometry$type

From a data.frame

ary('maps')
(us.cities)
son_json(us.cities[1:2, ], lat = 'lat', lon = 'long')
FeatureCollection> 
 type:  FeatureCollection 
 no. features:  2 
 features (1st 5):  Point, Point

son_list(us.cities[1:2, ], lat = 'lat', lon = 'long')
type
1] "FeatureCollection"

features
features[[1]]
features[[1]]$type
1] "Feature"

features[[1]]$geometry
features[[1]]$geometry$type

From SpatialPolygons class

ary('sp')
1 <- Polygons(list(Polygon(cbind(c(-100, -90, -85, -100),
40, 50, 45, 40)))), "1")
2 <- Polygons(list(Polygon(cbind(c(-90, -80, -75, -90),
30, 40, 35, 30)))), "2")
oly <- SpatialPolygons(list(poly1, poly2), 1:2)

to json

son_json(sp_poly)
FeatureCollection> 
 type:  FeatureCollection 
 no. features:  2 
 features (1st 5):  Polygon, Polygon

to list

son_list(sp_poly)$features[[1]]
type
1] "Feature"

id
1] 1

properties
properties$dummy
1] 0


Combine objects

geo_list + geo_list

Note: geo_list is the output type from geojson_list(), it's just a list with a class attached so we know it's geojson :)

<- c(-99.74, 32.45)
 geojson_list(vec)
 <- list(c(100.0, 0.0), c(101.0, 0.0), c(100.0, 0.0))
 geojson_list(vecs, geometry = "polygon")
b
type
1] "FeatureCollection"

features
features[[1]]
features[[1]]$type
1] "Feature"

features[[1]]$geometry
features[[1]]$geometry$type

json + json

 geojson_json(c(-99.74, 32.45))
 <- list(c(100.0, 0.0), c(101.0, 0.0), c(101.0, 1.0), c(100.0, 1.0), c(100.0, 0.0))
 geojson_json(vecs, geometry = "polygon")
d
"type":"FeatureCollection","features":[{"type":"Feature","geometry":{"type":"Point","coordinates":[-99.74,32.45]},"properties":{}},{"type":"Feature","geometry":{"type":"Polygon","coordinates":[[[100,0],[101,0],[101,1],[100,1],[100,0]]]},"properties":[]}]}
Write GeoJSON
ary('maps')
(us.cities)
son_write(us.cities[1:2, ], lat = 'lat', lon = 'long')
geojson-file>
 Path:       myfile.geojson
 From class: data.frame
Read GeoJSON
 <- system.file("examples", "california.geojson", package = "geojsonio")
<- geojson_read(file)
s(out)
1] "type"     "crs"      "features"
s(out$features[[1]])
1] "type"       "_id"        "properties" "geometry"
TopoJSON
to JSON
json_json(c(-99.74,32.45))
"type":"Topology","objects":{"foo":{"type":"GeometryCollection","geometries":[{"type":"Point","coordinates":[-99.74,32.45]}]}},"arcs":[],"bbox":[-99.74,32.45,-99.74,32.45]}
to list
ary(sp)
 c(1,2,3,4,5)
 c(3,2,5,1,4)
 SpatialPoints(cbind(x,y))
json_list(s)
type
1] "Topology"

objects
objects$foo
objects$foo$type
1] "GeometryCollection"

objects$foo$geometries
objects$foo$geometries[[1]]
objects$foo$geometries[[1]]$type
1] "Point"

objects$foo$geometries[[1]]$coordinates
1] 1 3

objects$foo$geometries[[1]]$id
1] 1

objects$foo$geometries[[1]]$properties

Write TopoJSON
ary('maps')
(us.cities)
json_write(us.cities[1:2, ], lat = 'lat', lon = 'long')
topojson-file>
 Path:       myfile.json
 From class: data.frame
Read TopoJSON
ary("sp")
<- "https://raw.githubusercontent.com/shawnbot/d3-cartogram/master/data/us-states.topojson"
<- topojson_read(url, verbose = FALSE)
(out)

plot of chunk unnamed-chunk-21

Use case: Play with US states

Using data from https://github.com/glynnbird/usstatesgeojson

Get some geojson

ary('httr')
<- GET('https://api.github.com/repos/glynnbird/usstatesgeojson/contents')
ames <- Filter(function(x) grepl("\\.geojson", x), sapply(content(res), "[[", "name"))
 <- 'https://raw.githubusercontent.com/glynnbird/usstatesgeojson/master/'
iles <- paste0(base, st_names)

Make a faceted plot

ary('ggplot2')
ary('plyr')
se <- st_files[7:13]
<- lapply(st_use, geojson_read, method = "local", what = "sp")
- ldply(setNames(lapply(geo, fortify), gsub("\\.geojson", "", st_names[7:13])))
ot(df, aes(long, lat, group = group)) +
om_polygon() +
cet_wrap(~.id, scales = "free")

plot of chunk unnamed-chunk-23

Okay, so the maps are not quite right (stretched to fit each panel), but you get the idea.

GeoJSON <-> TopoJSON

geo2topo() and topo2geo()

 '{"type": "LineString", "coordinates": [ [100.0, 0.0], [101.0, 1.0] ]}'
o_json <- geo2topo(x))
"type":"Topology","objects":{"foo":{"type":"LineString","arcs":[0]}},"arcs":[[[100,0],[101,1]]],"bbox":[100,0,101,1]}
2geo(topo_json)
GR data source with driver: GeoJSON 
ource: "{"type":"Topology","objects":{"foo":{"type":"LineString","arcs":[0]}},"arcs":[[[100,0],[101,1]]],"bbox":[100,0,101,1]}", layer: "TopoJSON"
ith 1 features
t has 1 fields
FeatureCollection> 
 type:  FeatureCollection 
 no. features:  1 
 features (1st 5):  LineString
Meta

This work is supported by the National Institutes of Health's National Center for Advancing Translational Sciences, Grant Number U24TR002306. This work is solely the responsibility of the creators and does not necessarily represent the official views of the National Institutes of Health.