-
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.
- Loading branch information
1 parent
cc9a985
commit 2ec452e
Showing
8 changed files
with
181 additions
and
38 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -37,6 +37,7 @@ Imports: | |
tools, | ||
utils, | ||
waiter, | ||
zip, | ||
yaml | ||
Suggests: | ||
spelling, | ||
|
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
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,56 @@ | ||
#' Parse spatial data and convert to [`sf`] format | ||
#' | ||
#' @description | ||
#' This is a small helper function that converts any type of spatial | ||
#' format to a [`sf`] object. | ||
#' @param file A [`character`] file path | ||
#' @param make_valid A [`logical`] on whether \code{'file'} should be ensured to | ||
#' be a valid geometry (Default: \code{FALSE}). | ||
#' @keywords internal | ||
#' @noRd | ||
spatial_to_sf <- function(file, make_valid = FALSE){ | ||
assertthat::assert_that(is.character(file), | ||
is.logical(make_valid)) | ||
|
||
# Get file extension | ||
ext <- tolower( tools::file_ext(file) ) | ||
|
||
# Found vector | ||
if(ext %in% c("shp","gpkg")){ | ||
out <- sf::st_read(file, quiet = TRUE) | ||
} else if( ext %in% c("tif","geotiff")){ | ||
out <- terra::rast(file) | ||
out[out>0] <- 1 # Replace all with 1 | ||
# If there are 0 assume those should be NA | ||
out[out==0] <- NA | ||
out <- out |> terra::as.polygons() |> sf::st_as_sf() | ||
} else { | ||
return( NULL ) | ||
} | ||
# --- # | ||
# Check for empty crs | ||
if(is.na(sf::st_crs(out))){ | ||
# Assume long-lat | ||
out <- sf::st_set_crs(out, value = sf::st_crs(4326)) | ||
} | ||
|
||
# Check for empty geometries | ||
if(all( sf::st_is_empty(out) )) return( NULL ) | ||
|
||
if(make_valid){ | ||
if(!all( sf::st_is_valid(out) )){ | ||
out <- sf::st_make_valid(out) | ||
} | ||
} | ||
|
||
# Convert to MULTIPOLYGON | ||
out <- out |> sf::st_cast("MULTIPOLYGON") | ||
|
||
# Transform | ||
out <- out |> sf::st_transform(crs = sf::st_crs(4326)) | ||
|
||
# Rename geometry name to be sure | ||
sf::st_geometry(out) <- "geometry" # rename | ||
|
||
return(out) | ||
} |
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
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
Oops, something went wrong.