Skip to content
Prev 27452 / 29559 Next

geoJSON and leaflet

Hi Benjamin,
What you get back from the jsonlite::fromJSON() call is a simplified list
with the data (including the geometry information) as a data frame in one
of the list slots. GeoJson is usually a character string. Therefore, if you
open your map in the browser and open the console (Ctrl + i) you will see
the error message: "Invalid GeoJson object".

I am no expert on geojson structure, but it seems that the data that you
request is not in standard format. I tried a few different ways of parsing
the data to a valid GeoJson string but did not have success. Maybe someone
else with more insight has some helpful ideas how to achieve this.

However, I found a workaround to get the polygons shown on the map using
library(sf) - see code below. Note, this only visualises the geometry
information only, so direct popup queries via "~" are not possible.

library(leaflet)
library(jsonlite)
library(sf)

url <- "
http://www.georisques.gouv.fr/api/v1/sis?rayon=1000&latlon=4.854899%2C%2045.763079&page=1&page_size=10
"

geojson <- jsonlite::fromJSON(url)

# convert coordinate arrays to matrices
geom = lapply(geojson$data$geom$coordinates, matrix, ncol = 2)
# create multipolygon from coordinate matrices
geom = st_cast(st_polygon(geom), "MULTIPOLYGON")

leaflet() %>%
  addTiles()%>%
  setView(lng = 4.854899, lat = 45.763079, zoom = 14) %>%
  addMarkers(lng = 4.872536, lat = 45.758321)%>%
  addPolygons(data = geom)

HTH,
Tim



On Tue, Jul 9, 2019 at 1:49 PM Benjamin Pauget <benjamin.pauget at tesora.fr>
wrote:
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://stat.ethz.ch/pipermail/r-sig-geo/attachments/20190709/60fb117b/attachment.html>

-------------- next part --------------
A non-text attachment was scrubbed...
Name: image001.png
Type: image/png
Size: 5152 bytes
Desc: not available
URL: <https://stat.ethz.ch/pipermail/r-sig-geo/attachments/20190709/60fb117b/attachment.png>