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) using geodesic calculations based on a specified reference ellipsoid

get_nearest_point(
  data,
  data.x,
  data.y,
  query,
  n = 1,
  ellipsoid = c("AA", "AN", "??", "BR", "BN", "CC", "CD", "EB", "EA", "EC", "EF", "EE",
    "ED", "RF", "HE", "HO", "ID", "IN", "KA", "AM", "FA", "SA", "WD", "WE"),
  duplicate = FALSE
)

Arguments

data

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

data.x

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

data.y

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

n

Number of nearest villages to select

ellipsoid

Two letter character value specifying the reference ellipsoid to use for distance calculations

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 kms). If duplicate is TRUE, the result has number of rows equal to nrow(query)

Examples

# Use get_nearest_point() 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_nearest_point(data = sennar_villages, data.x = "x", data.y = "y", query = samp.points, n = 3)
#> Warning: CRS object has comment, which is lost in output
#> Warning: Discarded datum Unknown based on WGS84 ellipsoid in CRS definition, #> but +towgs84= values preserved
#> spid id x y village #> 722 1 923 33.81400 12.11506 AlGreibin #> 723 1 924 33.83056 12.11183 Tagali (AlMazmoum) #> 724 1 925 33.82814 12.10714 Wad Bireiga #> 124 2 124 34.77686 12.85164 Om Sagit #> 126 2 126 34.79900 12.84908 Om Bagara Sharig #> 244 2 244 34.79008 12.82597 Um Bagara Garb #> 18 3 18 33.70356 12.77214 UmMarda #> 19 3 19 33.56717 12.72828 Abo Rawag #> 731 3 932 33.53467 12.72550 Hafir kamtor #> 257 4 257 34.12308 12.78933 Alazara Alrawaj #> 258 4 258 34.14547 12.77569 Tayba #> 259 4 259 34.13689 12.77933 Alwa #> 499 6 499 33.14697 13.41047 Dood Albataheen + Madani Ewiasa #> 500 6 500 33.15650 13.40961 Dood Albasheer #> 501 6 501 33.15042 13.40625 Dood Abdallah #> 303 7 303 33.84000 13.37733 niyala elmtma #> 304 7 304 33.86844 13.41642 fthelrhman #> 306 7 306 33.88831 13.40231 bahtelreda #> locality d #> 722 AlDali and AlMazmoum 16.536039 #> 723 AlDali and AlMazmoum 16.131654 #> 724 AlDali and AlMazmoum 15.970387 #> 124 Al Dindir 80.022405 #> 126 Al Dindir 78.461575 #> 244 Al Dindir 76.867685 #> 18 Abu Hijar 19.586365 #> 19 Abu Hijar 11.542403 #> 731 Abu Hijar 11.201451 #> 257 AlSooki 8.831588 #> 258 AlSooki 8.199247 #> 259 AlSooki 7.792855 #> 499 Sennar 5.885795 #> 500 Sennar 5.449042 #> 501 Sennar 4.854442 #> 303 AlSooki 3.640581 #> 304 AlSooki 2.696516 #> 306 AlSooki 1.799006