luukvdmeer / sfnetworks

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

Update attributes after subdivision #158

Open agila5 opened 3 years ago

agila5 commented 3 years ago

Is your feature request related to a problem? Please describe. The to_spatial_subdivison morpher is used to construct a subdivision of a network by subdividing edges at some interior points. The "new" edges have exactly the same characteristics as the old edges (and there is also a warning message related to this behaviour). I would like to modify the existing approach and apply a function to (some or all) numerical field such that, for example, the new values are equal to the old values times the length of the new edge divided by the length of the old edge. Something like: x_new = x_old * length_new / length_old (where length_new / length_old is like the proportion of the new edge wrt the old edge).

Describe the solution you'd like For example:

# packaages
library(sf)
#> Linking to GEOS 3.9.0, GDAL 3.2.1, PROJ 7.2.1
library(tidygraph)
#> 
#> Attaching package: 'tidygraph'
#> The following object is masked from 'package:stats':
#> 
#>     filter
library(sfnetworks)
library(tmap)

my_sf <- st_sf(
  data.frame(x = c(1, 1)), 
  geometry = st_sfc(
    st_linestring(rbind(c(-1, 0), c(0, 0), c(1, 0))), 
    st_linestring(rbind(c(0, -0.2), c(0, 0), c(0, 1))), 
    crs = 4326
  )
)

my_sfn <- as_sfnetwork(my_sf, directed = FALSE)

# Before subdivision
tm_shape(st_as_sf(my_sfn, "edges")) + 
  tm_lines(col = "x", palette = "Set1", lwd = 2)


# After subdivision
tm_shape(st_as_sf(my_sfn %>% convert(to_spatial_subdivision), "edges")) + 
  tm_lines(col = "x", palette = "Set1", lwd = 2)
#> Warning: to_spatial_subdivision assumes attributes are constant over geometries


# Suggested result
my_sf2 <- st_sf(
  data.frame(x = c(0.5, 0.5, 0.167, 0.83)), 
  geometry = st_sfc(
    st_linestring(rbind(c(-1, 0), c(0, 0))), 
    st_linestring(rbind(c( 1, 0), c(0, 0))),
    st_linestring(rbind(c(0, -0.2), c(0, 0))), 
    st_linestring(rbind(c(0, 0), c(0, 1))),
    crs = 4326
  )
)
tm_shape(my_sf2) + 
  tm_lines(col = "x", palette = "Set1", lwd = 2)

Created on 2021-05-21 by the reprex package (v2.0.0)

I'm not sure about the API, but I was thinking of one or two extra parameters into to_spatial_subdivison.

Describe alternatives you've considered Again, not sure 😅

(btw: feel free to suggest a better title)