Skip to content
Permalink
Browse files

Fix missing crs issue - line2points

  • Loading branch information
Robinlovelace committed Sep 19, 2019
1 parent f70f6aa commit 592fba2a6d191135d036af73e7c902c3ef4f758c
Showing with 62 additions and 3 deletions.
  1. +1 −0 .gitignore
  2. +2 −2 R/od-funs.R
  3. +59 −1 vignettes/stplanr-routing.Rmd
@@ -8,3 +8,4 @@ src/*.o
src/*.so
src/*.dll
*.html
iow.*
@@ -411,7 +411,7 @@ line2points.Spatial <- function(l, ids = rep(1:nrow(l), each = 2)) {
out
}
#' @export
line2points.sf <- function(l, ids = rep(1:nrow(l))) {
line2points.sf <- function(l, ids = rep(1:nrow(l), each = 2)) {
y_coords <- x_coords <- double(length = length(ids)) # initiate coords
d_indices <- 1:nrow(l) * 2
o_indices <- d_indices - 1
@@ -421,7 +421,7 @@ line2points.sf <- function(l, ids = rep(1:nrow(l))) {
y_coords[d_indices] <- sapply(l$geometry, tail, n = 1) # last (y) element of each line
p_multi <- sf::st_multipoint(cbind(x_coords, y_coords))
p <- sf::st_cast(sf::st_sfc(p_multi), "POINT")
sf::st_sf(data.frame(id = ids), p)
sf::st_sf(data.frame(id = ids), geometry = p, crs = sf::st_crs(l))
}

#' @rdname line_to_points
@@ -18,4 +18,62 @@ knitr::opts_chunk$set(
library(stplanr)
```

## This vignette is work in progress - watch this space!
## This vignette is work in progress - watch this space!


```{r, echo=FALSE, eval=FALSE}
# upload roads data
if(!file.exists("roads_iow.Rds")) {
download.file("https://github.com/ropensci/stplanr/releases/download/0.3.1/roads_iow.Rds", "roads_iow.Rds")
}
roads_iow = readRDS("roads_iow.Rds")
sf::write_sf(roads_iow, "roads_iow.geojson")
piggyback::pb_upload("roads_iow.geojson")
```

```{r}
roads_iow = sf::read_sf("https://github.com/ropensci/stplanr/releases/download/0.3.1/roads_iow.geojson")
```

# osrm

```{r, echo=FALSE}
if(!file.exists("iow.pbf"))
download.file("http://download.geofabrik.de/europe/great-britain/england/isle-of-wight-latest.osm.pbf", "iow.pbf")
options(osrm.server = "http://0.0.0.0:5000/", osrm.profile = "driving")
```

Then in bash run the following commands to make the [OSRM docker image](https://hub.docker.com/r/osrm/osrm-backend/) work for you.

```{r, engine='bash', eval=FALSE}
docker run -t -v "${PWD}:/data" osrm/osrm-backend osrm-extract -p /opt/car.lua /data/iow.pbf
docker run -t -v "${PWD}:/data" osrm/osrm-backend osrm-partition /data/iow.osrm
docker run -t -v "${PWD}:/data" osrm/osrm-backend osrm-customize /data/iow.osrm
docker run -t -i -p 5000:5000 -v "${PWD}:/data" osrm/osrm-backend osrm-routed --algorithm mld /data/iow.osrm
curl "http://127.0.0.1:5000/route/v1/driving/13.388860,52.517037;13.385983,52.496891?steps=true"
```

Now we can do routing in R!

```{r, eval=FALSE}
l = pct::wight_lines_30
p = line2points(l)
r = osrm::osrmRoute(src = p[1, ], dst = p[2, ], returnclass = "sf")
plot(r)
```

```{r, eval=FALSE}
route_osrm2 = function(l) {
p = line2points(l)
s = (1:nrow(l)) * 2 - 1
list_out = lapply(s, function(i) osrm::osrmRoute(p[i, ], dst = p[i + 1, ], returnclass = "sf"))
do.call(rbind, list_out)
}
routes_osrm = route_osrm2(l)
plot(routes_osrm)
```





0 comments on commit 592fba2

Please sign in to comment.
You can’t perform that action at this time.