-
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.
✨ Add function to set events to network
- Loading branch information
Showing
13 changed files
with
203 additions
and
54 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
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,53 @@ | ||
#' Set events on a space. | ||
#' | ||
#' This function sets events on a space. | ||
#' | ||
#' @param x A `road_network` or `segmented_network` object. | ||
#' @param events A `sf` object representing events. | ||
#' @param ... Additional arguments passed to or from other methods. | ||
#' @return The input object with the events added. | ||
#' @export | ||
#' @examples | ||
#' # Create the road network | ||
#' road_network <- create_road_network(sample_roads) | ||
#' | ||
#' # Set accidents on the road network | ||
#' road_network <- set_events(road_network, sample_accidents) | ||
set_events <- function(x, events, ...) { | ||
UseMethod("set_events") | ||
} | ||
|
||
#' @rdname set_events | ||
#' @export | ||
set_events.road_network <- function(x, events, ...) { | ||
x <- validate_and_set_events(x, events) | ||
|
||
return(x) | ||
} | ||
|
||
#' @rdname set_events | ||
#' @export | ||
set_events.segmented_network <- function(x, events, ...) { | ||
x <- validate_and_set_events(x, events) | ||
|
||
# Count events on each link | ||
event_counts <- table(st_nearest_feature(events, x$links)) | ||
# Get the indices of the links with events | ||
link_indices <- as.integer(names(event_counts)) | ||
# Update the count of events of each link | ||
x$links$count[link_indices] <- event_counts | ||
|
||
return(x) | ||
} | ||
|
||
validate_and_set_events <- function(x, events) { | ||
if (!inherits(events, "sf")) { | ||
stop("events must be a `sf` object") | ||
} | ||
if ("events" %in% names(x)) { | ||
warning("events have already been set on the road network") | ||
} | ||
x$events <- events | ||
|
||
return(x) | ||
} |
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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file was deleted.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
12 changes: 10 additions & 2 deletions
12
man/create_segmented_network.Rd → man/segmented_network.Rd
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.