Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add label to geojson #257

Open
sm2607 opened this issue Sep 15, 2022 · 15 comments
Open

add label to geojson #257

sm2607 opened this issue Sep 15, 2022 · 15 comments

Comments

@sm2607
Copy link

sm2607 commented Sep 15, 2022

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
Copy link
Collaborator

dcooley commented Sep 15, 2022

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

@sm2607
Copy link
Author

sm2607 commented Sep 16, 2022

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
Copy link
Collaborator

dcooley commented Sep 16, 2022

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

@sm2607
Copy link
Author

sm2607 commented Sep 16, 2022

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
Copy link
Collaborator

dcooley commented Sep 18, 2022

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
Copy link
Author

sm2607 commented Sep 19, 2022

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
Copy link
Collaborator

dcooley commented Sep 19, 2022

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
Copy link
Author

sm2607 commented Sep 19, 2022

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
Copy link
Collaborator

dcooley commented Sep 19, 2022

could you try openeing it in a browser?

@sm2607
Copy link
Author

sm2607 commented Sep 19, 2022

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
Copy link
Collaborator

dcooley commented Sep 19, 2022

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
Copy link
Author

sm2607 commented Sep 20, 2022

yes I got below console error:-

image

@dcooley
Copy link
Collaborator

dcooley commented Sep 20, 2022

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
Copy link
Author

sm2607 commented Sep 21, 2022

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
Copy link
Collaborator

dcooley commented Sep 22, 2022

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants