Skip to content

Commit

Permalink
✅ Add test for creating linestring
Browse files Browse the repository at this point in the history
  • Loading branch information
NONONOexe committed Sep 3, 2024
1 parent 3d06e6e commit a243fd0
Show file tree
Hide file tree
Showing 11 changed files with 93 additions and 54 deletions.
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# Generated by roxygen2: do not edit by hand

S3method(create_linestring,coordinates)
S3method(create_linestring,numeric)
S3method(create_points,coordinates)
S3method(create_points,numeric)
S3method(extract_segmented_network_nodes,road_network)
Expand Down
56 changes: 30 additions & 26 deletions R/create-linestring.R
Original file line number Diff line number Diff line change
@@ -1,40 +1,44 @@
#' Create a linestring geometry
#'
#' This function takes a series of numeric arguments representing x and y
#' coordinates and creates a linestring geometry object.
#' This function creates a simple feature linestring object
#' from a series of x, y coordinates.
#'
#' @param x A numeric vector, or the first x coordinate.
#' @param ... Additional numeric vectors.
#' @return A `sf` linestring object.
#' @export
#' @name create_linestring
#' @param ... A series of x, y coordinates.
#' @param coordinates A `coordinates` object.
#' @param crs The coordinate reference system of the points.
#' @return A simple feature linestring object.
#' @examples
#' # Create a linestring from individual coordinates
#' create_linestring(0, 0, 3, 0, 4, 3)
#' linestring_1 <- create_linestring(0, 1, 1, 0, 2, 1)
#' linestring_1
#' plot(linestring_1)
#'
#' # Create a linestring from a numeric vector
#' create_linestring(c(0, 0, 3, 0, 4, 3))
create_linestring <- function(x = NULL, ...) {
# Collect all arguments
arguments <- if (missing(...)) as.list(x) else list(x, ...)
#' linestring_2 <- create_linestring(
#' c(0, 1, 1, 1, 1, 0, 0, 0, 0, 0.5, 1, 0.5)
#' )
#' linestring_2
#' plot(linestring_2)
NULL

# If no arguments are provided, create an empty linestring
if (length(arguments) == 0) {
return(st_linestring())
}
#' @export
create_linestring <- function(...) {
UseMethod("create_linestring")
}

# Check if all arguments are numeric
if (!all(sapply(arguments, is.numeric))) {
stop("all arguments must be numeric")
}
# Check if the number of arguments is even
if (length(arguments) %% 2 != 0) {
stop("arguments must be provided in pairs (x, y) coordinates")
}
#'@export
create_linestring.numeric <- function(..., crs = NULL) {
coordinates <- create_coordinates(...)
if(is.null(crs) || is.na(crs)) crs <- "NA"

return(create_linestring.coordinates(coordinates, crs))
}

# Create a matrix of coordinates
coordinates <- matrix(unlist(arguments), ncol = 2, byrow = TRUE)
# Create a linestring object
#'@export
create_linestring.coordinates <- function(coordinates, crs = NULL, ...) {
linestring <- st_linestring(coordinates)
linestring <- st_sfc(linestring, crs = crs)

return(linestring)
}
18 changes: 12 additions & 6 deletions R/create-points.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#' Create a simple feature collection of points
#' Create points geometry
#'
#' This function creates a simple feature collection of points
#' from a series of x, y coordinates.
Expand All @@ -9,11 +9,17 @@
#' @param crs The coordinate reference system of the points.
#' @return A simple feature points object.
#' @examples
#' # Create points
#' points <- create_points(0, 1, 1, 0, 2, 1)
#' # Create a points from individual coordinates
#' points_1 <- create_points(0, 1, 1, 0, 2, 1)
#' points_1
#' plot(points_1)
#'
#' # Plot the points
#' plot(points)
#' # Create a linestring from a numeric vector
#' points_2 <- create_points(
#' c(0, 1, 1, 1, 1, 0, 0, 0, 0, 0.5, 1, 0.5)
#' )
#' points_2
#' plot(points_2)
NULL

#' @export
Expand All @@ -24,7 +30,7 @@ create_points <- function(...) {
#'@export
create_points.numeric <- function(..., crs = NULL) {
coordinates <- create_coordinates(...)
crs <- ifelse(is.null(crs), "NA", crs)
if(is.null(crs) || is.na(crs)) crs <- "NA"

return(create_points.coordinates(coordinates, crs))
}
Expand Down
6 changes: 4 additions & 2 deletions R/decompose-linestring.R
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,10 @@ decompose_linestring <- function(linestring) {
to <- coordinates[seq_len(num_points - 1) + 1, c("X", "Y"), drop = FALSE]

# Create a list of line segments
segments <- apply(cbind(from, to), 1, create_linestring, simplify = FALSE)
segments <- st_sfc(segments, crs = st_crs(linestring))
segments <- do.call(
c,
apply(cbind(from, to), 1, create_linestring, crs = st_crs(linestring))
)

return(segments)
}
2 changes: 1 addition & 1 deletion R/sample-points-along-linestring.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#' library(sf)
#'
#' # Create a linestrings
#' linestrings <- st_sfc(
#' linestrings <- c(
#' create_linestring(0, 1, 2, 1),
#' create_linestring(1, 1.3, 1, 0, 2, 0.5)
#' )
Expand Down
25 changes: 15 additions & 10 deletions man/create_linestring.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 11 additions & 5 deletions man/create_points.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion man/sample_points_along_linestring.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions tests/testthat/test-create-linestring.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
test_that("`create_linestring.numeric` works valid input", {
linestring <- create_linestring(1, 2, 3, 4, crs = 4326)
expect_s3_class(linestring, "sfc_LINESTRING")
expect_equal(length(linestring), 1)
expect_equal(st_crs(linestring)[[1]], "EPSG:4326")
})

test_that("`create_linestring.coordinates` works with valid input", {
coordinates <- create_coordinates(1, 2, 3, 4)
linestring <- create_linestring(coordinates, crs = 4326)
expect_s3_class(linestring, "sfc_LINESTRING")
expect_equal(length(linestring), 1)
expect_equal(st_crs(linestring)[[1]], "EPSG:4326")
})
4 changes: 2 additions & 2 deletions tests/testthat/test-decompose-linestring.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ test_that("`decompose_linestring` correctly decompose a linestring into segments
linestring <- create_linestring(0, 0, 1, 1, 2, 1, 4, 0)

# Define the expected segments after decomposition
expected_segments <- st_sfc(list(
expected_segments <- c(
create_linestring(0, 0, 1, 1),
create_linestring(1, 1, 2, 1),
create_linestring(2, 1, 4, 0)
))
)

# Decompose the linestring using the function
result <- decompose_linestring(linestring)
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-extract-road-network-links.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
test_that("`extract_road_network_links` works correctly", {
expected_link_linestrings <- st_sfc(
expected_link_linestrings <- c(
create_linestring(0, 0, 3, 0, 4, 3),
create_linestring(6, 1, 7, 1),
create_linestring(0, 1, 6, 1),
Expand Down

0 comments on commit a243fd0

Please sign in to comment.