Search code examples
rgoogle-earth-enginergee

Create time series of JRC MonthlyHistory surface water observations by region with R + rgee


I'm completely stumped, I'm trying to create a regional time series of JRC MonthlyHistory surface water count observations (by region) using R + rgee. I'm able to download the total observations of the band, but i am unable to filter by specific values, in my case i would like to select the count of "Surface water observations" for each region at each month. I think it may have something to do with the dataset which is a Bitmask i.e.

Bits 0-1: Water detection 0: No data 1: Not water 2: Water

library(rgee)
library(mapview)

ee_Initialize()

surface_water <- ee$ImageCollection("JRC/GSW1_2/MonthlyHistory")$
      filterDate("2006-01-01", "2006-12-31")$
      map(function(x) x$reproject("EPSG:4326")$select("water[1]"))
    
ee_sw <- ee_extract(x = surface_water, y = wnf_shapes,  scale = 30, fun = ee$Reducer$count(), sf = FALSE)
    
colnames(ee_sw) <- sprintf("%02d", 1:12)
    ee_sw$id <- wnf_shapes$id

link to shapes files - https://drive.google.com/file/d/1oWJ_ZpEQ4bEYr7R73oOXrQc9UhOH_oCB/view?usp=sharing


Solution

  • This code should work:

    library(rgee)
    
    ee_Initialize()
    
    geom_nauta <- ee$Geometry$Point(c(-73.47693, -4.44500))$buffer(10000)
    surface_water <- ee$ImageCollection("JRC/GSW1_2/MonthlyHistory") %>% 
      ee$ImageCollection$filterDate("2006-01-01", "2006-12-31") %>% 
      ee$ImageCollection$map(function(img) img$updateMask(img$eq(1)))
    
    ee_sw <- ee_extract(
      x = surface_water, 
      y = geom_nauta,
      scale = 30,
      fun = ee$Reducer$count(), 
      sf = FALSE
    )
    
    plot(ee_sw %>% as.numeric(), type="l")