r-spatial / rgee

Google Earth Engine for R
https://r-spatial.github.io/rgee/
Other
668 stars 146 forks source link

issue with ee$List and iteration over imageCollection #343

Open Remotsensei opened 1 year ago

Remotsensei commented 1 year ago

Description

As for the issue issue with get on ee$List #232 i am trying to replicate an iteration loop similar to that provided in the GEE examples https://developers.google.com/earth-engine/guides/ic_iterating

following the issue above , i changed the script adding 'list' to the calls to ee$List this works but only for the first iteration. at the second one it gives errors

# Load MODIS EVI imagery 
 collection =  ee$ImageCollection('MODIS/006/MYD13A1') $select('EVI');

# Define reference conditions from the first 10 years of data 
 reference = collection $filterDate('2001-01-01', '2010-12-31')$
# Sort chronologically in descending order 
 sort('system:time_start', FALSE);

# Compute the mean of the first 10 years 
 mean = reference$mean();

# Compute anomalies by subtracting the 2001-2010 mean from each image in a
# collection of 2011-2014 images .Copy the date metadata over to the
# computed anomaly images in the new collection 
 series = collection$filterDate('2011-01-01', '2014-12-31')$map(function(image) {
  return (image$subtract(mean)$set('system:time_start', image $get('system:time_start')))
});

# Get the timestamp from the most recent image in the reference collection 
 time0 = reference$first()$get('system:time_start');

# Use imageCollection $iterate() to make a collection of cumulative anomaly over time 
# The initial value for iterate() is a list of anomaly images already processed 
# The first anomaly image in the list is just 0, with the time0 timestamp 
 first =  ee$List(
  # Rename the first band 'EVI' 
   ee$Image(0)$set('system:time_start', time0)$rename('EVI'));

# This is a function to pass to Iterate() 
# As anomaly images are computed, add them to the list 
 accumulate = function(image, list1) {
  # Get the latest cumulative anomaly image from the end of the list with
  # get(-1).Since the type of the list argument to the function is unknown,
  # it needs to be cast to a List. Since the return type of get() is unknown,
  # cast it to Image 
   previous =  ee$Image(ee$List(list(list1))$get(-1))
   # Add the current anomaly to make a new cumulative anomaly image 

   added = image$add(previous)$
  # Propagate metadata to the new image 
   set('system:time_start', image$get('system:time_start'));
  # Return the list with the cumulative anomaly inserted 
  return  ((ee$List(list(list1)))$add(added))
};

# Create an ImageCollection of cumulative anomaly images by iterating 
# Since the return type of iterate is unknown, it needs to be cast to a List 
 cumulative =  ee$ImageCollection(ee$List(series$iterate(accumulate, first)))
 cumulative$getInfo()

Error

Error: ee.ee_exception.EEException: Image.add, argument 'image2': Invalid type. Expected type: Image. Actual type: List<Image<[EVI]>>. Actual value: [<Image<[EVI]>>, <Image<[EVI]>>]