Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

to_spatial_smooth returns multilinestring when merged edge crosses itself #117

Closed
loreabad6 opened this issue Jan 30, 2021 · 2 comments
Closed
Assignees
Labels
bug 🐛 Something isn't working to_spatial_smooth All issues related to function `to_spatial_smooth()`

Comments

@loreabad6
Copy link
Collaborator

Describe the bug
I have a test network, created from points. When I pass it to to_spatial_smooth() it returns a network with edges as MULTILINESTRING geometries,

Reproducible example

library(sf)
library(sfnetworks)
library(tidygraph)

# Toy sfnetwork
p1 = st_point(c(0, 1))
p2 = st_point(c(1, 1))
p3 = st_point(c(2, 1))
p4 = st_point(c(3, 1))
p5 = st_point(c(4, 1))
p6 = st_point(c(3, 2))
p7 = st_point(c(3, 0))
p8 = st_point(c(4, 3))
p9 = st_point(c(4, 2))
p10 = st_point(c(4, 0))
p11 = st_point(c(5, 2))
p12 = st_point(c(5, 0))
p13 = st_point(c(5, -1))


# Create network from points
points = st_sfc(st_multipoint(c(p1, p2, p3, p4, p5, p6, p4, p7, p5, p8,
                                p11, p9, p5, p10, p12, p13, p10)))
net_p = as_sfnetwork(st_cast(points, "POINT")) 
net_p
#> # A sfnetwork with 17 nodes and 16 edges
#> #
#> # CRS:  NA 
#> #
#> # A rooted tree with spatially explicit edges
#> #
#> # Node Data:     17 x 1 (active)
#> # Geometry type: POINT
#> # Dimension:     XY
#> # Bounding box:  xmin: 0 ymin: -1 xmax: 5 ymax: 3
#>         x
#>   <POINT>
#> 1   (0 1)
#> 2   (1 1)
#> 3   (2 1)
#> 4   (3 1)
#> 5   (4 1)
#> 6   (3 2)
#> # ... with 11 more rows
#> #
#> # Edge Data:     16 x 3
#> # Geometry type: LINESTRING
#> # Dimension:     XY
#> # Bounding box:  xmin: 0 ymin: -1 xmax: 5 ymax: 3
#>    from    to            x
#>   <int> <int> <LINESTRING>
#> 1     1     2   (0 1, 1 1)
#> 2     2     3   (1 1, 2 1)
#> 3     3     4   (2 1, 3 1)
#> # ... with 13 more rows
smoo_p = convert(net_p, to_spatial_smooth)
smoo_p
#> # A sfnetwork with 2 nodes and 1 edges
#> #
#> # CRS:  NA 
#> #
#> # A rooted tree with spatially explicit edges
#> #
#> # Node Data:     2 x 2 (active)
#> # Geometry type: POINT
#> # Dimension:     XY
#> # Bounding box:  xmin: 0 ymin: 0 xmax: 4 ymax: 1
#>         x .tidygraph_node_index
#>   <POINT>                 <int>
#> 1   (0 1)                     1
#> 2   (4 0)                    17
#> #
#> # Edge Data:     1 x 4
#> # Geometry type: MULTILINESTRING
#> # Dimension:     XY
#> # Bounding box:  xmin: 0 ymin: -1 xmax: 5 ymax: 3
#>    from    to                                               x .tidygraph_edge_i~
#>   <int> <int>                               <MULTILINESTRING> <named list>      
#> 1     1     2 ((0 1, 1 1, 2 1, 3 1), (3 1, 4 1), (4 1, 3 2, ~ <dbl [16]>

Expected behavior
The morpher should return edges with LINESTRINGs.

@luukvdmeer luukvdmeer added the bug 🐛 Something isn't working label Jan 30, 2021
@luukvdmeer luukvdmeer self-assigned this Jan 30, 2021
@luukvdmeer luukvdmeer changed the title to_spatial_smooth sometimes returns multilinestring edges to_spatial_smooth returns multilinestrings when merged edges cross itself Jan 31, 2021
@luukvdmeer luukvdmeer changed the title to_spatial_smooth returns multilinestrings when merged edges cross itself to_spatial_smooth returns multilinestring when merged edge crosses itself Jan 31, 2021
@luukvdmeer
Copy link
Owner

So this seems to happen when a merged edge crosses itself. That is not a valid linestring according to the simple feature standard and st_line_merge therefore refuses to merge them. This is not so easy to solve when using st_line_merge. The alternative could be to implement our own line merging function, e.g. by using sfheaders, but there are a lot of things to take into consideration when doing so.

Since this is quite an unusual case I'll mark this issue as low priority, but it is indeed a bug.

@luukvdmeer luukvdmeer added the low priority 💤 Would be nice to have but not necessary label Jan 31, 2021
@luukvdmeer luukvdmeer added to_spatial_smooth All issues related to function `to_spatial_smooth()` and removed low priority 💤 Would be nice to have but not necessary labels Nov 26, 2021
@luukvdmeer
Copy link
Owner

Fixed in v0.6.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug 🐛 Something isn't working to_spatial_smooth All issues related to function `to_spatial_smooth()`
Projects
None yet
Development

No branches or pull requests

2 participants