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

Error: [mask] raster has no values, with certain updatevalue #1636

Closed
mfisher5 opened this issue Oct 30, 2024 · 10 comments
Closed

Error: [mask] raster has no values, with certain updatevalue #1636

mfisher5 opened this issue Oct 30, 2024 · 10 comments

Comments

@mfisher5
Copy link

mfisher5 commented Oct 30, 2024

I'd like to set the updatevalue argument of the mask function to "-99" instead of NA. I want to apply a mask of counties (a SpatVector) to rasters of biomass, which may or may not have data for every county in the mask.

I'm able to run one of my rasters (file=SC_s1_2017.tif) through the following code, but the second (file=CC_SC_s1_2017.tif) is returning:

Error: [mask] raster has no values

The second raster is essentially the same as the first, with a slightly larger extent.

I'm running R v4.4.0 (2024-04-24 ucrt) with terra v1.7-82.

I copied some code below that reproduces the error, and attached the three files needed in a zipped folder. Please let me know if there is anything else I should include to help troubleshoot!

library(terra)
# This raster works
x1 <- rast("SC_s1_2017.tif")

# This raster doesn't
x2 <- rast("CC_SC_s1_2017.tif")

x1
#class       : SpatRaster 
#dimensions  : 7814, 10932, 1  (nrow, ncol, nlyr)
#resolution  : 0.000338, 0.00027  (x, y)
#extent      : -120.6634, -116.9684, 32.42012, 34.5299  (xmin, xmax, ymin, ymax)
#coord. ref. : lon/lat WGS 84 (EPSG:4326) 
#source      : SC_Spores_per_cell_s1_2017-2020_30m_SpatRaster.tif 
#name        : Spores_per_cell_2017 
#min value   :          0.00000e+00 
#max value   :          1.50621e+14

x2
#class       : SpatRaster 
#dimensions  : 17268, 16349, 1  (nrow, ncol, nlyr)
#resolution  : 0.000338, 0.00027  (x, y)
#extent      : -122.4944, -116.9684, 32.42012, 37.08248  (xmin, xmax, ymin, ymax)
#coord. ref. : lon/lat WGS 84 (EPSG:4326) 
#source      : CC_SC_Spores_per_cell_s1_2017-2024_30m_SpatRaster.tif 
#name        : Spores_per_cell_2017 
#min value   :         0.000000e+00 
#max value   :         1.725145e+14 

This is the mask, a SpatVector of California counties

county_waters3 <- readRDS("ca_counties_3nm_waters_20241009.RDS")
# project to same CRS as x
waters3_project <- terra::project(county_waters3,  x1)  

Mask a county not in the raster (too far north)

y <- waters3_project[waters3_project$subregion=='del_norte']
# Both of these work
x1_countyNA <- terra::mask(x1, mask=y, touches=TRUE, updatevalue=NA)
x1_county99 <- terra::mask(x1, mask=y, touches=TRUE, updatevalue=-99)

# So does this
x2_countyNA <- terra::mask(x2, mask=y, touches=TRUE, updatevalue=NA)

# But this doesn't
x2_county99 <- terra::mask(x2, mask=y, touches=TRUE, updatevalue=-99)
#Error: [mask] raster has no values**

terra_issue_mf.zip

@rhijmans
Copy link
Member

rhijmans commented Nov 2, 2024

I cannot reproduce this error. I assume you are not using the current version of "terra".

@rhijmans rhijmans closed this as completed Nov 2, 2024
@rhijmans
Copy link
Member

rhijmans commented Nov 2, 2024

The version you mention is behind the current CRAN version. But instead of making a statement, I would need to see what is actually printed when you run library(terra) or what is returned by sessionInfo(). Can you please try with the current CRAN version and let me know if it works with that version?

@mfisher5
Copy link
Author

mfisher5 commented Nov 4, 2024

I updated terra and re-started R, and am unfortunately still getting the same error message.
I'm waiting on IT to update R from v4.4.0 to 4.4.1; I can try it again once that is complete!

> library(terra)
terra 1.7.83
Warning message:
package ‘terra’ was built under R version 4.4.1 

> sessionInfo()
R version 4.4.0 (2024-04-24 ucrt)
Platform: x86_64-w64-mingw32/x64
Running under: Windows 11 x64 (build 22621)

Matrix products: default


locale:
[1] LC_COLLATE=English_United States.utf8  LC_CTYPE=English_United States.utf8   
[3] LC_MONETARY=English_United States.utf8 LC_NUMERIC=C                          
[5] LC_TIME=English_United States.utf8    

time zone: America/Los_Angeles
tzcode source: internal

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

other attached packages:
 [1] tidyterra_0.6.1 raster_3.6-30   sp_2.1-4        tictoc_1.2.1    here_1.0.1      magrittr_2.0.3  lubridate_1.9.3
 [8] forcats_1.0.0   stringr_1.5.1   dplyr_1.1.4     purrr_1.0.2     readr_2.1.5     tidyr_1.3.1     tibble_3.2.1   
[15] ggplot2_3.5.1   tidyverse_2.0.0 terra_1.7-83   

loaded via a namespace (and not attached):
 [1] utf8_1.2.4         generics_0.1.3     class_7.3-22       KernSmooth_2.23-22 stringi_1.8.4      lattice_0.22-6    
 [7] hms_1.1.3          digest_0.6.37      evaluate_1.0.1     grid_4.4.0         timechange_0.3.0   fastmap_1.2.0     
[13] rprojroot_2.0.4    e1071_1.7-16       DBI_1.2.3          fansi_1.0.6        scales_1.3.0       codetools_0.2-20  
[19] cli_3.6.2          rlang_1.1.4        units_0.8-5        munsell_0.5.1      withr_3.0.2        yaml_2.3.10       
[25] tools_4.4.0        tzdb_0.4.0         colorspace_2.1-1   vctrs_0.6.5        R6_2.5.1           proxy_0.4-27      
[31] classInt_0.4-10    lifecycle_1.0.4    pkgconfig_2.0.3    pillar_1.9.0       gtable_0.3.6       data.table_1.16.2 
[37] glue_1.8.0         Rcpp_1.0.13-1      sf_1.0-18          xfun_0.49          tidyselect_1.2.1   rstudioapi_0.17.1 
[43] knitr_1.48         farver_2.1.2       htmltools_0.5.8.1  labeling_0.4.3     rmarkdown_2.29     compiler_4.4.0    

@mfisher5
Copy link
Author

mfisher5 commented Nov 5, 2024

My R is now updated! And I am still getting the error.

> x2_county99 <- terra::mask(x2, mask=y, touches=TRUE, updatevalue=-99)
Error: [mask] raster has no values
> sessionInfo()
R version 4.4.2 (2024-10-31 ucrt)
Platform: x86_64-w64-mingw32/x64
Running under: Windows 11 x64 (build 22621)

Matrix products: default


locale:
[1] LC_COLLATE=English_United States.utf8  LC_CTYPE=English_United States.utf8   
[3] LC_MONETARY=English_United States.utf8 LC_NUMERIC=C                          
[5] LC_TIME=English_United States.utf8    

time zone: America/Los_Angeles
tzcode source: internal

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

other attached packages:
 [1] tidyterra_0.6.1 raster_3.6-30   sp_2.1-4        tictoc_1.2.1    here_1.0.1      magrittr_2.0.3  lubridate_1.9.3
 [8] forcats_1.0.0   stringr_1.5.1   dplyr_1.1.4     purrr_1.0.2     readr_2.1.5     tidyr_1.3.1     tibble_3.2.1   
[15] ggplot2_3.5.1   tidyverse_2.0.0 terra_1.7-83   

loaded via a namespace (and not attached):
 [1] utf8_1.2.4         generics_0.1.3     class_7.3-22       KernSmooth_2.23-24 stringi_1.8.4      lattice_0.22-6    
 [7] hms_1.1.3          digest_0.6.37      evaluate_1.0.1     grid_4.4.2         timechange_0.3.0   fastmap_1.2.0     
[13] rprojroot_2.0.4    e1071_1.7-16       DBI_1.2.3          fansi_1.0.6        scales_1.3.0       codetools_0.2-20  
[19] cli_3.6.2          rlang_1.1.4        units_0.8-5        munsell_0.5.1      withr_3.0.2        yaml_2.3.10       
[25] tools_4.4.2        tzdb_0.4.0         colorspace_2.1-1   vctrs_0.6.5        R6_2.5.1           proxy_0.4-27      
[31] classInt_0.4-10    lifecycle_1.0.4    pkgconfig_2.0.3    pillar_1.9.0       gtable_0.3.6       data.table_1.16.2 
[37] glue_1.8.0         Rcpp_1.0.13-1      sf_1.0-18          xfun_0.49          tidyselect_1.2.1   rstudioapi_0.17.1 
[43] knitr_1.48         farver_2.1.2       htmltools_0.5.8.1  labeling_0.4.3     rmarkdown_2.29     compiler_4.4.2    

@rhijmans rhijmans reopened this Nov 6, 2024
@rhijmans
Copy link
Member

rhijmans commented Nov 6, 2024

Can you please run the code above (that I simplified) in a clean R session, outside of R Studio? Since this works for me in windows it really ought to work for you too. And if it does not that is most likely because an old session is interfering. R-Studio tends to load those. If an old session is loaded you can remote it with unlink(".RData") and close R and start again.

@mfisher5
Copy link
Author

mfisher5 commented Nov 6, 2024

I ran my original code in R, outside of RStudio - I'm still getting the same error. Was there another version of the code that you meant to share with your previous comment?
I re-downloaded the files that I attached to my first comment on this issue, to make sure we're definitely using the same files.
I could try uninstalling and re-installing the base packages in the session, to make sure everything is up to date?

Thanks for taking so much time trying to figure this out!

R version 4.4.2 (2024-10-31 ucrt) -- "Pile of Leaves"
Copyright (C) 2024 The R Foundation for Statistical Computing
Platform: x86_64-w64-mingw32/x64

R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.

  Natural language support but running in an English locale

R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.

Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.

> library(here)
here() starts at C:/Users/marfishe/OneDrive - University of California, Davis/Documents
> library(terra)
terra 1.7.83
> x2 <- rast(here("california-kelp-SEN/R/testing/terra_issue/CC_SC_s1_2017.tif"))
> x2
class       : SpatRaster 
dimensions  : 17268, 16349, 1  (nrow, ncol, nlyr)
resolution  : 0.000338, 0.00027  (x, y)
extent      : -122.4944, -116.9684, 32.42012, 37.08248  (xmin, xmax, ymin, ymax)
coord. ref. : lon/lat WGS 84 (EPSG:4326) 
source      : CC_SC_s1_2017.tif 
name        : Spores_per_cell_2017 
min value   :         0.000000e+00 
max value   :         1.725145e+14 
> county_waters3 <- readRDS(here("california-kelp-SEN/R/testing/terra_issue/ca_counties_3nm_waters_20241009.RDS"))
> county_waters3
 class       : SpatVector 
 geometry    : polygons 
 dimensions  : 25, 1  (geometries, attributes)
 extent      : -124.475, -117.097, 32.53501, 41.99988  (xmin, xmax, ymin, ymax)
 coord. ref. : lon/lat WGS 84 (EPSG:4326) 
 names       :   subregion
 type        :       <chr>
 values      :   del_norte
                  humboldt
               los_angeles
> waters3_project <- terra::project(county_waters3,  x2)
> waters3_project
 class       : SpatVector 
 geometry    : polygons 
 dimensions  : 25, 1  (geometries, attributes)
 extent      : -124.475, -117.097, 32.53501, 41.99988  (xmin, xmax, ymin, ymax)
 coord. ref. : lon/lat WGS 84 (EPSG:4326) 
 names       :   subregion
 type        :       <chr>
 values      :   del_norte
                  humboldt
               los_angeles
> y <- waters3_project[waters3_project$subregion=='del_norte']
> plot(y)
> y
 class       : SpatVector 
 geometry    : polygons 
 dimensions  : 1, 1  (geometries, attributes)
 extent      : -124.3227, -124.0652, 41.46474, 41.99988  (xmin, xmax, ymin, ymax)
 coord. ref. : lon/lat WGS 84 (EPSG:4326) 
 names       : subregion
 type        :     <chr>
 values      : del_norte
> x2_county99 <- terra::mask(x2, mask=y, touches=TRUE, updatevalue=-99)
Error: [mask] raster has no values
> sessionInfo()
R version 4.4.2 (2024-10-31 ucrt)
Platform: x86_64-w64-mingw32/x64
Running under: Windows 11 x64 (build 22621)

Matrix products: default


locale:
[1] LC_COLLATE=English_United States.utf8  LC_CTYPE=English_United States.utf8    LC_MONETARY=English_United States.utf8 LC_NUMERIC=C                          
[5] LC_TIME=English_United States.utf8    

time zone: America/Los_Angeles
tzcode source: internal

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

other attached packages:
[1] terra_1.7-83 here_1.0.1  

loaded via a namespace (and not attached):
[1] compiler_4.4.2   rprojroot_2.0.4  tools_4.4.2      Rcpp_1.0.13-1    codetools_0.2-20

@kadyb
Copy link
Contributor

kadyb commented Nov 6, 2024

I could reproduce this issue. Could @rhijmans reproduce if you set the terraOptions(memfrac=0.1) maybe?

rhijmans added a commit that referenced this issue Nov 14, 2024
@rhijmans
Copy link
Member

I think this has now been fixed. I was able to reproduce the problem with terraOptions(todisk=TRUE).

@kadyb
Copy link
Contributor

kadyb commented Nov 15, 2024

I tested and after updating to dev version, this example works for me.

@mfisher5
Copy link
Author

It's working for me too! thanks so much!

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

3 participants