Skip to content

Commit

Permalink
Update find_nearest_coord to return a data column
Browse files Browse the repository at this point in the history
  • Loading branch information
truenomad committed May 20, 2024
1 parent 48fa37a commit 2295fed
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 25 deletions.
40 changes: 16 additions & 24 deletions R/find_nearest_coord.R
Original file line number Diff line number Diff line change
Expand Up @@ -23,36 +23,28 @@
#' @export
# Define the function
find_nearest_coord <- function(lon, lat,
shapefile = NULL,
shapefile = NULL,
level_to_return = "adm2") {

# Use default shapefile if none provided
if (is.null(shapefile)) {
shapefile <- poliprep::shp_global
}

# Prepare the `shp_global` dataset
res <- shapefile |>
dplyr::filter(ENDDATE > as.Date("9900-12-31")) |>
dplyr::rename(adm0 = ADM0_NAME,
adm1 = ADM1_NAME,
adm2 = ADM2_NAME) |>
dplyr::mutate(
CENTER_LON = as.numeric(CENTER_LON),
CENTER_LAT = as.numeric(CENTER_LAT)
) |>
sf::st_as_sf(coords = c("CENTER_LON", "CENTER_LAT"), crs = 4326)
shapefile_prepared <- shapefile |>
dplyr::filter(ENDDATE > as.Date("9900-12-31")) |>
dplyr::rename(adm0 = ADM0_NAME,
adm1 = ADM1_NAME,
lon = CENTER_LON,
lat = CENTER_LAT,
adm2 = ADM2_NAME) |>
dplyr::mutate(lon = as.numeric(lon),
lat = as.numeric(lat)) |>
sf::st_as_sf(coords = c("lon", "lat"), crs = 4326)

# Define the target point
target_point <- sf::st_sfc(
sf::st_point(c(lon, lat)), crs = 4326
) |>
sf::st_sf()
target_points <- sf::st_as_sf(data.frame(lon = lon, lat = lat),
coords = c("lon", "lat"), crs = 4326)

# Find the nearest coordinate
distances <- sf::st_distance(target_point, res)
nearest_index <- which.min(distances)
nearest_coord <- res[nearest_index, ][[level_to_return]]
nearest_indices <- sf::st_nearest_feature(target_points, shapefile_prepared)
nearest_coords <- shapefile_prepared[nearest_indices, ][[level_to_return]]

return(as.vector(nearest_coord))
return(nearest_coords)
}
2 changes: 1 addition & 1 deletion tests/testthat/test-find_nearest_coord.R
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@ testthat::test_that("find_nearest_coord returns correct area", {

result2 <- find_nearest_coord(
lon = -24.0324, lat = 15.93214, level_to_return = "adm2")
testthat::expect_equal(result2, "RIBERIA BRAVA")
testthat::expect_equal(as.vector(result2), "RIBERIA BRAVA")

})

0 comments on commit 2295fed

Please sign in to comment.