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

Add functions to split routes and add nodes route networks #342

Closed
Robinlovelace opened this issue Sep 23, 2019 · 6 comments
Closed

Add functions to split routes and add nodes route networks #342

Robinlovelace opened this issue Sep 23, 2019 · 6 comments

Comments

@Robinlovelace
Copy link
Member

Emerging from #237 (comment), suggestion from @rafapereirabr

@Robinlovelace
Copy link
Member Author

It's worth splitting the line splitting and node adding components into different functions.

Robinlovelace added a commit that referenced this issue Sep 23, 2019
Robinlovelace added a commit that referenced this issue Sep 24, 2019
@Robinlovelace
Copy link
Member Author

Now implemented in the rnet-add-node branch and works as follows:

remotes::install_github("ropensci/stplanr", "rnet-add-node")
#> Skipping install of 'stplanr' from a github remote, the SHA1 (d5e0b0a3) has not changed since last install.
#>   Use `force = TRUE` to force installation
library(stplanr)
#> Registered S3 method overwritten by 'R.oo':
#>   method        from       
#>   throw.default R.methodsS3
sample_routes = routes_fast_sf[2:6, NULL]
sample_routes$value = rep(1:3, length.out = 5)
rnet = overline2(sample_routes, attrib = "value")
#> 2019-09-24 09:37:14 constructing segments
#> 2019-09-24 09:37:14 building geometry
#> 2019-09-24 09:37:14 simplifying geometry
#> 2019-09-24 09:37:14 aggregating flows
#> 2019-09-24 09:37:14 rejoining segments into linestrings
sln = SpatialLinesNetwork(rnet)
#> Linking to GEOS 3.7.1, GDAL 2.4.2, PROJ 5.2.0
p = sf::st_sfc(sf::st_point(c(-1.540, 53.826)), crs = sf::st_crs(rnet))
sln_nodes = sln2points(sln)
sln_new = sln_add_node(sln, p)
#> although coordinates are longitude/latitude, st_nearest_feature assumes that they are planar
route = route_local(sln_new, p, sln_nodes[9, ])
plot(sln)
plot(sln_nodes, pch = as.character(1:nrow(sln_nodes)), add = TRUE)
plot(route$geometry, lwd = 9, add = TRUE)

Created on 2019-09-24 by the reprex package (v0.3.0)

Work for you @rafapereirabr? Any feedback welcome.

@Robinlovelace
Copy link
Member Author

Note that sln_add_node was the most concise yet clear name I could come up with. And based on the principle of modularity, I've not implemented it as a megafunction but a small family of related functions that could be useful in their own right. See here for the source code - comment on that very welcome. Heads-up @mpadge, @agila5, @richardellison: https:/ropensci/stplanr/blob/rnet-add-node/R/route-add-node.R

@mpadge
Copy link
Member

mpadge commented Sep 24, 2019

Great stuff. This functionality should come relatively soon into dodgr, and that is definitely a strong benchmark. Nice work

Robinlovelace added a commit that referenced this issue Sep 24, 2019
Robinlovelace added a commit that referenced this issue Sep 24, 2019
@rafapereirabr
Copy link

This works like a charm. Thanks Robin !

@rafapereirabr
Copy link

And please feel free to add an answer to that SO question:

library(stplanr)

# add node to network
 sln_new <- sln_add_node(sln = sln, p = p)

# identify nodes
 sln_nodes = sln2points(sln_new)

# route new node to any other node in the newtork
  route_new <- route_local(sln = sln_new, from = p, to = sln_nodes[15, ])

# plot results
  plot(sln_new)
  plot(p, add = TRUE)
  plot(route_new, lwd = 5, add = TRUE)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants