distSG {SpatialGraph} | R Documentation |
Calculate across-network distance for a set of sparse points
Description
This function obtains the across-network distance for a set
of sparse points, by using the distance slot in a SpatialGraph. The
calculation is supported by a previously calculated between vertex
distance matrix
[via a call to the library igraph
by the function distSGv].
The SpatialGraph is considered as undirected for distance
calculation.
If euc=TRUE
[default], the distance between two points is defined within this function
as the maximum of both the minimum along-network distance and the Euclidean
distance. The distance itself between the points
in x,y and the network is neglected in the function for the
along-network distance.
Both, x
and y
, are SpatialPointsDataFrame
objects, which must contain at least the fields
eID
and chain
, which describe their relationship with
the SpatialGraph
object defined by SG
. These can be
obtained with either the function pointsSLDFchain
or
pointsToLines (the latter is faster, but depends on GEOS)
Usage
distSG(SG, x, y = NULL, euc = TRUE, wei = NULL, getpath = FALSE)
Arguments
SG |
|
x |
|
y |
|
euc |
boolean scalar, whether to use Euclidean distance as minimum threshold for resulting distances |
wei |
if not null, field in |
getpath |
if TRUE (and wei != NULL), |
Details
The application of state-related weights in this version is a simple
state-dependent weight matrix related to some field in SG@e
[i.e. the
edges in the input SpatialGraph
]. The only current calculation
evaluates the path between queried points (x,y), and along the path, for
every junction and jump into a new edge, the ratio for the evaluated state variable (taken as
the highest value divided by the lowest value) between the two edges at
the junction is obtained. Currently a
maximum ratio equal to 10.0 is hard-coded. The product
of ratios along the path gives the weight.
Value
If wei=NULL
, a matrix of distances between x
and
y
. If wei
is not NULL
, a list with a distance
matrix and weight matrix (plus a matrix with eID identifiers
for the path, if getpath=TRUE
) is returned.
Author(s)
Javier Garcia-Pintado, e-mail: jgarciapintado@marum.de
Examples
if (1 > 2) { # not run
dem <- readGDAL(file.path(system.file('external',package='hydrosim'),
'watershed1','IDRISI_maps','dem','dem.rst')) # SpatialGridDataFrame
plotGmeta(layer=dem, xlim=662500 + 2500 * c(-1,+1),
ylim=4227500 + 2500 * c(-1,1), zlim='strloc', as.na=0)
# generate some crossing lines
zz <- list()
zz[[1]] <- digitGmeta(layer=dem, type='Lines', ID=1)
zz[[2]] <- digitGmeta(layer=dem, type='Lines', ID=2)
zz[[3]] <- digitGmeta(layer=dem, type='Lines', ID=3)
SL <- SpatialLines(zz)
SG <- sl2sg(SL, getpath=TRUE)
points(SG@v, cex=2) # plot SpatialGraph vertices
apath <- SG@path[[1,2]] # iteratively plot a path as an example
for (iv in 1:length(apath$v)) {
points(SG@v[apath$v[iv],], cex=2,pch=2)
if (iv == length(apath$v))
break
lines(SG@e[apath$e[iv],],col='blue',lwd=2,lty=2)
Sys.sleep(1)
}
# sample a few points [as a matrix] close to some edges
xy <- digit() # sample locations
xych <- pointsToLines(xy, SG@e) # SpatialPointsDataFrame mapping
points(xy, col='blue', pch=3)
points(xych, col='darkgreen', pch=19)
# along-network distance
xyndis <- distSG(SG, xych)
# state-dependent weighted along-network distance
SG@e@data$wxs <- 3+round(runif(nrow(SG@e@data)),2) # [m2] foo wetted cross-section areas
SG@e@data
xywdis <- distSG(SG, xych, wei='wxs')
xywdis <- xywdis$dis * xywdis$wei # Schur weight application into distance estimation
}