Get a table or matrix with the distances (Euclidean or cost distances) between pairs of nodes. It can be used as a connector file.

distancefile(
  nodes,
  id,
  type = "centroid",
  distance_unit = NULL,
  keep = NULL,
  resistance = NULL,
  resist.units = FALSE,
  CostFun = NULL,
  ngh = NULL,
  mask = NULL,
  threshold = NULL,
  geometry_out = NULL,
  bounding_circles = NULL,
  parallel = FALSE,
  edgeParallel = FALSE,
  least_cost.java = FALSE,
  cores.java = 1,
  ram.java = NULL,
  pairwise = TRUE,
  write = NULL
)

Arguments

nodes

object of class sf, SpatialPolygons, RasterLaryer or SpatRaster (terra package). It must be in a projected coordinate system. If nodes is a raster layer then raster values (Integer) will be taken as "id".

id

character. If nodes is a shapefile then you must specify the column name with the node ID in the shapefile data table.

type

character. Choose one of the distances: "centroid" (faster, default), where Euclidean distance is calculated from feature centroid; "edge", where Euclidean distance is calculated from feature edges; "least-cost" that takes into account obstacles and local friction of the landscape (see, "gdistance" package); "commute-time" that is analogous to the resistance distance of circuitscape. The commute-time distance is based on the random walk theory and calculated using the electrical circuit theory (See, gdistance package). If the type is equal to "least-cost" or "commute-time", then you have to use the "resistance" argument.

distance_unit

character. If Euclidean distance is selected or resist.units is TRUE then you can set a distance unit, "Makurhini::unit_covert()" compatible unit ("m", "km", "inch", "foot", "yard", "mile"). Default equal to meters "m".

keep

numeric. Argument for higher processing speed. In case you have selected the "edge" distance, use this option to simplify the geometry and reduce the number of vertices. The value can range from 0 to 1 and is the proportion of points to retain (default equal to NULL). The higher the value, the higher the speed but less precision.

resistance

raster. Raster object with resistance values.

resist.units

logical. If type = "least-cost" then yo can transform cost units to geographic units by multiplying the cost by the resolution of the raster.

CostFun

A function to compute the cost to move between cells. Available only if you you have selected the "least-cost" or "commute-time" distance. The default is the mean (isotropic cost distance): function(x) mean(x). If resistance is a conductance raster then you can use: function(x) 1/mean(x)

ngh

numeric. Neighbor graph (directions) for distance calculations: 4 (von Neu-mann neighbourhood), 8 (Moore neighborhood) or 16. Available only if you have selected the "least-cost" or "commute-time" distance.

mask

object of class sf, sfc, sfg or SpatialPolygons. For higher processing speed of "least-cost" or "commute-time" distances. Use this option to clip the resistance at the extent of the mask.

threshold

numeric. Distance threshold, pairs of nodes with a distance value above this threshold will be discarded.

geometry_out

numeric. You can use this argument if you have selected the "least-cost" or "commute-time" distance. If some spatial geometries are out of the resistance extent, then a buffer zone the large enough to cover these spatial geometries and with this numeric value will be added to the resistance, so that it is possible to calculate a cost distance value for the pairs of nodes that involve these geometries and avoid an error. If NULL, then a Euclidean distance (centroid) will be calculated to find these distances.

bounding_circles

numeric. If a value is entered, this will create bounding circles around pairs of core areas (recommended for speed, large resistance rasters or pixel resolution < 150 m). Buffer distances are entered in map units. Also, the function is parallelized using and furrr package and multiprocess plan, default = NULL.

parallel

logical. If nodes is a raster then you can use this argument for larges RasterLayer.

edgeParallel

logical. Parallelize the edge distance using furrr package and multiprocess plan, default = FALSE.

least_cost.java

logical. If TRUE then the programming language and computing platform 'java' will be used to estimate the least-cost distance USING only the FORMULA: function(x) 1/mean(x). It is necessary to have java installed. This option use the package 'graph4lg' to reduce computation times.

cores.java

numeric. Computer cores used to run the .jar file (see, graph4lg), default = 1.

ram.java

numeric. RAM gigabytes to run the .jar file (see, graph4lg), default = NULL.

pairwise

logical. If TRUE a pairwise table is returned (From, To, distance) otherwise it will be a matrix.

write

character. Output path, with name and extension ".txt".

Value

Exports a euclidean or cost distance table between pairs of nodes.

References

https://www.rdocumentation.org/packages/rgeos/versions/0.3-26/topics/gDistance
http://cgm.cs.mcgill.ca/~godfried/teaching/cg-projects/98/normand/main.html.