r-spatial / rgee

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

Question: Subsetting imageCollection to single image w/ `get` + `index` produces ComputedObject rather than image? #218

Closed zackarno closed 2 years ago

zackarno commented 2 years ago

Hello, I was wondering why with rgee an extra step is required to implement the below javascript work-flow:

var chirps = ee.ImageCollection("UCSB-CHG/CHIRPS/DAILY")
var chirps_list = chirps.toList(chirps.size())
var chirps_img2 = chirps_list.get(1)
print(chirps_img2)

In rgee I need to call ee$Image on the indexed list to convert it from a ComputedObject to a printable Image. In the Javascript code editor when I run the above I see the ComputedObject automatically being switched to an Image. Is it necessary for rgee to maintain the ComputedObject class when there is just 1 image in the list? If it is not necessary it seems like it would be intuitive to be able to leave it out. I am also just wondering because coming from using the code editor it took me some time to figure out what exactly the problem was and I would like to better understand the purpose of the ComputedObject class

library(rgee)
library(dplyr)

ee_Initialize()

chirps_src <- "UCSB-CHG/CHIRPS/DAILY"
chirps <- ee$ImageCollection(chirps_src)

chirps_list<- chirps$toList(chirps$size())

chirps_img_indx1 <- chirps_list$get(1)

# in order to access//print this object I need an extra step using  ee$Image
ee$Image(chirps_img_indx1) %>% ee_print()
#> Registered S3 method overwritten by 'geojsonsf':
#>   method        from   
#>   print.geojson geojson
#> ---------------------------------------------------------- Earth Engine Image --
#> Image Metadata:
#>  - Class                      : ee$Image
#>  - ID                         : UCSB-CHG/CHIRPS/DAILY/19810102
#>  - system:time_start          : 1981-01-02
#>  - system:time_end            : 1981-01-03
#>  - Number of Bands            : 1
#>  - Bands names                : precipitation
#>  - Number of Properties       : 5
#>  - Number of Pixels*          : 25920000
#>  - Approximate size*          : 4.16 MB
#> Band Metadata (img_band = precipitation):
#>  - EPSG (SRID)                : WGS 84 (EPSG:4326)
#>  - proj4string                : +proj=longlat +datum=WGS84 +no_defs
#>  - Geotransform               : 0.05 0 -180 0 -0.05 50
#>  - Nominal scale (meters)     : 5565.975
#>  - Dimensions                 : 7200 3600
#>  - Number of Pixels           : 25920000
#>  - Data type                  : FLOAT
#>  - Approximate size           : 4.16 MB
#>  --------------------------------------------------------------------------------
#>  NOTE: (*) Properties calculated considering a constant geotransform and data type.

# if I try to printing without the extra step I get an error.
chirps_img_indx1 %>%  ee_print()
#> Error in UseMethod("ee_print"): no applicable method for 'ee_print' applied to an object of class "c('ee.computedobject.ComputedObject', 'ee.encodable.Encodable', 'python.builtin.object')"

Created on 2022-01-17 by the reprex package (v2.0.1)

csaybar commented 2 years ago

Hi @zackarno, thanks for reporting it. It has a lot of sense to me to make ee_print more similar to the CodeEditor print. I will implement it in the next release. Thanks :)

zackarno commented 2 years ago

awesome - thanks @csaybar !

csaybar commented 2 years ago

Hi @zackarno sorry for the late reply,

Unfortunately, this is something related to the Python API. Since rgee depends on it, it can not be solved without using iterative searching (that would be quite slow).