Errors loading GeoJSON exported from R's sf package into Mapbox Studio

241 views Asked by At

I am trying to learn how to use Mapbox Studio, but I can't figure out how to upload GeoJSON files that I export from R's sf package using the st_write() function. For example, see the R code below:

library(tigris)
states <- states()
states <- st_transform(states, crs = "EPSG:3857")
st_write(states, "states.geojson")

When I load the exported states.geojson file into Mapbox Studio, I get the following error:

enter image description here

When loading other datasets I had created and exported as GeoJSON, I got a different error - "Error calculating min/max zoom: Bounds invalid".

I am new to Mapbox and GeoJSON so don't know much about this - but I would like to be able to export GeoJSON files from R using st_write() and load them into Mapbox! Any insights on why I am getting these errors and how to avoid them would be much appreciated. Thanks.

1

There are 1 answers

3
Jindra Lacko On BEST ANSWER

I suggest that you avoid using EPSG:3857 when working with GeoJSON files, and stick to WGS84 (EPSG:4326).

The current GeoJSON standard allows only WGS84 - see https://datatracker.ietf.org/doc/html/rfc7946#section-4 (there is some wiggle room about lat-long vs. long-lat convention, but degrees are non negotiable; EPSG:3857 is in meters).

The former versions of the standard allowed other coordinate reference systems, which is why R / {sf} allows you to export the data, but it is extremely bad practice to do so. You are quite likely to run into issues such as the one you are facing now.

The official reason for the more strict specification was that GeoJSON using code is likely to end up in unconnected installations, and it may be impractical to keep it up with the evolving nature of EPSG database. The unofficial reason involved IT folks not being able to handle the truth spherical geometry.