r-spatial / rgee

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

issue with `get` on `ee$List` #232

Closed zackarno closed 2 years ago

zackarno commented 2 years ago

Hello,

There seems to be an issue when using get on an earth engine list. Below I have created a reprex to show the issue.

Below is the JS which works fine in the code editor

var chirps_2016 = ee.ImageCollection("UCSB-CHG/CHIRPS/DAILY").filterDate("2016-01-01","2016-12-31");
var time0 = chirps_2016.first().get('system:time_start');

var first = ee.List([ee.Image(0).set('system:time_start', time0).select(0).rename('precipitation')]);

var previous = ee.Image(ee.List(first).get(-1));

print(previous, 'previous example')

image

Here is the R code that produces the error:

library(rgee)
ee_Initialize()
#> -- rgee 1.1.2.9000 ---------------------------------- earthengine-api 0.1.295 -- 
#> --------------------------------------------------------------------------------

chirps_2016 = ee$ImageCollection("UCSB-CHG/CHIRPS/DAILY")$filterDate("2016-01-01","2016-12-31")
time0 = chirps_2016$first()$get('system:time_start')

first = ee$List(
  ee$Image(0)$set('system:time_start', time0)$select(0)$rename("precipitation")
)

ee$Image(ee$List(first)$get(-1)) |> ee_print()
#> Error in py_call_impl(callable, dots$args, dots$keywords): EEException: List.get, argument 'list': Invalid type.
#> Expected type: List<Object>.
#> Actual type: Image<[precipitation]>.
#> 
#> Detailed traceback:
#>   File "C:\Users\ZACK~1.ARN\AppData\Local\R-MINI~1\envs\rgee\lib\site-packages\ee\computedobject.py", line 98, in getInfo
#>     return data.computeValue(self)
#>   File "C:\Users\ZACK~1.ARN\AppData\Local\R-MINI~1\envs\rgee\lib\site-packages\ee\data.py", line 672, in computeValue
#>     return _execute_cloud_call(
#>   File "C:\Users\ZACK~1.ARN\AppData\Local\R-MINI~1\envs\rgee\lib\site-packages\ee\data.py", line 336, in _execute_cloud_call
#>     raise _translate_cloud_exception(e)

Created on 2022-02-18 by the reprex package (v2.0.1)

It seems to be more than just an issue with the printing method. The reprex above might seem like it a strange process, but I have just isolated the part of my code that causes an error later when applied in an iteration. This error prevents me from replicating or modifying the GEE Iterating over an ImageCollection example with rgee

ambarja commented 2 years ago

Hello @zackarno just add list() inside ee$List() > ee$Image(ee$List(list(first))$get(-1)) %>% ee_print( )

> ee$Image(ee$List(list(first))$get(-1)) %>%  ee_print()
──────────────────────────────────────────────────────────────────────── Earth Engine Image ──
Image Metadata:
 - Class                      : ee$Image
 - ID                         : no_id
 - system:time_start          : 2016-01-01
 - Number of Bands            : 1
 - Bands names                : precipitation
 - Number of Properties       : 1
 - Number of Pixels*          : 64800
 - Approximate size*          : 101.25 KB
Band Metadata (img_band = precipitation):
 - EPSG (SRID)                : WGS 84 (EPSG:4326)
 - proj4string                : +proj=longlat +datum=WGS84 +no_defs
 - Geotransform               : 1 0 0 0 1 0
 - Nominal scale (meters)     : 111319.5
 - Dimensions                 : 360 180
 - Number of Pixels           : 64800
 - Data type                  : INT
 - Approximate size           : 101.25 KB
 ──────────────────────────────────────────────────────────────────────────────────────────────
 NOTE: (*) Properties calculated considering a constant geotransform and data type.
zackarno commented 2 years ago

amazing, thanks @ambarja for the quick reply - this works! So whenever using ee$List you need to also use base::list?

ambarja commented 2 years ago

@zackarno yes, here some examples, maybe @csaybar could clarify this case better.

> ee$List(list(1,2,3,4,5))$getInfo()
[1] 1 2 3 4 5
> ee$List(c(1,2,3,4,5))$getInfo()
[1] 1 2 3 4 5
> 
csaybar commented 2 years ago

Yes, this is a typical issue due to the different coercion rules implemented in the Javascript and Python EE client libraries. The elements of the ee$List must be either a list or a computer object. See https://developers.google.com/earth-engine/apidocs/ee-list

It seems that passing an ee.Image object in Javascript immediately changes it to a List, however, this does not happen in the Python Earth Engine client library!, on which rgee depends.