1

I have imported several shapefiles as a Fusion Table in Google EE. My goal is to calculate the amount of forest loss (from Hansen et al) and the MODIS VCF (percentage tree cover) for each shapefile, and later do a comparison to see if the two data sets are in agreement. To do so, I want be able to iterate through each shapefile and calculate the amount of pixels of forest loss/VCF per year.

I have one script for the Hansen data set and one for the MODIS VCF, but both are not working ideally for me.

MODIS VCF:

//Import fusion table containing the 8 shapefiles
// Without the ee.filter.eq, I only get a single graph where I don't know for sure from which area it is showing

var fc = ee.FeatureCollection('ft:1Sh6L7D67LeaMWvs9eYnfOQmp-ReR6oQEmWCi2trX')
    .filter(ee.Filter.eq("system:index", '2')) 
    ;

var aggregate_array = fc.aggregate_array('name');
print(aggregate_array, fc);

var region = fc;
Map.addLayer(region);

// Zoom to location.
Map.centerObject(region, 7);

// Choose date of interest
var StartDate = '2000-03-05';
var EndDate = '2015-03-07';

// Add the satellite time series
var modisVCF = ee.ImageCollection('MODIS/051/MOD44B')
    .select('Percent_Tree_Cover')
    .filterDate(StartDate,EndDate)
    .filterBounds(region);

print (modisVCF);

// Let's make a chart!
var TimeSeries = ui.Chart.image.seriesByRegion({
    imageCollection: modisVCF,
    regions: region,
    reducer: ee.Reducer.mean(),
    band: 'Percent_Tree_Cover',
    scale: 5000,
    xProperty: 'system:time_start',
    seriesProperty: 'label'
  }).setChartType('ScatterChart')
    .setOptions({
      title: 'Percentage Tree Cover',
      vAxis: {title: 'Tree Cover (%)'},
      lineWidth: 1,
      pointSize: 1,
    });

print(TimeSeries);

My issue with this piece of code is that it only gives me the data I want one location at a time. How can I plot all locations in one graph?

Hansen Global Forest Change:

Which I originally got from here

// Import fusion table
var region = ee.FeatureCollection('ft:1Sh6L7D67LeaMWvs9eYnfOQmp-ReR6oQEmWCi2trX');

// Load Hansen tree cover
var gfcImage = ee.Image('UMD/hansen/global_forest_change_2015');
var lossYear = gfcImage.select('lossyear');

var scale = 100;

//Sequence of years for which we need to extract loss
var years = ee.List.sequence(1, 14); //2001 to 2014

Map.centerObject(region);

var addVar = function(feature) {

  // function to iterate over the sequence of years
  var addVarYear = function(year, feat) {
    // cast var
    year = ee.Number(year).toInt();
    feat = ee.Feature(feat);

    // actual year to write as property
    var actual_year = ee.Number(2000).add(year);
    var year_str = actual_year.format();

    // filter year:
    // 1st: get mask
    var filter = lossYear.eq(year);
    // 2nd: apply mask
    var filtered = gfcImage.updateMask(filter);

    // reduce variables over the feature
    var reduc = filtered.reduceRegion({
      geometry: feature.geometry(),
      reducer: ee.Reducer.sum(),
      scale: scale
    });

    // get results
    var loss = ee.Number(reduc.get('loss'));

    // set names
    var nameloss = ee.String("loss").cat(year_str);

     // alternative 2: always set property
    // set properties to the feature
    return feat.set(nameloss, loss.format());
  };

  // iterate over the sequence
  var newfeat = ee.Feature(years.iterate(addVarYear, feature));

  // return feature with new properties
  return newfeat;
};

var lossstack = region.map(addVar);
print(lossstack);

Map.addLayer(lossstack, {}, "lossstack")

Export.table.toDrive({
  collection: lossstack,
  description: 'ForestLoss',
  folder: "Google EE results",
  fileFormat: "CSV",
  });

In this piece of code, I am able to get all of the forest loss per year per location. However I could use some help in making a more elegant CSV file. Can I choose not to include the geometry somehow?

Here is what the CSV file looks like: CSV file of forest loss

closed as off-topic by Kersten, Jochen Schwarze, csk, xunilk, nmtoken Jul 27 '18 at 10:04

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "When seeking help to debug/write/improve code always provide the desired behavior, a specific problem/error and the shortest code (as formatted text, not pictures) needed to reproduce it in the question body. Providing a clear problem statement and a code attempt helps others to help you." – Kersten, csk, xunilk
If this question can be reworded to fit the rules in the help center, please edit the question.

  • Can you share the assets and your code? When you import the shapefile asset you usually import it as a FeatureCollection - those can be merged. So without the code im uncertain what your issue is. – Kersten Jul 25 '18 at 9:22