Skip to content

Commit 001dde3

Browse files
committed
Fix EASIN functions
1 parent ea048f1 commit 001dde3

File tree

2 files changed

+21
-5
lines changed

2 files changed

+21
-5
lines changed

R/DWF_EASIN_Process.R

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -400,12 +400,26 @@ EASIN_Process <- function(
400400

401401
## Extract coordinates from WKT string ----
402402
IASDT.R::CatTime("Extract coordinates from WKT string", Level = 1)
403+
403404
WKTs <- dplyr::distinct(EASIN_Data, WKT) %>%
404-
dplyr::mutate(Coords = purrr::map(WKT, IASDT.R::Text2Coords)) %>%
405-
tidyr::unnest_wider(Coords) %>%
406405
dplyr::mutate(
407-
dplyr::across(
408-
.cols = c("Longitude", "Latitude"), .fns = ~ round(.x, 5)))
406+
Points = purrr::map(
407+
.x = WKT,
408+
.f = ~ {
409+
# Extract POINT coordinates from WKT string
410+
Points <- stringr::str_extract_all(
411+
.x, "POINT\\s*\\(\\s*-?\\d+\\.\\d+\\s+-?\\d+\\.\\d+\\s*\\)")[[1]]
412+
413+
if (length(Points) > 0) {
414+
purrr::map(Points, IASDT.R::Text2Coords) %>%
415+
dplyr::bind_rows() %>%
416+
dplyr::mutate(
417+
dplyr::across(
418+
.cols = c("Longitude", "Latitude"), .fns = ~ round(.x, 5)))
419+
} else {
420+
tibble::tibble(Longitude = NA_real_, Latitude = NA_real_)
421+
}
422+
}))
409423

410424
## Add coordinates to data and convert to sf ----
411425
IASDT.R::CatTime("Add coordinates to data and convert to sf", Level = 1)
@@ -416,6 +430,8 @@ EASIN_Process <- function(
416430
dplyr::pull("CellCode")
417431

418432
EASIN_Data <- dplyr::left_join(EASIN_Data, WKTs, by = "WKT") %>%
433+
tidyr::unnest(Points) %>%
434+
dplyr::filter(!is.na(Longitude) & !is.na(Latitude)) %>%
419435
# convert to sf object, while keeping original coordinates as columns
420436
sf::st_as_sf(
421437
coords = c("Longitude", "Latitude"), crs = 4326, remove = FALSE) %>%

R/Spat_Text2Coords.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ Text2Coords <- function(
4141

4242
String %>%
4343
# convert string to 2-columns data frame
44-
stringr::str_remove_all("POINT \\(|\\)") %>%
44+
stringr::str_remove_all("POINT \\(|POINT\\(|\\)") %>%
4545
stringr::str_split_fixed(" ", 2) %>%
4646
as.numeric() %>%
4747
matrix(ncol = 2, byrow = FALSE) %>%

0 commit comments

Comments
 (0)