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

Error in osrmTrip() when location coordinates are close to each other and/or not on roads #120

Closed
wlangera opened this issue Mar 22, 2023 · 2 comments
Labels

Comments

@wlangera
Copy link

wlangera commented Mar 22, 2023

Hi

I want to calculate the shortest route between sampling point locations which are often off-road and sometimes close to each other.
However I get the following error when I use the osrmTrip() function:
Error in data.frame(..., check.names = FALSE) : arguments imply differing number of rows: 12, 9

This is because the number of lines outputted by the function is larger than the number of points inputted.
Here is a reproducible example.

library(osrm)
#> Warning: package 'osrm' was built under R version 4.2.3
#> Data: (c) OpenStreetMap contributors, ODbL 1.0 - http://www.openstreetmap.org/copyright
#> Routing: OSRM - http://project-osrm.org/
library(sf)
#> Warning: package 'sf' was built under R version 4.2.2
#> Linking to GEOS 3.9.3, GDAL 3.5.2, PROJ 8.2.1; sf_use_s2() is TRUE
library(mapview)
#> Warning: package 'mapview' was built under R version 4.2.2

# Coordinates of points
point_1 <- c(25086.15, 194501.7)
point_2 <- c(25032.33, 195725.5)
point_3 <- c(24140.32, 193718.8)
point_4 <- c(24217, 194746)
point_5 <- c(24445.09, 195494.1)
point_6 <- c(24368.93, 194250.5)
point_7 <- c(25521.03, 195911.1)
point_8 <- c(25830.59, 195202.3)
point_9 <- c(25235.08, 194137.3)

# Create data frame
point_df <- do.call(rbind.data.frame, 
                    lapply(paste("point", 1:9, sep = "_"), get))
names(point_df) <- c("X", "Y")

# Create sf object
point_sf <- point_df |>
  st_as_sf(coords = c("X", "Y"), crs = st_crs(31370))

mapview(point_sf)
#> Warning in cbind(`Feature ID` = fid, mat): number of rows of result is not a
#> multiple of vector length (arg 1)

# Calculate trip
mytrip_car <- osrmTrip(point_sf)
#> Error in data.frame(..., check.names = FALSE): arguments imply differing number of rows: 12, 9
mytrip_foot <- osrmTrip(point_sf, osrm.profile = "foot")
#> Error in data.frame(..., check.names = FALSE): arguments imply differing number of rows: 12, 9

Created on 2023-03-22 with reprex v2.0.2

I created a workaround. Since I am only interested in the total distance and the geometry, I do not need to output the length and duration of the single line transects separately.

Original code in osrmTrip() where the error occurs:

# ...
sldf <- st_sf(start = start, end = end, 
              duration = res$trips[nt, ]$legs[[1]][, "duration"]/60, 
              distance = res$trips[nt, ]$legs[[1]][, "distance"]/1000, 
              geometry = st_as_sfc(wktl, crs = 4326))
# ...

Workaround:

# ...
sldf <- tryCatch(
          expr  = st_sf(start = start, end = end, 
                  duration = res$trips[nt, ]$legs[[1]][, "duration"]/60, 
                  distance = res$trips[nt, ]$legs[[1]][, "distance"]/1000, 
                  geometry = st_as_sfc(wktl, crs = 4326)),
          error = function(call) {
            message("An error occurred, only returning geometry of trip.")
            message("Here's the original error message:")
            message(paste(call))

            return(st_sf(geometry = st_as_sfc(wktl, crs = 4326)))
            }
          )
# ...
@rCarto
Copy link
Member

rCarto commented Mar 23, 2023

Hi,
I first thought this problem was already solved in the dev version of the pkg.
See

remotes::install_github('riatelab/osrm')`

then

library(osrm)
#> Data: (c) OpenStreetMap contributors, ODbL 1.0 - http://www.openstreetmap.org/copyright
#> Routing: OSRM - http://project-osrm.org/
library(sf)
#> Linking to GEOS 3.9.0, GDAL 3.2.2, PROJ 7.2.1; sf_use_s2() is TRUE

point_1 <- c(25086.15, 194501.7)
point_2 <- c(25032.33, 195725.5)
point_3 <- c(24140.32, 193718.8)
point_4 <- c(24217, 194746)
point_5 <- c(24445.09, 195494.1)
point_6 <- c(24368.93, 194250.5)
point_7 <- c(25521.03, 195911.1)
point_8 <- c(25830.59, 195202.3)
point_9 <- c(25235.08, 194137.3)

# Create data frame
point_df <- do.call(rbind.data.frame, 
                    lapply(paste("point", 1:9, sep = "_"), get))
names(point_df) <- c("X", "Y")


# Create sf object
point_sf <- point_df |>
  st_as_sf(coords = c("X", "Y"), crs = st_crs(31370))

# Calculate trip
mytrip_car <- osrmTrip(point_sf)
mytrip_foot <- osrmTrip(point_sf, osrm.profile = "foot")

(mytrip_car[[1]]$trip)
#> Simple feature collection with 9 features and 4 fields
#> Geometry type: LINESTRING
#> Dimension:     XY
#> Bounding box:  xmin: 23624.07 ymin: 193204.7 xmax: 25996.78 ymax: 195831.9
#> Projected CRS: Belge 1972 / Belgian Lambert 72
#>   start end  duration distance                       geometry
#> 1     1   3  6.970000   1.5851 LINESTRING (25159.03 194109...
#> 2     3   6 10.055000   2.6630 LINESTRING (25159.03 194109...
#> 3     6   4  6.096667   1.3394 LINESTRING (24374.11 193425...
#> 4     4   5  1.160000   0.4828 LINESTRING (23958.38 194241...
#> 5     5   7  3.550000   1.3431 LINESTRING (24511.62 195110...
#> 6     7   2  1.270000   0.4323 LINESTRING (24133.11 194961...
#> 7     2   8  1.161667   0.7748 LINESTRING (25381.3 195646....
#> 8     8   9  8.516667   3.1316 LINESTRING (25381.3 195646....
#> 9     9   1  0.000000   0.0000 LINESTRING (25309.59 195831...
(mytrip_foot[[1]]$trip)
#> Simple feature collection with 9 features and 4 fields
#> Geometry type: LINESTRING
#> Dimension:     XY
#> Bounding box:  xmin: 23624.07 ymin: 193204.7 xmax: 25996.78 ymax: 195831.9
#> Projected CRS: Belge 1972 / Belgian Lambert 72
#>   start end duration distance                       geometry
#> 1     1   3 21.13833   1.5851 LINESTRING (25159.03 194109...
#> 2     3   6 35.50833   2.6630 LINESTRING (25159.03 194109...
#> 3     6   4 17.85667   1.3394 LINESTRING (24374.11 193425...
#> 4     4   5  6.43500   0.4828 LINESTRING (23958.38 194241...
#> 5     5   2 17.45833   1.3092 LINESTRING (24511.62 195110...
#> 6     2   7  5.76500   0.4323 LINESTRING (24133.11 194961...
#> 7     7   8 10.78167   0.8087 LINESTRING (25446.87 195476...
#> 8     8   9 41.79167   3.1316 LINESTRING (25381.3 195646....
#> 9     9   1  0.00000   0.0000 LINESTRING (25381.3 195646....

Created on 2023-03-23 with reprex v2.0.2

But inspecting more in details actual road lengths, I see there is still a problem, geometries do not match trip segments. There is a kind of offset. I'll look into it asap. Thank you for the bug report.

trip <- mytrip_car[[1]]$trip
trip$length <- st_length(trip)

(trip)
#> Simple feature collection with 9 features and 5 fields
#> Geometry type: LINESTRING
#> Dimension:     XY
#> Bounding box:  xmin: 23624.07 ymin: 193204.7 xmax: 25996.78 ymax: 195831.9
#> Projected CRS: Belge 1972 / Belgian Lambert 72
#>   start end  duration distance                       geometry        length
#> 1     1   3  6.970000   1.5851 LINESTRING (25159.03 194109...    0.0000 [m]
#> 2     3   6 10.055000   2.6630 LINESTRING (25159.03 194109... 1224.1767 [m]
#> 3     6   4  6.096667   1.3394 LINESTRING (24374.11 193425... 2692.4443 [m]
#> 4     4   5  1.160000   0.4828 LINESTRING (23958.38 194241... 1250.3470 [m]
#> 5     5   7  3.550000   1.3431 LINESTRING (24511.62 195110...  407.1056 [m]
#> 6     7   2  1.270000   0.4323 LINESTRING (24133.11 194961... 1593.2942 [m]
#> 7     2   8  1.161667   0.7748 LINESTRING (25381.3 195646....  466.3010 [m]
#> 8     8   9  8.516667   3.1316 LINESTRING (25381.3 195646....  199.2373 [m]
#> 9     9   1  0.000000   0.0000 LINESTRING (25309.59 195831... 3905.0552 [m]

Created on 2023-03-23 with reprex v2.0.2

@wlangera
Copy link
Author

Ok, thank you for your fast reply.
I installed the package from CRAN, not the dev version.

Kind regards,
Ward

@rCarto rCarto added the bug label Mar 23, 2023
@rCarto rCarto closed this as completed in 6e2bd6c Mar 29, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

No branches or pull requests

2 participants