ndgeo {geojson} | R Documentation |
Read and write newline-delimited GeoJSON (GeoJSON text sequences)
Description
There are various flavors of newline-delimited GeoJSON, all of which we aim to handle here. See Details for more.
Usage
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)
Arguments
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: |
Details
-
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
Value
a geojson
class object
Note
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)
References
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
Examples
# 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)