I am trying to apply reduceRegions to extract average precipitation from the TerraClimate dataset (developers.google.com/earth-engine/datasets/catalog/IDAHO_EPSCOR_TERRACLIMATE) within each polygon of a feature collection. The problem is that the output of the reduction is not written to each feature property as expected. It only appears in the columns of the output feature collection. The code below is a simple implementation on the first image of the collection, using an arbitrary feature collection with 3 polygons. My end goal is to map the ReduceRegions function to the whole TerraClimate collection. For this I followed the steps to use reduceRegions outlined in this previous post: Calculate mean EVI for multiple polygons across an image collection in Google Earth Engine

The problem, and my question, is why is the result of reduceRegions not writing / added to the feature properties as expected..? It does so in the example given in the post above (mean EVI index example). As a result I cannot map the reduceRegions function to the whole collection...

Link to code: https://code.earthengine.google.com/49680c8f2c6d505ddf437a59b015e906

So. coming back to my post after some more works, it seems this is caused by having null values returned by reduceRegions, so that the property is not set in this case. Now, the only way to NOT have null values is to use scale: 30 with the reduceRegions. The TerraClimate data is in geographic projections with a 2.5 arcminute resolution (about 4 km). Whenever I use a different scale value (1, 20, 40, 100.. 1000) I only get null values. It only works with 30 meters for scale. Anyone knows what is going on?


Someone else asked a similar question on the earth-engine list, and it seems indeed this is a bug! A quick workaround was found by Gennadii (ee list link, code link), suggesting to add a dummy operation like .add(0) which apparently will resolve the underlying caching issue.

Your code would be:

var table = /* color: #d63000 */ee.FeatureCollection(
        [ee.Feature(ee.Geometry.Polygon([[[-90.91436262497096, 53.98605219620492],
                  [-86.5198, 49.4155],[-77.5549, 52.4602]]]),
            {"system:index": "0"}),
                [[[-67.7991, 53.674],[-65.0745, 50.935],[-59.8010, 53.308]]]),
            {"system:index": "1"}),
                [[[-110.9534, 50.99],[-107.6135, 49.75],[-104.7131, 52.46]]]),
            {"system:index": "2"})]);

print ('table',table);

var startYr = 2018;
var endYr = 2018; 
var startDate = ee.Date.fromYMD(startYr,01,01);
//var endDate = ee.Date.fromYMD(endYr+1,01,01); //**NOTE: the end date in .filterDate() is exclusive, so this allows all days in 2019 to be included
var endDate = ee.Date.fromYMD(endYr,03,01); //**NOTE: the end date in .filterDate() is exclusive, so this allows all days in 2019 to be included

var precipCollection = ee.ImageCollection("IDAHO_EPSCOR/TERRACLIMATE").select('pr').filterDate(startDate, endDate);

// ---Function to extract spatial averages of monthly TerraClim variables within basins---
var precipproj = ee.Image(precipCollection.first()).projection();

var im1 = precipCollection.first().toDouble().add(0);

var im1_reduced = im1.reduceRegions({
    collection: table,
    reducer: ee.Reducer.mean(),
    crs: precipproj
print('im1 reduced',im1_reduced)

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.