knitr::opts_chunk$set( collapse = TRUE, comment = "#>" )
Quickly converts between GeoJSON and sf
objects
As per GeoJSON (RFC 7946 specification), foreign members are ignored, and nested objects and arrays inside the properties
object are converted to string/characters.
library(geojsonsf) # library(sf)
You would also typically load library(sf)
to take advantange of its plot
methods.
p <- '{"type":"Point","coordinates":[0,0]}' sf <- geojson_sf(p) head(sf)
gc <- '{ "type": "GeometryCollection", "geometries": [ {"type": "Point", "coordinates": [100.0, 0.0]}, {"type": "LineString", "coordinates": [[101.0, 0.0], [102.0, 1.0]]}, {"type" : "MultiPoint", "coordinates" : [[0,0], [1,1], [2,2]]} ] }' sf <- geojson_sf(gc) head(sf)
Some web-plotting libraries (such as googleway
) can't plot GeometryCollections directly. Therefore, you can set expand_geometries
to TRUE
and it will extract each geometry from a GeometryCollection and create a row for each one
gc <- '{ "type": "GeometryCollection", "geometries": [ {"type": "Point", "coordinates": [100.0, 0.0]}, {"type": "LineString", "coordinates": [[101.0, 0.0], [102.0, 1.0]]}, {"type" : "MultiPoint", "coordinates" : [[0,0], [1,1], [2,2]]} ] }' sf <- geojson_sf(gc, expand_geometries = T) head(sf)
f <- '{ "type": "Feature", "properties": {"id":1,"value":100,"text":"the quick brown fox"}, "geometry": { "type": "LineString", "coordinates": [[101.0, 0.0], [102.0, 1.0]] } }' sf <- geojson_sf(f) head(sf)
fc <- '{ "type": "FeatureCollection", "features": [ { "type": "Feature", "properties": {"foo" : "feature 1.1", "bar" : "feature 1.2"}, "geometry": {"type": "Point", "coordinates": [100.0, 0.0]} }, { "type": "Feature", "properties": null, "geometry": {"type": "LineString", "coordinates": [[101.0, 0.0], [102.0, 1.0]]} }, { "type": "Feature", "properties": {"foo" : "feature 3.1", "bar" : "feature 3.2"}, "geometry": {"type": "LineString", "coordinates": [[101.0, 0.0], [102.0, 1.0]]} } ] }' sf <- geojson_sf(fc) head(sf)
geo <- c(p, gc, f, fc) sf <- geojson_sf(geo) head(sf)
js <- '[ { "type": "FeatureCollection", "features": [ { "type": "Feature", "properties": null, "geometry": {"type": "Point", "coordinates": [100.0, 0.0]} }, { "type": "Feature", "properties": null, "geometry": {"type": "LineString", "coordinates": [[201.0, 0.0], [102.0, 1.0]]} }, { "type": "Feature", "properties": null, "geometry": {"type": "LineString", "coordinates": [[301.0, 0.0], [102.0, 1.0]]} } ] }, { "type": "FeatureCollection", "features": [ { "type": "Feature", "properties": null, "geometry": {"type": "Point", "coordinates": [100.0, 0.0]} }, { "type": "Feature", "properties": null, "geometry": {"type": "LineString", "coordinates": [[501.0, 0.0], [102.0, 1.0]]} }, { "type": "Feature", "properties": null, "geometry": {"type": "LineString", "coordinates": [[601.0, 0.0], [102.0, 1.0]]} } ] } ]' sf <- geojson_sf(js) head(sf)
url <- "http://eric.clst.org/assets/wiki/uploads/Stuff/gz_2010_us_050_00_500k.json" sf <- geojson_sf(url) #head(sf)
sf <- geojson_sf(system.file("examples", "geo_melbourne.geojson", package = "geojsonsf")) head(sf)
geo <- sf_geojson(sf) substr(geo, 1, 80)
Sometimes it's useful to convert each row of an sf
object into it's own GeoJSON object, for example when you want to keep each geometry separate in a database. Set atomise = TRUE
to return a vector of GeoJSON
geo <- sf_geojson(sf, atomise = T)
now geo
is a vector of GeoJSON
substr(geo[1], 1, 80) substr(geo[2], 1, 80)
It also converts GeoJSON to Well-Known Text and returns a data.frame
fc <- '{ "type": "FeatureCollection", "features": [ { "type": "Feature", "properties": {"foo" : "feature 1.1", "bar" : "feature 1.2"}, "geometry": {"type": "Point", "coordinates": [100.0, 0.0]} }, { "type": "Feature", "properties": null, "geometry": {"type": "LineString", "coordinates": [[101.0, 0.0], [102.0, 1.0]]} }, { "type": "Feature", "properties": {"foo" : "feature 3.1", "bar" : "feature 3.2"}, "geometry": {"type": "LineString", "coordinates": [[101.0, 0.0], [102.0, 1.0]]} } ] }' df <- geojson_wkt(fc) head(df)
Any scripts or data that you put into this service are public.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.