0

Learning R for mapping with Leaflet.

Seems like a great guide here, but I’m stuck with a Chropleths map.

https://rstudio.github.io/leaflet/choropleths.html

Download the USA geoson file. It’s in my local R directory, the path is correct. Have changed zero. My understanding it’s a solid geoson file for the USA.

Used in the demo.

https://leafletjs.com/examples/choropleth/us-states.js

I can see the file is there, no problems.

file.exists("json/us-states.geojson")

Returns [TRUE]

Yet I get an error. It’s invalid.

Image 1

So I got over to http://geojsonlint.com/. Paste the entire file in, and I get the attached error. It's not a valid geoson file.

Image 2.

So I’m stuck at square 1. My file is there, yet somehow it’s been corrupted, and not able to be read. Not sure how that could happen.

Wondering what I’m doing wrong here. Would love to get to square 2.

thanks :-)

RStudio

GEOSON link checking error

6
  • 1
    You linked a JavaScript script (something that can be executed by a JS engine), not a JSON object (an object that can be parsed using a JS engine). The big difference is the var statesData = part, and the semicolon at the very end. Get rid of those, and it will be just the JSON object, without the assignment that would be part of a JS script.
    – Brian
    Commented Apr 27, 2020 at 17:11
  • hmmmm. I removed the var statesData = and the ";" at the end. Still getting the same error both in R and with the link checker. Here's the link to the file i'm using, saved, then removed offending parts. Same errors. leafletjs.com/examples/choropleth/us-states.js Commented Apr 27, 2020 at 17:35
  • 1
    I tested it with sf::read_sf and it worked fine, although it gave me a warning about potentially invalid bounding box. It appears that some of the longitudes extend to -188.90491 to wrap Alaska across the antimeridian.
    – Brian
    Commented Apr 27, 2020 at 18:08
  • 1
    Thanks for your help! I can't mark u as solved I see, since i posted an answer. But upvoted both your comments if that can help tally scores in anyway. Commented Apr 27, 2020 at 23:33
  • Posting (and accepting!) your own answer is encouraged, but please do encapsulate any clues you found in the comments that solved the actual problem.
    – Brian
    Commented Apr 28, 2020 at 0:45

1 Answer 1

0

Boom!

Downloaded a new json file, for some reason the old one was not working with the latest R + geojsonio::geojson_read

https://eric.clst.org/tech/usgeojson/

This right hand rule looked like a thing to keep an eye on.

In 2016, the geoJSON specification became more strict and many old geoJSONs will not work in new tools like Mapbox, or throw errors. GeoJSONLint has not updated and many of its polygons are broken. It’s important to “rewind" your geoJSONs so that they fit with this more tight format.

https://mapster.me/right-hand-rule-geojson-fixer/

Working! :-)

enter image description here

Not the answer you're looking for? Browse other questions tagged or ask your own question.