There are two problems here. First, the API returns a fragment of a GeoJSON file, which would be a valid file for st_read
to consume if it wasn't a fragment. One fix to this would be to use paste
functions (or other text manipulation) to build a complete GeoJSON file and then read that.
The downloadable "territories" JSON file given here https://native-land.ca/resources/api-docs/ is a valid geoJSON file, so you could use that if you want to get all the data and aren't doing anything more complex with their API calls.
But then you hit the second problem.
The second problem is that the data has a mixture of 2-D polygons and 3-D polygons. While I think this is valid under the OGC Simple Features data model (QGIS and other GDAL/OGR tools read it in okay) it seems the R sf
package isn't happy with it:
> terr = st_read("./indigenousTerritories.json")
Reading layer `indigenousTerritories' from data source
`/path/indigenousTerritories.json'
using driver `GeoJSON'
Error in CPL_get_z_range(obj, 3) : z error - expecting three columns;
I can't see any option to handle this except maybe the "type" parameter, which still gives the same error even when trying to read as a "GEOMETRYCOLLECTION" which I thought might treat each geometry separately, so that the varying dimensions would be allowed for, but no:
> terr = st_read("./indigenousTerritories.json", type=7)
Reading layer `indigenousTerritories' from data source
`/path/indigenousTerritories.json'
using driver `GeoJSON'
converted into: GEOMETRYCOLLECTION
Error in CPL_get_z_range(obj, 1) : z error - expecting three columns;
Fix I have now is to use the ogr2ogr
command-line tool with its -dim
option to convert to something with consistent dimension:
$ ogr2ogr indigenousTerritories.gpkg indigenousTerritories.json -dim XY
Warning 1: Field 'ID' already exists. Renaming it as 'ID2'
Then you can read from that GeoPackage:
> library(sf)
Linking to GEOS 3.8.0, GDAL 3.0.4, PROJ 6.3.1; sf_use_s2() is TRUE
> terr = st_read("./indigenousTerritories.gpkg")
Reading layer `indigenousTerritories' from data source
`/nobackup/rowlings/Downloads/SO/indigenousTerritories.gpkg'
using driver `GPKG'
Simple feature collection with 2024 features and 6 fields
Geometry type: MULTIPOLYGON
Dimension: XY
Bounding box: xmin: -189.8148 ymin: -56.02359 xmax: 178.5443 ymax: 83.77334
Geodetic CRS: WGS 84
If you don't have the command line tools you can get them with the gdalUtils
R package and call them from R:
> library(gdalUtils)
> ogr2ogr("./indigenousTerritories.json", "indigenousTerritories.gpkg", dim="XY")
Warning 1: Field 'ID' already exists. Renaming it as 'ID2'
character(0)
> terr = st_read("./indigenousTerritories.gpkg", quiet=TRUE)
>
This issue with mixed dimensions has come up before and was marked closed: https://github.com/r-spatial/sf/issues/1592 so I'm not sure why its still failing on my sf: 1.0-5
and later, according to reports.
st_read
.sf
doesn't like and throws an error. This is also a problem with the downloaded geoJSON objects of the whole data set, like: 149613070.v2.pressablecdn.com/coordinates/…