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

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)

Examples

# 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)
#> Warning: Discarded datum WGS_1984 in CRS definition, #> but +towgs84= values preserved
#> Warning: Discarded datum WGS_1984 in CRS definition, #> but +towgs84= values preserved
#> Warning: Discarded datum WGS_1984 in CRS definition, #> but +towgs84= values preserved
#> Warning: CRS object has comment, which is lost in output
get_nn(data = sennar_villages, x1 = "x", y1 = "y", query = samp.points, x2 = "x", y2 = "y", n = 3)
#> id x y village locality d #> 732 933 33.75706 12.41561 Arab AlBalf (Kockry) Abu Hijar 0.12172316 #> 244 244 34.79008 12.82597 Um Bagara Garb Al Dindir 0.56646537 #> 520 520 33.60339 13.17444 Al Kharasabi Sinja 0.17244608 #> 100 100 34.24214 13.01453 Kamrab Al Dindir 0.03399852 #> 179 179 34.86331 13.02667 Um Bagaa Al Dindir 0.01658150 #> 565 565 33.89186 13.57608 Kanbo 7 Sharg Sennar 0.01826824 #> 58 58 34.03142 12.47114 Tozi Abu Hijar 0.15822616 #> 126 126 34.79900 12.84908 Om Bagara Sharig Al Dindir 0.57625866 #> 317 317 33.63758 13.21264 wd salmaan AlSooki 0.22078325 #> 95 95 34.27258 13.00028 Al Fereesh Al Dindir 0.06543542 #> 564 564 33.88842 13.55625 Al Mansoora Sharg Sennar 0.03224762 #> 63 63 33.70803 12.59786 Alsahbaa Abu Hijar 0.23246016 #> 124 124 34.77686 12.85164 Om Sagit Al Dindir 0.59375885 #> 528 528 33.69922 13.20789 Dar Alsalam Alkurtan Sinja 0.24909832 #> 101 101 34.23367 13.07678 Khameesa Al Dindir 0.06949234 #> 563 563 33.83750 13.56875 Mabriika Wa Barakat Sharg Sennar 0.04296518