Title: | Constrained distance calculation and associated geotools |
---|---|
Description: | This package allows the calculation of distances that are constrained by frontiers, islands, mountains, ... These distances are then implemented in classical geotools like kriging with a modified version of geoR functions. |
Authors: | Sébastien Rochette |
Maintainer: | Sébastien Rochette <[email protected]> |
License: | GPL-3 |
Version: | 0.1.0.8000 |
Built: | 2025-02-23 03:32:41 UTC |
Source: | https://github.com/statnmap/GeoDist |
Automatically fitting a variogram to the data on which it is applied. The automatic fitting is done through fit.variogram. In fit.variogram the user had to supply an initial estimate for the sill, range etc. autofitVariogram provides this estimate based on the data and then calls fit.variogram.
For details, see autofitVariogram
autofitVariogram.dist( formula, input_data, model = c("Sph", "Exp", "Gau", "Ste"), kappa = c(0.05, seq(0.2, 2, 0.1), 5, 10), fix.values = c(NA, NA, NA), verbose = FALSE, GLS.model = NA, start_vals = c(NA, NA, NA), cutoff, miscFitOptions = list(), dist.mat = NULL, ... )
autofitVariogram.dist( formula, input_data, model = c("Sph", "Exp", "Gau", "Ste"), kappa = c(0.05, seq(0.2, 2, 0.1), 5, 10), fix.values = c(NA, NA, NA), verbose = FALSE, GLS.model = NA, start_vals = c(NA, NA, NA), cutoff, miscFitOptions = list(), dist.mat = NULL, ... )
formula |
formula that defines the dependent variable as a linear model of independent variables; suppose the dependent variable has name 'z', for ordinary and simple kriging use the formula 'z~1'; for simple kriging also define 'beta' (see below); for universal kriging, suppose 'z' is linearly dependent on 'x' and 'y', use the formula 'z~x+y'. |
input_data |
An object of SpatialPointsDataFrame-class. |
model |
The list of variogrammodels that will be tested. |
kappa |
Smoothing parameter of the Matern model. Provide a list if you want to check more than one value. |
fix.values |
Can be used to fix a variogram parameter to a certain value. It consists of a list with a length of three. The items describe the fixed value for the nugget, range and sill respectively. They need to be given in that order. Setting the value to NA means that the value is not fixed. |
verbose |
logical, if TRUE the function will give extra feedback on the fitting process |
GLS.model |
If a variogram model is passed on through this parameter a Generalized Least Squares sample variogram is calculated. |
start_vals |
Can be used to give the starting values for the variogram fitting. The items describe the fixed value for the nugget, range and sill respectively. They need to be given in that order. Setting the value to NA means that the value will be automatically chosen. |
cutoff |
Parameter included in variogram (maximum distance of variogram). Default to diagonal * 0.35. |
miscFitOptions |
A list with named arguments that provide additional control over the fitting process.
For example:
|
dist.mat |
Square matrix of distances between points of the dataset ## Not implemented yet |
... |
parameters that are passed on to variogram when calculating the sample variogram. |
Calculate 2D distances from points to points avoiding obstacles
dist.obstacle( from, to, r.ref, dref.r, n.cores, closest = FALSE, step.ang = 5, r.ref3D = FALSE, filename = paste0(tempdir(), "/dref.r.RData"), small.dist = TRUE, keep.path = FALSE, longlat = raster::isLonLat(r.ref), tol = 1e-05, igraph = FALSE )
dist.obstacle( from, to, r.ref, dref.r, n.cores, closest = FALSE, step.ang = 5, r.ref3D = FALSE, filename = paste0(tempdir(), "/dref.r.RData"), small.dist = TRUE, keep.path = FALSE, longlat = raster::isLonLat(r.ref), tol = 1e-05, igraph = FALSE )
from |
Point data set from which to calculate distances. May be a 2-col matrix of (x,y) coordinates or a SpatialPointDataFrame. Should be in same projection than r.ref. This can also be an object of class distref.data. |
to |
Point data set from which to calculate distances. May be a 2-col matrix of (x,y) coordinates or a SpatialPointDataFrame. Should be in same projection than r.ref. If not set to = from. This can also be an object of class distref.data. If "to" is provided, it should be the bigger dataset. |
r.ref |
reference raster used for distance without obstacles calculation |
dref.r |
object of class distref.raster. Reference raster already processed using distref.raster (e.g. usefull if previously saved using filename.) |
n.cores |
number of cores to run parallel calculation. Default to all cores using parallel::detectCores() |
closest |
if a point is over an obstacle, set to TRUE to find the closest non-obstacle r.ref cell |
step.ang |
Step for angles classes breaks (in degrees). This means that a distance error is allowed in future calculations. Distance error (%) = 100 * (1 - cos((step.ang/2)*pi/180)). With step.ang = 5 degrees, error < 0.1%. Default set to 5. |
r.ref3D |
Logical. Wether the reference raster should be used as a 3D surface to calculate distance accounting for elevation. |
filename |
Path where to save the dref.r issued from reference raster. This may be usefull for re-using the raster for different datasets. Default to paste0(tempdir(),"/dref.r.RData") |
small.dist |
if TRUE, distances <= 1 paths are directly calculated. |
keep.path |
Logical. Whether to keep SpatialLines of paths. This allows real distances when 2 points are in the same raster cell or close. |
longlat |
Logical. if FALSE, Euclidean distance, if TRUE Great Circle distance. Defined from r.ref. |
tol |
numeric Tolerance to define distances between points to be null. |
igraph |
Logical. Wether to calculate all distances through igraph (TRUE)
or use |
If keep.path is false, returns a matrix of distances with rows = "from" and cols = "to". If keep.path is true, returns a list where dist.mat is the matrix of distance and listLines is a list of SpatialLines. Each list is a SpatialLinesDataFrame corresponding to a starting point in "from".
## Not run: # For "from" and "to" being SpatialPoints allpath <- dist.obstacle(from, to, r.ref, keep.path = TRUE) plot(from) points(to) # All path from "from[1,]" to all "to" lines(allpath$allLines[[1]]) ## End(Not run)
## Not run: # For "from" and "to" being SpatialPoints allpath <- dist.obstacle(from, to, r.ref, keep.path = TRUE) plot(from) points(to) # All path from "from[1,]" to all "to" lines(allpath$allLines[[1]]) ## End(Not run)
Get characteristics of data regarding the reference raster
distref.data( data.pt, r.ref, closest = FALSE, longlat = raster::isLonLat(r.ref) )
distref.data( data.pt, r.ref, closest = FALSE, longlat = raster::isLonLat(r.ref) )
data.pt |
Point data set. May be a 2-col matrix of (x,y) coordinates or a SpatialPointDataFrame. Should be in same projection than r.ref |
r.ref |
Reference raster |
closest |
if a point is over an obstacle, set to TRUE to find the closest non-obstacle r.ref cell |
longlat |
Logical. if FALSE, Euclidean distance, if TRUE Great Circle distance. Defined from r.ref. |
Class for distref.data
data.pt.N
numeric.
NA.pos
numeric
NA.dist
numeric
Transfrom reference raster as a network of paths for distance calculation
distref.raster( r.ref, step.ang = 5, filename = paste0(tempdir(), "/dref.r.RData"), r.ref3D = FALSE, n.cores, save.rdists )
distref.raster( r.ref, step.ang = 5, filename = paste0(tempdir(), "/dref.r.RData"), r.ref3D = FALSE, n.cores, save.rdists )
r.ref |
Raster (1-layer) that will be used to calculate distances. Its resolution impacts precision on distances. |
step.ang |
Step for angles classes breaks (in degrees). This means that a distance error is allowed in future calculations. Distance error (%) = 100 * (1 - cos((step.ang/2)*pi/180)). With step.ang = 5 degrees, error < 0.1%. Default set to 5. |
filename |
Path where to save the dref.r issued from reference raster. This may be usefull for re-using the raster for different datasets. Default to paste0(tempdir(), "/dref.r.RData") |
r.ref3D |
Logical. Wether the reference raster should be used as a 3D surface to calculate distance accounting for elevation. |
n.cores |
number of cores to run parallel calculation. Default to all cores using parallel::detectCores() |
save.rdists |
optional. Path to file where to save the distances matrix between all non-NA raster cells. Saved as csv. If not provided, distances matrix will not be calculated as it may be to big to be stored in memory. |
Not all direct possible paths are calculated. Direct paths are retained in a focal window around cells. The size of the focal window is determined by the step.ang value. All paths retained are stored in a undirected graph (as from library igraph). All paths real distances are also stored to be weights for future points to points distances calculations.
coords.r coordinates of all cells of the raster r.ref.NA.nb cell that are NA in the reference raster (obstacles) cell.Relpos.ngb row and col of neighbors retained after the angle calculation, relative to the cell of origin (moving window) cell.Relpos.cross row and col of cells crossed by lines between origin and neighbors, relative to the cell of origin (moving window)
Class for distref.raster
r.ref.path
path of the raster
r.ref.dim
dimensions of the raster
r.ref.N
cells numerotation
r.ref.NA.nb
numerotation of NA cells
adj.ref.graph
adjacency graph
path.w
weight (distance) of paths
Eyefit function of geoR with higher range for phi and sigma
eyefit.large(vario, max.phi = 2, max.sigma = 2, silent = FALSE)
eyefit.large(vario, max.phi = 2, max.sigma = 2, silent = FALSE)
vario |
An empirical variogram object as returned by the function
|
max.phi |
Numeric as multiplied by max(vario$u). Default to 2. |
max.sigma |
Numeric as multiplied by max(vario$v). Default to 2. |
silent |
logical indicating wheather or not the fitted variogram must be returned. |
Find neighbours without obstacles in different directions
Find.ngb.wo.obstacle( i, r.ref, coords.r, r.ref.NA.nb, cell.Relpos.ngb, cell.Relpos.cross )
Find.ngb.wo.obstacle( i, r.ref, coords.r, r.ref.NA.nb, cell.Relpos.ngb, cell.Relpos.cross )
i |
cell number |
r.ref |
reference raster |
coords.r |
coordinates of the reference raster |
r.ref.NA.nb |
Numerotation of NA ref.raster cells |
cell.Relpos.ngb |
Relative position of neighbours in the moving window |
cell.Relpos.cross |
Numerotation of positions of neighbours of the moving window |
matrix with all neighbours of each cell that are not NA
Inverse distance calculation using custom distances
idw.dist( data, coords, locations, loc.dist, idp = 2, max.dist, nmin = 1, nmax, longlat = TRUE )
idw.dist( data, coords, locations, loc.dist, idp = 2, max.dist, nmin = 1, nmax, longlat = TRUE )
data |
vector of values to be interpolated at data coordinates |
coords |
coordinates of observation points. Can also be an object of class SpatialPoints*. Not necessary with loc.dist. |
locations |
coordinates of points where to provide predictions. Can also be an object of class SpatialPoints*. Not necessary with loc.dist. |
loc.dist |
Matrix of distances between data (rows) and locations (cols) |
idp |
specify the inverse distance weighting power. Default to 2. |
max.dist |
Maximum distance radius for neighbours search in the idw interpolation. maxdist is in km when longlat is TRUE (non-projected crs), in meters otherwise. If loc.dist is specified, maxdist should be in same distance unit. |
nmin |
if the number of nearest observations within distance maxdist is less than nmin, a missing value will be generated; see maxdist |
nmax |
the number of nearest observations that should be used for a kriging prediction or simulation, where nearest is defined in terms of the space of the spatial locations. By default, all observations are used |
longlat |
Logical Wether coordinates are not-projected (TRUE) or are projected (FALSE). |
Special case idw for reduced time calculation
idw.dist.special(data, loc.dist.order, loc.dist.nmax, idp)
idw.dist.special(data, loc.dist.order, loc.dist.nmax, idp)
data |
vector of data to interpolate on points. Refer to output of Prep_loc_dist |
loc.dist.order |
matrix of nmax lines and nb points columns with position of data to keep |
loc.dist.nmax |
matrix of nmax lines and nb points columns with distances between kept data and points position |
idp |
specify the inverse distance weighting power. Default to 2. |
Kriging. Modified from function krige.conv in geoR library.
krige.conv.dist( geodata, coords = geodata$coords, data = geodata$data, locations, borders, krige, output, dist.mat, loc.dist, loc.loc.dist )
krige.conv.dist( geodata, coords = geodata$coords, data = geodata$data, locations, borders, krige, output, dist.mat, loc.dist, loc.loc.dist )
geodata |
a list containing elements |
coords |
an |
data |
a vector with n data values. By default it takes the
component |
locations |
an |
borders |
optional. By default reads the element |
krige |
a list defining the model components and the type of
kriging. It can take an output to a call to |
output |
a list specifying output options.
It can take an output to a call to |
dist.mat |
Square matrix of distances between points of the dataset |
loc.dist |
is a n (data) x N (locations) matrix with distances between data points and prediction locations. |
loc.loc.dist |
is a N (locations) x N (locations) matrix with distances between prediction locations. |
Fit of gaussian field. Modified from function likfit in geoR.
likfit.dist( geodata, coords = geodata$coords, data = geodata$data, trend = "cte", ini.cov.pars, fix.nugget = FALSE, nugget = 0, fix.kappa = TRUE, kappa = 0.5, fix.lambda = TRUE, lambda = 1, fix.psiA = TRUE, psiA = 0, fix.psiR = TRUE, psiR = 1, cov.model, dist.mat, realisations, lik.method = "ML", components = TRUE, nospatial = TRUE, limits = pars.limits(), print.pars = FALSE, messages, ... )
likfit.dist( geodata, coords = geodata$coords, data = geodata$data, trend = "cte", ini.cov.pars, fix.nugget = FALSE, nugget = 0, fix.kappa = TRUE, kappa = 0.5, fix.lambda = TRUE, lambda = 1, fix.psiA = TRUE, psiA = 0, fix.psiR = TRUE, psiR = 1, cov.model, dist.mat, realisations, lik.method = "ML", components = TRUE, nospatial = TRUE, limits = pars.limits(), print.pars = FALSE, messages, ... )
geodata |
a list containing elements |
coords |
an |
data |
a vector with n data values. By default it takes the
component |
trend |
specifies the mean part of the model. See documentation
of |
ini.cov.pars |
initial values for the covariance parameters:
|
fix.nugget |
logical, indicating whether the parameter
|
nugget |
value of the nugget parameter.
Regarded as a fixed value if |
fix.kappa |
logical, indicating whether the extra parameter
|
kappa |
value of the extra parameter |
fix.lambda |
logical, indicating whether the Box-Cox transformation parameter
|
lambda |
value of the Box-Cox transformation parameter
|
fix.psiA |
logical, indicating whether the anisotropy angle parameter
|
psiA |
value (in radians) for the anisotropy angle parameter
|
fix.psiR |
logical, indicating whether the anisotropy ratio parameter
|
psiR |
value, always greater than 1, for the anisotropy ratio parameter
|
cov.model |
a string specifying the model for the correlation
function. For further details see documentation for |
dist.mat |
Square matrix of distances between data points |
realisations |
optional. Logical or a vector indicating the number of replication
for each datum. For further information see |
lik.method |
(formely method.lik) options are |
components |
an |
nospatial |
logical. If |
limits |
values defining lower and upper limits for the model
parameters used in the numerical minimisation.
The auxiliary function |
print.pars |
logical. If |
messages |
logical. Indicates whether status messages should be printed on the screen (or output device) while the function is running. |
... |
additional parameters to be passed to the minimisation
function. Typically arguments of the type |
Prepare loc.dist for idw special case
Prep_loc_dist(loc.dist, nmin = 1, nmax, max.dist)
Prep_loc_dist(loc.dist, nmin = 1, nmax, max.dist)
loc.dist |
Matrix of distances between data (rows) and locations (cols) |
nmin |
if the number of nearest observations within distance maxdist is less than nmin, a missing value will be generated; see maxdist |
nmax |
the number of nearest observations that should be used for a kriging prediction or simulation, where nearest is defined in terms of the space of the spatial locations. By default, all observations are used |
max.dist |
Maximum distance radius for neighbours search in the idw interpolation. |
Calculate distance from one point to a set of points using adjacency matrix
Pt2Pts.wo.obstacle( dref.from, dref.to, dref.r, r.ref, longlat, small.dist = TRUE, keep.path = FALSE )
Pt2Pts.wo.obstacle( dref.from, dref.to, dref.r, r.ref, longlat, small.dist = TRUE, keep.path = FALSE )
dref.from |
object of class distref.data for a single point |
dref.to |
object of class distref.data |
dref.r |
object of class distref.raster corresponding to r.ref |
r.ref |
reference raster (only used for its coordinates here) |
longlat |
Logical. if FALSE, Euclidean distance, if TRUE Great Circle distance. Defined from r.ref. |
small.dist |
if TRUE, distances <= 1 paths are directly calculated. |
keep.path |
Logical. Whether to keep SpatialLines of paths. This allows real distances when 2 points are in the same raster cell or close. |
Vector of distances of length(B)
## Not run: # Need gdistance ?? ## End(Not run)
## Not run: # Need gdistance ?? ## End(Not run)
Computes cartesian coordinates from long,lat geographical coordinates Use of geoid with radius and flattening to calculate correct x,y,z coordinates
sph2car(long, lat, radius = 6378137, f = 1/298.257223563, deg = TRUE)
sph2car(long, lat, radius = 6378137, f = 1/298.257223563, deg = TRUE)
long |
longitude values, can also contain a matrix of (long, lat), or (long, lat and and radius) in that order. |
lat |
latitude values. |
radius |
major (equatorial) radius of the ellipsoid. The default value is for WGS84 |
f |
numeric. Ellipsoid flattening. The default value is for WGS84 |
deg |
Specifies if input is in degrees (default) or radians. |
Use geosphere::refEllipsoids() for other radius and f possibilites.
Variogram calculation. Modified from geoR.
variog.dist( geodata, coords = geodata$coords, data = geodata$data, uvec = "default", breaks = "default", trend = "cte", lambda = 1, option = c("bin", "cloud", "smooth"), estimator.type = c("classical", "modulus"), nugget.tolerance, max.dist, dist.mat, pairs.min = 2, bin.cloud = FALSE, direction = "omnidirectional", tolerance = pi/8, unit.angle = c("radians", "degrees"), angles = FALSE, messages, ... )
variog.dist( geodata, coords = geodata$coords, data = geodata$data, uvec = "default", breaks = "default", trend = "cte", lambda = 1, option = c("bin", "cloud", "smooth"), estimator.type = c("classical", "modulus"), nugget.tolerance, max.dist, dist.mat, pairs.min = 2, bin.cloud = FALSE, direction = "omnidirectional", tolerance = pi/8, unit.angle = c("radians", "degrees"), angles = FALSE, messages, ... )
geodata |
a list containing element |
coords |
an |
data |
a vector or matrix with data values.
If a matrix is provided, each column is regarded as one variable or realization.
Defaults to |
uvec |
a vector with values used to define the variogram binning. Only
used when |
breaks |
a vector with values to define the variogram binning. Only
used when |
trend |
specifies the mean part of the model.
See documentation of |
lambda |
values of the Box-Cox transformation parameter.
Defaults to |
option |
defines the output type: the options |
estimator.type |
|
nugget.tolerance |
a numeric value. Points which are separated by a distance less than this value are considered co-located. Defaults to zero. |
max.dist |
a numerical value defining the maximum distance for the variogram. Pairs of locations separated for distance larger than this value are ignored for the variogram calculation. If not provided defaults takes the maximum distance among all pairs of data locations. |
dist.mat |
Square matrix of distances between points of the dataset |
pairs.min |
a integer number defining the minimum numbers of
pairs for the bins.
For |
bin.cloud |
logical. If |
direction |
a numerical value for the directional (azimuth) angle. This
used to specify directional variograms. Default defines the
omnidirectional variogram. The value must be in the interval
|
tolerance |
numerical value for the tolerance angle, when
computing directional variograms. The value must be in the interval
|
unit.angle |
defines the unit for the specification of angles in
the two previous arguments. Options are |
angles |
Logical with default to |
messages |
logical. Indicates whether status messages should be printed on the screen (or output device) while the function is running. |
... |
arguments to be passed to the function |