sp_distance | R Documentation |
sp_distance
calculates the distance between a spatial object and
another spatial object. An example of usage is when an object containing
locations of cities is tested against coastline information to determine how
far cities are away from the coast.
sp_distance(sp_1, sp_2, features = FALSE, unit = "m")
sp_1 |
Spatial object one. |
sp_2 |
Spatial object two. |
features |
Should the function be applied to all features in
|
unit |
If |
sp_distance
uses rgeos::gDistance
for the distance calculations.
This function returns distances based on the unit of the projection system
contained within the spatial objects and both geometries' projection must be
identical.
Numeric vector or matrix.
Stuart K. Grange
gDistance
, sp_transform
## Not run:
# Simple usage
# Calculate the distances of places from the coastline
distances <- sp_distance(sp_places, sp_coast_line, unit = "km")
# Speed the function up by using multiple system cores
distances <- sp_distance(sp_places, sp_coast_line, unit = "km")
# Usage for transforming a data frame
# Load shapefiles
# Coastline, spatial lines
sp_coast_line <- sp_read("coastlines/coastlines")
# Places in the UK, spatial points
sp_places <- sp_read("great-britain/places")
# Get a data frame from the sp_places object
data_places <- sp_places@data
# Find distances between every place in sp_places and sp_coast_line
# We are in the UK, therefore the British National Grid has been used as the
# projection system
# Apply parallel function and add variable to data frame
data_places$distance <- sp_distance(sp_places, sp_coast_line, unit = "km")
# Have a look
head(data_places)
London 33.4
Basingstoke 43.5
York 37
Tobermory 0.22
Charlbury 60.3
# London will be dependent on where the coastline is set after the River
# Thames's mouth.
## End(Not run)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.