0

What is the recommended approach to get the min and max pixel values of a raster in Google Earth Engine?

Let's take this example, in which I use the CHIRPS precipitation raster to sum up values for an entire year for a given country.

var rainfall2019 = ee.ImageCollection('UCSB-CHG/CHIRPS/PENTAD')
                  .select('precipitation')
                  .filterDate('2019-01-01', '2020-01-01')
                  .sum()
                  .clip(country);

var rainfall_palette = 'ff0000, ffffff, 0000ff';

var vis = {
  min: ???,
  max: ???,
  palette: rainfall_palette
};

Map.addLayer(rainfall2019, vis);

Now, how will I know what to add for min and max (in the above snippet I added 3 question marks)?

In a GIS I would just open the raster's properties window.

What I found is something like this:

var max = rainfall2019.reduceRegion(ee.Reducer.max(), country);
var min = rainfall2019.reduceRegion(ee.Reducer.min(), country);
print('max', max);
print('min', min);

Although this does work, it often randomly gives me an error, making me wonder if it is the correct approach:

enter image description here

1
  • When using reduce regions, if you don't specify the scale or a crs and crs transform, it will perform the calculations using a geographic coordinate system rather than a projected coordinate system. If you know the spatial resolution of the dataset then you could add it var max = rainfall2019.reduceRegion({ reducer: ee.Reducer.max(), geometry: country, scale: ??? }); [developers.google.com/earth-engine/apidocs/…
    – rpr_mt
    Feb 10, 2023 at 21:19

1 Answer 1

0

As the error message says, you need to specify the scale or crs & crs-transform. In th e following code, just put the resolution of your data (instead of 10) and you will have the min and the max:

var minMax = rainfall2019.reduceRegion({
      reducer: ee.Reducer.minMax(),
      geometry: country, 
      scale: 10,
      maxPixels: 1e13
    })
print(minMax)

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.