luukvdmeer / sfnetworks

Tidy Geospatial Networks in R
https://luukvdmeer.github.io/sfnetworks/
Other
333 stars 20 forks source link

Implement to_spatial_segmentation #210

Open agila5 opened 2 years ago

agila5 commented 2 years ago

Showcase of the new spatial morpher:

# packages
library(sf)
library(tidygraph)
library(spatstat)
library(sfnetworks)

# simulated data
my_sfc <- st_sfc(
  st_linestring(rbind(c(-1, 0), c(1, 0), c(1, 1), c(1, 2), c(3, 2))), 
  st_linestring(rbind(c(3, 2), c(3, 3))), 
  st_linestring(rbind(c(3, 3), c(3, 4), c(4, 4))), 
  st_linestring(rbind(c(5, 0), c(5, 2), c(5, 5))), 
  st_linestring(rbind(c(0.5, 0.5), c(1.5, 0.5))) 
)
my_sfn <- as_sfnetwork(my_sfc)
par(mfrow = c(1, 2))
plot(my_sfn, graticule = TRUE, axes = TRUE, xlim = c(-2, 6), lwd = 2, cex = 2)
plot(as.linnet(my_sfn), main = "", vertices = TRUE, axes = TRUE, lwd = 2)
#> Warning: Network is not connected

# Run the new morpher and plot
my_sfn1 <- convert(my_sfn, to_spatial_dump_segments, .clean = TRUE)
plot(my_sfn1, graticule = TRUE, axes = TRUE, xlim = c(-2, 6), lwd = 2, cex = 2)
plot(as.linnet(my_sfn1), main = "", vertices = TRUE, axes = TRUE, lwd = 2)
#> Warning: Network is not connected

# Add two fields, CRS and precision
my_sf <- st_sf(
  data.frame(x = c("A", "B", "C", "D", "E"), y = runif(5)), 
  geometry = my_sfc, 
  agr = c(x = "constant"), 
  crs = 3003, 
  precision = 1
)
my_sfn <- as_sfnetwork(my_sf)
my_sfn1 <- convert(my_sfn, to_spatial_dump_segments, .clean = TRUE)
#> Warning: to_spatial_subdivision assumes attributes are constant over geometries

identical(st_crs(my_sfn, "edges"), st_crs(my_sfn1, "edges"))
#> [1] TRUE
identical(st_agr(my_sfn, "edges"), st_agr(my_sfn1, "edges"))
#> [1] TRUE

Created on 2022-06-08 by the reprex package (v2.0.1)

I know that the approach is not perfect (since we could also infer the new nodes without running as_sfnetwork() again), but I think it's good enough for the moment. If you don't have any particular comments, I will add examples and tests as soon as possible.

cc @mkvasnicka

luukvdmeer commented 1 year ago

One question: since the network is recreated with as_sfnetwork this would also connect edges that share an interior point right, like to_spatial_subdivision does? I am not sure if that is expected or not

I see the name comes from PostGIS, but I think to_spatial_segmentation would be clearer, considering our other morpher names

agila5 commented 1 year ago

I see the name comes from PostGIS, but I think to_spatial_segmentation would be clearer, considering our other morpher names

Fixed

One question: since the network is recreated with as_sfnetwork this would also connect edges that share an interior point right, like to_spatial_subdivision does? I am not sure if that is expected or not

Do you mean something like the following example?

# packages
library(sf)
library(tidygraph)
library(sfnetworks)

# data
my_sfc <- st_sfc(
  st_linestring(rbind(c(-1, 0), c(0, 0), c(1, 0))), 
  st_linestring(rbind(c(0, -1), c(0, 0), c(0, 1))) 
)
my_sfn <- as_sfnetwork(my_sfc)
my_sfn1 <- convert(my_sfn, to_spatial_segmentation, .clean = TRUE)

# plot
par(mfrow = c(1, 2), mar = rep(0, 4))
plot(my_sfn)
plot(my_sfn1)

Created on 2022-09-08 by the reprex package (v2.0.1)

We can notice that the morpher creates a new "link" between the existing edges (i.e. the new dot in the middle of the map).

I'm not 100% sure that this is expected but:

  1. this is something that will never happen as long as you run the to_spatial_subdivision morpher before this other morpher (and this is particularly relevant for OSM data);
  2. I think we can merge this PR, document this behaviour, and then adjust it if someone explicitly complains (and, meanwhile, create a new issue so we don't forget about it).
codecov-commenter commented 1 year ago

Codecov Report

Patch coverage: 25.00% and project coverage change: -0.63 :warning:

Comparison is base (d3374b5) 67.92% compared to head (befbf31) 67.29%.

:exclamation: Current head befbf31 differs from pull request most recent head 5fb61a6. Consider uploading reports for the commit 5fb61a6 to get more accurate results

:mega: This organization is not using Codecov’s GitHub App Integration. We recommend you install it so Codecov can continue to function properly for your repositories. Learn more

Additional details and impacted files ```diff @@ Coverage Diff @@ ## main #210 +/- ## ========================================== - Coverage 67.92% 67.29% -0.63% ========================================== Files 21 21 Lines 1593 1581 -12 ========================================== - Hits 1082 1064 -18 - Misses 511 517 +6 ``` | [Impacted Files](https://app.codecov.io/gh/luukvdmeer/sfnetworks/pull/210?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None) | Coverage Δ | | |---|---|---| | [R/morphers.R](https://app.codecov.io/gh/luukvdmeer/sfnetworks/pull/210?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None#diff-Ui9tb3JwaGVycy5S) | `70.04% <0.00%> (-4.70%)` | :arrow_down: | | [R/spatstat.R](https://app.codecov.io/gh/luukvdmeer/sfnetworks/pull/210?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None#diff-Ui9zcGF0c3RhdC5S) | `50.00% <ø> (ø)` | | | [R/sf.R](https://app.codecov.io/gh/luukvdmeer/sfnetworks/pull/210?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None#diff-Ui9zZi5S) | `66.03% <61.11%> (+2.83%)` | :arrow_up: |

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Do you have feedback about the report comment? Let us know in this issue.