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
)
string or vector of GeoJSON, or a URL or file pointing to a geojson file
logical indicating whether to unnest GEOMETRYCOLLECTION rows. see details
user input for coordinate reference system object
well-known text for coordinate reference system object
deprecated. coordinate reference system. See Details
deprecated. proj4string. See Details
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)
#> [[1]]
#> [1] 0 0
#> attr(,"class")
#> [1] "XY" "POINT" "sfg"
#>
#> attr(,"n_empty")
#> [1] 0
#> attr(,"crs")
#> $input
#> [1] "4326"
#>
#> $wkt
#> [1] "GEOGCS[\"WGS 84\",\n DATUM[\"WGS_1984\",\n SPHEROID[\"WGS 84\",6378137,298.257223563,\n AUTHORITY[\"EPSG\",\"7030\"]],\n AUTHORITY[\"EPSG\",\"6326\"]],\n PRIMEM[\"Greenwich\",0,\n AUTHORITY[\"EPSG\",\"8901\"]],\n UNIT[\"degree\",0.0174532925199433,\n AUTHORITY[\"EPSG\",\"9122\"]],\n AXIS[\"Latitude\",NORTH],\n AXIS[\"Longitude\",EAST],\n AUTHORITY[\"EPSG\",\"4326\"]]"
#>
#> attr(,"class")
#> [1] "crs"
#> attr(,"class")
#> [1] "sfc_POINT" "sfc"
#> attr(,"precision")
#> [1] 0
#> attr(,"bbox")
#> xmin ymin xmax ymax
#> 0 0 0 0
#> attr(,"class")
#> [1] "bbox"
geojson <- '[
{ "type":"Point","coordinates":[0,0]},
{"type":"LineString","coordinates":[[0,0],[1,1]]}
]'
geojson_sfc( geojson )
#> [[1]]
#> [1] 0 0
#> attr(,"class")
#> [1] "XY" "POINT" "sfg"
#>
#> [[2]]
#> [,1] [,2]
#> [1,] 0 0
#> [2,] 1 1
#> attr(,"class")
#> [1] "XY" "LINESTRING" "sfg"
#>
#> attr(,"classes")
#> [1] "POINT" "LINESTRING"
#> attr(,"n_empty")
#> [1] 0
#> attr(,"crs")
#> $input
#> [1] "4326"
#>
#> $wkt
#> [1] "GEOGCS[\"WGS 84\",\n DATUM[\"WGS_1984\",\n SPHEROID[\"WGS 84\",6378137,298.257223563,\n AUTHORITY[\"EPSG\",\"7030\"]],\n AUTHORITY[\"EPSG\",\"6326\"]],\n PRIMEM[\"Greenwich\",0,\n AUTHORITY[\"EPSG\",\"8901\"]],\n UNIT[\"degree\",0.0174532925199433,\n AUTHORITY[\"EPSG\",\"9122\"]],\n AXIS[\"Latitude\",NORTH],\n AXIS[\"Longitude\",EAST],\n AUTHORITY[\"EPSG\",\"4326\"]]"
#>
#> attr(,"class")
#> [1] "crs"
#> attr(,"class")
#> [1] "sfc_GEOMETRY" "sfc"
#> attr(,"precision")
#> [1] 0
#> attr(,"bbox")
#> xmin ymin xmax ymax
#> 0 0 1 1
#> attr(,"class")
#> [1] "bbox"
if (FALSE) { # \dontrun{
## GeoJSON at a url
myurl <- "http://eric.clst.org/assets/wiki/uploads/Stuff/gz_2010_us_050_00_500k.json"
sf <- geojson_sfc(myurl)
} # }