Title: | Classes for 'GeoJSON' |
---|---|
Description: | Classes for 'GeoJSON' to make working with 'GeoJSON' easier. Includes S3 classes for 'GeoJSON' classes with brief summary output, and a few methods such as extracting and adding bounding boxes, properties, and coordinate reference systems; working with newline delimited 'GeoJSON'; and serializing to/from 'Geobuf' binary 'GeoJSON' format. |
Authors: | Scott Chamberlain [aut] |
Maintainer: | Michael Sumner <[email protected]> |
License: | MIT + file LICENSE |
Version: | 0.3.5.9000 |
Built: | 2024-08-29 23:17:28 UTC |
Source: | https://github.com/ropensci/geojson |
Classes for GeoJSON to make working with GeoJSON easier
GeoJSON objects:
feature
- Feature
featurecollection
- FeatureCollection
geometrycollection
- GeometryCollection
linestring
- LineString
multilinestring
- MultiLineString
multipoint
- MultiPoint
multipolygon
- MultiPolygon
point
- Point
polygon
- Polygon
The above are assigned two classes. All of them are class geojson, but also have a class name that is geo plus the name of the geometry, e.g., geopolygon for polygon.
GeoJSON properties:
properties_add
, properties_get
- Add
or get properties
GeoJSON operations:
geo_bbox
- calculate a bounding box for any GeoJSON
object
geo_pretty
- pretty print any GeoJSON object
geo_type
- get the object type for any GeoJSON
object
geo_write
- easily write any GeoJSON to a file
More complete GeoJSON operations are provdied in the package geoops
GeoJSON/Geobuf serialization:
from_geobuf
- Geobuf to GeoJSON
to_geobuf
- GeoJSON to Geobuf
Check out https://github.com/mapbox/geobuf for inormation on the Geobuf format
According to RFC 7946
(https://datatracker.ietf.org/doc/html/rfc7946#page-12) the CRS for all GeoJSON
objects must be WGS-84, equivalent to urn:ogc:def:crs:OGC::CRS84
.
And lat/long must be in decimal degrees.
Given the above, but considering that GeoJSON blobs exist that have CRS attributes in them, we provide CRS helpers in this package. But moving forward these are not likely to be used much.
According to RFC 7946 (https://datatracker.ietf.org/doc/html/rfc7946#section-11.2) consider that 6 decimal places amoutns to ~10 centimeters, a precision well within that of current GPS sytems. Further, A GeoJSON text containing many detailed Polygons can be inflated almost by a factor of two by increasing coordinate precision from 6 to 15 decimal places - so consider whether it is worth it to have more decimal places.
Scott Chamberlain, Jeroen Ooms
Geojson class
as.geojson(x) ## S4 method for signature 'json' as.geojson(x) ## S4 method for signature 'geojson' as.geojson(x) ## S4 method for signature 'character' as.geojson(x) ## S4 method for signature 'SpatialPointsDataFrame' as.geojson(x) ## S4 method for signature 'SpatialPoints' as.geojson(x) ## S4 method for signature 'SpatialLinesDataFrame' as.geojson(x) ## S4 method for signature 'SpatialLines' as.geojson(x) ## S4 method for signature 'SpatialPolygonsDataFrame' as.geojson(x) ## S4 method for signature 'SpatialPolygons' as.geojson(x)
as.geojson(x) ## S4 method for signature 'json' as.geojson(x) ## S4 method for signature 'geojson' as.geojson(x) ## S4 method for signature 'character' as.geojson(x) ## S4 method for signature 'SpatialPointsDataFrame' as.geojson(x) ## S4 method for signature 'SpatialPoints' as.geojson(x) ## S4 method for signature 'SpatialLinesDataFrame' as.geojson(x) ## S4 method for signature 'SpatialLines' as.geojson(x) ## S4 method for signature 'SpatialPolygonsDataFrame' as.geojson(x) ## S4 method for signature 'SpatialPolygons' as.geojson(x)
x |
input, an object of class character, json, SpatialPoints, SpatialPointsDataFrame, SpatialLines, SpatialLinesDataFrame, SpatialPolygons, or SpatialPolygonsDataFrame |
The print.geojson
method prints the geojson geometry
type, the bounding box, number of features (if applicable), and the
geometries and their lengths
an object of class geojson/json
# character as.geojson(geojson_data$featurecollection_point) as.geojson(geojson_data$polygons_average) as.geojson(geojson_data$polygons_aggregate) as.geojson(geojson_data$points_count) # sp classes ## SpatialPoints library(sp) x <- c(1,2,3,4,5) y <- c(3,2,5,1,4) s <- SpatialPoints(cbind(x,y)) as.geojson(s) ## SpatialPointsDataFrame s <- SpatialPointsDataFrame(cbind(x,y), mtcars[1:5,]) as.geojson(s) ## SpatialLines L1 <- Line(cbind(c(1,2,3), c(3,2,2))) L2 <- Line(cbind(c(1.05,2.05,3.05), c(3.05,2.05,2.05))) L3 <- Line(cbind(c(1,2,3),c(1,1.5,1))) Ls1 <- Lines(list(L1), ID = "a") Ls2 <- Lines(list(L2, L3), ID = "b") sl1 <- SpatialLines(list(Ls1)) as.geojson(sl1) ## SpatialLinesDataFrame sl12 <- SpatialLines(list(Ls1, Ls2)) dat <- data.frame(X = c("Blue", "Green"), Y = c("Train", "Plane"), Z = c("Road", "River"), row.names = c("a", "b")) sldf <- SpatialLinesDataFrame(sl12, dat) as.geojson(sldf) ## SpatialPolygons 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) as.geojson(sp_poly) ## SpatialPolygonsDataFrame sp_polydf <- as(sp_poly, "SpatialPolygonsDataFrame") as.geojson(sp_polydf) ## sf objects if (requireNamespace('sf')) { nc <- sf::st_read(system.file("shape/nc.shp", package = "sf"), quiet = TRUE) as.geojson(nc) }
# character as.geojson(geojson_data$featurecollection_point) as.geojson(geojson_data$polygons_average) as.geojson(geojson_data$polygons_aggregate) as.geojson(geojson_data$points_count) # sp classes ## SpatialPoints library(sp) x <- c(1,2,3,4,5) y <- c(3,2,5,1,4) s <- SpatialPoints(cbind(x,y)) as.geojson(s) ## SpatialPointsDataFrame s <- SpatialPointsDataFrame(cbind(x,y), mtcars[1:5,]) as.geojson(s) ## SpatialLines L1 <- Line(cbind(c(1,2,3), c(3,2,2))) L2 <- Line(cbind(c(1.05,2.05,3.05), c(3.05,2.05,2.05))) L3 <- Line(cbind(c(1,2,3),c(1,1.5,1))) Ls1 <- Lines(list(L1), ID = "a") Ls2 <- Lines(list(L2, L3), ID = "b") sl1 <- SpatialLines(list(Ls1)) as.geojson(sl1) ## SpatialLinesDataFrame sl12 <- SpatialLines(list(Ls1, Ls2)) dat <- data.frame(X = c("Blue", "Green"), Y = c("Train", "Plane"), Z = c("Road", "River"), row.names = c("a", "b")) sldf <- SpatialLinesDataFrame(sl12, dat) as.geojson(sldf) ## SpatialPolygons 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) as.geojson(sp_poly) ## SpatialPolygonsDataFrame sp_polydf <- as(sp_poly, "SpatialPolygonsDataFrame") as.geojson(sp_polydf) ## sf objects if (requireNamespace('sf')) { nc <- sf::st_read(system.file("shape/nc.shp", package = "sf"), quiet = TRUE) as.geojson(nc) }
Add or get bounding box
bbox_add(x, bbox = NULL) bbox_get(x)
bbox_add(x, bbox = NULL) bbox_get(x)
x |
An object of class |
bbox |
(numeric) a vector or list of length 4 for a 2D bounding box
or length 6 for a 3D bounding box. If |
Note that bbox_get
outputs the bbox if it exists, but
does not calculate it from the geojson. See geo_bbox
to calculate a bounding box. Bounding boxes can be 2D or 3D.
bbox_add: an object of class jqson/character from jqr
bbox_get: a bounding box, of the form
[west, south, east, north]
for 2D or of the form
[west, south, min-altitude, east, north, max-altitude]
for 3D
https://datatracker.ietf.org/doc/html/rfc7946#section-5
# make a polygon 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)) # add bbox - without an input, we figure out the 2D bbox for you y %>% feature() %>% bbox_add() ## 2D bbox y %>% feature() %>% bbox_add(c(100.0, -10.0, 105.0, 10.0)) ## 3D bbox y %>% feature() %>% bbox_add(c(100.0, -10.0, 3, 105.0, 10.0, 17)) # get bounding box z <- y %>% feature() %>% bbox_add() bbox_get(z) ## returns NULL if no bounding box bbox_get(x)
# make a polygon 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)) # add bbox - without an input, we figure out the 2D bbox for you y %>% feature() %>% bbox_add() ## 2D bbox y %>% feature() %>% bbox_add(c(100.0, -10.0, 105.0, 10.0)) ## 3D bbox y %>% feature() %>% bbox_add(c(100.0, -10.0, 3, 105.0, 10.0, 17)) # get bounding box z <- y %>% feature() %>% bbox_add() bbox_get(z) ## returns NULL if no bounding box bbox_get(x)
Add or get CRS
crs_add(x, crs) crs_get(x)
crs_add(x, crs) crs_get(x)
x |
An object of class |
crs |
(character) a CRS string. required. |
According to RFC 7946
(https://datatracker.ietf.org/doc/html/rfc7946#page-12) the CRS for all GeoJSON
objects must be WGS-84, equivalent to urn:ogc:def:crs:OGC::CRS84
.
And lat/long must be in decimal degrees.
Given the above, but considering that GeoJSON blobs exist that have CRS attributes in them, we provide CRS helpers here. But moving forward these are not likely to be used much.
https://github.com/OSGeo/PROJ, https://geojson.org/geojson-spec.html#coordinate-reference-system-objects
x <- '{ "type": "Polygon", "coordinates": [ [ [100.0, 0.0], [101.0, 0.0], [101.0, 1.0], [100.0, 1.0], [100.0, 0.0] ] ] }' # add crs crs <- '{"type": "name", "properties": { "name": "urn:ogc:def:crs:OGC:1.3:CRS84" }}' x %>% feature() %>% crs_add(crs) # get crs z <- x %>% feature() %>% crs_add(crs) crs_get(z)
x <- '{ "type": "Polygon", "coordinates": [ [ [100.0, 0.0], [101.0, 0.0], [101.0, 1.0], [100.0, 1.0], [100.0, 0.0] ] ] }' # add crs crs <- '{"type": "name", "properties": { "name": "urn:ogc:def:crs:OGC:1.3:CRS84" }}' x %>% feature() %>% crs_add(crs) # get crs z <- x %>% feature() %>% crs_add(crs) crs_get(z)
feature class
feature(x)
feature(x)
x |
input |
Feature objects:
A feature object must have a member with the name "geometry". The value of the geometry member is a geometry object as defined above or a JSON null value.
A feature object must have a member with the name "properties". The value of the properties member is an object (any JSON object or a JSON null value).
If a feature has a commonly used identifier, that identifier should be included as a member of the feature object with the name "id".
# point -> feature x <- '{ "type": "Point", "coordinates": [100.0, 0.0] }' point(x) %>% feature() # multipoint -> feature x <- '{"type": "MultiPoint", "coordinates": [ [100.0, 0.0], [101.0, 1.0] ] }' multipoint(x) %>% feature() # linestring -> feature x <- '{ "type": "LineString", "coordinates": [ [100.0, 0.0], [101.0, 1.0] ] }' linestring(x) %>% feature() # multilinestring -> feature x <- '{ "type": "MultiLineString", "coordinates": [ [ [100.0, 0.0], [101.0, 1.0] ], [ [102.0, 2.0], [103.0, 3.0] ] ] }' multilinestring(x) %>% feature() # add to a data.frame library('tibble') tibble(a = 1:5, b = list(multilinestring(x)))
# point -> feature x <- '{ "type": "Point", "coordinates": [100.0, 0.0] }' point(x) %>% feature() # multipoint -> feature x <- '{"type": "MultiPoint", "coordinates": [ [100.0, 0.0], [101.0, 1.0] ] }' multipoint(x) %>% feature() # linestring -> feature x <- '{ "type": "LineString", "coordinates": [ [100.0, 0.0], [101.0, 1.0] ] }' linestring(x) %>% feature() # multilinestring -> feature x <- '{ "type": "MultiLineString", "coordinates": [ [ [100.0, 0.0], [101.0, 1.0] ], [ [102.0, 2.0], [103.0, 3.0] ] ] }' multilinestring(x) %>% feature() # add to a data.frame library('tibble') tibble(a = 1:5, b = list(multilinestring(x)))
featurecollection class
featurecollection(x)
featurecollection(x)
x |
input |
file <- system.file("examples", 'featurecollection1.geojson', package = "geojson") file <- system.file("examples", 'featurecollection2.geojson', package = "geojson") str <- paste0(readLines(file), collapse = " ") (y <- featurecollection(str)) geo_type(y) geo_pretty(y) geo_write(y, f <- tempfile(fileext = ".geojson")) jsonlite::fromJSON(f, FALSE) unlink(f) # add to a data.frame library('tibble') tibble(a = 1:5, b = list(y)) # features to featurecollection x <- '{ "type": "Point", "coordinates": [100.0, 0.0] }' point(x) %>% feature() %>% featurecollection() ## all points x <- '{ "type": "Point", "coordinates": [100.0, 0.0] }' y <- '{ "type": "Point", "coordinates": [100.0, 50.0] }' featls <- lapply(list(x, y), function(z) feature(point(z))) featurecollection(featls)
file <- system.file("examples", 'featurecollection1.geojson', package = "geojson") file <- system.file("examples", 'featurecollection2.geojson', package = "geojson") str <- paste0(readLines(file), collapse = " ") (y <- featurecollection(str)) geo_type(y) geo_pretty(y) geo_write(y, f <- tempfile(fileext = ".geojson")) jsonlite::fromJSON(f, FALSE) unlink(f) # add to a data.frame library('tibble') tibble(a = 1:5, b = list(y)) # features to featurecollection x <- '{ "type": "Point", "coordinates": [100.0, 0.0] }' point(x) %>% feature() %>% featurecollection() ## all points x <- '{ "type": "Point", "coordinates": [100.0, 0.0] }' y <- '{ "type": "Point", "coordinates": [100.0, 50.0] }' featls <- lapply(list(x, y), function(z) feature(point(z))) featurecollection(featls)
Calculate a bounding box
geo_bbox(x)
geo_bbox(x)
x |
an object of class geojson |
Supports inputs of type: character, point, multipoint, linestring, multilinestring, polygon, multipoygon, feature, and featurecollection
On character inputs, we lint the input to make sure it's proper JSON and GeoJSON, then caculate the bounding box
a vector of four doubles: min lon, min lat, max lon, max lat
# point x <- '{ "type": "Point", "coordinates": [100.0, 0.0] }' (y <- point(x)) geo_bbox(y) y %>% feature() %>% geo_bbox() # multipoint x <- '{"type": "MultiPoint", "coordinates": [ [100.0, 0.0], [101.0, 1.0] ] }' (y <- multipoint(x)) geo_bbox(y) y %>% feature() %>% geo_bbox() # linestring x <- '{ "type": "LineString", "coordinates": [ [100.0, 0.0], [101.0, 1.0] ]}' (y <- linestring(x)) geo_bbox(y) y %>% feature() %>% geo_bbox() file <- system.file("examples", 'linestring_one.geojson', package = "geojson") con <- file(file) str <- paste0(readLines(con), collapse = " ") (y <- linestring(str)) geo_bbox(y) y %>% feature() %>% geo_bbox() close(con) ## Not run: # multilinestring x <- '{ "type": "MultiLineString", "coordinates": [ [ [100.0, 0.0], [101.0, 1.0] ], [ [102.0, 2.0], [103.0, 3.0] ] ] }' (y <- multilinestring(x)) geo_bbox(y) y %>% feature() %>% geo_bbox() # polygon 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)) geo_bbox(y) y %>% feature() %>% geo_bbox() # multipolygon x <- '{ "type": "MultiPolygon", "coordinates": [ [[[102.0, 2.0], [103.0, 2.0], [103.0, 3.0], [102.0, 3.0], [102.0, 2.0]]], [[[100.0, 0.0], [101.0, 0.0], [101.0, 1.0], [100.0, 1.0], [100.0, 0.0]], [[100.2, 0.2], [100.8, 0.2], [100.8, 0.8], [100.2, 0.8], [100.2, 0.2]]] ] }' (y <- multipolygon(x)) geo_bbox(y) y %>% feature() %>% geo_bbox() # featurecollection file <- system.file("examples", 'featurecollection2.geojson', package = "geojson") str <- paste0(readLines(file), collapse = " ") x <- featurecollection(str) geo_bbox(x) # character file <- system.file("examples", 'featurecollection2.geojson', package = "geojson") str <- paste0(readLines(file), collapse = " ") geo_bbox(str) # json library('jsonlite') geo_bbox(toJSON(fromJSON(str), auto_unbox = TRUE)) ## End(Not run)
# point x <- '{ "type": "Point", "coordinates": [100.0, 0.0] }' (y <- point(x)) geo_bbox(y) y %>% feature() %>% geo_bbox() # multipoint x <- '{"type": "MultiPoint", "coordinates": [ [100.0, 0.0], [101.0, 1.0] ] }' (y <- multipoint(x)) geo_bbox(y) y %>% feature() %>% geo_bbox() # linestring x <- '{ "type": "LineString", "coordinates": [ [100.0, 0.0], [101.0, 1.0] ]}' (y <- linestring(x)) geo_bbox(y) y %>% feature() %>% geo_bbox() file <- system.file("examples", 'linestring_one.geojson', package = "geojson") con <- file(file) str <- paste0(readLines(con), collapse = " ") (y <- linestring(str)) geo_bbox(y) y %>% feature() %>% geo_bbox() close(con) ## Not run: # multilinestring x <- '{ "type": "MultiLineString", "coordinates": [ [ [100.0, 0.0], [101.0, 1.0] ], [ [102.0, 2.0], [103.0, 3.0] ] ] }' (y <- multilinestring(x)) geo_bbox(y) y %>% feature() %>% geo_bbox() # polygon 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)) geo_bbox(y) y %>% feature() %>% geo_bbox() # multipolygon x <- '{ "type": "MultiPolygon", "coordinates": [ [[[102.0, 2.0], [103.0, 2.0], [103.0, 3.0], [102.0, 3.0], [102.0, 2.0]]], [[[100.0, 0.0], [101.0, 0.0], [101.0, 1.0], [100.0, 1.0], [100.0, 0.0]], [[100.2, 0.2], [100.8, 0.2], [100.8, 0.8], [100.2, 0.8], [100.2, 0.2]]] ] }' (y <- multipolygon(x)) geo_bbox(y) y %>% feature() %>% geo_bbox() # featurecollection file <- system.file("examples", 'featurecollection2.geojson', package = "geojson") str <- paste0(readLines(file), collapse = " ") x <- featurecollection(str) geo_bbox(x) # character file <- system.file("examples", 'featurecollection2.geojson', package = "geojson") str <- paste0(readLines(file), collapse = " ") geo_bbox(str) # json library('jsonlite') geo_bbox(toJSON(fromJSON(str), auto_unbox = TRUE)) ## End(Not run)
Pretty print geojson
geo_pretty(x)
geo_pretty(x)
x |
input, an object of class |
Wrapper around prettify
geo_pretty(point('{ "type": "Point", "coordinates": [100.0, 0.0] }')) x <- '{ "type": "Polygon", "coordinates": [ [ [100.0, 0.0], [100.0, 1.0], [101.0, 1.0], [101.0, 0.0], [100.0, 0.0] ] ] }' poly <- polygon(x) geo_pretty(poly)
geo_pretty(point('{ "type": "Point", "coordinates": [100.0, 0.0] }')) x <- '{ "type": "Polygon", "coordinates": [ [ [100.0, 0.0], [100.0, 1.0], [101.0, 1.0], [101.0, 0.0], [100.0, 0.0] ] ] }' poly <- polygon(x) geo_pretty(poly)
Get geometry type
geo_type(x)
geo_type(x)
x |
input, an object of class |
geo_type(point('{ "type": "Point", "coordinates": [100.0, 0.0] }')) x <- '{ "type": "Polygon", "coordinates": [ [ [100.0, 0.0], [100.0, 1.0], [101.0, 1.0], [101.0, 0.0], [100.0, 0.0] ] ] }' poly <- polygon(x) geo_type(poly)
geo_type(point('{ "type": "Point", "coordinates": [100.0, 0.0] }')) x <- '{ "type": "Polygon", "coordinates": [ [ [100.0, 0.0], [100.0, 1.0], [101.0, 1.0], [101.0, 0.0], [100.0, 0.0] ] ] }' poly <- polygon(x) geo_type(poly)
Write geojson to disk
geo_write(x, file)
geo_write(x, file)
x |
input, an object of class |
file |
(character) a file path, or connection |
Wrapper around jsonlite::toJSON()
and
cat
file <- tempfile(fileext = ".geojson") geo_write( point('{ "type": "Point", "coordinates": [100.0, 0.0] }'), file ) readLines(file) unlink(file)
file <- tempfile(fileext = ".geojson") geo_write( point('{ "type": "Point", "coordinates": [100.0, 0.0] }'), file ) readLines(file) unlink(file)
Geobuf serialization
from_geobuf(x, pretty = FALSE) to_geobuf(x, file = NULL, decimals = 6)
from_geobuf(x, pretty = FALSE) to_geobuf(x, file = NULL, decimals = 6)
x |
(character) a file or raw object for |
pretty |
(logical) pretty print JSON. Default: |
file |
(character) file to write protobuf to. if NULL, geobuf raw binary returned |
decimals |
(integer) how many decimals (digits behind the dot) to store for numbers |
from_geobuf
uses protolite::geobuf2json()
,
while to_geobuf
uses protolite::json2geobuf()
Note that protolite expects either a Feature,
FeatureCollection, or Geometry class geojson
object, Thus, for to_geobuf
we check the geojson class, and
convert to a Feature if the class is something other than
the acceptable set.
for from_geobuf
JSON as a character string, and for
to_geobuf
raw or file written to disk
Geobuf is a compact binary encoding for geographic data using protocol buffers https://github.com/mapbox/geobuf
file <- system.file("examples/test.pb", package = "geojson") (json <- from_geobuf(file)) from_geobuf(file, pretty = TRUE) pb <- to_geobuf(json) f <- tempfile(fileext = ".pb") to_geobuf(json, f) from_geobuf(f) object.size(json) object.size(pb) file.info(file)$size file.info(f)$size file <- system.file("examples/featurecollection1.geojson", package = "geojson") json <- paste0(readLines(file), collapse = "") to_geobuf(json) # other geojson class objects 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)
file <- system.file("examples/test.pb", package = "geojson") (json <- from_geobuf(file)) from_geobuf(file, pretty = TRUE) pb <- to_geobuf(json) f <- tempfile(fileext = ".pb") to_geobuf(json, f) from_geobuf(f) object.size(json) object.size(pb) file.info(file)$size file.info(f)$size file <- system.file("examples/featurecollection1.geojson", package = "geojson") json <- paste0(readLines(file), collapse = "") to_geobuf(json) # other geojson class objects 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)
Data for use in examples
A list of character strings of points or polygons in FeatureCollection or Feature Geojson formats.
The data objects included in the list, accessible by name
featurecollection_point - FeatureCollection with a single point
filter_features - FeatureCollection of points
points_average - FeatureCollection of points
polygons_average - FeatureCollection of polygons
points_count - FeatureCollection of points
polygons_count - FeatureCollection of polygons
points_within - FeatureCollection of points
polygons_within - FeatureCollection of polygons
poly - Feaure of a single 1 degree by 1 degree polygon
multipoly - FeatureCollection of two 1 degree by 1 degree polygons
polygons_aggregate - FeatureCollection of Polygons from turf.js examples
points_aggregate - FeatureCollection of Points from turf.js examples
geometrycollection class
geometrycollection(x)
geometrycollection(x)
x |
input |
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)) geo_type(y) geo_pretty(y) geo_write(y, f <- tempfile(fileext = ".geojson")) jsonlite::fromJSON(f, FALSE) unlink(f) # bigger geometrycollection file <- system.file("examples", "geometrycollection1.geojson", package = "geojson") (y <- geometrycollection(paste0(readLines(file), collapse=""))) geo_type(y) geo_pretty(y)
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)) geo_type(y) geo_pretty(y) geo_write(y, f <- tempfile(fileext = ".geojson")) jsonlite::fromJSON(f, FALSE) unlink(f) # bigger geometrycollection file <- system.file("examples", "geometrycollection1.geojson", package = "geojson") (y <- geometrycollection(paste0(readLines(file), collapse=""))) geo_type(y) geo_pretty(y)
linestring class
linestring(x)
linestring(x)
x |
input |
x <- '{ "type": "LineString", "coordinates": [ [100.0, 0.0], [101.0, 1.0] ] }' (y <- linestring(x)) geo_type(y) geo_pretty(y) geo_write(y, f <- tempfile(fileext = ".geojson")) jsonlite::fromJSON(f, FALSE) unlink(f) # add to a data.frame library('tibble') tibble(a = 1:5, b = list(y))
x <- '{ "type": "LineString", "coordinates": [ [100.0, 0.0], [101.0, 1.0] ] }' (y <- linestring(x)) geo_type(y) geo_pretty(y) geo_write(y, f <- tempfile(fileext = ".geojson")) jsonlite::fromJSON(f, FALSE) unlink(f) # add to a data.frame library('tibble') tibble(a = 1:5, b = list(y))
GeoJSON Linting
linting_opts( lint = FALSE, method = "hint", error = FALSE, suppress_pkgcheck_warnings = FALSE )
linting_opts( lint = FALSE, method = "hint", error = FALSE, suppress_pkgcheck_warnings = FALSE )
lint |
(logical) lint geojson or not. Default: |
method |
(character) method to use:
|
error |
(logical) Throw an error on parse failure? If |
suppress_pkgcheck_warnings |
(logical) Suppress warning when
|
linting_opts was deprecated in 0.3.5
multilinestring class
multilinestring(x)
multilinestring(x)
x |
input |
x <- '{ "type": "MultiLineString", "coordinates": [ [ [100.0, 0.0], [101.0, 1.0] ], [ [102.0, 2.0], [103.0, 3.0] ] ] }' (y <- multilinestring(x)) y[1] geo_type(y) geo_pretty(y) geo_write(y, f <- tempfile(fileext = ".geojson")) jsonlite::fromJSON(f, FALSE) unlink(f) file <- system.file("examples", 'multilinestring_one.geojson', package = "geojson") con <- file(file) str <- paste0(readLines(con), collapse = " ") (y <- multilinestring(str)) y[1] geo_type(y) geo_pretty(y) close(con) # add to a data.frame library('tibble') tibble(a = 1:5, b = list(y))
x <- '{ "type": "MultiLineString", "coordinates": [ [ [100.0, 0.0], [101.0, 1.0] ], [ [102.0, 2.0], [103.0, 3.0] ] ] }' (y <- multilinestring(x)) y[1] geo_type(y) geo_pretty(y) geo_write(y, f <- tempfile(fileext = ".geojson")) jsonlite::fromJSON(f, FALSE) unlink(f) file <- system.file("examples", 'multilinestring_one.geojson', package = "geojson") con <- file(file) str <- paste0(readLines(con), collapse = " ") (y <- multilinestring(str)) y[1] geo_type(y) geo_pretty(y) close(con) # add to a data.frame library('tibble') tibble(a = 1:5, b = list(y))
multipoint class
multipoint(x)
multipoint(x)
x |
input |
x <- '{"type": "MultiPoint", "coordinates": [ [100.0, 0.0], [101.0, 1.0] ] }' (y <- multipoint(x)) geo_type(y) geo_pretty(y) geo_write(y, f <- tempfile(fileext = ".geojson")) jsonlite::fromJSON(f, FALSE) unlink(f) # add to a data.frame library('tibble') tibble(a = 1:5, b = list(y)) # as.geojson coercion as.geojson(x)
x <- '{"type": "MultiPoint", "coordinates": [ [100.0, 0.0], [101.0, 1.0] ] }' (y <- multipoint(x)) geo_type(y) geo_pretty(y) geo_write(y, f <- tempfile(fileext = ".geojson")) jsonlite::fromJSON(f, FALSE) unlink(f) # add to a data.frame library('tibble') tibble(a = 1:5, b = list(y)) # as.geojson coercion as.geojson(x)
multipolygon class
multipolygon(x)
multipolygon(x)
x |
input |
x <- '{ "type": "MultiPolygon", "coordinates": [ [[[102.0, 2.0], [103.0, 2.0], [103.0, 3.0], [102.0, 3.0], [102.0, 2.0]]], [[[100.0, 0.0], [101.0, 0.0], [101.0, 1.0], [100.0, 1.0], [100.0, 0.0]], [[100.2, 0.2], [100.8, 0.2], [100.8, 0.8], [100.2, 0.8], [100.2, 0.2]]] ] }' (y <- multipolygon(x)) geo_type(y) geo_pretty(y) geo_write(y, f <- tempfile(fileext = ".geojson")) jsonlite::fromJSON(f, FALSE) unlink(f) # add to a data.frame library('tibble') tibble(a = 1:5, b = list(y))
x <- '{ "type": "MultiPolygon", "coordinates": [ [[[102.0, 2.0], [103.0, 2.0], [103.0, 3.0], [102.0, 3.0], [102.0, 2.0]]], [[[100.0, 0.0], [101.0, 0.0], [101.0, 1.0], [100.0, 1.0], [100.0, 0.0]], [[100.2, 0.2], [100.8, 0.2], [100.8, 0.8], [100.2, 0.8], [100.2, 0.2]]] ] }' (y <- multipolygon(x)) geo_type(y) geo_pretty(y) geo_write(y, f <- tempfile(fileext = ".geojson")) jsonlite::fromJSON(f, FALSE) unlink(f) # add to a data.frame library('tibble') tibble(a = 1:5, b = list(y))
There are various flavors of newline-delimited GeoJSON, all of which we aim to handle here. See Details for more.
ndgeo_write(x, file, sep = "\n") ## Default S3 method: ndgeo_write(x, file, sep = "\n") ## S3 method for class 'geofeaturecollection' ndgeo_write(x, file, sep = "\n") ## S3 method for class 'geofeature' ndgeo_write(x, file, sep = "\n") ndgeo_read(txt, pagesize = 500, verbose = TRUE)
ndgeo_write(x, file, sep = "\n") ## Default S3 method: ndgeo_write(x, file, sep = "\n") ## S3 method for class 'geofeaturecollection' ndgeo_write(x, file, sep = "\n") ## S3 method for class 'geofeature' ndgeo_write(x, file, sep = "\n") ndgeo_read(txt, pagesize = 500, verbose = TRUE)
x |
input, an object of class |
file |
(character) a file. not a connection. required. |
sep |
(character) a character separator to use in |
txt |
text, a file, or a url. required. |
pagesize |
(integer) number of lines to read/write from/to the connection per iteration |
verbose |
(logical) print messages. default: |
ndgeo_write
: writes geojson package types as
newline-delimited GeoJSON to a file
ndgeo_read
: reads newline-delimited GeoJSON from a string,
file, or URL into the appropriate geojson type
As an alternative to ndgeo_read
, you can simply use
jsonlite::stream_in()
to convert newline-delimited GeoJSON
to a data.frame
a geojson
class object
IMPORTANT: ngeo_read
for now only handles lines of geojson
in your file that are either features or geometry objects (e.g., point,
multipoint, polygon, multipolygon, linestring, multilinestring)
Newline-delimited JSON has a few flavors. The only difference between ndjson http://ndjson.org/ and JSON Lines https://jsonlines.org/ I can tell is that the former requires UTF-8 encoding, while the latter does not.
GeoJSON text sequences has a specification found at https://datatracker.ietf.org/doc/html/rfc8142. The spec states that:
a GeoJSON text sequence is any number of GeoJSON RFC7946 texts
each line encoded in UTF-8 RFC3629
each line preceded by one ASCII RFC20 record separator (RS; "0x1e") character
each line followed by a line feed (LF)
each JSON text MUST contain a single GeoJSON object as defined in RFC7946
See also the GeoJSON specification https://datatracker.ietf.org/doc/html/rfc7946
# featurecollection ## write file <- system.file("examples", 'featurecollection2.geojson', package = "geojson") str <- paste0(readLines(file), collapse = " ") (x <- featurecollection(str)) outfile <- tempfile(fileext = ".geojson") ndgeo_write(x, outfile) readLines(outfile) jsonlite::stream_in(file(outfile)) ## read ndgeo_read(outfile) unlink(outfile) # read from an existing file ## GeoJSON objects all of same type: Feature file <- system.file("examples", 'ndgeojson1.json', package = "geojson") ndgeo_read(file) ## GeoJSON objects all of same type: Point file <- system.file("examples", 'ndgeojson2.json', package = "geojson") ndgeo_read(file) ## GeoJSON objects of mixed type: Point, and Feature file <- system.file("examples", 'ndgeojson3.json', package = "geojson") ndgeo_read(file) ## Not run: # read from a file url <- "https://raw.githubusercontent.com/ropensci/geojson/main/inst/examples/ndgeojson1.json" f <- tempfile(fileext = ".geojsonl") download.file(url, f) x <- ndgeo_read(f) x unlink(f) # read from a URL url <- "https://raw.githubusercontent.com/ropensci/geojson/main/inst/examples/ndgeojson1.json" x <- ndgeo_read(url) x # geojson text sequences from file file <- system.file("examples", 'featurecollection2.geojson', package = "geojson") str <- paste0(readLines(file), collapse = " ") x <- featurecollection(str) outfile <- tempfile(fileext = ".geojson") ndgeo_write(x, outfile, sep = "\u001e\n") con <- file(outfile) readLines(con) close(con) ndgeo_read(outfile) unlink(outfile) ## End(Not run)
# featurecollection ## write file <- system.file("examples", 'featurecollection2.geojson', package = "geojson") str <- paste0(readLines(file), collapse = " ") (x <- featurecollection(str)) outfile <- tempfile(fileext = ".geojson") ndgeo_write(x, outfile) readLines(outfile) jsonlite::stream_in(file(outfile)) ## read ndgeo_read(outfile) unlink(outfile) # read from an existing file ## GeoJSON objects all of same type: Feature file <- system.file("examples", 'ndgeojson1.json', package = "geojson") ndgeo_read(file) ## GeoJSON objects all of same type: Point file <- system.file("examples", 'ndgeojson2.json', package = "geojson") ndgeo_read(file) ## GeoJSON objects of mixed type: Point, and Feature file <- system.file("examples", 'ndgeojson3.json', package = "geojson") ndgeo_read(file) ## Not run: # read from a file url <- "https://raw.githubusercontent.com/ropensci/geojson/main/inst/examples/ndgeojson1.json" f <- tempfile(fileext = ".geojsonl") download.file(url, f) x <- ndgeo_read(f) x unlink(f) # read from a URL url <- "https://raw.githubusercontent.com/ropensci/geojson/main/inst/examples/ndgeojson1.json" x <- ndgeo_read(url) x # geojson text sequences from file file <- system.file("examples", 'featurecollection2.geojson', package = "geojson") str <- paste0(readLines(file), collapse = " ") x <- featurecollection(str) outfile <- tempfile(fileext = ".geojson") ndgeo_write(x, outfile, sep = "\u001e\n") con <- file(outfile) readLines(con) close(con) ndgeo_read(outfile) unlink(outfile) ## End(Not run)
point class
point(x)
point(x)
x |
input |
x <- '{ "type": "Point", "coordinates": [100.0, 0.0] }' (y <- point(x)) geo_type(y) geo_pretty(y) geo_write(y, f <- tempfile(fileext = ".geojson")) jsonlite::fromJSON(f, FALSE) unlink(f) # add to a data.frame library('tibble') tibble(a = 1:5, b = list(y)) # as.geojson coercion as.geojson(x)
x <- '{ "type": "Point", "coordinates": [100.0, 0.0] }' (y <- point(x)) geo_type(y) geo_pretty(y) geo_write(y, f <- tempfile(fileext = ".geojson")) jsonlite::fromJSON(f, FALSE) unlink(f) # add to a data.frame library('tibble') tibble(a = 1:5, b = list(y)) # as.geojson coercion as.geojson(x)
polygon class
polygon(x)
polygon(x)
x |
input |
x <- '{ "type": "Polygon", "coordinates": [ [ [100.0, 0.0], [100.0, 1.0], [101.0, 1.0], [101.0, 0.0], [100.0, 0.0] ] ] }' (y <- polygon(x)) y[1] geo_type(y) geo_pretty(y) geo_write(y, f <- tempfile(fileext = ".geojson")) jsonlite::fromJSON(f, FALSE) unlink(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] ], [ [100.2, 0.2], [100.8, 0.2], [100.8, 0.8], [100.2, 0.8], [100.2, 0.2] ] ] }' (y <- polygon(x)) y[1] geo_type(y) geo_pretty(y) # add to a data.frame library('tibble') tibble(a = 1:5, b = list(y))
x <- '{ "type": "Polygon", "coordinates": [ [ [100.0, 0.0], [100.0, 1.0], [101.0, 1.0], [101.0, 0.0], [100.0, 0.0] ] ] }' (y <- polygon(x)) y[1] geo_type(y) geo_pretty(y) geo_write(y, f <- tempfile(fileext = ".geojson")) jsonlite::fromJSON(f, FALSE) unlink(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] ], [ [100.2, 0.2], [100.8, 0.2], [100.8, 0.8], [100.2, 0.8], [100.2, 0.2] ] ] }' (y <- polygon(x)) y[1] geo_type(y) geo_pretty(y) # add to a data.frame library('tibble') tibble(a = 1:5, b = list(y))
Add or get properties
properties_add(x, ..., .list = NULL) properties_get(x, property)
properties_add(x, ..., .list = NULL) properties_get(x, property)
x |
An object of class |
... |
Properties to be added, supports NSE as well as SE |
.list |
a named list of properties to add. must be named |
property |
(character) property name |
https://geojson.org/geojson-spec.html
# add properties x <- '{ "type": "LineString", "coordinates": [ [100.0, 0.0], [101.0, 1.0] ]}' (y <- linestring(x)) y %>% feature() %>% properties_add(population = 1000) ## add with a named list already created x <- '{ "type": "LineString", "coordinates": [ [100.0, 0.0], [101.0, 1.0] ]}' (y <- linestring(x)) props <- list(population = 1000, temperature = 89, size = 5) y %>% feature() %>% properties_add(.list = props) ## combination of NSE and .list x <- '{ "type": "LineString", "coordinates": [ [100.0, 0.0], [101.0, 1.0] ]}' (y <- linestring(x)) props <- list(population = 1000, temperature = 89, size = 5) y %>% feature() %>% properties_add(stuff = 4, .list = props) # features to featurecollection x <- '{ "type": "Point", "coordinates": [100.0, 0.0] }' point(x) %>% feature() %>% featurecollection() %>% properties_add(population = 10) # get property x <- '{ "type": "LineString", "coordinates": [ [100.0, 0.0], [101.0, 1.0] ]}' (y <- linestring(x)) x <- y %>% feature() %>% properties_add(population = 1000) properties_get(x, property = 'population')
# add properties x <- '{ "type": "LineString", "coordinates": [ [100.0, 0.0], [101.0, 1.0] ]}' (y <- linestring(x)) y %>% feature() %>% properties_add(population = 1000) ## add with a named list already created x <- '{ "type": "LineString", "coordinates": [ [100.0, 0.0], [101.0, 1.0] ]}' (y <- linestring(x)) props <- list(population = 1000, temperature = 89, size = 5) y %>% feature() %>% properties_add(.list = props) ## combination of NSE and .list x <- '{ "type": "LineString", "coordinates": [ [100.0, 0.0], [101.0, 1.0] ]}' (y <- linestring(x)) props <- list(population = 1000, temperature = 89, size = 5) y %>% feature() %>% properties_add(stuff = 4, .list = props) # features to featurecollection x <- '{ "type": "Point", "coordinates": [100.0, 0.0] }' point(x) %>% feature() %>% featurecollection() %>% properties_add(population = 10) # get property x <- '{ "type": "LineString", "coordinates": [ [100.0, 0.0], [101.0, 1.0] ]}' (y <- linestring(x)) x <- y %>% feature() %>% properties_add(population = 1000) properties_get(x, property = 'population')
Automatically detects and adds the class
to_geojson(x)
to_geojson(x)
x |
GeoJSON character string |
mp <- '{"type":"MultiPoint","coordinates":[[100,0],[101,1]]}' to_geojson(mp) ft <- '{"type":"Feature","properties":{"a":"b"}, "geometry":{"type": "MultiPoint","coordinates": [ [100.0, 0.0], [101.0, 1.0] ]}}' to_geojson(mp) fc <- '{"type":"FeatureCollection","features":[{"type":"Feature","properties":{"a":"b"}, "geometry":{"type": "MultiPoint","coordinates": [ [100.0, 0.0], [101.0, 1.0] ]}}]}' to_geojson(fc)
mp <- '{"type":"MultiPoint","coordinates":[[100,0],[101,1]]}' to_geojson(mp) ft <- '{"type":"Feature","properties":{"a":"b"}, "geometry":{"type": "MultiPoint","coordinates": [ [100.0, 0.0], [101.0, 1.0] ]}}' to_geojson(mp) fc <- '{"type":"FeatureCollection","features":[{"type":"Feature","properties":{"a":"b"}, "geometry":{"type": "MultiPoint","coordinates": [ [100.0, 0.0], [101.0, 1.0] ]}}]}' to_geojson(fc)