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

as.polygons() fails on categorical SpatRaster with factors #370

Closed
pat-s opened this issue Oct 21, 2021 · 1 comment
Closed

as.polygons() fails on categorical SpatRaster with factors #370

pat-s opened this issue Oct 21, 2021 · 1 comment

Comments

@pat-s
Copy link

pat-s commented Oct 21, 2021

I've found the following categorical raster to fail when calling as.polygons() on it.
It works for a minimal example so something must be wrong/different with the custom raster in the following reprex?

remotes::install_github("mlr-org"/mlr3spatial")

library(mlr3spatial)
#> Loading required package: mlr3
library(terra)
#> terra version 1.4.11
#> 
#> Attaching package: 'terra'
#> The following object is masked from 'package:mlr3':
#> 
#>     resample

# works
stack_classif <- demo_stack_spatraster(0.1)
as.polygons(stack_classif)
#>  class       : SpatVector 
#>  geometry    : polygons 
#>  dimensions  : 9, 1  (geometries, attributes)
#>  extent      : 0, 1, 0, 1  (xmin, xmax, ymin, ymax)
#>  coord. ref. : lon/lat WGS 84 
#>  names       :   x_1
#>  type        : <num>
#>  values      :    -3
#>                   -2
#>                   -1

# also works
r1 <- rast(matrix(rep(c("FALSE", "TRUE"), 3), nrow = 2))
value <- data.frame(ID = c(0, 1), y = c("negative", "positive"))
setCats(r1, layer = "y", value = value)
as.polygons(r1)
#>  class       : SpatVector 
#>  geometry    : polygons 
#>  dimensions  : 2, 1  (geometries, attributes)
#>  extent      : 0, 1, 0, 1  (xmin, xmax, ymin, ymax)
#>  coord. ref. : lon/lat WGS 84 (EPSG:4326) 
#>  names       :    lyr.1
#>  type        :    <chr>
#>  values      : negative
#>                positive

# fails
value <- data.frame(ID = c(0, 1), y = c("negative", "positive"))
setCats(stack_classif, layer = "y", value = value)

as.polygons(stack_classif)
#> Error in .local(x, i, j = j, ..., value): i > 0 && i <= ncol(x) is not TRUE

Created on 2021-10-21 by the reprex package (v2.0.1)

@rhijmans
Copy link
Member

rhijmans commented Oct 21, 2021

Thanks, I believe this has now been fixed. The error was related to having multiple layers in the SpatRaster, and a categorical layer that is not the first layer. Note that as.polygons will only process the first layer of dissolve=TRUE

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