Skip to content

Commit

Permalink
🐛 Refactor code to support sfc class
Browse files Browse the repository at this point in the history
  • Loading branch information
NONONOexe committed Aug 30, 2024
1 parent a5b6194 commit fa6eaf7
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 28 deletions.
11 changes: 1 addition & 10 deletions R/create-graph-links.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
#' If `TRUE`, the links are directed.
#' @param unique_links If `TRUE`, the default, the links are unique.
#' Duplicate links are removed. If `FALSE`, duplicate links are retained.
#' @param link_ids A vector of link IDs.
#' @return A data frame of graph links with columns `from` and `to`.
#' @export
#' @examples
Expand All @@ -22,16 +21,11 @@
create_graph_links <- function(source_nodes,
target_nodes,
directed = FALSE,
unique_links = TRUE,
link_id = NULL) {
unique_links = TRUE) {
# Check if the number of source and target nodes are the same
if (length(source_nodes) != length(target_nodes)) {
stop("number of source and target nodes must be the same")
}
# Check if the number of link IDs are the same as the number of links
if (!is.null(link_id) && length(link_id) != length(source_nodes)) {
stop("number of link IDs must be the same as the number of links")
}

# Create links matrix of links
links_mat <- cbind(source_nodes, target_nodes)
Expand All @@ -48,8 +42,5 @@ create_graph_links <- function(source_nodes,
to = links_mat[, 2]
)

# Add link ID if provided
if (!is.null(link_id)) graph_links$id <- link_id

return(graph_links)
}
3 changes: 2 additions & 1 deletion R/filter-points-within-tolerance.R
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ filter_points_within_tolerance <- function(points,
linestring,
tolerance = 0.01) {
# Calculate the distance between the points and the linestring
distances_to_linestring <- as.vector(st_distance(points, linestring))
linestring_sfc <- st_sfc(linestring, crs = st_crs(points))
distances_to_linestring <- as.vector(st_distance(points, linestring_sfc))

# Filter points that are within the tolerance distance
filtered_points <- points[distances_to_linestring <= tolerance]
Expand Down
5 changes: 3 additions & 2 deletions R/remove-points-near-endpoints.R
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,9 @@ remove_points_near_endpoints <- function(points,
linestring,
tolerance = 0.01) {
# Get the start and end points of the linestring
start_point <- st_startpoint(linestring)
end_point <- st_endpoint(linestring)
linestring_sfc <- st_sfc(linestring, crs = st_crs(points))
start_point <- st_startpoint(linestring_sfc)
end_point <- st_endpoint(linestring_sfc)

# Filter points based on distance from start and end points
filtered_points <- points[
Expand Down
2 changes: 1 addition & 1 deletion R/sample-points-along-linestring.R
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ sample_points_along_linestring <- function(linestrings, segment_length) {

# Calculate the number of segments to sample along each linestring
linestrings_length <- st_length(linestrings)
num_segments <- round(linestrings_length / segment_length)
num_segments <- as.integer(round(linestrings_length / segment_length))

# Sample points along each linestring segment
sampled_points <- lapply(
Expand Down
31 changes: 17 additions & 14 deletions R/split-linestring.R
Original file line number Diff line number Diff line change
Expand Up @@ -36,25 +36,27 @@ split_linestring <- function(linestring, split_points, tolerance = 0.01) {
}

#' @export
split_linestring.sfc_LINESTRING <- function(linestring,
split_points,
tolerance = 0.01) {
# Check if `linestring` is a single linestring
if (length(linestring) != 1) {
stop("`linestring` must be a single linestring")
}
split_linestring.LINESTRING <- function(linestring,
split_points,
tolerance = 0.01) {
linestring_sfc <- st_sfc(linestring, crs = st_crs(split_points))

# Split the linestring into segments
segments <- split_linestring.LINESTRING(linestring[[1]], split_points)
segments <- split_linestring.sfc_LINESTRING(linestring_sfc, split_points)
segments <- st_sfc(segments, crs = st_crs(linestring))

return(segments)
}

#' @export
split_linestring.LINESTRING <- function(linestring,
split_linestring.sfc_LINESTRING <- function(linestring,
split_points,
tolerance = 0.01) {
# Check if `linestring` is a single linestring
if (length(linestring) != 1) {
stop("`linestring` must be a single linestring")
}

# Decompose the linestring into a list of line segments
line_segments <- decompose_linestring(linestring)

Expand All @@ -71,18 +73,19 @@ split_linestring.LINESTRING <- function(linestring,
line_segment,
tolerance
)
line_segment_sfc <- st_sfc(line_segment, crs = st_crs(split_points))
distances_to_start_point <- as.vector(
st_distance(valid_points, st_startpoint(line_segment))
st_distance(valid_points, st_startpoint(line_segment_sfc))
)
valid_points <- valid_points[order(distances_to_start_point)]
c(st_startpoint(line_segment), valid_points)
c(st_startpoint(line_segment_sfc), valid_points)
})

# Combine all split points and add the end point of the linestring
all_points <- st_sfc(c(
unlist(points_on_lines, recursive = FALSE),
st_endpoint(linestring)
))
), crs = st_crs(split_points))

# Determine which points belong to each segment by calculating
# the segment index
Expand All @@ -96,14 +99,14 @@ split_linestring.LINESTRING <- function(linestring,
segment_points_list[-length(segment_points_list)] <- mapply(
c,
segment_points_list[-length(segment_points_list)],
lapply(all_points[is_split_point], st_sfc),
lapply(all_points[is_split_point], st_sfc, crs = st_crs(split_points)),
SIMPLIFY = FALSE
)

# Create linestring objects for each linestring segment
segments <- st_sfc(lapply(segment_points_list, function(segment_points) {
st_linestring(st_coordinates(segment_points))
}))
}), crs = st_crs(split_points))

return(segments)
}

0 comments on commit fa6eaf7

Please sign in to comment.