How to display multiple polygons at once using leaflet addGeoJSON() function?

蹲街弑〆低调 提交于 2019-12-12 03:37:03

问题


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:


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)

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

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

then pass that to addGeoJSON



来源:https://stackoverflow.com/questions/37933084/how-to-display-multiple-polygons-at-once-using-leaflet-addgeojson-function

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!