AgrDataSci / gdistance

Calculate distances and routes on geographic grids.
https://agrdatasci.github.io/gdistance/
14 stars 6 forks source link

Error in transition function #20

Closed luuciasainz closed 1 year ago

luuciasainz commented 1 year ago

I am getting the following error when I I run the function transition to create transition matrices:

Error in methods::as(x, "SpatRaster") : no method or default for coercing “RasterLayer” to “SpatRaster”

This is my raster layer cost1:

class : RasterLayer dimensions : 4469, 4936, 22058984 (nrow, ncol, ncell) resolution : 100, 100 (x, y) extent : 165035.2, 658635.2, 4342174, 4789074 (xmin, xmax, ymin, ymax) crs : +proj=utm +zone=30 +ellps=GRS80 +units=m +no_defs source : resist_nat2 names : resist_nat2 values : 0, 100 (min, max) attributes : ID COUNT from: 0 568492 to : 100 131719

The code I wrote was:

tr.cost1 <- gdistance::transition(cost1, transitionFunction=mean, directions=8)

I have changed the function several times but the error is still the same.

Anyone who can help?

Thank you in advance!

Lucía

andrewmarx commented 1 year ago

Basically, the issue could be a bug in my last update, an issue with the raster itself, or an issue with one of the dependencies (namely raster and terra), so we need to narrow that down.

After running it and getting an error, please run sessionInfo() and paste the info here.

After that, try restarting R, updating raster and terra, and running it again. If the issue persists, run sessionInfo() again and include that here with the original.

Finally, have you tried it with any other rasters? Please try it with these two, and let me know if either produces errors:

r1 = raster::raster(matrix(0:11,3))

r2 = r1
r2[3:7] = NA
luuciasainz commented 1 year ago

Hi Andrew,

thank you so much for your help!. It worked when I updated raster and terra packages as you suggested. Here is the information from sessionInfo() after getting the error

sessionInfo()R version 4.1.3 (2022-03-10) Platform: x86_64-w64-mingw32/x64 (64-bit) Running under: Windows 10 x64 (build 19045)

Matrix products: default

locale: [1] LC_COLLATE=Spanish_Spain.1252 LC_CTYPE=Spanish_Spain.1252 LC_MONETARY=Spanish_Spain.1252 [4] LC_NUMERIC=C LC_TIME=Spanish_Spain.1252

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

other attached packages: [1] gdistance_1.6.4 Matrix_1.4-0 igraph_1.3.5 GeNetIt_0.1-5 [5] nlme_3.1-155 LandGenCourse_1.6.0 landscapegenomics_0.1.0 raster_3.4-13 [9] sp_1.4-5

loaded via a namespace (and not attached): [1] Rcpp_1.0.8.3 lattice_0.20-45 codetools_0.2-18 grid_4.1.3 magrittr_2.0.3 rlang_1.1.0 [7] cli_3.6.0 rstudioapi_0.14 rgdal_1.5-29 tools_4.1.3 compiler_4.1.3 terra_1.7-3 [13] pkgconfig_2.0.3

After transition function run, this is the information of the new layer:

class : TransitionLayer dimensions : 4469, 4936, 22058984 (nrow, ncol, ncell) resolution : 100, 100 (x, y) extent : 165035.2, 658635.2, 4342174, 4789074 (xmin, xmax, ymin, ymax) crs : +proj=utm +zone=30 +ellps=GRS80 +units=m +no_defs values : conductance matrix class: dsCMatrix

Thank you so so much.

Kind regards,

Lucía

El lun, 31 jul 2023 a las 16:27, Andrew Marx @.***>) escribió:

Basically, the issue could be a bug in my last update, an issue with the raster itself, or an issue with one of the dependencies (namely raster and terra), so we need to narrow that down.

After running it and getting an error, please run sessionInfo() and paste the info here.

After that, try restarting R, updating raster and terra, and running it again. If the issue persists, run sessionInfo() again and include that here with the original.

Finally, have you tried it with any other rasters? Please try it with these two, and let me know if either produces errors:

r1 = raster::raster(matrix(0:11,3))

r2 = r1 r2[3:7] = NA

— Reply to this email directly, view it on GitHub https://github.com/AgrDataSci/gdistance/issues/20#issuecomment-1658480396, or unsubscribe https://github.com/notifications/unsubscribe-auth/AZ7CKALE4S6AJVIDRA3HFDDXS66GLANCNFSM6AAAAAA26JAP5U . You are receiving this because you authored the thread.Message ID: @.***>

-- Lucía Sainz Escudero Departamento de Biodiversidad y Biología Evolutiva Museo Nacional de Ciencias Naturales (CSIC) José Gutiérrez Abascal 2 28006, Madrid