Skip to content
Prev 27453 / 29559 Next

geoJSON and leaflet

Hi Benjamin,
please respond to the list as well. Other people may have similar issues
and this way we can find the solutions online.

If you want to extract the data along with the geometries, here's how you
could do it:

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 = lapply(geom, function(i) st_polygon(list(i)))
# overwrite the geom column of data in geojson
geojson$data$geom = st_sfc(geom, crs = 4326)
# exract data from geojson and turn into an sf object
dat = st_as_sf(geojson$data)

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

This way, as you can see, you can use the tilde (~) notation to include
popups referring to a column of the data.

I don't think that what you get using RSONIO is any closer to a valid
geojson.

Best
Tim

On Tue, Jul 9, 2019 at 3:28 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/8810dbe8/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/8810dbe8/attachment.png>