Title: | GeoJSON to Simple Feature Converter |
---|---|
Description: | Converts Between GeoJSON and simple feature objects. |
Authors: | David Cooley [aut, cre] |
Maintainer: | David Cooley <[email protected]> |
License: | MIT + file LICENSE |
Version: | 2.0.3 |
Built: | 2024-11-04 06:40:45 UTC |
Source: | CRAN |
Converts data.frame objects to GeoJSON. Each row is considerd a POINT
df_geojson( df, lon, lat, z = NULL, m = NULL, atomise = FALSE, simplify = TRUE, digits = NULL, factors_as_string = TRUE )
df_geojson( df, lon, lat, z = NULL, m = NULL, atomise = FALSE, simplify = TRUE, digits = NULL, factors_as_string = TRUE )
df |
data.frame |
lon |
column of |
lat |
column of |
z |
column of |
m |
column of |
atomise |
logical indicating if the data.frame should be converted into a vector of GeoJSON objects |
simplify |
logical indicating if data.frame without property columns should simplify
( |
digits |
integer specifying the number of decimal places to round numerics.
numeric values are coorced using |
factors_as_string |
logical indicating if factors should be treated as strings. Defaults to TRUE. |
vector of GeoJSON
df <- data.frame(lon = c(1:5, NA), lat = c(1:5, NA), id = 1:6, val = letters[1:6]) df_geojson( df, lon = "lon", lat = "lat") df_geojson( df, lon = "lon", lat = "lat", atomise = TRUE) df <- data.frame(lon = c(1:5, NA), lat = c(1:5, NA) ) df_geojson( df, lon = "lon", lat = "lat") df_geojson( df, lon = "lon", lat = "lat", simplify = FALSE) df <- data.frame(lon = c(1:5), lat = c(1:5), elevation = c(1:5) ) df_geojson( df, lon = "lon", lat = "lat", z = "elevation") df_geojson( df, lon = "lon", lat = "lat", z = "elevation", simplify = FALSE) df <- data.frame(lon = c(1:5), lat = c(1:5), elevation = c(1:5), id = 1:5 ) df_geojson( df, lon = "lon", lat = "lat", z = "elevation") df_geojson( df, lon = "lon", lat = "lat", z = "elevation", atomise = TRUE) ## to sf objects geo <- df_geojson( df, lon = "lon", lat = "lat", z = "elevation") sf <- geojson_sf( geo )
df <- data.frame(lon = c(1:5, NA), lat = c(1:5, NA), id = 1:6, val = letters[1:6]) df_geojson( df, lon = "lon", lat = "lat") df_geojson( df, lon = "lon", lat = "lat", atomise = TRUE) df <- data.frame(lon = c(1:5, NA), lat = c(1:5, NA) ) df_geojson( df, lon = "lon", lat = "lat") df_geojson( df, lon = "lon", lat = "lat", simplify = FALSE) df <- data.frame(lon = c(1:5), lat = c(1:5), elevation = c(1:5) ) df_geojson( df, lon = "lon", lat = "lat", z = "elevation") df_geojson( df, lon = "lon", lat = "lat", z = "elevation", simplify = FALSE) df <- data.frame(lon = c(1:5), lat = c(1:5), elevation = c(1:5), id = 1:5 ) df_geojson( df, lon = "lon", lat = "lat", z = "elevation") df_geojson( df, lon = "lon", lat = "lat", z = "elevation", atomise = TRUE) ## to sf objects geo <- df_geojson( df, lon = "lon", lat = "lat", z = "elevation") sf <- geojson_sf( geo )
GeoJSON data of Melbourne's Inner suburbs.
geo_melbourne
geo_melbourne
An object of class geojson
(inherits from json
) of length 1.
Converts GeoJSON to an 'sf' object
geojson_sf( geojson, expand_geometries = FALSE, input = NULL, wkt = NULL, crs = NULL, proj4string = NULL, buffer_size = 1024 )
geojson_sf( geojson, expand_geometries = FALSE, input = NULL, wkt = NULL, crs = NULL, proj4string = NULL, buffer_size = 1024 )
geojson |
string or vector of GeoJSON, or a URL or file pointing to a geojson file |
expand_geometries |
logical indicating whether to unnest GEOMETRYCOLLECTION rows. see details |
input |
user input for coordinate reference system object |
wkt |
well-known text for coordinate reference system object |
crs |
deprecated. coordinate reference system. See Details |
proj4string |
deprecated. proj4string. See Details |
buffer_size |
size of buffer used when reading a file from disk. Defaults 1024 |
specifying expand_geometries = TRUE
will expand individual GEOMETRYCOLLECTION
geometries to their own row in the resulting 'sf' object. If the geometries are part
of a Feature
(i.e., with properties), the properties will be repeated on each row.
The GEOMETRYCOLLECTION
information is not kept when using expand_geometries = TRUE
. Therefore,
it is not possible to reconstruct the GEOMETRYCOLLECTION
after unnesting it.
Geojson specification RFC7946 https://tools.ietf.org/html/rfc7946#page-12 says all CRS should be the World Geodetic System 1984 (WGS 84) [WGS84] datum, with longitude and latitude units of decimal degrees. This is equivalent to the coordinate reference system identified by the Open Geospatial Consortium (OGC) URN urn:ogc:def:crs:OGC::CRS84
geojson_sfc
and geojson_sf
automatically set the CRS to WGS 84.
The fields input
and wkt
let you to overwrite the defaults.
## character string of GeoJSON ## load 'sf' for print methods # library(sf) geojson <- '{ "type" : "Point", "coordinates" : [0, 0] }' geojson_sf(geojson) ## Not run: ## GeoJSON at a url myurl <- "http://eric.clst.org/assets/wiki/uploads/Stuff/gz_2010_us_050_00_500k.json" sf <- geojson_sf(myurl) ## End(Not run)
## character string of GeoJSON ## load 'sf' for print methods # library(sf) geojson <- '{ "type" : "Point", "coordinates" : [0, 0] }' geojson_sf(geojson) ## Not run: ## GeoJSON at a url myurl <- "http://eric.clst.org/assets/wiki/uploads/Stuff/gz_2010_us_050_00_500k.json" sf <- geojson_sf(myurl) ## End(Not run)
Extracts geometries from GeoJSON and returns an 'sfc' object
geojson_sfc( geojson, expand_geometries = FALSE, input = NULL, wkt = NULL, crs = NULL, proj4string = NULL, buffer_size = 1024 )
geojson_sfc( geojson, expand_geometries = FALSE, input = NULL, wkt = NULL, crs = NULL, proj4string = NULL, buffer_size = 1024 )
geojson |
string or vector of GeoJSON, or a URL or file pointing to a geojson file |
expand_geometries |
logical indicating whether to unnest GEOMETRYCOLLECTION rows. see details |
input |
user input for coordinate reference system object |
wkt |
well-known text for coordinate reference system object |
crs |
deprecated. coordinate reference system. See Details |
proj4string |
deprecated. proj4string. See Details |
buffer_size |
size of buffer used when reading a file from disk. Defaults 1024 |
specifying expand_geometries = TRUE
will expand individual GEOMETRYCOLLECTION
geometries to their own row in the resulting 'sf' object. If the geometries are part
of a Feature
(i.e., with properties), the properties will be repeated on each row.
The GEOMETRYCOLLECTION
information is not kept when using expand_geometries = TRUE
. Therefore,
it is not possible to reconstruct the GEOMETRYCOLLECTION
after unnesting it.
Geojson specification RFC7946 https://tools.ietf.org/html/rfc7946#page-12 says all CRS should be the World Geodetic System 1984 (WGS 84) [WGS84] datum, with longitude and latitude units of decimal degrees. This is equivalent to the coordinate reference system identified by the Open Geospatial Consortium (OGC) URN urn:ogc:def:crs:OGC::CRS84
geojson_sfc
and geojson_sf
automatically set the CRS to WGS 84.
The fields input
and wkt
let you to overwrite the defaults.
## character string of GeoJSON ## load 'sf' for print methods # library(sf) geojson <- '{ "type":"Point","coordinates":[0,0] }' geojson_sfc(geojson) geojson <- '[ { "type":"Point","coordinates":[0,0]}, {"type":"LineString","coordinates":[[0,0],[1,1]]} ]' geojson_sfc( geojson ) ## Not run: ## GeoJSON at a url myurl <- "http://eric.clst.org/assets/wiki/uploads/Stuff/gz_2010_us_050_00_500k.json" sf <- geojson_sfc(myurl) ## End(Not run)
## character string of GeoJSON ## load 'sf' for print methods # library(sf) geojson <- '{ "type":"Point","coordinates":[0,0] }' geojson_sfc(geojson) geojson <- '[ { "type":"Point","coordinates":[0,0]}, {"type":"LineString","coordinates":[[0,0],[1,1]]} ]' geojson_sfc( geojson ) ## Not run: ## GeoJSON at a url myurl <- "http://eric.clst.org/assets/wiki/uploads/Stuff/gz_2010_us_050_00_500k.json" sf <- geojson_sfc(myurl) ## End(Not run)
Converts GeoJSON to Well-Known Text
geojson_wkt(geojson)
geojson_wkt(geojson)
geojson |
string or vector of GeoJSON, or a URL or file pointing to a geojson file |
data.frame with a 'geometry' column of well-known text
geojson <- '{ "type" : "Point", "coordinates" : [0, 0] }' geojson_wkt(geojson)
geojson <- '{ "type" : "Point", "coordinates" : [0, 0] }' geojson_wkt(geojson)
Converts 'sf' objects to GeoJSON
sf_geojson( sf, atomise = FALSE, simplify = TRUE, digits = NULL, factors_as_string = TRUE )
sf_geojson( sf, atomise = FALSE, simplify = TRUE, digits = NULL, factors_as_string = TRUE )
sf |
simple feature object |
atomise |
logical indicating if the sf object should be converted into a vector of GeoJSON objects |
simplify |
logical indicating if sf objects without property columns should simplify
( |
digits |
integer specifying the number of decimal places to round numerics.
numeric values are coorced using |
factors_as_string |
logical indicating if factors should be treated as strings. Defaults to TRUE. |
vector of GeoJSON
## Not run: library(sf) sf <- sf::st_sf(geometry = sf::st_sfc(list(sf::st_point(c(0,0)), sf::st_point(c(1,1))))) sf$id <- 1:2 sf_geojson(sf) sf_geojson(sf, atomise = T) ls <- st_linestring(rbind(c(0,0),c(1,1),c(2,1))) mls <- st_multilinestring(list(rbind(c(2,2),c(1,3)), rbind(c(0,0),c(1,1),c(2,1)))) sfc <- st_sfc(ls,mls) sf <- st_sf(sfc) sf_geojson( sf ) sf_geojson( sf, simplify = FALSE ) ## End(Not run)
## Not run: library(sf) sf <- sf::st_sf(geometry = sf::st_sfc(list(sf::st_point(c(0,0)), sf::st_point(c(1,1))))) sf$id <- 1:2 sf_geojson(sf) sf_geojson(sf, atomise = T) ls <- st_linestring(rbind(c(0,0),c(1,1),c(2,1))) mls <- st_multilinestring(list(rbind(c(2,2),c(1,3)), rbind(c(0,0),c(1,1),c(2,1)))) sfc <- st_sfc(ls,mls) sf <- st_sf(sfc) sf_geojson( sf ) sf_geojson( sf, simplify = FALSE ) ## End(Not run)
Converts 'sfc' objects to GeoJSON
sfc_geojson(sfc, digits = NULL)
sfc_geojson(sfc, digits = NULL)
sfc |
simple feature collection object |
digits |
integer specifying the number of decimal places to round numeric coordinates.
numeric values are coorced using |
vector of GeoJSON
## Not run: library(sf) sf <- sf::st_sfc(list(sf::st_point(c(0,0)), sf::st_point(c(1,1)))) sfc_geojson(sf) ## End(Not run)
## Not run: library(sf) sf <- sf::st_sfc(list(sf::st_point(c(0,0)), sf::st_point(c(1,1)))) sfc_geojson(sf) ## End(Not run)