r-spatial / rgee

Google Earth Engine for R
https://r-spatial.github.io/rgee/
Other
677 stars 146 forks source link

Removing options to select Map Tiles and Layers #187

Closed dariovanderlinden closed 2 years ago

dariovanderlinden commented 2 years ago

Description

I would like to create a map using the Hansen Global Forest Change v1.8 (2000-2020) dataset on Google Earth Engine and host this on R Shiny. I noticed when creating the map, we have the option to select the tiles (i.e. Open Streetmap, CarbonDB.Positron) on the map as well as select the layer (in my case, tree cover and loss year).

Is it possible to create the map in a way that it only shows one tile e.g. CarbonDB.Positron and doesn't give the option to change this or select/deselect layers on the map?

Also, is it possible to change the palette in my map layer from black & green to transparent/no colour & green?

What I Did


library(tidyverse)
library(rgee)
library(rgeeExtra)
library(leaflet)
library(leaflet.extras2)
library(shiny)
library(reticulate)

## Create the R Shiny app that hosts the map -----------------------------------

  # 1. UI

    ui <- fluidPage(
      leaflet::leafletOutput("Map")
    )

  # 2. Server

    server <- function(input, output, session) {

      output$Map <- leaflet::renderLeaflet({

        rgee::ee_Initialize()

        # Import image data

        gfc <- rgee::ee$Image("UMD/hansen/global_forest_change_2020_v1_8")

        # filter by tree cover and tree cover loss

        gfc_treecover <- gfc[["treecover2000"]]
        gfc_lossyear <- gfc[["lossyear"]]

        tree_cover <- Map$addLayer(
          eeObject = gfc_treecover,
          visParams = list(
            min = 0,
            max = 100,
            opacity = 0.5,
            palette = c('black','green')),
          name = 'Tree Cover')

        loss_year <- Map$addLayer(
          eeObject = gfc_treecover,
          visParams = list(
            min = 0,
            max = 100,
            opacity = 0.5,
            palette = c('black','green')),
          name = 'Tree Cover') +
          Map$addLayer(
            eeObject = gfc_lossyear,
            visParams = list(
              min = 0,
              max = 20,
              opacity = 0.7,
              palette = c('red','red')),
            name = 'Loss year')

        Map$setCenter(26.8208, 30.8025, 2)

        visParams <- list(min = 0, max = 1, palette = c('green','red'), values = c("Tree Cover", "Forest Loss"))
        Map$addLegend(visParams = visParams, name = "Legend", color_mapping = "character", position = "bottomright")

        loss_year | tree_cover + legend_2019

      })

    }

    # 3. Run the application 

      shinyApp(ui = ui, server = server)
csaybar commented 2 years ago

Hi @dariovanderlinden sorry my late reply.

You can change the transparency of an eeObject with opacity.

image

I think is not a good idea to create a map service using Map$addLayer (The map resource will disappear in a couple of hours), soon I will add an example about how to create Shiny apps using rgee.