0

I'm trying count the number of monthly water pixels from JRC Water History Collection using Google Earth Engine - Rgee, over a set of regions.

https://developers.google.com/earth-engine/datasets/catalog/JRC_GSW1_2_MonthlyHistory

This is what i am trying to achieve:

  1. Select the image collection and filter by dates for the time series.

  2. Select water only pixels and create masks containing only water pixels for each monthly image. Note this dataset uses a Bitmask: Bits 0-1: Water detection 0: No data 1: Not water 2: Water

  3. Count the number of water pixels in each region for each monthly image (create regional time-series)

I think something is going wrong in step 2 - as some pixel counts are missing, i'm not confident the results are correct, can anyone help?

library(rgee)
library(mapview)
library(sf)

ee_Initialize

wnf_shapes <- st_read("./wnf_shapes.shp")


# Step 1
filteredJRCDataset <- ee$ImageCollection("JRC/GSW1_2/MonthlyHistory")$
  filterDate("2006-01-01", "2006-12-31")

# Step 2
surface_water <- filteredJRCDataset$map(function (image) {
  waterImage <-image$select('water')$eq(2)
  image = image$updateMask(waterImage)
  
  })

# Step 3
ee_sw <- ee_extract(x = surface_water, y = wnf_shapes,  scale = 100, 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

I could also do this using Javascript or Python APIs for anyone not using Rgee.

1 Answer 1

0

Answer:

Step 1

filteredJRCDataset <- ee$ImageCollection("JRC/GSW1_2/MonthlyHistory")$ filterDate("2006-01-01", "2006-12-31")

Step 2

surface_water <- filteredJRCDataset$map(function (image) { waterImage <-image$select('water')$eq(2) # mask is already created here, 1 is returned for water and 0 all other observation

})

Step 3

ee_sw <- ee_extract(x = surface_water, y = wnf_shapes, scale = 100, fun = ee$Reducer$sum(), sf = FALSE) # we just need to sum the 1s here instead of count observations

colnames(ee_sw) <- sprintf("%02d", 1:12) ee_sw$id <- wnf_shapes$id

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.