rspatial / terra

R package for spatial data handling https://rspatial.github.io/terra/reference/terra-package.html
GNU General Public License v3.0
543 stars 90 forks source link

Cannot make a categorical raster #1464

Closed ManuelSpinola closed 7 months ago

ManuelSpinola commented 7 months ago

I cannot make a categorical raster, this is my example. I can send the file if it is necessary.

r <- rast("cr_forests/raster.tif")
r

class : SpatRaster dimensions : 21230, 16881, 1 (nrow, ncol, nlyr) resolution : 0.0002694946, 0.0002694946 (x, y) extent : -87.10173, -82.55239, 5.498498, 11.21987 (xmin, xmax, ymin, ymax) coord. ref. : lon/lat WGS 84 (EPSG:4326) source : raster.tif name : b1 min value : 0 max value : 70

freq(r)

layer value count 1 1 0 26943 2 1 10 18721491 3 1 20 6138863 4 1 30 1244649 5 1 41 1707466 6 1 42 43817 7 1 43 1864399 8 1 50 190476 9 1 60 403085 10 1 70 27821371

clases <- data.frame(id = 1:10,
                  cover = c("a", "b", "c", "d", "e", "f", "g", "h", "I", "j"))
levels(r) <- clases
r

class : SpatRaster dimensions : 21230, 16881, 1 (nrow, ncol, nlyr) resolution : 0.0002694946, 0.0002694946 (x, y) extent : -87.10173, -82.55239, 5.498498, 11.21987 (xmin, xmax, ymin, ymax) coord. ref. : lon/lat WGS 84 (EPSG:4326) source : raster.tif categories : cover name : cover min value : NA max value : NA

Rapsodia86 commented 7 months ago

'id' in the dataframe is 1:10, but your values in the raster are not. So, you would rather need c(seq(0,30,10),41:43,50,60,70) instead of 1:10.

ManuelSpinola commented 7 months ago

Thank you very much. I understand now.

El mar, 26 mar 2024 a las 20:59, Monika Anna Tomaszewska (< @.***>) escribió:

Id in the dataframe is 1:10, but your values in the raster are not. So, you would rather need c(seq(0,30,10),41:43,50,60,70) instead of 1:10.

— Reply to this email directly, view it on GitHub https://github.com/rspatial/terra/issues/1464#issuecomment-2021827452, or unsubscribe https://github.com/notifications/unsubscribe-auth/AFI3FB2LQFSS6YD7N2S72GDY2IYYDAVCNFSM6AAAAABFJ2LNVOVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDAMRRHAZDONBVGI . You are receiving this because you authored the thread.Message ID: @.***>

-- Manuel Spínola, Ph.D. Instituto Internacional en Conservación y Manejo de Vida Silvestre Universidad Nacional Apartado 1350-3000 Heredia COSTA RICA @. @.> @.*** Teléfono: (506) 8706 - 4662 Sitio web institucional: ICOMVIS http://www.icomvis.una.ac.cr/index.php/manuel Sitio web personal: Sitio personal https://mspinola-sitioweb.netlify.app Blog sobre Ciencia de Datos: Blog de Ciencia de Datos https://mspinola-ciencia-de-datos.netlify.app