Google Earth Engine – How to Reduce and Export NLCD Land Cover Data

google-earth-engineland-covernlcd

Within Google Earth Engine, I am trying to (1) reduce the USGS NLCD land cover dataset to my shapefile of 952 counties within the United States, (2) calculate the proportion of each land cover type in each county, and (3) export the results as a csv for analyses in R.

Specifically within the export, I need the 'GEOID' included for each county so that I can easily manipulate the county-level data within R for the analyses. I am new to Google Earth Engine, and the code snippets I have tried from other similar questions don't seem to work for me. I have also currently limited my shapefile to the first 10 counties for simplicity until I get code that works and then I will run this over my entire shapefile of 952 counties.

Here is my current code:


//Shapefile: 15 states, all counties
var countyList = ee.FeatureCollection(table).limit(10);
//  .select ('GEOID');

print(countyList.size(), 'counties');

countyList = ee.FeatureCollection(countyList.map(function (feat) {
  var GEOID = feat.get('GEOID')
  return ee.Feature(feat.geometry().simplify({'maxError':1}), {'GEOID': feat.id()})
    .set('GEOID', GEOID);
   //return ee.Feature(feat.geometry());
})); 

//Imports NLCD Land Cover Data
var landcover19 = ee.ImageCollection('USGS/NLCD_RELEASES/2019_REL/NLCD')
  .filterBounds(countyList.geometry())
  .filter(ee.Filter.eq('system:index', '2019')).first()
  .select('landcover');
  
print(landcover19);

//I have been trying to reduce frequency of land cover occurrence to each county based 
  //on other examples-- 
  //But I actually need proportion of landcover occurrence in each county
var frequency = countyList.map(function(feature){
  return feature
    landcover19.reduceRegions({
    collection: feature.geometry(),
    reducer:ee.Reducer.frequencyHistogram(),
    scale:30
    })
  return ee.Feature ((null, {'GEOID': feature.id()})
   .set(frequency, feature.get('frequency'))
   .set('GEOID', feature.get('GEOID'))
)
});

print(frequency);

//Export county-level NLCD data to Google Drive
Export.table.toDrive({
    collection: frequency, 
    description: 'nlcd10', 
    folder: 'nlcd',
    fileNamePrefix: 'nlcd10', 
    fileFormat: 'CSV',
});

Here is a link to my code as well: https://code.earthengine.google.com/e1e15e01fa76a0f72cf7fc511ccfc852

Best Answer

In the mapping function used in countyList.map(function(feature){ the first line of the function is return feature, which immediately returns the input feature unchanged, so none of the rest of the code runs.

To make it a useful mapping function, you would want to change it to be something like this, with a single return statement:

var frequency = countyList.map(function(feature){
  return landcover19.reduceRegions({
      collection: feature.geometry(),
      reducer:ee.Reducer.frequencyHistogram(),
      scale:30
  }).set('GEOID', feature.get('GEOID'));
});

But, that is not how reduceRegions should be used. You don't map with reduceRegions; reduceRegions itself handles mapping over a feature collection, and should be used like this:

var frequency = landcover19.reduceRegions({
  collection: countyList,
  reducer: ee.Reducer.frequencyHistogram(),
  scale: 30,
});

Then you do not need to do anything to preserve GEOID; it will be there because reduceRegions takes a feature collection and adds reduced properties to its features, keeping the existing ones.

Related Question