sfnetworks icon indicating copy to clipboard operation
sfnetworks copied to clipboard

Implement to_spatial_segmentation

Open agila5 opened this issue 3 years ago • 3 comments

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

agila5 avatar Jun 08 '22 10:06 agila5

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

luukvdmeer avatar Aug 18 '22 20:08 luukvdmeer

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).

agila5 avatar Sep 08 '22 13:09 agila5

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
@@            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 Coverage Δ
R/morphers.R 70.04% <0.00%> (-4.70%) :arrow_down:
R/spatstat.R 50.00% <ø> (ø)
R/sf.R 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.

codecov-commenter avatar May 03 '23 07:05 codecov-commenter