I am trying to extract the pixel value from the last image of an image collection. The problem is that when I do so, I get an error stating the following:

Error printed after executing code.

This is my code for extracting the pixel value of the image:

var precipCol = imageCollection5.select(['hourlyPrecipRateGC'])
    .filterDate(getFirst(new Date()), currentDate);

var lastPrecip = precipCol.reduce(ee.Reducer.last());

var precipNum = lastPrecip.reduceRegion(ee.Reducer.last(), geom)

When I click on the inspector tab, the code editor does display the number as it should. The value I get when I click in the inspector is what I need to get from the code above.

Inspector Tab Display

The variable geometry is a polygon consisting of the following vertices:

geometry var vertices

The variable geom is the coordinate: (-106.224911, 31.548092).

The variable currentDate holds a string with the current date in YYYY-MM-DD.

function getFirst(date) returns current week's first date.

imageCollection5 is the following source:

source image collection

What can I do?


In this section

var precipNum = lastPrecip.reduceRegion(ee.Reducer.last(), geom)

The issue is that lastPrecip is a temporary layer only stored in memory and doesn't have a nominal scale so GEE can't determine the scale by itself. There is a simple solution to this i.e. just specify the scale you want. So the code would now be

var precipNum = lastPrecip.reduceRegion(ee.Reducer.last(), geom,30) //or w/e resolution you are aiming for

But using the ee.Reducer.last() reducer here is not that useful as this will return the last value from its inputs. So from within your polygon it is just returning you the value of last pixel in the array generated for reducer. Since the last reducer is already used to get last valid pixel from the collection, here you probably want to use something else for example mean which would mean mean precipitation in your region from the latest valid pixels. But if you only want to get pixel value from one pixel, it might be better to use point geometry and sampleRegion function.

  • Thanks for the help. It worked correctly.
    – cdcerv
    Aug 14 '19 at 22:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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