SymbolixAU / googleway

R Package for accessing and plotting Google Maps
http://symbolixau.github.io/googleway/
Other
233 stars 46 forks source link

add label to geojson #257

Open sm2607 opened 2 years ago

sm2607 commented 2 years ago

Hi, I was trying to add label to geojson, similar to polygon with attribute, mouse_over, but it through an error that its only accept TRUE/FALSE, can you please suggest how to add label to geojson feature.

dcooley commented 2 years ago

can you give me the code you're using please?

sm2607 commented 2 years ago

geo <- sf_geojson(sf_data) style <- list(fillColor = 'color',fillOpacity=0.4, strokeColor = "blue", strokeWeight = 0) map1<-map1 %>% add_geojson(data = geo, style = style, mouse_over=T)

dcooley commented 2 years ago

Can you make it reproducible please, so I can copy it into my own R session and reproduce what you're doing?

sm2607 commented 2 years ago

As in this example, if we need to add label to each polygon/shape , eg. as we hover over shape it shows "area" and "name" of that shape

library(googleway) style <- list(fillColor = "red" , strokeColor = "blue", strokeWeight = 0.5) google_map(key = api_key) %>% add_geojson(data = geo_melbourne, style = style)

dcooley commented 2 years ago

The only way this is possible is to convert the geojosn to an sf object and use the add_polygons()

sf <- geojsonsf::geojson_sf(geo_melbourne)

google_map(key = secret::get_secret("GOOGLE")) %>%
  add_polygons(data = sf, mouse_over = "SA2_NAME")
sm2607 commented 2 years ago

Firstly I tried with "add_polygon" function, but my shapefile has almost ~300 circles, and "add_polygon" function is not able to load shapefile even after ~30 minutes, So, as a workaround I was trying geojson

dcooley commented 2 years ago

Can I suggest you try with {mapdeck} layers, as they can easily handle that many circles:

google_map(key = secret::get_secret("GOOGLE")) %>%
  mapdeck::add_dependencies() %>%
  mapdeck::add_polygon(data = sf, tooltip = "SA2_NAME")

Article / Vignette is here

sm2607 commented 2 years ago

hi, I tried both (example from article and in comment), but layer is not loading in my case. Can you please suggest what is missing here.

library(mapdeck) library(googleway) set_key(api_key)

googleway::google_map() %>% mapdeck::add_dependencies() %>% mapdeck::add_path( data = roads , stroke_colour = "ROAD_TYPE" , legend = T ) image

dcooley commented 2 years ago

could you try openeing it in a browser?

sm2607 commented 2 years ago

I tried below example code,

library(shiny)
library(shinydashboard)
library(mapdeck)
library(googleway)

ui <- dashboardPage(

  dashboardHeader(),
  dashboardSidebar(),
  dashboardBody(
    mapdeck::mapdeck_dependencies()
    , box(
      width = 12
      , google_mapOutput(
        outputId = "map"
        , height = "600"
      )
    )
  )
)

server <- function(input, output) {

  output$map <- renderGoogle_map({
    sf <- geojsonsf::geojson_sf(geo_melbourne)
    google_map() %>%
      mapdeck::add_dependencies() %>%
      mapdeck::add_polygon(data = sf,stroke_width = 5,stroke_colour = 'black', tooltip = "SA2_NAME")
  })
}

shinyApp(ui, server)

In browser, able to see shapefile but tooltip, stroke_width etc. features are not working

image

dcooley commented 2 years ago

stroke_colour is expecting either a value / column from sf, or a hex String:

  output$map <- renderGoogle_map({
    sf <- geojsonsf::geojson_sf(geo_melbourne)
    google_map() %>%
      mapdeck::add_dependencies() %>%
      mapdeck::add_polygon(data = sf,stroke_width = 5,stroke_colour = '#000000', tooltip = "SA2_NAME")
  })

For me the tooltip is working:

Screen Shot 2022-09-20 at 8 27 20 am

Do you get any errors in the javascript console? (In Chrome press Ctrl / Cmd + Shift + C, and go to the 'console' tab)?

Next thing to try is to install the github / dev versions:

remotes::install_github("SymbolixAU/googleway")
remotes::install_github("dcooley/geometries")
remotes::install_github("dcooley/sfheaders")
remotes::install_github("SymbolixAU/spatialwidget")
remotes::install_github("SymbolixAU/mapdeck")
sm2607 commented 2 years ago

yes I got below console error:-

image

dcooley commented 2 years ago

That's not an error I recognise.

I can see the stroke_colour is working. Are you certain the tooltip is not? Do you get an error in the console when you over the mouse over a polygon?

sm2607 commented 2 years ago

It was not working earlier but than I installed all below packages from github and update their dependent packages after removing their previous version, now its working. thankyou so much for your support. But its still slow with my data compared to googlway add_geojson and leaflet addPolygon function

remotes::install_github("SymbolixAU/googleway")
remotes::install_github("dcooley/geometries")
remotes::install_github("dcooley/sfheaders")
remotes::install_github("SymbolixAU/spatialwidget")
remotes::install_github("SymbolixAU/mapdeck")

Also, is there any feature available in any googleway or mapdeck package similar to leaflet function GroupLayer/ControlGroupLayer? or any workaround for this?

dcooley commented 2 years ago

Also, is there any feature available in any googleway or mapdeck package similar to leaflet function GroupLayer/ControlGroupLayer? or any workaround for this?

There is no such featuer in googleway or mapdeck. The work-around is to to use a radiobutton / checkboxgroup in shiny to toggle layers on / off.