hutilscpp (version 0.1.0)

match_nrst_haversine: Match coordinates to nearest coordinates

Description

When geocoding coordinates to known addresses, an efficient way to match the given coordinates with the known is necessary. This function provides this efficiency by using C++ and allowing approximate matching.

Usage

match_nrst_haversine(lat, lon, addresses_lat, addresses_lon,
  Index = seq_along(addresses_lat), cartesian_R = NULL,
  close_enough = 10, excl_self = FALSE, as.data.table = TRUE,
  .verify_box = TRUE)

Arguments

lat, lon

Coordinates to be geocoded. Numeric vectors of equal length.

addresses_lat, addresses_lon

Coordinates of known locations. Numeric vectors of equal length (likely to be a different length than the length of lat, except when excl_self = TRUE).

Index

A vector the same length as lat to encode the match between lat,lon and addresses_lat,addresses_lon. The default is to use the integer position of the nearest match to addresses_lat,addresses_lon.

cartesian_R

The maximum radius of any address from the points to be geocoded. Used to accelerate the detection of minimum distances. Note, as the argument name suggests, the distance is in cartesian coordinates, so a small number is likely.

close_enough

The distance, in metres, below which a match will be considered to have occurred. (The distance that is considered "close enough" to be a match.)

For example, close_enough = 10 means the first location within ten metres will be matched, even if a more distant match occurs later.

May be provided as a string to emphasize the units, e.g. close_enough = "0.25km". Only km and m are permitted.

excl_self

(bool, default: FALSE) For each \(x_i\) of the first coordinates, exclude the \(y_i\)-th point when determining closest match. Useful to determine the nearest neighbour within a set of coordinates, viz. match_nrst_haversine(x, y, x, y, excl_self = TRUE).

as.data.table

Return result as a data.table? If FALSE, a list is returned. TRUE by default to avoid dumping a huge list to the console.

.verify_box

Check the initial guess against other points within the box of radius \(\ell^\infty\).

Value

A list (or data.table if as.data.table = TRUE) with two elements, both the same length as lat, giving for point lat,lon:

pos

the position (or corresponding value in Table) in addresses_lat,addresses_lon nearest to lat, lon.

dist

the distance, in kilometres, between the two points.

Examples

Run this code
# NOT RUN {
lat2 <- runif(5, -38, -37.8)
lon2 <- rep(145, 5)

lat1 <- c(-37.875, -37.91)
lon1 <- c(144.96, 144.978)

match_nrst_haversine(lat1, lon1, lat2, lon2, 0L)
match_nrst_haversine(lat1, lon1, lat1, lon1, 11:12, excl_self = TRUE)

# }

Run the code above in your browser using DataLab