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

Rasterizing using the "by" argument produce raster with possibly wrong layer names #1435

Closed
PMassicotte opened this issue Feb 24, 2024 · 5 comments

Comments

@PMassicotte
Copy link

I am rasterizing using the by argument and found out that the resulting names in the created raster are kinda strange.

library(terra)
#> terra 1.7.71

r <- rast(xmin = 0, ncols = 18, nrows = 18)

# generate points
p <- spatSample(r, 1000, xy = TRUE, replace = TRUE)

p <- vect(p)

p$grp <- sample(c("a", "b"), nrow(p), replace = TRUE)

rasterize(p, r, fun = sum, by = "grp")
#> class       : SpatRaster 
#> dimensions  : 18, 18, 2  (nrow, ncol, nlyr)
#> resolution  : 10, 10  (x, y)
#> extent      : 0, 180, -90, 90  (xmin, xmax, ymin, ymax)
#> coord. ref. : lon/lat WGS 84 (CRS84) (OGC:CRS84) 
#> source(s)   : memory
#> names       : c("a", "a", "a"~ "a", "a", "a"), c("b", "b", "b"~ "b", "b", "b") 
#> min values  :                               1,                               1 
#> max values  :                               7,                               6

I was expecting names to be “a” and “b”. Is it expected? Any ideas on how to fix that?

Created on 2024-02-24 with reprex v2.1.0

@PMassicotte PMassicotte changed the title Rasterizing using the "by"argument produce raster with possibly wrong layer names Rasterizing using the "by" argument produce raster with possibly wrong layer names Feb 27, 2024
@rhijmans
Copy link
Member

Than you, I now get:

rasterize(p, r, fun = sum, by = "grp")
#class       : SpatRaster 
#dimensions  : 18, 18, 2  (nrow, ncol, nlyr)
#resolution  : 10, 10  (x, y)
#extent      : 0, 180, -90, 90  (xmin, xmax, ymin, ymax)
#coord. ref. : lon/lat WGS 84 (CRS84) (OGC:CRS84) 
#source(s)   : memory
#names       : a, b 
#min values  : 1, 1 
#max values  : 6, 9 

@PMassicotte
Copy link
Author

Thank you very much!

@ejlundgren
Copy link

ejlundgren commented Aug 23, 2024

Hi, first, thank you for the wonderful package. It's fantastic.

I am having a similar—but I would say weirder—problem. I am using terra version 1.7-78 on a macOS Sonoma 14.3.1 with R version 4.4.1. I don't think I can make a reproducible example, but here is my code and the output. This is code running on a random subsample of my data for testing purposes.

out <- terra::rasterize(x = fire_polys.vect[sample(nrow(fire_polys.vect), 50), ],
                          y = template,
                          field = "value",
                          touches = TRUE,
                          cover = TRUE,
                          by = "Fire_Year")
  out
  names(out)

[19] "qXKE>\004&\xc1l\xd7Z\x81\x85\xcb%\xc1Z\xd1\xe9\037^\xc9CAegetation/equids_wildfir \x8a\030\xb7(rCEAxW\xb4\x94\xd7CEA\xeb\x91\xf2HGDEAD2_20180916_bil/PRISM_ppTS\n65 \xc0\r\x85N\001"
[20] "egetation/equids_wildfir \x8a\030\xb7(rCEAxW\xb4\x94\xd7CEA\xeb\x91\xf2HGDEAD2_20180916_bil/PRISM_ppTS\n65 \xc0\r\x85N\001"
[21] " \x8a\030\xb7(rCEAxW\xb4\x94\xd7CEA\xeb\x91\xf2HGDEAD2_20180916_bil/PRISM_ppTS\n65 \xc0\r\x85N\001"
[22] "\x8a\030\xb7(rCEAxW\xb4\x94\xd7CEA\xeb\x91\xf2HGDEAD2_20180916_bil/PRISM_ppTS\n65 \xc0\r\x85N\001"
[23] "D2_20180916_bil/PRISM_ppTS\n65 \xc0\r\x85N\001"
[24] "TS\n65 \xc0\r\x85N\001"
[25] "\xc0\r\x85N\001"
[26] ",}w\027CHEA\xb3\x90廂HEA\xbaP\x84~\xadHEAH\x82\x88N\001"
[27] "H\x82\x88N\001"
[28] "E\x9b\xd9\xfb\xf9IEA\xda\xebЊ,JEAo\031\xaf\xcctJEA"
[29] ""
[30] "\b\x95\xe5W\003"
[31] "\b\037\xe2W\003"
[32] "4kmM3_201408_bil.hdr"
[33] ""
[34] "\xc0\xee\xdc\n\003"
[35] "
\002"
[36] ""

These appear to be bits and pieces of file paths in my hard drive (though I'm confused by the '廂' symbol in output 26). When I try to run the same code on the entire dataset (without the sample() subset) I get the following error:

Error: basic_string

Which I suspect is related to some issue with these layer names.

If there's any way I can make this more reproducible let me know!

@rhijmans
Copy link
Member

Hi, it is not a good idea to post more in a closed issue.
Have you inspected

unique(fire_polys.vect$Fire_Year)

I suspect it has these strange values.

@ejlundgren
Copy link

Hi, sorry. I guess I should have opened a new issue.

Fire_Year is a numeric variable as expected...

head(unique(fire_polys.vect$Fire_Year))

[1] 2021 2022 2023 2024 1860 1870

class(fire_polys.vect$Fire_Year)

[1] "numeric"

Very much a mystery. An easy work around is just putting the rasterization in a loop by Fire_Year, which is what I've done. Anyways, just wanted to flag this.

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