3

I am trying to filter/subset/extract a specific value from the output returned by frequencyHistogram().

I am not sure how to do this, and not sure what is the class of that output (help file says list), or whether I should convert this to another class, like dictionary or array?

Say I wanted to know the cell count associated to cell value 10001, how can I do this?

// Take raster and polygon
var image = ee.Image('LANDSAT/LC8_L1T/LC80440342014077LGN00').select('B[2-5]');
var region = ee.Geometry.Rectangle(-122.45, 37.74, -122.4, 37.8);

// run histogram on raster, on region defined by polygon
var tryit = image.reduceRegion(ee.Reducer.frequencyHistogram(), region, 30).get("B2");
print(tryit, "tryit")

//how can I extract the cell count associated to cell value, say 10001? 

1 Answer 1

5

In this case reduceRegion() returns a nested dictionary, with one entry for each band of the image that contains a dictionary with value frequency counts. You can extract the cell count by using the dictionary's get(key) method twice, once to retrieve the band dictionary, and once to retrieve the frequency count. However, since get() is an method that can return many data types, you also need to explicitly cast the result to the correct data type (ee.Dictionary in this case) before calling any methods of the object.

// Define a raster and polygon.
var image = ee.Image('LANDSAT/LC8_L1T/LC80440342014077LGN00').select('B[2-5]');
var region = ee.Geometry.Rectangle(-122.45, 37.74, -122.4, 37.8);

// Run histogram on raster, on region defined by polygon.
var tryit = ee.Dictionary(
  image.reduceRegion(ee.Reducer.frequencyHistogram(), region, 30).get("B2")
);
print('tryit', tryit);

// Extract the cell count associated with a cell value (10001) 
print('cell count for value 10001', tryit.get('10001'));
0

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.