1

I have a list of cumulative proportions based off of a histogram that I calculated in Google Earth Engine. Now I want to find the index of this list where the value is >= 0.995 and then use that index to get the appropriate value from sortedKeys. Any ideas? Here is my working code...

Deforestation = Deforestation.updateMask(Deforestation.neq(0))
var Deforestation_binnedDNF = Deforestation.multiply(DNF)
var rasterBounds = Deforestation.geometry();

var minValue = Deforestation_binnedDNF.reduceRegion({
  reducer: ee.Reducer.min(),
  geometry: rasterBounds,  
  scale: 30,          
  maxPixels: 10000000000  
});

var minValue = minValue.get('b1')
print(minValue, 'minValue')

var maxValue = Deforestation_binnedDNF.reduceRegion({
  reducer: ee.Reducer.max(),
  geometry: rasterBounds,  
  scale: 30,          
  maxPixels: 10000000000  
});

var maxValue = maxValue.get('b1')
print(maxValue, 'maxValue')

var class_width = 2

// Calculate the number of bins
var num_bins = ee.Number(maxValue).subtract(ee.Number(minValue)).divide(class_width).add(1).floor();
print(num_bins)

// Compute the histogram using reduceRegion.
var histogram = Deforestation_binnedDNF.reduceRegion({
  reducer: ee.Reducer.histogram({maxBuckets: num_bins, minBucketWidth: 2}),
  geometry: Deforestation.geometry(),
  scale: 30, // Adjust the scale as needed.
  maxPixels: 1e13 // Adjust the maxPixels as needed.
});

histogram = ee.Dictionary(histogram.get('b1'))
var sortedKeys = histogram.get('bucketMeans')
print('Sorted Keys:', sortedKeys)
var sortedCounts = histogram.get('histogram')

// Convert bin values and counts to ee.Arrays
var binValuesArray = ee.Array(sortedKeys);
var binCountsArray = ee.Array(sortedCounts);

// Compute cumulative sum of histogram counts using ee.Array.accum
var cumulativeCountsArray = binCountsArray.accum(0, 'sum');

// Convert cumulativeCountsArray to a list
var cumulativeCounts = cumulativeCountsArray.toList();
print('Cumulative Sum:', cumulativeCounts)

var sumDeforestation = ee.Number(cumulativeCounts.get(-1));
print('Sum Actual Deforestation (pixels)', sumDeforestation)

var cumulativeProp = cumulativeCounts.map(function(count) {
  return ee.Number(count).divide(sumDeforestation);
});
print('Cumulative Proportions:', cumulativeProp)

// Define the target cumulative proportion.
var targetCumulativeProp = 0.995;

Link to full code

1 Answer 1

2

To get the index of a value based on condition, first, a filter is necessary. The ee.Filter.gte() is appropriate according to your requirement. This will return a list of all values that satisfy the condition.

Secondly, by applying indexOfSublist(), the starting position of the first occurrence in the filtered list will be returned. Finally, you can get the value from the sortedKeys list using the index.

var index = cumulativeProp.indexOfSublist(cumulativeProp.filter(ee.Filter.gte('item',0.995)))
var value = ee.List(sortedKeys).get(index)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.