sp_distance: Function to find distances between two spatial objects.

View source: R/sp_distance.R

sp_distanceR Documentation

Function to find distances between two spatial objects.

Description

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.

Usage

sp_distance(sp_1, sp_2, features = FALSE, unit = "m")

Arguments

sp_1

Spatial object one.

sp_2

Spatial object two.

features

Should the function be applied to all features in sp_1?

unit

If "km", the returned vector is returned in kilometres rather than metres.

Details

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.

Value

Numeric vector or matrix.

Author(s)

Stuart K. Grange

See Also

gDistance, sp_transform

Examples

## 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)


skgrange/gissr documentation built on June 20, 2024, 12:02 a.m.