1

I am extracting mean NDVI over many years for each polygon in a shapefile, masked by each landcover within each polygon.

I want each chart/CSV to be stored in my Google Drive. Ideally for each masked set of files, the CSV filenames should be appended by the mask name so I can keep track.

Here's the code so far: https://code.earthengine.google.com/3001abdda6eb3c3c51774535f5586e32

// Import the example feature collection           
print(Catchments);
// Import the NLCD collection.
var dataset = ee.ImageCollection('USGS/NLCD_RELEASES/2019_REL/NLCD');

// The collection contains images for multiple years and regions in the USA.
print('Products:', dataset.aggregate_array('system:index'));

// Filter the collection to the 2019 product.
var nlcd2019 = dataset.filter(ee.Filter.eq('system:index', '2019')).first();

// Each product has multiple bands for describing aspects of land cover.
print('Bands:', nlcd2019.bandNames());

// Select the land cover band.
var landcover = nlcd2019.select('landcover');

// Create a binary mask.
var urbanmask = landcover.expression("b('landcover') == 41 || b('landcover') == 42 || b('landcover') == 44");
// Update the composite mask with the water mask.
var watermask = landcover.eq(11);//all pixels not 11 are masked
var forestmask = landcover.expression("b('landcover') == 41 || b('landcover') == 42 || b('landcover') == 44");
var nonforestmask = landcover.expression("b('landcover') == 51 || b('landcover') == 52 || \
                    b('landcover') == 71 || b('landcover') == 72 || b('landcover') == 73 || \
                    b('landcover') == 74 || b('landcover') == 90 || b('landcover') == 95");
var barrenmask = landcover.expression("b('landcover') == 12 || b('landcover') == 31");
var cultivatedmask = landcover.expression("b('landcover') == 81 || b('landcover') == 82");
var devopenspacemask = landcover.expression("b('landcover') == 21");
var devlowintmask = landcover.expression("b('landcover') == 22");
var devmedintmask = landcover.expression("b('landcover') == 23");
var devhighintmask = landcover.expression("b('landcover') == 24");
// Update the mask with the appropriate mask.
var maskedLandcover = landcover.updateMask(forestmask);
//var maskedLandcover = landcover.updateMask(mask);
    
// Load MODIS vegetation indices data and subset a decade of images.
var vegIndices = ee.ImageCollection('MODIS/006/MOD13A1')
                 .filter(ee.Filter.date('2010-01-01', '2022-09-30'))
                 .select(['NDVI', 'EVI']);
print(vegIndices)
// create masked image collection
//var maskedvegIndices = vegIndices.updateMask(maskedLandcover);
    var maskedvegIndices = vegIndices.map(function (image) {
 return image.updateMask(maskedLandcover);
});


// Subset catchments to only one polygon
var onecatch=ee.FeatureCollection("projects/ee-vishal-mehta/assets/WEAPCatchment")
              .filter(ee.Filter.eq('Name','P508_Yuba_I_ENGLB'));
    //Subset catchment to about 20 catchments starting with the string P508
var P508=ee.FeatureCollection("projects/ee-vishal-mehta/assets/WEAPCatchment")
              .filter(ee.Filter.stringContains('Name','P508_Yuba'));                  
// "Collection query aborted after accumulating over 5000 elements."
// Modified per polygon using solution here:
// https://gis.stackexchange.com/questions/348157/error-generating-chart-collection-query-aborted-after-accumulating-over-5000-el
    var regions = Catchments.toList(Catchments.size())
    var regionCount = Catchments.size().evaluate(function (count) {
       for (var i = 0; i < count; i++) {
        var plotNDVI = ui.Chart.image.seriesByRegion({
          imageCollection:maskedvegIndices,
          regions: ee.FeatureCollection([ee.Feature(regions.get(i))]),
  band:'EVI',
          reducer: ee.Reducer.mean(),
          xProperty: 'system:time_start',
          seriesProperty: 'Name'})
          .setChartType('LineChart').setOptions({
            title: i,
            hAxis: {title: 'Date'},
            vAxis: {title: 'EVI'},
    });
        print(plotNDVI);    
  }
})

    //Map.addLayer(table,"","catchments");
    Map.addLayer(Catchments,"","Catchments");
    Map.setCenter(-120.84497, 39.01918, 8);
    Map.style().set('cursor', 'crosshair');
2
  • What happens when you run the code that you’ve presented? You’ve told us what you want to do but not where you’re stuck with your code attempts.
    – PolyGeo
    Aug 22, 2023 at 7:01
  • when i run the code, each chart (1 for each of the 276 polygons in the shapefile) appears in the console. I want to automate the export of the data for each chart to google drive. I need to use that data in a hydrologic model. Aug 22, 2023 at 19:33

1 Answer 1

0

There is no automated/automatable mechanism for saving a chart. Your best bet is to export the collections as CSV files and use an external charting tool (maybe Google Sheets)

2
  • How do I modify this script to export the collections as CSV's? Aug 22, 2023 at 19:34
  • Instead of doing a seriesByRegion chart, you just do the equivalent reduceRegions by mapping over the collection and Export of those results. Aug 24, 2023 at 16:37

Your Answer

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

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