2

Following the previous question, I have a new question. From the following code, I estimate the water areas for selected regions in Google Earth Engine. However, as you could see from the second line of the code, it only estimates for selected date (.filterDate("2010-03-01");). I want it to estimate for all the available dates (months in the dataset) and the regions. Shortly, I would like to add a simple loop for the dates in this code.

//Opening the JRC Water Data
var water_data = ee.ImageCollection("JRC/GSW1_2/MonthlyHistory").filterDate("2010-03-01");
var water_area = ee.Image(water_data.first())

//Creating Region Variable
var turkeyProvinces = ee.FeatureCollection("FAO/GAUL_SIMPLIFIED_500m/2015/level1")
    .filter(ee.Filter.eq("ADM0_NAME", "Turkey"));

//Calculating Water Areas
var calculateClassArea = function(feature) {
    var areas = water_area.eq(2).selfMask().multiply(ee.Image.pixelArea()).divide(1e6)
    .reduceRegion({
      reducer: ee.Reducer.sum(),
      geometry: feature.geometry(),
      scale: 30,
      maxPixels: 1e10
    })

    return feature.set(areas)
}
var waterAreas = turkeyProvinces.map(calculateClassArea);

//Exporting the Results
Export.table.toDrive({
    collection: waterAreas,
    description: 'water_area_by_province',
    folder: 'earthengine',
    fileNamePrefix: 'water_area_by_province',
    fileFormat: 'CSV',
    selectors: ['ADM1_NAME', 'water']
    })

Here is the Google Earth Engine link: https://code.earthengine.google.com/f024358da930db6444ea29820867a7dd

1 Answer 1

2

I want to paste the solution to the problem from the discussion with Kuik in the previous post's comment section:

//Opening the JRC Water Data
var water_data = ee.ImageCollection("JRC/GSW1_2/MonthlyHistory");

//Creating Region Variable
var turkeyProvinces = ee.FeatureCollection("FAO/GAUL_SIMPLIFIED_500m/2015/level1")
    .filter(ee.Filter.eq("ADM0_NAME", "Turkey"));

//Calculating Water Areas
var waterAreas = water_data.map(function(image){
  var water = image.eq(2).selfMask().multiply(ee.Image.pixelArea()).divide(1e6);
  var areas = water.reduceRegions({
      reducer: ee.Reducer.sum(),
      collection: turkeyProvinces,
      scale: 30
    });
  return areas.map(function(feat){
  return feat.set(image.toDictionary(['system:time_start']));
  });
}).flatten().select(['ADM1_NAME', 'sum','system:time_start']);

//Exporting the Results
Export.table.toDrive({
    collection: waterAreas,
    description: 'water_area_by_province',
    folder: 'earthengine',
    fileNamePrefix: 'water_area_by_province',
    fileFormat: 'CSV',
    selectors: ['ADM1_NAME', 'sum','system:time_start']
    })

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.