0

Background

I would like to access properties of images within an ImageCollection, using the .map() function. Here is an example for an image collection, using yearly rainfall for Tanzania (link to code):

// Define ROI
var TZ_outline = ee.FeatureCollection('USDOS/LSIB_SIMPLE/2017')
    .filterMetadata('country_na', 'equals', 'Tanzania');

// Define time range
var startyear = 2015;
var endyear = 2019; 

// List years
var years = ee.List.sequence(startyear,endyear);

//Aggregate total rainfall over year
var yearTotal = ee.ImageCollection.fromImages(
    years.map(function(y) {
        var start = ee.String(ee.Number(y).subtract(1).int()).cat('-05-01') // start rain year from May April-1
        var end = ee.String(ee.Number(y).int()).cat('-04-30')
        
        return ee.ImageCollection('UCSB-CHG/CHIRPS/PENTAD')
                  .map(function(im){return(im.clip(TZ_outline))})
                  .filterDate(start, end)
                  .sum()
                  .set('year', y);
    })
    .flatten()
);

For a single image, I would use .get() to access a property, and this works for my example:

print(yearTotal.first().get('year')) 

Using this code within the .map() function returns an error:

var col = yearTotal.map(function(img) {
 var year = img.get('year')
 print(year)
 return(year)
});

enter image description here

Question

How do I make this work? Does it have something to do with server vs. client side?

2

.map() is a server-side function and print is a client-side function. They both cannot be mixed (read more on this here).

Earth engine has a helpful function called .aggregate_array() for the task you mentioned. Add this line print(yearTotal.aggregate_array('year')) to the end of your script and it will output year property of all images in your yearTotal collection.

Link to complete code.

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.