Skip to content

Instantly share code, notes, and snippets.

@PietrH
Created July 1, 2024 12:28
Show Gist options
  • Save PietrH/7a21af67fbffd848eb54bb5c48b7717a to your computer and use it in GitHub Desktop.
Save PietrH/7a21af67fbffd848eb54bb5c48b7717a to your computer and use it in GitHub Desktop.
Round coordinates in WKT strings
round_coordinates <- function(wkt_string, digits = 4) {
wkt_object_type <- wkt_string %>%
stringr::str_extract(".*?(?=\\()")
x_coord <- wkt_string %>%
stringr::str_extract("(?<=POINT\\()[0-9]+\\.[0-9]+") %>%
as.numeric()
y_coord <- wkt_string %>%
stringr::str_extract("[0-9]+\\.[0-9]+(?=\\))") %>%
as.numeric()
x_rounded <- round(x_coord, digits = digits)
y_rounded <- round(y_coord, digits = digits)
glue::glue("{wkt_object_type}({x_rounded} {y_rounded})")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment