Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

22 testing removedangles #23

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ Imports:
dbscan,
purrr,
units,
rlang,
rlang
Suggests:
tibble,
tmaptools,
Expand All @@ -35,4 +35,6 @@ Suggests:
Encoding: UTF-8
LazyData: true
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.3.1
RoxygenNote: 7.3.2
Additional_repositories: https://josiahparry.r-universe.dev

16 changes: 16 additions & 0 deletions R/corenet.R
Original file line number Diff line number Diff line change
Expand Up @@ -580,6 +580,22 @@ removeDangles = function(network, tolerance = 0.001) {
}


removeDangles_geos = function(network, tolerance = 0.001, tol_distance) {
network = geos::as_geos_geometry(central_leeds_osm)
network_linestring = geos::geos_unnest(network, keep_multi = FALSE)
coordinates_start = geos::geos_point_start(network_linestring)
coordinates_end = geos::geos_point_end(network_linestring)
coordinates = c(coordinates_start, coordinates_end)
coordinates_matrix = data.frame(
x = geos::geos_x(coordinates),
y = geos::geos_y(coordinates)
)
# Find the unique coordinates:
c_txt = paste0(coordinates_matrix$x, coordinates_matrix$y)
c_tbl = table(c_txt)
}



#' Create coherent network PMtiles
#'
Expand Down
27 changes: 27 additions & 0 deletions R/data.R
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,33 @@
#' data(NPT_demo_6km)
#' head(NPT_demo_6km)
"NPT_demo_6km"
NULL

#' Central Leeds OSM Network
#'
#' See the `data-raw` folder for the code used to generate this data set.
#'
#' @docType data
#' @keywords datasets
#' @name central_leeds_osm
#' @format An object of class \code{sf} (inherits from \code{data.frame}).
#' @examples
#' head(central_leeds_osm)
#' library(sf) # for plotting
#' plot(central_leeds_osm$geometry)
NULL

#' Edinburgh off road network
#'
#' This data set contains network data for Edinburgh's off-road network.
#'
#' @docType data
#' @keywords datasets
#' @name edinburgh_offroad
#' @format An object of class \code{sf} (inherits from \code{data.frame}).
#' @examples
#' data(edinburgh_offroad)
#' head(edinburgh_offroad)
#' library(sf) # for plotting
#' plot(edinburgh_offroad$geometry)
NULL
12 changes: 11 additions & 1 deletion README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,15 @@ if (!require("remotes")) {
remotes::install_github("nptscot/corenet")
```

To install `corenet`, you may need to add an additional repository to access the `rsgeo` package:

```{r}
options(repos = c(
josiahparry = 'https://josiahparry.r-universe.dev',
CRAN = 'https://cloud.r-project.org'
))
install.packages('corenet')
```
Load the package with the following for local development:

```{r, include=FALSE}
Expand Down Expand Up @@ -599,4 +608,5 @@ cycle_network = function(area, NPT_network, length_threshold = 1) {

return(summarized_data)
}
```
```

26 changes: 26 additions & 0 deletions data-raw/central_leeds.qmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
---
format: gfm
---

The aim of this script is to generate a minimal example route network dataset for central Leeds for use in the package and testing.

```{r, eval=FALSE, echo=FALSE}
# Test removeDangles
library(tidyverse)
headrow = central_leeds_osm |>
filter(name == "The Headrow")
plot(headrow$geometry)
central_leeds = sf::st_buffer(headrow, 100)
sf::sf_use_s2(FALSE)
central_leeds_osm = osmactive::get_travel_network(central_leeds, boundary = central_leeds, boundary_type = "clipsrc")

plot(central_leeds_osm$geometry)
central_leeds_nodangle_10 = removeDangles(central_leeds_osm, tolerance = 10)
central_leeds_nodangle_default = removeDangles(central_leeds_osm)
plot(central_leeds_nodangle_10$geometry) # it works!
plot(central_leeds_nodangle_default$geometry)

usethis::use_data(central_leeds_osm, overwrite = TRUE)

```

54 changes: 54 additions & 0 deletions data-raw/osm_edinburgh_demo.R
Original file line number Diff line number Diff line change
Expand Up @@ -152,3 +152,57 @@ cycle_net |>
plot()

usethis::use_data(osm_edinburgh_demo, overwrite = TRUE)

find_component= function(rnet, threshold = 50) {

sf::st_crs(rnet) = 27700

# Calculate the distance matrix between features
dist_matrix = sf::st_distance(rnet)

# Convert the threshold to units of meters
threshold = units::set_units(threshold, "m")

# Create a connectivity matrix where connections are based on the threshold distance

connectivity_matrix = Matrix::Matrix(dist_matrix < threshold, sparse = TRUE)

# Create an undirected graph from the adjacency matrix
graph = igraph::graph_from_adjacency_matrix(connectivity_matrix, mode = "undirected", diag = FALSE)

# Find the connected components in the graph
components = igraph::components(graph)

# Assign component membership to the road network
rnet$component = components$membership

# Return the updated road network with component membership
return(rnet)
}


lads = sf::read_sf("D:/Github/nptscot/npt/inputdata/boundaries/la_regions_2023.geojson")

target_zone = lads |>
dplyr::filter(LAD23NM == "City of Edinburgh") |>
sf::st_transform(crs = 27700)

osm = osmactive::get_travel_network("Scotland", boundary = target_zone, boundary_type = "clipsrc")
cycle_net = osmactive::get_cycling_network(osm)
drive_net = osmactive::get_driving_network_major(osm)
cycle_net = osmactive::distance_to_road(cycle_net, drive_net)
cycle_net = osmactive::classify_cycle_infrastructure(cycle_net)
# filter cycle_net based on column bicycle is yes dismount adn designated
cycle_net = cycle_net |>
dplyr::filter(bicycle %in% c("yes", "dismount", "designated")) |>
dplyr::filter(cycle_segregation == "Separated cycle track") |>
dplyr::mutate(length = as.numeric(sf::st_length(geometry))) |>
dplyr::filter(length > 1) |>
sf::st_transform(crs = 27700)

cycle_net = sf::st_cast(cycle_net, "LINESTRING")
cycle_net = cycle_net |> dplyr::select(geometry)
cycle_net$length = sf::st_length(cycle_net)
edinburgh_offroad = find_component(cycle_net, threshold = 1)

usethis::use_data(edinburgh_offroad, overwrite = TRUE)
Loading
Loading