Learn R Programming

SpatialPosition (version 1.0)

CreateDistMatrix: Create a Distance Matrix Between Two Sp Objects

Description

This function creates a distance matrix between two sp objects (SpatialPointsDataFrame or SpatialPolygonsDataFrame).

Usage

CreateDistMatrix(knownpts, unknownpts, longlat = FALSE, bypassctrl = FALSE)

Arguments

knownpts
sp object; rows of the distance matrix.
unknownpts
sp object; columns of the distance matrix.
longlat
logical; euclidean distance (FALSE, default) or Great Circle distance (TRUE).
bypassctrl
logical; bypass the distance matrix size control (see Details).

Value

  • A distance matrix, row names are knownpts row names, column names are unknownpts row names.

Details

The function returns a full matrix of distances in the metric of the points if longlat is FALSE, or in kilometers if longlat is TRUE. This is a wrapper for the spDists function.

If the matrix to compute is too large (more than 100,000,000 cells or more than 10,000,000 origins or destinations) the function sends a confirmation message to warn users about the amount of RAM mobilized. Use bypassctrl = TRUE to skip this control.

See Also

CreateGrid.

Examples

Run this code
# Create a SpatialPointsDataFrame grid of spatMask extent and 200 meters 
# resolution
data(spatData)
mygrid <- CreateGrid(w = spatMask, resolution = 200)
# Create a distance matrix between known spatPts and mygrid
mymat <- CreateDistMatrix(knownpts = spatPts, unknownpts = mygrid, 
                          longlat = FALSE, bypassctrl = FALSE)
mymat[1:5,1:5]
nrow(spatPts)
nrow(mygrid)
dim(mymat)

Run the code above in your browser using DataLab