From 4c2e4ea21c08443f34df8e265a46a6593f05b151 Mon Sep 17 00:00:00 2001 From: Claudiu Forgaci Date: Thu, 21 Nov 2024 14:47:10 +0100 Subject: [PATCH 01/13] Add rcoins vignette --- .gitignore | 1 + DESCRIPTION | 5 +- vignettes/.gitignore | 2 + vignettes/using-rcoins.Rmd | 94 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 101 insertions(+), 1 deletion(-) create mode 100644 vignettes/.gitignore create mode 100644 vignettes/using-rcoins.Rmd diff --git a/.gitignore b/.gitignore index 7c794aa..72e042c 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,4 @@ .httr-oauth .DS_Store .quarto +inst/doc diff --git a/DESCRIPTION b/DESCRIPTION index 8148373..1062ca8 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -15,9 +15,12 @@ Roxygen: list(markdown = TRUE) RoxygenNote: 7.3.2 Suggests: testthat (>= 3.0.0), - sfnetworks + sfnetworks, + knitr, + rmarkdown Config/testthat/edition: 3 Imports: dplyr, sf, sfheaders +VignetteBuilder: knitr diff --git a/vignettes/.gitignore b/vignettes/.gitignore new file mode 100644 index 0000000..097b241 --- /dev/null +++ b/vignettes/.gitignore @@ -0,0 +1,2 @@ +*.html +*.R diff --git a/vignettes/using-rcoins.Rmd b/vignettes/using-rcoins.Rmd new file mode 100644 index 0000000..5edeacf --- /dev/null +++ b/vignettes/using-rcoins.Rmd @@ -0,0 +1,94 @@ +--- +title: "Tracing continuous streets using rcoins" +output: rmarkdown::html_vignette +vignette: > + %\VignetteIndexEntry{Tracing continuous streets using rcoins} + %\VignetteEngine{knitr::rmarkdown} + %\VignetteEncoding{UTF-8} +--- + +```{r, include = FALSE} +knitr::opts_chunk$set( + collapse = TRUE, + comment = "#>" +) +``` + +```{r setup} +library(rcoins) +library(sf) +library(ggplot2) +``` + +In this article we demonstrate how to trace continuous streets using the `stroke()` function. The function takes an `sf` object of streets and returns a new `sf` object with continuous streets. + +```{r} +# Load streets from example data +streets <- bucharest$streets +``` + +## Tracing on the entire network + +If we run the `stroke()` function with the default values, strokes will be calculated on the network as a whole. + +```{r} +# Trace continuous streets +continuous_streets <- stroke(streets) +``` + +To visualise the strokes in a more intuitive way, we map the line weight of the streets to the length of the streets. The thicker a line is, the longer the street. + +```{r echo=FALSE} +ggplot() + + geom_sf(data = continuous_streets, aes(linewidth = as.numeric(length))) + + scale_linewidth_continuous(name = "Continuous lines", range = c(0.1, 1.2)) + + xlim(418500, 437500) + + ylim(4909800, 4931500) + + coord_sf(datum = st_crs(32635)) + + labs(title = "Continuous streets along the main street network of Bucharest", + subtitle = "Lineweight by length", + caption = "Data: OpenStreetMap") +``` + +## Tracing from specified streets + +To trace continuous streets from a given set of streets, we can add the edge indices in the `from_edge` argument. We demonstrate this by tracing all continuous streets crossing river Dâmbovița in Bucharest. We load the river centerline from the package data. + +```{r} +# Load river centerline from example data +river_centerline <- bucharest$river_centerline + +# TODO add any necessary steps to get the IDs of the edges crossing the river +crossing_edges <- st_intersects(streets, river_centerline) + +# Trace continuous streets crossing the river +continuous_streets_crossing <- stroke(streets, from_edge = crossing_edges) +``` + +We plot the street network and emphasize the continuous streets crossing the river. + +```{r echo=FALSE} +ggplot() + + geom_sf(data = continuous_streets_crossing, linewidth = 2, colour = "red") + + xlim(418500, 437500) + + ylim(4909800, 4931500) + + coord_sf(datum = st_crs(32635)) + + labs(title = "Continuous streets crossing River Dâmbovița in Bucharest", + subtitle = "Crossing streets thicker", + caption = "Data: OpenStreetMap") +``` + +## Maintaining the initial structure + +The `flow_mode` argument allows us to maintain the initial structure of the streets. With `flow_mode = FALSE`, the function will split the streets in individual segments and calculate the continuous streets purely based on minimum angle deviations with `angle_threshold`. With `flow_mode = TRUE`, the function will not break the initial line strings. This is necessary when we want to maintain the attributes of the initial linestrings in the resulting strokes. + + + +## Tracing with attributes + +By enabling `flow_mode` and `attribute_mode` arguments, we can maintain the attributes of the input network. This is useful if we want to keep attributes such as degree of the initial streets ("primary", "secondary", "tertiary", etc.) in the resulting continuous streets to calculate, for instance, the relationship between street degree and street length. + +```{r} +# TODO add code to demonstrate how to maintain the attributes of the initial streets +``` + From aa68816c52f8a796c1d328e2312264c9fbe146f5 Mon Sep 17 00:00:00 2001 From: Claudiu Forgaci Date: Thu, 21 Nov 2024 14:49:08 +0100 Subject: [PATCH 02/13] Prevent code chunks from running while drafting --- vignettes/using-rcoins.Rmd | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/vignettes/using-rcoins.Rmd b/vignettes/using-rcoins.Rmd index 5edeacf..8346ea2 100644 --- a/vignettes/using-rcoins.Rmd +++ b/vignettes/using-rcoins.Rmd @@ -10,7 +10,8 @@ vignette: > ```{r, include = FALSE} knitr::opts_chunk$set( collapse = TRUE, - comment = "#>" + comment = "#>", + eval = FALSE ) ``` From 05fcf03c786884bd263b437602e4bfc4214a49ab Mon Sep 17 00:00:00 2001 From: Claudiu Forgaci Date: Thu, 21 Nov 2024 15:03:26 +0100 Subject: [PATCH 03/13] Fix lintr issues --- vignettes/using-rcoins.Rmd | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/vignettes/using-rcoins.Rmd b/vignettes/using-rcoins.Rmd index 8346ea2..a73fe99 100644 --- a/vignettes/using-rcoins.Rmd +++ b/vignettes/using-rcoins.Rmd @@ -46,7 +46,7 @@ ggplot() + xlim(418500, 437500) + ylim(4909800, 4931500) + coord_sf(datum = st_crs(32635)) + - labs(title = "Continuous streets along the main street network of Bucharest", + labs(title = "Continuous streets along the main street network of Bucharest", subtitle = "Lineweight by length", caption = "Data: OpenStreetMap") ``` @@ -74,7 +74,7 @@ ggplot() + xlim(418500, 437500) + ylim(4909800, 4931500) + coord_sf(datum = st_crs(32635)) + - labs(title = "Continuous streets crossing River Dâmbovița in Bucharest", + labs(title = "Continuous streets crossing River Dâmbovița in Bucharest", subtitle = "Crossing streets thicker", caption = "Data: OpenStreetMap") ``` @@ -90,6 +90,6 @@ The `flow_mode` argument allows us to maintain the initial structure of the stre By enabling `flow_mode` and `attribute_mode` arguments, we can maintain the attributes of the input network. This is useful if we want to keep attributes such as degree of the initial streets ("primary", "secondary", "tertiary", etc.) in the resulting continuous streets to calculate, for instance, the relationship between street degree and street length. ```{r} -# TODO add code to demonstrate how to maintain the attributes of the initial streets +# TODO add code to demo how to maintain the attributes of the initial streets ``` From 0ad658333ec274000b76fac965d3d2646c7a75e1 Mon Sep 17 00:00:00 2001 From: Claudiu Forgaci <33600128+cforgaci@users.noreply.github.com> Date: Fri, 22 Nov 2024 13:11:26 +0100 Subject: [PATCH 04/13] Update vignettes/using-rcoins.Rmd Co-authored-by: Francesco Nattino <49899980+fnattino@users.noreply.github.com> --- vignettes/using-rcoins.Rmd | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/vignettes/using-rcoins.Rmd b/vignettes/using-rcoins.Rmd index a73fe99..4da4d7e 100644 --- a/vignettes/using-rcoins.Rmd +++ b/vignettes/using-rcoins.Rmd @@ -37,7 +37,7 @@ If we run the `stroke()` function with the default values, strokes will be calcu continuous_streets <- stroke(streets) ``` -To visualise the strokes in a more intuitive way, we map the line weight of the streets to the length of the streets. The thicker a line is, the longer the street. +To visualise the strokes in a more intuitive way, we map the line weight in the plot to the length of the strokes. The thicker a line is, the longer the stroke. ```{r echo=FALSE} ggplot() + From 0547946eef602299e1950d5ec89a71668447f1d2 Mon Sep 17 00:00:00 2001 From: Claudiu Forgaci <33600128+cforgaci@users.noreply.github.com> Date: Fri, 22 Nov 2024 13:11:48 +0100 Subject: [PATCH 05/13] Update vignettes/using-rcoins.Rmd Co-authored-by: Francesco Nattino <49899980+fnattino@users.noreply.github.com> --- vignettes/using-rcoins.Rmd | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/vignettes/using-rcoins.Rmd b/vignettes/using-rcoins.Rmd index 4da4d7e..c38aa0a 100644 --- a/vignettes/using-rcoins.Rmd +++ b/vignettes/using-rcoins.Rmd @@ -59,8 +59,7 @@ To trace continuous streets from a given set of streets, we can add the edge ind # Load river centerline from example data river_centerline <- bucharest$river_centerline -# TODO add any necessary steps to get the IDs of the edges crossing the river -crossing_edges <- st_intersects(streets, river_centerline) +crossing_edges <- which(st_intersects(streets, river_centerline, sparse = FALSE)) # Trace continuous streets crossing the river continuous_streets_crossing <- stroke(streets, from_edge = crossing_edges) From 3acd010437e27da705d863cef36e4bc9c48d9af5 Mon Sep 17 00:00:00 2001 From: Claudiu Forgaci <33600128+cforgaci@users.noreply.github.com> Date: Fri, 22 Nov 2024 13:12:24 +0100 Subject: [PATCH 06/13] Update vignettes/using-rcoins.Rmd Co-authored-by: Francesco Nattino <49899980+fnattino@users.noreply.github.com> --- vignettes/using-rcoins.Rmd | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/vignettes/using-rcoins.Rmd b/vignettes/using-rcoins.Rmd index c38aa0a..f2b8cb1 100644 --- a/vignettes/using-rcoins.Rmd +++ b/vignettes/using-rcoins.Rmd @@ -86,7 +86,7 @@ The `flow_mode` argument allows us to maintain the initial structure of the stre ## Tracing with attributes -By enabling `flow_mode` and `attribute_mode` arguments, we can maintain the attributes of the input network. This is useful if we want to keep attributes such as degree of the initial streets ("primary", "secondary", "tertiary", etc.) in the resulting continuous streets to calculate, for instance, the relationship between street degree and street length. +By enabling `flow_mode` and `attribute` arguments, we can maintain the attributes of the input network. This is useful if we want to keep attributes such as degree of the initial streets ("primary", "secondary", "tertiary", etc.) in the resulting continuous streets to calculate, for instance, the relationship between street degree and street length. ```{r} # TODO add code to demo how to maintain the attributes of the initial streets From f8f8fed5485615721ec67cdbfdd63a10f0066fb3 Mon Sep 17 00:00:00 2001 From: Claudiu Forgaci Date: Sat, 23 Nov 2024 21:49:07 +0100 Subject: [PATCH 07/13] Add ggplot2 to Suggests --- DESCRIPTION | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/DESCRIPTION b/DESCRIPTION index 1062ca8..a6a4da6 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -17,7 +17,8 @@ Suggests: testthat (>= 3.0.0), sfnetworks, knitr, - rmarkdown + rmarkdown, + ggplot2 Config/testthat/edition: 3 Imports: dplyr, From 02619db35540d0bc087ceab94d6b9553bba7b171 Mon Sep 17 00:00:00 2001 From: Claudiu Forgaci Date: Thu, 28 Nov 2024 22:35:27 +0100 Subject: [PATCH 08/13] Fix long line detected by linter --- vignettes/using-rcoins.Rmd | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/vignettes/using-rcoins.Rmd b/vignettes/using-rcoins.Rmd index f2b8cb1..c62944c 100644 --- a/vignettes/using-rcoins.Rmd +++ b/vignettes/using-rcoins.Rmd @@ -59,7 +59,8 @@ To trace continuous streets from a given set of streets, we can add the edge ind # Load river centerline from example data river_centerline <- bucharest$river_centerline -crossing_edges <- which(st_intersects(streets, river_centerline, sparse = FALSE)) +crossing_edges <- which( + st_intersects(streets, river_centerline, sparse = FALSE)) # Trace continuous streets crossing the river continuous_streets_crossing <- stroke(streets, from_edge = crossing_edges) From 7c4a852fadcd2616ea86c5a68584493ec947e5c4 Mon Sep 17 00:00:00 2001 From: Claudiu Forgaci Date: Thu, 28 Nov 2024 22:35:27 +0100 Subject: [PATCH 09/13] Fix long line detected by linter --- vignettes/using-rcoins.Rmd | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/vignettes/using-rcoins.Rmd b/vignettes/using-rcoins.Rmd index f2b8cb1..f97d265 100644 --- a/vignettes/using-rcoins.Rmd +++ b/vignettes/using-rcoins.Rmd @@ -59,7 +59,9 @@ To trace continuous streets from a given set of streets, we can add the edge ind # Load river centerline from example data river_centerline <- bucharest$river_centerline -crossing_edges <- which(st_intersects(streets, river_centerline, sparse = FALSE)) +crossing_edges <- which(st_intersects(streets, + river_centerline, + sparse = FALSE)) # Trace continuous streets crossing the river continuous_streets_crossing <- stroke(streets, from_edge = crossing_edges) From 2b4f5eea7d7724a84877499a655070ceea292560 Mon Sep 17 00:00:00 2001 From: Claudiu Forgaci <33600128+cforgaci@users.noreply.github.com> Date: Mon, 16 Dec 2024 14:55:36 +0100 Subject: [PATCH 10/13] Update vignettes/using-rcoins.Rmd Co-authored-by: Francesco Nattino <49899980+fnattino@users.noreply.github.com> --- vignettes/using-rcoins.Rmd | 1 + 1 file changed, 1 insertion(+) diff --git a/vignettes/using-rcoins.Rmd b/vignettes/using-rcoins.Rmd index f473be2..95e3ddd 100644 --- a/vignettes/using-rcoins.Rmd +++ b/vignettes/using-rcoins.Rmd @@ -40,6 +40,7 @@ continuous_streets <- stroke(streets) To visualise the strokes in a more intuitive way, we map the line weight in the plot to the length of the strokes. The thicker a line is, the longer the stroke. ```{r echo=FALSE} +length <- sf::st_length(continuous_streets) ggplot() + geom_sf(data = continuous_streets, aes(linewidth = as.numeric(length))) + scale_linewidth_continuous(name = "Continuous lines", range = c(0.1, 1.2)) + From e12c75910b0fbec701f369b08ac69a1af79ba59b Mon Sep 17 00:00:00 2001 From: Claudiu Forgaci <33600128+cforgaci@users.noreply.github.com> Date: Mon, 16 Dec 2024 14:57:22 +0100 Subject: [PATCH 11/13] Apply suggestions from code review Co-authored-by: Francesco Nattino <49899980+fnattino@users.noreply.github.com> --- vignettes/using-rcoins.Rmd | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/vignettes/using-rcoins.Rmd b/vignettes/using-rcoins.Rmd index 95e3ddd..2360c6d 100644 --- a/vignettes/using-rcoins.Rmd +++ b/vignettes/using-rcoins.Rmd @@ -65,13 +65,18 @@ crossing_edges <- which(st_intersects(streets, sparse = FALSE)) # Trace continuous streets crossing the river -continuous_streets_crossing <- stroke(streets, from_edge = crossing_edges) +continuous_streets_crossing <- stroke(streets, from_edge = crossing_edges, + angle_threshold = 120) ``` +Note that the input argument `angle_threshold` sets the minimum internal angle between consecutive line segments that can be considered part of a continuous stroke. + We plot the street network and emphasize the continuous streets crossing the river. ```{r echo=FALSE} ggplot() + + geom_sf(data = river_centerline, linewidth = 1, colour = "blue") + + geom_sf(data = streets, linewidth = 0.2, colour = "black") + geom_sf(data = continuous_streets_crossing, linewidth = 2, colour = "red") + xlim(418500, 437500) + ylim(4909800, 4931500) + @@ -83,15 +88,11 @@ ggplot() + ## Maintaining the initial structure -The `flow_mode` argument allows us to maintain the initial structure of the streets. With `flow_mode = FALSE`, the function will split the streets in individual segments and calculate the continuous streets purely based on minimum angle deviations with `angle_threshold`. With `flow_mode = TRUE`, the function will not break the initial line strings. This is necessary when we want to maintain the attributes of the initial linestrings in the resulting strokes. +The `flow_mode` argument allows us to maintain the initial structure of the streets. With `flow_mode = FALSE` (the default), the function will split the initial streets in individual line segments and calculate the continuous streets purely based on minimum angle deviations. With `flow_mode = TRUE`, the function will not break the initial line strings, but only group and connect them on the basis of minimum angle deviations. ## Tracing with attributes -By enabling `flow_mode` and `attribute` arguments, we can maintain the attributes of the input network. This is useful if we want to keep attributes such as degree of the initial streets ("primary", "secondary", "tertiary", etc.) in the resulting continuous streets to calculate, for instance, the relationship between street degree and street length. - -```{r} -# TODO add code to demo how to maintain the attributes of the initial streets -``` +By enabling `flow_mode` and `attributes` arguments, `stroke` will still group streets on the basis of minimum-angle connectivity, but return group labels instead of the new aggregated geometries. This is useful if we want to keep attributes such as degree of the initial streets ("primary", "secondary", "tertiary", etc.) in the resulting continuous streets to calculate, for instance, the relationship between street degree and street length. From be9551e0b55e56bbf43aab392aeb9690c1a8163c Mon Sep 17 00:00:00 2001 From: Claudiu Forgaci <33600128+cforgaci@users.noreply.github.com> Date: Mon, 16 Dec 2024 16:06:28 +0100 Subject: [PATCH 12/13] Update vignettes/using-rcoins.Rmd Co-authored-by: Francesco Nattino <49899980+fnattino@users.noreply.github.com> --- vignettes/using-rcoins.Rmd | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/vignettes/using-rcoins.Rmd b/vignettes/using-rcoins.Rmd index 2360c6d..1a50c94 100644 --- a/vignettes/using-rcoins.Rmd +++ b/vignettes/using-rcoins.Rmd @@ -10,8 +10,7 @@ vignette: > ```{r, include = FALSE} knitr::opts_chunk$set( collapse = TRUE, - comment = "#>", - eval = FALSE + comment = "#>" ) ``` From cef1edc81e48285a11c5f8c36c03cebdbed4e76a Mon Sep 17 00:00:00 2001 From: Claudiu Forgaci Date: Mon, 16 Dec 2024 16:18:38 +0100 Subject: [PATCH 13/13] Remove trailing white space --- vignettes/using-rcoins.Rmd | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/vignettes/using-rcoins.Rmd b/vignettes/using-rcoins.Rmd index 1a50c94..3343f9c 100644 --- a/vignettes/using-rcoins.Rmd +++ b/vignettes/using-rcoins.Rmd @@ -64,7 +64,7 @@ crossing_edges <- which(st_intersects(streets, sparse = FALSE)) # Trace continuous streets crossing the river -continuous_streets_crossing <- stroke(streets, from_edge = crossing_edges, +continuous_streets_crossing <- stroke(streets, from_edge = crossing_edges, angle_threshold = 120) ```