-
Notifications
You must be signed in to change notification settings - Fork 1
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
d8c0608
commit 6366aba
Showing
4 changed files
with
170 additions
and
151 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,44 @@ | ||
# Inter-class transformers ----------------------------------------------- | ||
# Terra to SF transformer | ||
# This function is five time faster than | ||
# st::st_as_sf() due to {wk} package | ||
terra_to_sf <- | ||
function(input) { | ||
spatial_data <- | ||
terra::as.data.frame(input) | ||
|
||
if (length(spatial_data) == 0) { | ||
terra::geom(input, wk = TRUE) |> | ||
wk::as_wkt() |> | ||
sf::st_as_sf() |> | ||
sf::st_set_crs(terra::crs(input)) | ||
} else { | ||
terra::geom(input, wk = TRUE) |> | ||
wk::as_wkt() |> | ||
sf::st_as_sf() |> | ||
sf::st_set_crs(terra::crs(input)) |> | ||
cbind(terra::as.data.frame(spatial_data)) | ||
} | ||
} | ||
|
||
# Terra to GEOS transformer | ||
terra_to_geos <- | ||
function(input) { | ||
input |> | ||
sf::st_as_sf() |> | ||
geos::as_geos_geometry() | ||
# input |> | ||
# terra::geom(wkt = TRUE) |> | ||
# geos::as_geos_geometry(crs = sf::st_crs(input)) | ||
} | ||
|
||
# GEOS to terra transformer | ||
geos_to_terra <- | ||
function(input) { | ||
wk_input <- wk::as_wkt(input) | ||
|
||
terra::vect( | ||
as.character(wk_input), | ||
crs = wk::wk_crs(wk_input)$wkt | ||
) | ||
} |
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,107 @@ | ||
# Check is package installed | ||
check_package <- | ||
function(package) { | ||
if (!requireNamespace(package, quietly = TRUE)) { | ||
stop(paste(package, "is required but not installed.")) | ||
} | ||
} | ||
|
||
# Check that all objects share the same class | ||
check_same_class <- | ||
function(obj1, obj2, obj3) { | ||
class1 <- class(obj1) | ||
class2 <- class(obj2) | ||
class3 <- class(obj3) | ||
|
||
class_check <- | ||
base::identical(class1, class2) && | ||
base::identical(class1, class3) | ||
|
||
if (!class_check) { | ||
stop("All objects must share the same class.") | ||
} | ||
} | ||
|
||
# Get geometry type of the spatial object | ||
get_geom_type <- | ||
function(input) { | ||
if (inherits(input, "sf") || inherits(input, "sfc")) { | ||
sf::st_geometry_type(input, by_geometry = TRUE) | ||
} else if (inherits(input, "SpatVector")) { | ||
terra::geomtype(input) | ||
} else if (inherits(input, "geos_geometry")) { | ||
geos::geos_type(input) | ||
} | ||
} | ||
|
||
# Checks for polygon geometries | ||
check_polygons <- | ||
function(input) { | ||
# Check if input is of class 'sf', 'sfc', 'SpatVector', or 'geos_geometry' | ||
if (!inherits(input, c("sf", "sfc", "SpatVector", "geos_geometry"))) { | ||
stop( | ||
"Input is not of | ||
class 'sf', 'sfc', 'SpatVector', or 'geos_geometry'." | ||
) | ||
} | ||
|
||
# Check if geometry type is POLYGON | ||
geom_type <- get_geom_type(input) | ||
if ( | ||
!all(geom_type %in% | ||
c("POLYGON", "polygons", "polygon", "multipolygon", "MULTIPOLYGON")) | ||
) { | ||
stop("Input does not contain 'POLYGON' or 'MULTIPOLYGON' geometries.") | ||
} | ||
|
||
# If checks pass | ||
return(TRUE) | ||
} | ||
|
||
# Checks for linestring geometries | ||
check_lines <- | ||
function(input) { | ||
# Check if input is of class 'sf', 'sfc', 'SpatVector', or 'geos_geometry' | ||
if (!inherits(input, c("sf", "sfc", "SpatVector", "geos_geometry"))) { | ||
stop( | ||
"Input skeleton is not of | ||
class 'sf', 'sfc', 'SpatVector', or 'geos_geometry'." | ||
) | ||
} | ||
|
||
# Check if geometry type is LINESTRING | ||
geom_type <- get_geom_type(input) | ||
if ( | ||
!all(geom_type %in% | ||
c( | ||
"LINESTRING", "lines", "linestring", | ||
"multilinestring", "MULTILINESTRING" | ||
))) { | ||
stop("Input skeleton does not contain 'LINESTRING' geometry.") | ||
} | ||
|
||
# If checks pass | ||
return(TRUE) | ||
} | ||
|
||
# Checks for points geometries | ||
check_points <- | ||
function(input) { | ||
# Check if input is of class 'sf', 'sfc', | ||
# 'SpatVector', or 'geos_geometry' | ||
if (!inherits(input, c("sf", "sfc", "SpatVector", "geos_geometry"))) { | ||
stop( | ||
"Input point is not of | ||
class 'sf', 'sfc', 'SpatVector', or 'geos_geometry'." | ||
) | ||
} | ||
|
||
# Check if geometry type is POINT | ||
geom_type <- get_geom_type(input) | ||
if (!all(geom_type %in% c("POINT", "points", "point"))) { | ||
stop("Input point does not contain 'POINT' geometry.") | ||
} | ||
|
||
# If checks pass | ||
return(TRUE) | ||
} |
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
6366aba
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🎉 Published on https://centerline.anatolii.nz as production
🚀 Deployed on https://66f8bf6f67e03e99c9511498--centerline.netlify.app