nick-gauthier / anthromes

An R package for analyzing historical land use and population on regional to global scales.
https://nick-gauthier.github.io/anthromes/
Other
7 stars 1 forks source link

Issue when hyde_read reads data in as stars proxy object? #2

Open JocelyneSze opened 2 years ago

JocelyneSze commented 2 years ago

Hi, I'm trying to read in HYDE raw data and inputs and cropping to my area of interest (across the tropics) with hyde_read and st_crop, then classifying the anthromes with anthrome_classify. However I run into the following error message Error in dat/inputs["land_area"] : non-numeric argument to binary operator In addition: Warning message: In dplyr::mutate(., used = crops + grazing + urban, trees = pot_veg %in% : Incompatible methods ("Ops.stars_proxy", "Ops.stars") for "/" So I tried to specify the data as a stars object with anthromes_tropics <- anthrome_classify(st_as_stars(hyde_tropics), inputs_tropics), however I get a different error message that I'm not sure how to resolve Error in st_crop.stars(x = x, y = y, crop = crop, epsilon = epsilon) : for cropping, the CRS of both objects have to be identical As I have checked using st_crs that the CRS of both objects are identical. I'm not quite sure how this can be resolved, if this is an issue with stars package and how stars proxy objects are dealt with, or if it's possible to read the hyde data as a stars object instead of stars proxy object?

If it helps, this is what I did to read the data in

> hyde <- hyde_read(dir = "../Data/Ellis2021-Anthromes")
> hyde
stars_proxy object with 6 attributes in 6 file(s):
$cropland
[1] "[...]/cropland.tif"

$grazing
[1] "[...]/grazing.tif"

$ir_rice
[1] "[...]/ir_rice.tif"

$popc
[1] "[...]/popc.tif"

$tot_irri
[1] "[...]/tot_irri.tif"

$uopp
[1] "[...]/uopp.tif"

dimension(s):
     from   to  offset      delta refsys point             values x/y
x       1 4320    -180  0.0833333 WGS 84 FALSE               NULL [x]
y       1 2160 89.9999 -0.0833333 WGS 84 FALSE               NULL [y]
time    1   75      NA         NA     NA    NA 10000BC,...,2017AD    

> inputs <- hyde_read(vars="inputs", dir="../Data/Ellis2021-Anthromes")
> inputs
stars object with 2 dimensions and 5 attributes
attribute(s), summary of first 1e+05 cells:
   land_area                                            pot_veg                              regions            iso       
 Min.   : NA     Tropical Evergreen Woodland                :     0   Africa                     :     0   Min.   : NA    
 1st Qu.: NA     Tropical Deciduous Woodland                :     0   Asia                       :     0   1st Qu.: NA    
 Median : NA     Temperate Broadleaf and Evergreen Woodland :     0   Eurasia                    :     0   Median : NA    
 Mean   :NaN     Temperate Needleleaf and Evergreen Woodland:     0   Europe                     :     0   Mean   :NaN    
 3rd Qu.: NA     Temperate Deciduous Woodland               :     0   Latin America and Caribbean:     0   3rd Qu.: NA    
 Max.   : NA     (Other)                                    :     0   (Other)                    :     0   Max.   : NA    
 NA's   :1e+05   NA's                                       :100000   NA's                       :100000   NA's   :1e+05  
   pot_vill     
 Min.   : NA    
 1st Qu.: NA    
 Median : NA    
 Mean   :NaN    
 3rd Qu.: NA    
 Max.   : NA    
 NA's   :1e+05  
dimension(s):
  from   to  offset      delta refsys point values x/y
x    1 4320    -180  0.0833333 WGS 84 FALSE   NULL [x]
y    1 2160 89.9999 -0.0833333 WGS 84 FALSE   NULL [y]
nick-gauthier commented 2 years ago

Thanks for this issue @JocelyneSze.

For the error in st_crop.stars(), does the bounding box you're using also have a crs defined? For example, I can reproduce that error message when running: st_crop(hyde_med, st_bbox(c(xmin = 30, ymin = 35, xmax = 35, ymax = 40))), but st_crop(hyde_med, st_bbox(c(xmin = 30, ymin = 35, xmax = 35, ymax = 40), crs = 4326)) runs fine.

I'll have to look into the error with the stars proxy object further.

nick-gauthier commented 2 years ago

OK, can confirm this is an issue in the stars package as stars_proxy objects do not play nicely with regular stars objects:

> file = system.file("tif/L7_ETMs.tif", package = "stars")
> ras = read_stars(file, proxy = FALSE)
> ras_proxy = read_stars(file, proxy = TRUE)
> 
> ras / ras_proxy
Warning: Incompatible methods ("Ops.stars", "Ops.stars_proxy") for "/"
Error in ras/ras_proxy : non-numeric argument to binary operator

The issue here in anthromes is that hyde_read() uses read_stars() under the hood, and the latter function defaults to reading hyde_tropics as a stars_proxy object but not inputs_tropics. The solution you have here of just using st_as_stars() will work fine, but anthromes should ultimately do a better job using stars_proxy objects or warning the user about this behavior.

JocelyneSze commented 2 years ago

Hi, thanks for confirming the issue, unfortunately the anthromes_classify function didn't work with using st_as_stars() on the stars_proxy object, I'm not entirely sure why (but I've also found the already-classified data to work with instead, so not pursuing this issue anymore, though others might face the same problem?).