Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unexpected behaviour with spatSample(method='stratified', ext=...,) #1628

Closed
barnabasharris opened this issue Oct 23, 2024 · 2 comments
Closed

Comments

@barnabasharris
Copy link

barnabasharris commented Oct 23, 2024

I'm not sure I understand the behavior of the following. I'm trying to take stratified samples of a categorical raster using spatSample, whilst also using the ext=... argument to limit the area of the raster to sample. However, the sample locations returned are positioned along the top of the raster. If I remove method='stratified' then the expected behaviour occurs.

library(terra)
r <-  rast(xmin=-2,  xmax=-1, ymin=51,  ymax=52,  crs='EPSG:4326',  resolution=0.01,  vals = sample(letters[1:4], 10000, replace=T))
r.grid <- rast(xmin=-2, xmax=-1, ymin=51, ymax=52, crs='EPSG:4326', resolution=0.1)
r.pol <- as.polygons(r.grid)
r.pol$id <- paste0('p',1:nrow(r.pol))

plot(r)
lines(r.grid)

r.samp.strat <- spatSample(r, size=40, method='stratified', replace=T,  ext = ext(r.pol[45,]),  xy=T )
points(r.samp.strat[,1:2], col='red')

r.samp.default <- spatSample(r,  size=40, method='random', replace=T, ext = ext(r.pol[45,]),  xy=T )
points(r.samp.default[,1:2], col='blue')

image

R version 4.4.0 (2024-04-24)
Platform: x86_64-pc-linux-gnu
Running under: Ubuntu 20.04.6 LTS

Matrix products: default
BLAS:   /usr/lib/x86_64-linux-gnu/openblas-pthread/libblas.so.3 
LAPACK: /usr/lib/x86_64-linux-gnu/openblas-pthread/liblapack.so.3;  LAPACK version 3.9.0

locale:
 [1] LC_CTYPE=en_GB.UTF-8       LC_NUMERIC=C               LC_TIME=en_GB.UTF-8       
 [4] LC_COLLATE=en_GB.UTF-8     LC_MONETARY=en_GB.UTF-8    LC_MESSAGES=en_GB.UTF-8   
 [7] LC_PAPER=en_GB.UTF-8       LC_NAME=C                  LC_ADDRESS=C              
[10] LC_TELEPHONE=C             LC_MEASUREMENT=en_GB.UTF-8 LC_IDENTIFICATION=C       

time zone: Europe/London
tzcode source: system (glibc)

attached base packages:
[1] parallel  stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
 [1] patchwork_1.2.0        ggplot2_3.5.1          tibble_3.2.1          
 [4] stringr_1.5.1          Microsoft365R_2.4.0    here_1.0.1            
 [7] janitor_2.2.0          broom_1.0.6            qs_0.26.3             
[10] cobalt_4.5.5           marginaleffects_0.23.0 spatialRF_1.1.4       
[13] WeightIt_1.3.1         ENMevalDEV_2.0.4       magrittr_2.0.3        
[16] tidyr_1.3.1            dplyr_1.1.4            purrr_1.0.2           
[19] terra_1.7-78          

loaded via a namespace (and not attached):
  [1] RColorBrewer_1.1-3   rstudioapi_0.16.0    farver_2.1.1         ENMeval_2.0.4       
  [5] fs_1.6.3             vctrs_0.6.5          base64enc_0.1-3      rstatix_0.7.2       
  [9] leafsync_0.1.0       htmltools_0.5.7      virtualspecies_1.6   raster_3.6-26       
 [13] cellranger_1.1.0     pROC_1.18.5          caret_6.0-94         GenSA_1.1.14        
 [17] huxtable_5.5.6       parallelly_1.36.0    KernSmooth_2.23-24   htmlwidgets_1.6.4   
 [21] plyr_1.8.9           stars_0.6-5          lubridate_1.9.3      elbow_0.0.0.9000    
 [25] lifecycle_1.0.4      iterators_1.0.14     pkgconfig_2.0.3      fastmap_1.1.1       
 [29] Matrix_1.6-5         R6_2.5.1             future_1.33.1        snakecase_0.11.1    
 [33] AzureAuth_1.3.3      digest_0.6.34        colorspace_2.1-1     furrr_0.3.1         
 [37] rprojroot_2.0.4      RSpectra_0.16-1      leafem_0.2.3         crosstalk_1.2.1     
 [41] ggpubr_0.6.0         lwgeom_0.2-14        fansi_1.0.6          timechange_0.3.0    
 [45] httr_1.4.7           polyclip_1.10-6      abind_1.4-5          compiler_4.4.0      
 [49] proxy_0.4-27         withr_3.0.0          doParallel_1.0.17    backports_1.5.0     
 [53] carData_3.0-5        viridis_0.6.5        DBI_1.2.3            AzureGraph_1.3.4    
 [57] ggforce_0.4.2        ggsignif_0.6.4       MASS_7.3-60.0.1      lava_1.7.3          
 [61] rappdirs_0.3.3       tmaptools_3.1-1      leaflet_2.2.2        classInt_0.4-10     
 [65] ModelMetrics_1.2.2.2 tools_4.4.0          units_0.8-5          tmap_3.3-4          
 [69] future.apply_1.11.1  nnet_7.3-19          glue_1.7.0           leafgl_0.1.1        
 [73] nlme_3.1-165         grid_4.4.0           sf_1.0-16            reshape2_1.4.4      
 [77] generics_0.1.3       recipes_1.0.9        predicts_0.1-11      gtable_0.3.4        
 [81] class_7.3-22         dismo_1.3-14         data.table_1.15.0    RApiSerialize_0.1.3 
 [85] RcppProgress_0.4.2   sp_2.1-4             xml2_1.3.6           stringfish_0.16.0   
 [89] car_3.1-2            utf8_1.2.4           foreach_1.5.2        pillar_1.9.0        
 [93] ggspatial_1.1.9      splines_4.4.0        tweenr_2.0.3         lattice_0.22-5      
 [97] survival_3.7-0       tidyselect_1.2.0     spatialrisk_0.7.1    knitr_1.45          
[101] gridExtra_2.3        stats4_4.4.0         xfun_0.42            hardhat_1.3.1       
[105] colourvalues_0.3.9   timeDate_4032.109    stringi_1.8.3        codetools_0.2-19    
[109] filematrix_1.3       cli_3.6.2            RcppParallel_5.1.7   rpart_4.1.23        
[113] munsell_0.5.0        dichromat_2.0-0.1    Rcpp_1.0.12          readxl_1.4.3        
[117] globals_0.16.2       png_0.1-8            XML_3.99-0.16.1      ggh4x_0.2.8         
[121] gower_1.0.1          assertthat_0.2.1     geohashTools_0.3.3   listenv_0.9.1       
[125] viridisLite_0.4.2    ipred_0.9-14         scales_1.3.0         prodlim_2023.08.28  
[129] e1071_1.7-14         ggridges_0.5.6       crayon_1.5.2         rlang_1.1.3         
[133] rvest_1.0.4 
@rhijmans
Copy link
Member

Thank you for reporting this bug. Fixed.

@barnabasharris
Copy link
Author

No worries. Thanks for an awesome package!

netbsd-srcmastr pushed a commit to NetBSD/pkgsrc that referenced this issue Jan 5, 2025
# version 1.8-5

## bug fixes

- `Spatsample(method='stratified', ext=e)` returned the wrong sampling
  coordinates [#1628](rspatial/terra#1628)
  by Barnabas Harris

- `spatSample(method='stratified')` could fail with small sample sizes
  [#1503](rspatial/terra#1503) by karluf

- transparency (alpha) did not work with RGB
  plotting. [#1642](rspatial/terra#1642) by
  Timothée Giraud

- rasterization failed on very large rasters
  [#1636](rspatial/terra#1636) by Mary
  Fisher, [#1463](rspatial/terra#1463) by
  Nic Spono and [#1281](rspatial/terra#1281)
  by Sebastian Dunnett

- `tmpFiles` only looked in the default temp files folder
  [#1630](rspatial/terra#1630) by
  smckenzie1986

- `where.min` did not work well if there were negative values
  [#1634](rspatial/terra#1634) by Michael
  Sumner

- `plet<SpatRaster>` now works for RGB rasters and rasters with a
  color table [#1596](rspatial/terra#1596)
  by Agustin Lobo

- `vect<MULTIPOINT WKT>` did not work properly
  [#1376](rspatial/terra#1376) by
  silasprincipe

- `compareGeom<SpatVector>` did not work
  [#1654](rspatial/terra#1654) by Jason
  Flower

- `buffer<SpatVector>` is now more accurate buffers for lonlat
  polygons [#1616](rspatial/terra#1616) by
  Roberto Amaral-Santos

- `terra:interpNear` used square windows, not circles, beyond 100
  points [#1509](rspatial/terra#1509) by
  Jean-Luc Dupouey

- `vect` read INT64 fields as integers, sometimes leading to
  overflows. [#1666](rspatial/terra#1666) by
  bengannon-fc

- `plot` showed a legend title even if none was requestd if title
  parameters were specified
  . [#1664](rspatial/terra#1664) by Márcia
  Barbosa



## enhancements

n- improved documentation of `writeVector` overwrite when using
  layers. [#1573](rspatial/terra#1573) by
  Todd West

- improved treatment of (supposedly) flipped rasters by Timothée
  Giraud [#1627](rspatial/terra#1627) and
  fchianucci [#1646](rspatial/terra#1646)

- added `map.pal("random")`
  [#1631](rspatial/terra#1631) by Agustin
  Lobo

- expressions can now be used in legend titles
  [#1626](rspatial/terra#1626) by Noah
  Goodkind

- `app` and `tapp` now emit a warning when factors are coerced to
  numeric [#1566](rspatial/terra#1566) by
  shuysman

- `plet<SpatRaster>` now has argument "stretch" for RGB rasters
  [#1596](rspatial/terra#1596) by Agustin

- `%%` and `%/%` now behave the same for SpatRaster as for (base R)
  numbers [#1661](rspatial/terra#1661) by
  Klaus Huebert

## new

- `patches` with option `valus=TRUE` can now distinguish regions based
  on their cell values (instead of only NA vs not-NA)
  [#495](rspatial/terra#495) by Jakub
  Nowosad and [#1632](rspatial/terra#1632)
  by Agustin Lobo

- `rowSums`, `rowMeans`, `colSums` and `colMeans` for SpatRaster

- `metags` for SpatRasterDataset
  [#1624](rspatial/terra#1624) by Andrea
  Manica

- `metags` for layers (bands) of SpatRaster are now saved to and read
  from GTiff files
  [#1071](rspatial/terra#1071) by Mike
  Koontz

- `global` has new effcient functions "anyNA" and "anynotNA"
  [#1540](rspatial/terra#1540) by Kevin J
  Wolz

- `wrap`, `saveRDS` and `serialize` for
  SpatExtent. [#1430](rspatial/terra#1430)
  by BastienFR

- `vect<SpatGraticule>` method suggested in relation to [tidyterra
  #155](dieghernan/tidyterra#155) by Diego
  Hernangómez

- `toMemory<SpatRaster>` and `<SpatRasterDataset>` methods
  [#1660](rspatial/terra#1660) by Derek Friend
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants