geoJSON and leaflet

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

geoJSON and leaflet

Benjamin Pauget

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

cid:image005.png@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

 

 


_______________________________________________
R-sig-Geo mailing list
[hidden email]
https://stat.ethz.ch/mailman/listinfo/r-sig-geo
Reply | Threaded
Open this post in threaded view
|

Re: geoJSON and leaflet

Tim Salabim
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 <[hidden email]> 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

cid:image005.png@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

 

 

_______________________________________________
R-sig-Geo mailing list
[hidden email]
https://stat.ethz.ch/mailman/listinfo/r-sig-geo

_______________________________________________
R-sig-Geo mailing list
[hidden email]
https://stat.ethz.ch/mailman/listinfo/r-sig-geo
Reply | Threaded
Open this post in threaded view
|

Re: geoJSON and leaflet

Tim Salabim
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 <[hidden email]> 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

cid:image005.png@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

 

 

De : Tim Salabim <[hidden email]>
Envoyé : mardi 9 juillet 2019 15:14
À : Benjamin Pauget <[hidden email]>
Cc : [hidden email]
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 <[hidden email]> 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

cid:image005.png@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

 

 

_______________________________________________
R-sig-Geo mailing list
[hidden email]
https://stat.ethz.ch/mailman/listinfo/r-sig-geo


_______________________________________________
R-sig-Geo mailing list
[hidden email]
https://stat.ethz.ch/mailman/listinfo/r-sig-geo
Reply | Threaded
Open this post in threaded view
|

Re: geoJSON and leaflet

Benjamin Pauget

Hi Tim,

Thanks again for your response !

It fit perfectly !

Best

 

Ben

 

 

 

 

De : Tim Salabim <[hidden email]>
Envoyé : mardi 9 juillet 2019 16:25
À : Benjamin Pauget <[hidden email]>; r-sig-geo <[hidden email]>
Objet : Re: [R-sig-Geo] 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 <[hidden email]> 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,

 Ben

 

 

 

 

De : Tim Salabim <[hidden email]>
Envoyé : mardi 9 juillet 2019 15:14
À : Benjamin Pauget <[hidden email]>
Cc : [hidden email]
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 <[hidden email]> 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

cid:image005.png@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

 

 

_______________________________________________
R-sig-Geo mailing list
[hidden email]
https://stat.ethz.ch/mailman/listinfo/r-sig-geo


_______________________________________________
R-sig-Geo mailing list
[hidden email]
https://stat.ethz.ch/mailman/listinfo/r-sig-geo
Reply | Threaded
Open this post in threaded view
|

individual plots for farm fields in mapview

David Hine
Dear List

I want to present a scatter plot (on time) of soil sample data for each block in a tree crop, using r. I have a csv file with the value of the selected attribute, the block name, the x and y, and the dates for 9 locations, making 34 observations in total. I wish to present it as a leafpop::popupGraph on a mapview map. The user will be click on the point(a block centroid) within the block and see the history/plot for that location only.

The leafpop package manual provides the svg example using the meuse data set. Using it as the basis, I have a map with the points, and the popup. I have not been able to supply only the plot for the specific block to each point instance within it. My plot is a faceted ggplot with one facet for each block. Onclick on the map, each of my 9 locations shows the same full faceted plot (all 9). I want just the plot for that location.

The meuse example shows rep iteration through one plot, changing the colour of the value in that plot to highlight data point for the geographic location selected. While elegant, it is no guide to achieving my target.

Tim Salabim kindly pointed me a step forward suggesting lapply, but I do have not yet the depth of skill to successfully implement it. Below is an extract of the code, to show the steps I have taken so far.

I am not succeeding at getting the plot images. It also appears that the process loops through all 34 observations to make plots for each.

I am hoping the members can guide me a few steps further forward.

Here is the code:
block.graph <- function(select_organic_matter_by_blockname_with_xy_range, na.rm = TRUE){
    
     # create list of blocks in data to loop over
     block_list <- unique(som_xy$block_name)
    
     # create for loop to produce ggplot2 graphs
     for (i in seq_along(block_list)) {
        
         # create plot for each block in the dataframe
         soil_om <-
             ggplot(subset(som_xy, som_xy$block_name==block_list[i]),
                    aes(x= date, y = organic_matter)) +
             theme_light() +
             geom_point() +
             ylim(0, 10) +
             geom_hline(data = som_xy, linetype="dotted", colour="#ff710c",alpha = 0.5, aes(yintercept = range_min)) +
             geom_hline(data = som_xy, linetype="dotted", colour="#ff710c",alpha = 0.5, aes(yintercept = range_max))
         p <- (soil_om +
                   theme(
                       axis.text.x=element_text(angle=90,hjust=1, vjust=1),
                       axis.title.x = element_blank(),
                       axis.title.y = element_text(angle=90, hjust=0.5),
                       legend.title = element_blank(),
                       plot.title = element_text(hjust = 0.5)
                   )+
                   ggtitle("Organic Matter Time Series")+
                   ylab("%"))
     }}
 individ_plot <- lapply(som_xy$block_name, block.graph)
 
 mapview(collins_farm,
         data = select_organic_matter_by_blockname_with_xy_range,
         zcol = "block_name",
         popup= leafpop::popupImage(individ_plot))

Thanks in advance
David

Land and Water Management logo

David Hine
Land and Water Management PL
Level 7, 127 Creek St
Brisbane, Qld 4000
Australia

m: 0429 886 146 +61 429 886 146
t: (07) 4015 3470 +61 7 4015 3470

GeoPortal with example presentations of spatial data for horticulture users and others.
Land and Water Management PL



_______________________________________________
R-sig-Geo mailing list
[hidden email]
https://stat.ethz.ch/mailman/listinfo/r-sig-geo