I would like to add my own features to the map of Native Land:
> library(leaflet)
> url = "https://native-land.ca/coordinates/indigenousTerritories.json"
> NL <- sf::st_read(url)
> ggplot(NL) + geom_sf()
But I get the follow error:
Error in `geom_sf()`:
! Problem while converting geom to grob.
ℹ Error occurred in the 1st layer.
When I use this site to validate the GeoJSON, it indicates:
Line 1: Polygons and MultiPolygons should follow the right-hand rule
Any insight to use this dataset is welcome !
Previous efforts:
Not sure why, but this works:
native_land_json <- readLines(url)
leaflet() %>%
addProviderTiles("Esri.WorldImagery") %>%
addGeoJSON(native_land_json)
2
Answers
It looks like some of the coordinates in NL have 3 and not 2 coordinates. Row 84 if the first row to show the error. Compare NL$geometry[83] with NL$geometry[84]. The first point for in row 84 is: (-102.3898 22.67024 0).
I am not sure why there is an extra column in the data.
Here is a brute force method to separate the differences.
I am sure there is a nice sf method to determine the number of coordinate columns but I am not the familiar with the package. As it turns out there 2008 rows with a long&latitude and 51 row with long, lat and other.
Here is the plot of the 2008 rows:
Just to add to the previous answer. The excluded section of that ggplot error message, along with the backtrace, would give a few valuable hints:
So what could cause
rbind
to fail with that error? When checking theNL
object, it reportsDimension: XY, XYZ
andz_range: zmin: 0 zmax: 0
, first being an indicator that not all geometries share the same dimension:And we can see that the number of columns in coordinate matrices are indeed not matching:
To drop
Z
from all geometries in that set, we can usest_zm()
,note the
Dimension: XY
; resultingsf
object is also acceptable for ggplot:One option to detect/subset individual geometries with Z dimension is through checking class inheritance (
XY
vsXYZ
) :Created on 2024-01-22 with reprex v2.0.2