get_nn: Function to select nearest community to a given sampling...

Description Usage Arguments Value Author(s) Examples

View source: R/nearest_point_nn.R

Description

Function to select nearest community to a given sampling point (usually the centroid of a square grid for CSAS or of a hexagonal grid for S3M) based on a nearest neighbour algorithm using Euclidean calculations

Usage

1
get_nn(data, x1, y1, query, x2, y2, n, duplicate = FALSE)

Arguments

data

An input data frame or matrix containing longitude and latitude coordinates of village locations from which to find nearest neighbour

x1

A character value specifying the variable name in data containing the longitude coordinates

y1

A character value specifying the variable name in data containing the latitude coordinates

query

an object of class 'SpatialPoints' containing sampling point locations. This is usually the output from applying 'spsample()' function from package 'gstat' to create an even spatial sample across the entire sampling area

x2

A character value specifying the variable name in query containing the longitude coordinates

y2

A character value specifying the variable name in query containing the latitude coordinates

n

A numeric value speciyfing the maximum number of nearest neighbours to search for

duplicate

Logical. Specify if duplicate selected villages are to be kept or discarded. Default is FALSE (discard).

Value

A subset data frame of data of selected villages/communities nearest to the sampling points with a new variable d indicating the distance of the village/community to the sampling point (in coordinate units). If duplicate is TRUE, the result has number of rows equal to nrow(query)

Author(s)

Ernest Guevarra <ernest@guevarra.io>

Examples

1
2
3
4
5
6
7
# Use get_nn() with test sampling points in Sennar to find 3 nearest communities
# from the sampling points
sennar <- subset(sudan01, STATE == "Sennar")
samp.points <- sp::spsample(sennar, type = "hexagonal", n = 10)
get_nn(data = sennar_villages, x1 = "x", y1 = "y",
       query = samp.points, x2 = "x", y2 = "y",
       n = 3)

validmeasures/spatialsampler documentation built on Oct. 16, 2021, 3:15 a.m.