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) }) knitr::opts_chunk$set( comment = "#>", collapse = TRUE, warning = FALSE, message = FALSE )
geojson
aims to deal only with geojson data in a lightweight way.
We've defined classes (S3
) following the GeoJSON spec. These
classes sort of overlap with sp
's classes, but not really. There's also some
overlap in GeoJSON classes with Well-Known Text (WKT) classes, but GeoJSON has a
subset of WKT's classes.
The package geoops supports manipulations on the classes defined in this package. This package is used within geojsonio to make some tasks easier.
Stable CRAN version
install.packages("geojson")
Dev version
remotes::install_github("ropensci/geojson")
library("geojson")
Essentially a character string with S3 class geojson
attached to make it
easy to perform operations on
x <- "{\"type\":\"FeatureCollection\",\"features\":[{\"type\":\"Feature\",\"geometry\":{\"type\":\"Point\",\"coordinates\":[-99.74,32.45]},\"properties\":{}}]}" as.geojson(x)
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
tt <- x %>% feature() %>% bbox_add() tt
Get bbox
bbox_get(tt)
x <- '{ "type": "Point", "coordinates": [100.0, 0.0] }' (pt <- point(x))
library("tibble") tibble(a = 1:5, b = list(pt))
x <- '{ "type": "MultiLineString", "coordinates": [ [ [100.0, 0.0], [101.0, 1.0] ], [ [102.0, 2.0], [103.0, 3.0] ] ] }' (mls <- multilinestring(x))
tibble(a = 1:5, b = list(mls))
tibble(a = 1:5, b = list(pt), c = list(mls))
Geobuf is a compact binary encoding for geographic data using protocol buffers https://github.com/mapbox/geobuf (via the protolite) package.
file <- system.file("examples/test.pb", package = "geojson") (json <- from_geobuf(file)) pb <- to_geobuf(json) class(pb) f <- tempfile(fileext = ".pb") to_geobuf(json, f) from_geobuf(f) x <- '{ "type": "Polygon", "coordinates": [ [ [100.0, 0.0], [101.0, 0.0], [101.0, 1.0], [100.0, 1.0], [100.0, 0.0] ] ] }' y <- polygon(x) to_geobuf(y) x <- '{"type": "MultiPoint", "coordinates": [ [100.0, 0.0], [101.0, 1.0] ] }' y <- multipoint(x) to_geobuf(y)
read nd-GeoJSON
url <- "https://raw.githubusercontent.com/ropensci/geojson/main/inst/examples/ndgeojson1.json" f <- tempfile(fileext = ".geojsonl") download.file(url, f) x <- ndgeo_read(f, verbose = FALSE) x
unlink(f)
Write nd-GeoJSON
One would think we could take the output of ndego_read()
above
and pass to ndgeo_write()
. However, in this example, the json is too big
for jqr
to handle, the underlying parser tool. So here's a smaller
example:
file <- system.file("examples", "featurecollection2.geojson", package = "geojson") str <- paste0(readLines(file), collapse = " ") (x <- featurecollection(str))
outfile <- tempfile(fileext = ".geojson") ndgeo_write(x, outfile) jsonlite::stream_in(file(outfile))
geojson
in R doing citation(package = 'geojson')
(This was originally setup without requiring any of the
GEOS/GDAL
stack but now the package sp depends on sf it can't be avoided without overhaul).
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.