The purpose
What I am trying to achieve: I am trying to calculate the friction weighted connectivity values of the polygons in a shapefile using a friction raster. The shapefiles symbolize habitats for the butterfly Euphydryas aurinia, and the friction raster represents the dispersal matrix between these habitats. I have been trying to accomplish this with the gdistance package.
The input data:
A shapefile of 81 polygons in a shape file.
The friction raster, where each of the pixels represent how difficult it is for the butterflies to disperse yhtough that particular habitat
The problem is that all the different methods I have tried so far all return the same error message over and over, no matter what I try and do, which is:
Example 1
library(sf)
library(raster)
library(gdistance)
Ihopslagen_path <- "censored"
IhopslagenSfReadF <- st_read(Ihopslagen_path)
IhopslagenSfReadZm <- st_zm(IhopslagenSfRead)
print(IhopslagenSfRead)
FrictionRasterPath <- "censored"
FrictionRaster <- raster(FrictionRasterPath)
print(FrictionRaster)
FricPoly_raster <- rasterize(IhopslagenSfReadZm, FrictionRaster)
num_polygonsF <- nrow(IhopslagenSfReadZm) FricDistMatrix <- matrix(0, nrow = num_polygonsF, ncol = num_polygonsF)
transition <- transition(FricPoly_raster, function(x) 1, directions = 8)
for (i in 1:num_polygonsF) {
for (j in 1:num_polygonsF) {
FricDistMatrix[i, j] <- costDistance(FricPoly_raster[i], FricPoly_raster[j], transition)
}
}
Error in (function (classes, fdef, mtable) : unable to find an inherited method for function ‘costDistance’ for signature ‘"numeric", "numeric", "TransitionLayer
Example 2
library(sf)
library(raster)
library(gdistance)
Ihopslagen_path <- "censored"
IhopslagenSfReadF <- st_read(Ihopslagen_path)
IhopslagenSfReadZm <- st_zm(IhopslagenSfReadF)
print(IhopslagenSfReadZm)
FrictionRasterPath <- "censored"
FrictionRaster <- raster(FrictionRasterPath)
print(FrictionRaster)
FricPoly_raster <- rasterize(IhopslagenSfReadZm, FrictionRaster)
num_polygonsF <- nrow(IhopslagenSfReadZm)
FricDistMatrix <- matrix(0, nrow = num_polygonsF, ncol = num_polygonsF)
ransition <- transition(FricPoly_raster, function(x) 1, directions = 8)
for (i in 1:num_polygonsF) { for (j in 1:num_polygonsF) { FricDistMatrix[i, j] <- costDistance(FricPoly_raster[[i]], FricPoly_raster[[j]], transition) } }
Error in (function (classes, fdef, mtable) : unable to find an inherited method for function ‘costDistance’ for signature ‘"RasterLayer", "RasterLayer", "TransitionLayer"
Example 3
library(sf)
library(raster)
library(gdistance)
Ihopslagen_path <- "censored"
IhopslagenSfReadF <- st_read(Ihopslagen_path)
IhopslagenSfReadZm <- st_zm(IhopslagenSfReadF)
FrictionRasterPath <- "censored"
FrictionRaster <- raster(FrictionRasterPath)
FricPoly_raster <- rasterize(IhopslagenSfReadZm, FrictionRaster)
transition <- transition(FricPoly_raster, function(x) 1, directions = 8)
FricDistMatrix <- costDistance(FricPoly_raster, transition)
Error in (function (classes, fdef, mtable) : unable to find an inherited method for function ‘costDistance’ for signature ‘"RasterLayer", "TransitionLayer", "missing
Example 4
library(sf)
library(raster)
library(gdistance)
Ihopslagen_path <- "censored"
IhopslagenSfReadF <- st_read(Ihopslagen_path)
IhopslagenSfReadZm <- st_zm(IhopslagenSfReadF)
FrictionRasterPath <- "censored"
FrictionRaster <- raster(FrictionRasterPath)
FricPoly_raster <- rasterize(IhopslagenSfReadZm, FrictionRaster)
transition <- transition(FricPoly_raster, function(x) 1, directions = 8)
FricDistMatrix <- costDistance(FricPoly_raster, FricPoly_raster, transition)
Error in (function (classes, fdef, mtable) : unable to find an inherited method for function ‘costDistance’ for signature ‘"RasterLayer", "RasterLayer", "TransitionLayer"’
I got into this line of thinking by being suggested the cost distance function after sending in:
# Which function from the gDistance package do I need to use in this code where the ??? is? library(sf) library(raster) library(terra) library(gdistance)
Ihopslagen_path <- "censored" IhopslagenSfRead <- st_read(Ihopslagen_path) IhopslagenSfReadZm <- st_zm(IhopslagenSfRead)
print(IhopslagenSfRead) FrictionRasterPath <- "censored" FrictionRaster <- raster(FrictionRasterPath) print(FrictionRaster)
FricPoly <- st_geometry(IhopslagenSfRead) num_polygonsF <- nrow(IhopslagenSfRead) FricDistMatrix <- matrix(0, nrow = num_polygonsF, ncol = num_polygonsF)
for (i in 1:num_polygonsF) { for (j in 1:num_polygonsF) { FricDistMatrix <- ???(FricPoly[i], FricPoly[j], FricPoly) } }
The second and third argument to
gdistance::costDistanceareYou are providing other data types, so it won't work.
See
?gdistance::costDistance