[R-sig-Geo] geoJSON and leaflet

Tim Salabim t|m@@ppe|h@n@ @end|ng |rom gm@||@com
Tue Jul 9 16:24:58 CEST 2019


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 using tesora.fr>
wrote:

> Dear Tim,
>
> Tanks you soooo much for your response !
>
> I am a beginner and working with geospatial is still unclear.
>
>
>
> I have tried to open my json file with “from_JSON” and I have another
> format of my data :
>
> url <- "
> http://www.georisques.gouv.fr/api/v1/sis?rayon=5000&latlon=4.854899%2C%2045.763079&page=1&page_size=10
> "
>
> geojson4 <- RJSONIO::fromJSON(url)
>
>
>
> Do you think this format is more relevant and will allow to display the
> pop’up info ?
>
>
>
> Thank you again for your code (I just pass the two last day on trying to
> display the polygons) !
>
>
>
> Best regards,
>
>
>
>
>
>
>
> *Benjamin PAUGET*
>
> Responsable R&D
>
> [image: cid:image005.png using 01D4F3BB.3531D840]
>
>
>
> +33 (0)1 81 94 13 70
>
> +33 (0)6 47 01 85 92
>
> Le Visium
>
> 22 Av Aristide Briand
>
> 94110 ARCUEIL
>
> https://tesora.fr/
>
> Linkedin <https://www.linkedin.com/company/tesora-france>
>
>
>
>
>
> *De :* Tim Salabim <tim.appelhans using gmail.com>
> *Envoyé :* mardi 9 juillet 2019 15:14
> *À :* Benjamin Pauget <benjamin.pauget using tesora.fr>
> *Cc :* r-sig-geo using r-project.org
> *Objet :* Re: [R-sig-Geo] 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 using tesora.fr>
> wrote:
>
> Hi,
>
> I’m writting because I have some trouble with a geoJSON file and a leaflet.
>
> I’m trying to display polygon on a leaflet map, but nothing append ☹
>
> I have no error message.
>
> Do you have some advice/ideas?
>
>
>
> Best regards
>
>
>
>
>
> Here is my code :
>
> library(leaflet)
>
> library(jsonlite)
>
> 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)
>
> leaflet() %>%
>
>   addTiles()%>%
>
>   setView(lng = 4.854899, lat = 45.763079, zoom = 14) %>%
>
>   addMarkers(lng = 4.872536, lat = 45.758321)%>%
>
>   addGeoJSON(geojson) # doesn’t work by using geojson$data or
> geojson$data$geom
>
>
>
>
>
>
>
>
>
> *Benjamin PAUGET*
>
> Responsable R&D
>
> [image: cid:image005.png using 01D4F3BB.3531D840]
>
>
>
> +33 (0)1 81 94 13 70
>
> +33 (0)6 47 01 85 92
>
> Le Visium
>
> 22 Av Aristide Briand
>
> 94110 ARCUEIL
>
> https://tesora.fr/
>
> Linkedin <https://www.linkedin.com/company/tesora-france>
>
>
>
>
>
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo using r-project.org
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>
>

-------------- 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>


More information about the R-sig-Geo mailing list