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