1

As the function documentation suggests, we know that the reduceRegion() function comes in handy when we get the 'Too many pixels' error due to server restrictions. I'm currently working on Google Colab and my code is the following:

image = ee.Image('LANDSAT/LE7_TOA_5YEAR/2008_2012')
aoi = getPolygon((55.2708, 25.2048), 0.15) 

# Reduce the region. The region parameter is the Feature geometry.
meanDictionary = image.reduceRegion(reducer=ee.Reducer.mean(), geometry=aoi, scale=90, maxPixels=262144)

The result I naturally get after printing meanDictionary.getInfo() is a dictionary of different band values:

{'B1': 48.80214808618162,
 'B2': 50.636738705868815,
 'B3': 53.1672805851419,
 'B4': 58.36716764069926,
 'B5': 56.440968073087255,
 'B6_VCID_2': 209.51128253916946,
 'B7': 43.215088529918695}

My question is how do I get the actual reduced Image as a ee.image.Image object after reduceRegion() has been called with its respective parameters, since it only returns a dictionary. I could be off-track, if so, some guidance in the right direction would help please!

Hiba Jamal
  • 43
  • 5

1 Answers1

1

There is no “reduced Image”, or rather, the action of reduceRegion is to convert a lot of pixels into one numerical or other value (per band). The output is not an image because there is no spatial information any more — the entire result of the reduction is described in that dictionary.

The documentation about "Too many pixels" errors is referring to how to make a reduction work when it is otherwise too large. reduceRegion is the thing that can have those errors, not a solution to them.

Kevin Reid
  • 37,492
  • 13
  • 80
  • 108
  • I got the conversion to one numerical value given the dictionary of single band values I got as output, thank you for the clarification on that. Is there any way I could get a _scaled_ version of the image as output so as to get rid of the "Too many pixels" error? – Hiba Jamal Jun 04 '20 at 08:00
  • @HibaJamal Adding a **separate** step to scale the image is not necessary and would lead to worse results. If you are using an algorithm that _can produce_ the "Too many pixels" error, then _that operation_ will have the `scale` and `maxPixels` parameters. You use `scale` to choose the resolution at which it proceeds, and if you really want to do a slow, high resolution operation, you increase `maxPixels` (it's a sanity check, not a hard limit). – Kevin Reid Jun 04 '20 at 15:04
  • Thanks for the lead on looking up for those parameters but I am only using image.sampleRectangle to get specific band values-arrays, Geometry.Polygon to specify a region and now have tried image.reproject to use its _scale_ parameter. Only the last one provides a scale parameter, and either gives a black image (since default value for masked pixels out of band range is 0), or white image (still trying to figure that out). – Hiba Jamal Jun 04 '20 at 22:23
  • @HibaJamal Ah, if you are using `sampleRectangle()` then note that it says it samples in the band's projection. To change that you use `reproject()` with the same crs and a different scale. – Kevin Reid Jun 04 '20 at 22:29
  • Yup, Used `band_arrs = img.reproject(crs=img.projection().crs(), scale=sclx).sampleRectangle(region=aoi, defaultValue=0)`, do you think there will be an error in the output image (which I am plotting using pyplot after extracting arrays of band values) if `sclx` is too high? – Hiba Jamal Jun 05 '20 at 06:57
  • @HibaJamal As the scale increases you will sample from pixels over a larger area. If the area approaches the entire globe there may be failures due to projection math problems, but it wouldn't be particularly useful to do that anyway. – Kevin Reid Jun 05 '20 at 14:43
  • Ah okay. Thank you so much! – Hiba Jamal Jun 05 '20 at 17:45