1

I am working on Billion Tree Tsunami Project in Pakistan. I want to extract PTC data for each tile of 250mx250m with polygons. I have district-level polygons.

I have run a code but it gives me aggregates at the district level. https://code.earthengine.google.com/08899f7d7c1a289c5b9cb3aded088b07

// get the geometry of the feature collections (with only 1 feature)
var district1  = table1.first().geometry();
var district2  = table2.first().geometry();
var district3  = table3.first().geometry();
var district4  = table4.first().geometry();
var district5  = table5.first().geometry();
var district6  = table6.first().geometry();
var district7  = table7.first().geometry();
var district8  = table8.first().geometry();
var district9  = table9.first().geometry();
var district10 = table10.first().geometry();
var district11 = table11.first().geometry();
var district12 = table12.first().geometry();
var district13 = table13.first().geometry();
var district14 = table14.first().geometry();
var district15 = table15.first().geometry();
var district16 = table16.first().geometry();
var district17 = table17.first().geometry();
var district18 = table18.first().geometry();
var district19 = table19.first().geometry();
var district20 = table20.first().geometry();
var district21 = table21.first().geometry();
var district22 = table22.first().geometry();
var district23 = table23.first().geometry();
var district24 = table24.first().geometry();
var district25 = table25.first().geometry();
var district26 = table26.first().geometry();
var district27 = table27.first().geometry();
var district28 = table28.first().geometry();
var district29 = table29.first().geometry();
var district30 = table30.first().geometry();
var district31 = table31.first().geometry();
var district32 = table32.first().geometry();
var district33 = table33.first().geometry();
var district34 = table34.first().geometry();
var district35 = table35.first().geometry();
var district36 = table36.first().geometry();
var district37 = table37.first().geometry();
var district38 = table38.first().geometry();

// rebuild a feature collection with custom labels
var districts = ee.FeatureCollection([
  ee.Feature(district1,{'District' : 'Abbottabad'}),
  ee.Feature(district2,{'District' : 'Bajaur'}),
  ee.Feature(district3,{'District' : 'Battagram'}),
  ee.Feature(district4,{'District' : 'Buner'}),
  ee.Feature(district5,{'District' : 'Charsadda'}),
  ee.Feature(district6,{'District' : 'Chitral'}),
  ee.Feature(district7,{'District' : 'DIKhan'}),
  ee.Feature(district8,{'District' : 'FR Bannu'}),
  ee.Feature(district9,{'District' : 'FR DIKhan'}),
  ee.Feature(district10,{'District' : 'FR Kohat'}),
  ee.Feature(district11,{'District' : 'FR Lakki Marwat'}),
  ee.Feature(district12,{'District' : 'FR Peshawar'}),
  ee.Feature(district13,{'District' : 'FR Tank'}),
  ee.Feature(district14,{'District' : 'Hangu'}),
  ee.Feature(district15,{'District' : 'Haripur'}),
  ee.Feature(district16,{'District' : 'Karak'}),
  ee.Feature(district17,{'District' : 'Khyber'}),
  ee.Feature(district18,{'District' : 'Kohat'}),
  ee.Feature(district19,{'District' : 'Kohistan'}),
  ee.Feature(district20,{'District' : 'Kurram'}),
  ee.Feature(district21,{'District' : 'Lakki Marwat'}),
  ee.Feature(district22,{'District' : 'Lower Dir'}),
  ee.Feature(district23,{'District' : 'Malakand'}),
  ee.Feature(district24,{'District' : 'Mansehra'}),
  ee.Feature(district25,{'District' : 'Mardan'}),
  ee.Feature(district26,{'District' : 'North Waziristan'}),
  ee.Feature(district27,{'District' : 'Nowshera'}),
  ee.Feature(district28,{'District' : 'Orakzai'}),
  ee.Feature(district29,{'District' : 'Peshawar'}),
  ee.Feature(district30,{'District' : 'Shangla'}),
  ee.Feature(district31,{'District' : 'South Waziristan'}),
  ee.Feature(district32,{'District' : 'Swabi'}),
  ee.Feature(district33,{'District' : 'Swat'}),
  ee.Feature(district34,{'District' : 'Tank'}),
  ee.Feature(district35,{'District' : 'Tor Ghar'}),
  ee.Feature(district36,{'District' : 'Upper Dir'}),
  ee.Feature(district37,{'District' : 'Bannu'}),
  ee.Feature(district38,{'District' : 'Mohmand'})]
  );

var startYear = 2000
var endYear = 2019
var startMonth = 3
var endMonth = 3

var yearlyMeans = ee.FeatureCollection(
  ee.List.sequence(startYear, endYear)
    .map(calculateYearlyMeans)
).flatten()
  
print(yearlyMeans)  
print(
  ui.Chart.feature.groups({
    features: yearlyMeans, 
    xProperty: 'Year', 
    yProperty: 'Percent Tree Cover', 
    seriesProperty: 'District'
  })
)

Export.table.toDrive({
  collection: yearlyMeans, 
  description: 'yearlyMeans',
  selectors: ['District', 'Year', 'Percent Tree Cover']
})


function calculateYearlyMeans(year) {
  year = ee.Number(year)
  var mean = ee.ImageCollection('MODIS/006/MOD44B')
    .select("Percent_Tree_Cover")
    .filter(ee.Filter.calendarRange(year, year.add(1), 'year'))
    .filter(ee.Filter.calendarRange(startMonth, endMonth, 'month'))      
    .mean()
  return districts.map(function (district) {
    return calculateDistrictMean(district, year, mean)
  })
}

function calculateDistrictMean(district, year, mean) {
  var districtMean = mean.reduceRegion({
    reducer: ee.Reducer.mean(),
    geometry: district.geometry(),
    scale: 250,
    maxPixels: 1e12
  })
  return district
    .set('Percent Tree Cover', districtMean.get('Percent_Tree_Cover'))  
    .set('Year', year)
}
1
  • 1
    Welcome to GIS SE! We're a little different from other sites; this isn't a discussion forum but a Q&A site. Please check out our short tour to learn about our focussed Q&A format. Please do not include chit chat like greetings, statements of appreciation/thanks, etc - see meta.stackexchange.com/questions/2950/… – PolyGeo Mar 26 at 5:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.