From 115d53073a1d9744bd382406d158158de47519c5 Mon Sep 17 00:00:00 2001 From: robinlovelace Date: Thu, 8 Aug 2024 16:43:14 +0100 Subject: [PATCH 1/2] Add sfnetworks --- sfnetworks.qmd | 66 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 sfnetworks.qmd diff --git a/sfnetworks.qmd b/sfnetworks.qmd new file mode 100644 index 0000000..a60a036 --- /dev/null +++ b/sfnetworks.qmd @@ -0,0 +1,66 @@ +--- +format: gfm +--- + +```{r} +library(sfnetworks) +library(sf) +library(tidygraph) +library(tidyverse) +sf::sf_use_s2(FALSE) +``` + + +```{r} +od_geo = sf::read_sf("input_data/od_data_100_sf.geojson") +zones = sf::read_sf("input_data/zones_msoa_leeds.geojson") +study_area = zones |> + sf::st_union() |> + sf::st_buffer(0.00001) +plot(study_area) +osm_leeds = osmextract::oe_get("west-yorkshire", boundary = study_area, boundary_type = "clipsrc", force_vectortranslate = TRUE) +osm_leeds_net = osmextract::oe_get_network("west-yorkshire", boundary = study_area, boundary_type = "clipsrc", force_vectortranslate = TRUE) +osm_leeds_net |> + sf::st_geometry() |> + plot() +``` + +Connect disconnected bits: + +```{r} + +``` + + +Convert to sfn: + +```{r} +net = osm_leeds_net |> + sf::st_cast("LINESTRING") |> + as_sfnetwork(directed = FALSE) |> + # st_transform(27700) |> + activate("edges") |> + mutate(weight = edge_length()) + +net = net |> + activate("edges") |> + filter(!edge_is_multiple()) |> + filter(!edge_is_loop()) |> + # remove isolated paths: + activate(nodes) |> + filter(!node_is_isolated()) + +``` + + +```{r} +# Routing between 2 points: +p1 = od_geo |> + slice(1) |> + lwgeom::st_startpoint() +p2 = od_geo |> + slice(1) |> + lwgeom::st_endpoint() +paths = st_network_paths(net, from = p1, to = c(p2), weights = "weight") +``` + From f4e586ac86e9794e37b9b46512043e2b856d02a0 Mon Sep 17 00:00:00 2001 From: robinlovelace Date: Thu, 8 Aug 2024 20:01:57 +0100 Subject: [PATCH 2/2] Add placeholder for GRASS bit, cc @wangzhao0217 --- sfnetworks.qmd | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sfnetworks.qmd b/sfnetworks.qmd index a60a036..17e301d 100644 --- a/sfnetworks.qmd +++ b/sfnetworks.qmd @@ -25,7 +25,7 @@ osm_leeds_net |> plot() ``` -Connect disconnected bits: +Connect disconnected bits with GRASS: ```{r}