Within Google Earth Engine, I have reduced the USGS NLCD land cover dataset to my specific shapefile (consisting of 952 counties within the United States; I have limited my shapefile to the first 10 counties for simplicity until I get the code to work and then I will run this over my entire shapefile).
I need to calculate the proportion of each land cover type in each county and export the results (while keeping the 'GEOID' in the csv) as a csv for analyses in R.
I have tried code from previous questions that have worked for others doing similar things, but the code (labeled: "//Convert frequencies to proportions for each county") always returns the error: "Dictionary (Error) Array: No numbers in 'values', must provide a type."
The previous examples used 'reduceRegion' rather than 'reduceRegions'; I'm new to Google Earth Engine, but is it possible that this is the reason for my error, and is there a way to write the code so I am able to avoid this error and calculate the proportion of each land cover type in each county?
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 landcover = ee.ImageCollection('USGS/NLCD_RELEASES/2019_REL/NLCD')
.filterBounds(countyList.geometry())
.filter(ee.Filter.eq('system:index', '2019')).first()
.select('landcover');
print(landcover);
//Reduce frequency of land cover occurrence to each county
var frequency = landcover.reduceRegions({
collection: countyList,
reducer:ee.Reducer.frequencyHistogram(),
scale:30
})
;
print(frequency);
////Convert histogram frequencies (from the reducer) to new properties for each county
////I would prefer the results in this format, but I am not sure how to do this in
////conjunction with converting the frequencies to proportions
//var frequency = frequency.map(function(feature){
// var dict = ee.Dictionary(feature.toDictionary().get('histogram'))
// feature = feature.set(dict)
// return feature
//});
//print('per class landcover frequency properties', frequency);
//Convert frequencies to proportions for each county
var dict = ee.Dictionary(frequency.get('landcover'))
var sum = ee.Array(dict.values()).reduce(ee.Reducer.sum(),[0]).get([0]);
var new_dict = dict.map(function(k,v) {
return ee.Number(v).divide(sum);
});
print('Land Cover (%)',new_dict);
//Export county-level NLCD proportion data to Google Drive
Export.table.toDrive({
collection: new_dict,
description: 'nlcd10',
folder: 'nlcd',
fileNamePrefix: 'nlcd10',
fileFormat: 'CSV',
});
Here is a link to my code: https://code.earthengine.google.com/00faca61cc789f6c54aa85bf837cf324