stars icon indicating copy to clipboard operation
stars copied to clipboard

Croping a stars object takes 16.89 hours while terra takes 0.16 seconds

Open alexyshr opened this issue 1 year ago • 3 comments

Croping a stars object takes 16.89 hours while terra takes 0.16 seconds. I just installed sf and stars from GitHub.

library(stars)
#> Loading required package: abind
#> Loading required package: sf
#> Linking to GEOS 3.12.1, GDAL 3.8.4, PROJ 9.3.1; sf_use_s2() is TRUE
library(terra)
#> terra 1.7.78
library(sf)
myurl = "http://geocorp.co/wind/MWP_2017242_090W040N_3D3OT.tif"
the_40n_file = "./MWP_2017242_090W040N_3D3OT.tif"
download.file(myurl, the_40n_file, mode = "wb")

myurl = "http://geocorp.co/wind/MWP_2017242_090W030N_3D3OT.tif"
the_30n_file = "./MWP_2017242_090W030N_3D3OT.tif"
download.file(myurl, the_30n_file, mode = "wb")

myurl = "http://geocorp.co/wind/counties_florida.zip"
download.file(myurl, "counties_florida.zip", mode = "wb")
unzip("counties_florida.zip")
the_florida_file = "./counties_florida.shp"
#read the shapefile
sf_florida <- sf::st_read(the_florida_file, quiet = TRUE)
#transform st_florida to 4326
sf_florida <- sf::st_transform(sf_florida, 4326)

#read th files with stars
st_30n <- stars::read_stars(the_30n_file, quiet = TRUE, proxy = FALSE)
st_40n <- stars::read_stars(the_40n_file, quiet = TRUE, proxy = FALSE)

#convert the files to terra
r_30n <- terra::rast(the_30n_file)
r_40n <- terra::rast(the_40n_file)

#mosaic the files the files with stars
system.time({
  st_mosaic <- stars::st_mosaic(st_30n, st_40n)
})
#>    user  system elapsed 
#>    2.46    0.68    3.73

#plot st_mosaic
plot(st_mosaic)
#> downsample set to 10

#mosaic the files the files with terra
system.time({
  r_mosaic <- terra::mosaic(r_30n, r_40n)
})
#>    user  system elapsed 
#>    1.57    1.39    5.19

#crop the files with stars
#it is taking 16.89 hours
#system.time({
#  st_crop <- sf::st_crop(st_mosaic, sf_florida)
#})
#system.time({
#  st_crop <- st_mosaic[sf_florida, as_points = T]
#})
#    user   system  elapsed 
#21499.75 39246.67 60792.59 
#

#crop the files with terra
system.time({
  r_crop <- terra::crop(r_mosaic, sf_florida)
})
#>    user  system elapsed 
#>    0.11    0.05    0.16
plot(r_crop)

Created on 2024-07-04 with reprex v2.1.0

Session info
sessionInfo()
#> R version 4.4.1 (2024-06-14 ucrt)
#> Platform: x86_64-w64-mingw32/x64
#> Running under: Windows 10 x64 (build 19045)
#> 
#> Matrix products: default
#> 
#> 
#> locale:
#> [1] LC_COLLATE=English_United States.utf8 
#> [2] LC_CTYPE=English_United States.utf8   
#> [3] LC_MONETARY=English_United States.utf8
#> [4] LC_NUMERIC=C                          
#> [5] LC_TIME=English_United States.utf8    
#> 
#> time zone: America/New_York
#> tzcode source: internal
#> 
#> attached base packages:
#> [1] stats     graphics  grDevices utils     datasets  methods   base     
#> 
#> other attached packages:
#> [1] terra_1.7-78 stars_0.6-6  sf_1.0-17    abind_1.4-5 
#> 
#> loaded via a namespace (and not attached):
#>  [1] vctrs_0.6.5        cli_3.6.3          knitr_1.47         rlang_1.1.4       
#>  [5] xfun_0.45          highr_0.11         KernSmooth_2.23-24 DBI_1.2.3         
#>  [9] purrr_1.0.2        styler_1.10.3      glue_1.7.0         htmltools_0.5.8.1 
#> [13] e1071_1.7-14       rmarkdown_2.27     grid_4.4.1         R.cache_0.16.0    
#> [17] evaluate_0.24.0    classInt_0.4-10    fastmap_1.2.0      yaml_2.3.8        
#> [21] lifecycle_1.0.4    compiler_4.4.1     codetools_0.2-20   fs_1.6.4          
#> [25] Rcpp_1.0.12        R.oo_1.26.0        R.utils_2.12.3     digest_0.6.36     
#> [29] class_7.3-22       reprex_2.1.0       curl_5.2.1         parallel_4.4.1    
#> [33] magrittr_2.0.3     R.methodsS3_1.8.2  tools_4.4.1        withr_3.0.0       
#> [37] proxy_0.4-27       xml2_1.3.6         units_0.8-5

alexyshr avatar Jul 04 '24 16:07 alexyshr

Did you try cutting to the bounding box? Then it should work fast. See also this issue.

st_crop <- st_crop(st_mosaic, st_bbox(sf_florida))

kadyb avatar Jul 05 '24 17:07 kadyb

Yes! It is taking 0.12 secs (faster than 0.17 with terra)

Thank you!

alexyshr avatar Jul 05 '24 19:07 alexyshr

In my initial code, I was comparing two different types of crops. So the time comparison was not valid. If we compare the crop to the bounding box, stars is 1.41 times faster.

alexyshr avatar Jul 05 '24 20:07 alexyshr