1
votes

I am trying to display several zipcodes (thus polygons...) on a leaflet map. Data is available as a geojson file here. I chose as an example some zip codes from Seattle.

I tried the following (reproducible example):

library(jsonlite) 
library(leaflet)  
url <- "https://raw.githubusercontent.com/openseattle/seattle-boundaries/master/data/zip-codes.geojson"
geojson <- fromJSON(url) 
map <- leaflet() %>% addTiles() %>% addGeoJSON(geojson)
map

I could not figure out how to properly set addGeoJSON parameters, and calling map only displays the leaflet() %>% addTiles() part...

Documentation is too light for the non json advanced user that I am:

geojson: a GeoJSON list, or character vector of length 1

How should I proceed? Thank you very much in advance for your views on this issue

Regards

1

1 Answers

3
votes

You just needed to not parse the geojson to a data.frame, fromJSON(url, FALSE)

library(jsonlite) 
library(leaflet)  
url <- "https://raw.githubusercontent.com/openseattle/seattle-boundaries/master/data/zip-codes.geojson"
geojson <- fromJSON(url, simplifyVector = FALSE) 
leaflet() %>% 
  addTiles() %>% 
  addGeoJSON(geojson) %>% 
  setView(lng = -122.2, lat = 47.6, zoom = 10)

enter image description here

addGeoJSON() will also accept a string, e.g.

geojson_str <- paste0(readLines(url), collapse = "")

then pass that to addGeoJSON