-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
⭐ Add new find_nearest_coord as a new function
- Loading branch information
Showing
7 changed files
with
123 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
#' Find Nearest Coordinate from shapefile Dataset | ||
#' | ||
#' This function takes a pair of coordinates (longitude and latitude) and finds | ||
#' the nearest coordinate from the `shp_global` dataset. | ||
#' | ||
#' The shapefile dataset should contain longitude and latitude information | ||
#' with columns named `CENTER_LON` and `CENTER_LAT`. The function calculates the | ||
#' distance between the target point and all points in the dataset, returning | ||
#' the nearest coordinate. | ||
#' | ||
#' @param lon Numeric. Longitude of the target point. | ||
#' @param lat Numeric. Latitude of the target point. | ||
#' @param shapefile An optional `sf` object containing the shapefile data. | ||
#' Defaults to `poliprep::shp_global`. | ||
#' @param level_to_return The admin level to return. | ||
#' @return A vector of admin level names containing the nearest coordinate from | ||
#' the provided shapefile. | ||
#' @examples | ||
#' # Example usage | ||
#' # Assuming `poliprep::shp_global` is already loaded in the environment | ||
#' nearest_coord <- find_nearest_coord(15.28172, -4.271301) | ||
#' print(nearest_coord) | ||
#' @export | ||
# Define the function | ||
find_nearest_coord <- function(lon, lat, | ||
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) | ||
|
||
# Define the target point | ||
target_point <- sf::st_sfc( | ||
sf::st_point(c(lon, lat)), crs = 4326 | ||
) |> | ||
sf::st_sf() | ||
|
||
# Find the nearest coordinate | ||
distances <- sf::st_distance(target_point, res) | ||
nearest_index <- which.min(distances) | ||
nearest_coord <- res[nearest_index, ][[level_to_return]] | ||
|
||
return(as.vector(nearest_coord)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
|
||
# set up mocek shapefile | ||
mock_data <- data.frame( | ||
CENTER_LON = c(-4.222, -3.2, -24.5), | ||
CENTER_LAT = c(15.02222, 43.2, 23.5), | ||
ENDDATE = c("9999-12-31 01:00:00", "9999-12-31 01:00:00", | ||
"9999-12-31 01:00:00"), | ||
ADM0_NAME = c("Mock Area 1", "Mock Area 2", "Mock Area 3"), | ||
ADM1_NAME = c("Mock Area 1", "Mock Area 2", "Mock Area 3"), | ||
ADM2_NAME = c("Mock Area 1", "Mock Area 2", "Mock Area 3") | ||
) | ||
|
||
# Test cases | ||
testthat::test_that("find_nearest_coord returns correct area", { | ||
result <- find_nearest_coord(shapefile = mock_data, -24.0, 15.9) | ||
testthat::expect_equal(result, "Mock Area 3") | ||
|
||
result2 <- find_nearest_coord( | ||
lon = -24.0324, lat = 15.93214, level_to_return = "adm2") | ||
testthat::expect_equal(result2, "RIBERIA BRAVA") | ||
|
||
}) |