I would like to get a table (or maybe an array) that I can export from google earth engine with the results of some analysis I am running. I have to issues: I am new to GEE and javascript and I am confused how to create a table without inputting the data manually + in each row I have to input some 0 values and some values from a dictionary I get in the analysis (more on that in the third paragraph).
On a nutshell, I have a raster (CDL) and a FeatureCollection
(CLU). I want to buffer each Feature
in CLU and extract the number of cells with each land use code in CDL that are within each of these CLU Features. I have managed to do so and to get this data in a dictionary, but I am unsure how to proceed to get the data in a table and how to export it afterwards.
There are 132 possible land use codes in CDL, but in each buffer I only get a handful of them (also, I get them in no specific order), so I would need to get a table in which I have one column for each land use code (and the first column registers the features ids), and in each row I input the number of cells with a given land use, assigning 0 to any land use code that does not show up in my dictionary.
Below I have my code so far (which gets me to the histogramDict
dictionary, which is the one that has the input for the rows in the table). I also show the output I get for the object histogram
in the gee console to make stuff clearer. I also have an image of an example table of what I would like to get
CODE TO GET THE histogramDict
dictionary:
// Open CDL
var CDL = ee.ImageCollection('USDA/NASS/CDL').filter(ee.Filter.date('2018-01-01', '2018-12-31'));
var CDL2018 = ee.Image(CDL.first())
// Open CLU - This is an asset I have and that I have uploaded myself to gee
var CLU = ee.FeatureCollection("projects/ee-my-project/assets/IA001");
// Convert the FeatureCollection CLU to a list
var CLUlist = CLU.toList(CLU.size());
// Define buffer distance in meters
var bufferDistance = 500;
// I have lots of polygons (10,000+), but right now I am looping over the first two only to develop
// the code
for (var i = 0; i < 2; i++) {
var polygon = ee.Feature(CLUlist.get(i));
var polygonId = polygon.id()
// Create a buffer around the polygon
var bufferedGeometry = polygon.geometry().buffer(bufferDistance);
var polygonGeometry = polygon.geometry()
var bufferDifference = bufferedGeometry.difference(polygonGeometry);
// Extracting the number of cells of each crop type that intersect with the buffer
var intersectedFeatures = CDL2018.reduceRegion({
geometry: bufferDifference,
reducer: ee.Reducer.frequencyHistogram(),
scale: 30
});
// Extract histogram values from the result
var histogram = intersectedFeatures.get('cropland');
print('histogram', histogram);
var histogramDict = ee.Dictionary(histogram);
}
CONSOLE OUTPUT FOR ONE OF THE HISTOGRAM OBJECT (as you can see, for a given polygon
I do not get all the 132 land uses in my buffer):
histogram
Object (11 properties)
1: 304.6980392156863
111: 8.329411764705881
121: 31.415686274509802
122: 6
141: 25
152: 1.5176470588235293
176: 288.235294117647
195: 4
36: 8.8
37: 101.37254901960785
5: 301.23921568627446
EXAMPLE TABLE OF WHAT I AM TRYING TO GET:
Thanks for your help!