Converting between GeoJSON and sf

knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)

What it does

Quickly converts between GeoJSON and sf objects

What it doesn't do

As per GeoJSON (RFC 7946 specification), foreign members are ignored, and nested objects and arrays inside the properties object are converted to string/characters.

library(geojsonsf)
# library(sf)

You would also typically load library(sf) to take advantange of its plot methods.

GeoJSON

Geometry

p <- '{"type":"Point","coordinates":[0,0]}'
sf <- geojson_sf(p)
head(sf)

Geometry Collection

gc <- '{
  "type": "GeometryCollection",
  "geometries": [
    {"type": "Point", "coordinates": [100.0, 0.0]},
    {"type": "LineString", "coordinates": [[101.0, 0.0], [102.0, 1.0]]},
    {"type" : "MultiPoint", "coordinates" : [[0,0], [1,1], [2,2]]}
  ]
}'
sf <- geojson_sf(gc)
head(sf)

Some web-plotting libraries (such as googleway) can't plot GeometryCollections directly. Therefore, you can set expand_geometries to TRUE and it will extract each geometry from a GeometryCollection and create a row for each one

gc <- '{
  "type": "GeometryCollection",
  "geometries": [
    {"type": "Point", "coordinates": [100.0, 0.0]},
    {"type": "LineString", "coordinates": [[101.0, 0.0], [102.0, 1.0]]},
    {"type" : "MultiPoint", "coordinates" : [[0,0], [1,1], [2,2]]}
  ]
}'
sf <- geojson_sf(gc, expand_geometries = T)
head(sf)

Feature

f <- '{
    "type": "Feature",
    "properties": {"id":1,"value":100,"text":"the quick brown fox"},
    "geometry": {
      "type": "LineString", 
      "coordinates": [[101.0, 0.0], [102.0, 1.0]]
      }
    }'
sf <- geojson_sf(f)
head(sf)

Feature Collection

fc <- '{
  "type": "FeatureCollection",
  "features": [
  {
    "type": "Feature",
    "properties": {"foo" : "feature 1.1", "bar" : "feature 1.2"},
    "geometry": {"type": "Point", "coordinates": [100.0, 0.0]}
  },
  {
    "type": "Feature",
    "properties": null,
    "geometry": {"type": "LineString", "coordinates": [[101.0, 0.0], [102.0, 1.0]]}
  },
  {
    "type": "Feature",
        "properties": {"foo" : "feature 3.1", "bar" : "feature 3.2"},
        "geometry": {"type": "LineString", "coordinates": [[101.0, 0.0], [102.0, 1.0]]}
    }
 ]
}'
sf <- geojson_sf(fc)
head(sf)

Vectors of GeoJSON

geo <- c(p, gc, f, fc)
sf <- geojson_sf(geo)
head(sf)

JSON Arrays of GeoJSON

js <- '[
{
  "type": "FeatureCollection",
  "features": [
  {
    "type": "Feature",
    "properties": null,
    "geometry": {"type": "Point", "coordinates": [100.0, 0.0]}
  },
  {
    "type": "Feature",
    "properties": null,
    "geometry": {"type": "LineString", "coordinates": [[201.0, 0.0], [102.0, 1.0]]}
  },
  {
    "type": "Feature",
        "properties": null,
        "geometry": {"type": "LineString", "coordinates": [[301.0, 0.0], [102.0, 1.0]]}
    }
 ]
},
{
  "type": "FeatureCollection",
    "features": [
    {
      "type": "Feature",
      "properties": null,
      "geometry": {"type": "Point", "coordinates": [100.0, 0.0]}
    },
    {
      "type": "Feature",
      "properties": null,
      "geometry": {"type": "LineString", "coordinates": [[501.0, 0.0], [102.0, 1.0]]}
    },
    {
      "type": "Feature",
      "properties": null,
      "geometry": {"type": "LineString", "coordinates": [[601.0, 0.0], [102.0, 1.0]]}
    }
  ]
}
]'

sf <- geojson_sf(js)
head(sf)

Reading from URL

url <- "http://eric.clst.org/assets/wiki/uploads/Stuff/gz_2010_us_050_00_500k.json"
sf <- geojson_sf(url)
#head(sf)

Reading from file

sf <- geojson_sf(system.file("examples", "geo_melbourne.geojson", package = "geojsonsf"))
head(sf)

sf to GeoJSON

geo <- sf_geojson(sf)
substr(geo, 1, 80)

Atomising

Sometimes it's useful to convert each row of an sf object into it's own GeoJSON object, for example when you want to keep each geometry separate in a database. Set atomise = TRUE to return a vector of GeoJSON

geo <- sf_geojson(sf, atomise = T)

now geo is a vector of GeoJSON

substr(geo[1], 1, 80)
substr(geo[2], 1, 80)

Well-known Text

It also converts GeoJSON to Well-Known Text and returns a data.frame

fc <- '{
  "type": "FeatureCollection",
  "features": [
  {
    "type": "Feature",
    "properties": {"foo" : "feature 1.1", "bar" : "feature 1.2"},
    "geometry": {"type": "Point", "coordinates": [100.0, 0.0]}
  },
  {
    "type": "Feature",
    "properties": null,
    "geometry": {"type": "LineString", "coordinates": [[101.0, 0.0], [102.0, 1.0]]}
  },
  {
    "type": "Feature",
        "properties": {"foo" : "feature 3.1", "bar" : "feature 3.2"},
        "geometry": {"type": "LineString", "coordinates": [[101.0, 0.0], [102.0, 1.0]]}
    }
 ]
}'
df <- geojson_wkt(fc)
head(df)


Try the geojsonsf package in your browser

Any scripts or data that you put into this service are public.

geojsonsf documentation built on May 31, 2022, 1:05 a.m.