knitr::opts_chunk$set( comment = "#>", collapse = TRUE, warning = FALSE, message = FALSE )
The geojson
package has functions to do basic operations on GeoJSON classes.
library("geojson")
First, let's make a GeoJSON object
x <- '{ "type": "GeometryCollection", "geometries": [ { "type": "Point", "coordinates": [100.0, 0.0] }, { "type": "LineString", "coordinates": [ [101.0, 0.0], [102.0, 1.0] ] } ] }' (y <- geometrycollection(x))
Get the string
y[[1]]
Get the type
geo_type(y)
Pretty print the geojson
geo_pretty(y)
Write to disk
geo_write(y, f <- tempfile(fileext = ".geojson")) jsonlite::fromJSON(f, FALSE)
unlink(f)
Add properties
x <- '{ "type": "LineString", "coordinates": [ [100.0, 0.0], [101.0, 1.0] ]}' res <- linestring(x) %>% feature() %>% properties_add(population = 1000) res
Get a property
properties_get(res, property = 'population')
Add crs
crs <- '{ "type": "name", "properties": { "name": "urn:ogc:def:crs:OGC:1.3:CRS84" } }' z <- x %>% feature() %>% crs_add(crs) z
Get crs
crs_get(z)
Add bbox - by default, if you don't pass a bbox into bbox_add()
we attempt
to calculate the bbox for you. You can also pass in your own bbox.
tt <- x %>% feature() %>% bbox_add() tt
Get bbox
bbox_get(tt)
It's really easy to put geojson
class objects into data.frame's as well.
The ideal solution is to put them into tbl
's (see the tibble
package)
Make a point
x <- '{ "type": "Point", "coordinates": [100.0, 0.0] }' (pt <- point(x))
Put the point into a tbl
library("tibble") data_frame(a = 1:5, b = list(pt))
Another object, here a multilinestring
x <- '{ "type": "MultiLineString", "coordinates": [ [ [100.0, 0.0], [101.0, 1.0] ], [ [102.0, 2.0], [103.0, 3.0] ] ] }' (mls <- multilinestring(x))
Put into a tbl
data_frame(a = 1:5, b = list(mls))
Put the point
and multilinestring
into the same tbl
(df <- data_frame(a = 1:5, b = list(pt), c = list(mls)))
And you can pull the geojson back out
df$b df$b[[1]]
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.