Normally you can read geojson files into R with trusty readOGR
, as illustrated here.
However, this fails for multifeature geojsons.
Reproducible example:
downloader::download("https://github.com/Robinlovelace/Creating-maps-in-R/raw/master/data/test-multifeature.geojson", "test.geojson")
test <- rgdal::readOGR("test.geojson", "OGRGeoJSON") # fails with:
Error in ogrInfo(dsn = dsn, layer = layer, encoding = encoding, use_iconv = use_iconv, :
Multiple incompatible geometries: wkbPoint: 98; wkbLineString: 660; wkbPolygon: 23
The error message is clear-enough and indicates a solution: split the features. Aside from doing this with regex, I don't know how, however.
Any ideas very much welcome.
The amazing thing: GitHub displays the data natively on the browser, whereas R cannot even (seemingly) read it in!
Alternative way to a solution:
test <- geojsonio::geojson_read("test.geojson")
You can use the
require_geomType
parameter for various GDAL functions to extract the features that you need: