Multiple geometries returned for trips along same road
grobins opened this issue · 3 comments
I have a local server running osrm, and I'm trying to pass in 1000 trips with a start & end location, and 100 waypoints.
I want to return the trips with their geometries snapped to the road, so that I can join by location in QGIS and produce a heatmap of the number of trips on each road.
When I run the following code I get back the trips as pictured
trip_ids <- unique(trip_points$trip_id)
for (i in 1:length(trip_ids)) {
testtrip <- trip_points %>% filter(trip_id == trip_ids[i])
fl <- testtrip %>% rowid_to_column() %>% slice(c(1,n()))
mid <- testtrip %>% rowid_to_column() %>% slice_sample(n=100)
testtrip <- bind_rows(fl, mid) %>% arrange(rowid)
testtrip.sf <- st_as_sf(testtrip, coords = c("longitude", "latitude"), crs = 4326)
route2 <- osrmRoute(loc = testtrip.sf, returnclass = "sf", overview = 'full')
if (i == 1) {
results <- route2
} else {
results <- bind_rows(results, route2)
}
}
results %>%
mutate(linestring = st_as_text(st_sfc(results$geometry), crs = 4326)) %>%
as.data.frame() %>%
select(-geometry) %>%
write_delim("test_snapped_linestrings_local.csv", ";")
I expected the same geometry to be returned for each trip in the image, but as you can see many are offset. Any help would be appreciated
I would have said that osrmRoute(..., overview = "full")
was the solution, but you use it already.
Otherwise, maybe this part could be problematic, specifically the mutate part :
results %>%
mutate(linestring = st_as_text(st_sfc(results$geometry), crs = 4326)) %>%
as.data.frame() %>%
select(-geometry) %>%
write_delim("test_snapped_linestrings_local.csv", ";")
Can you post a reproducible example please.
Thanks, that helped a lot...
I changed from writing out a WKT to using results %>% st_write('test_snapped_linestrings_local.shp')
and it's now perfect.
the highlighted line actually has 197 overlapping
I also changed from osrmRoute()
to osrmTrip()
- not too sure what the difference is rn but this seems to work ok
Nice!
osrmRoute()
computes the shortest path between 2 (or more) ordered points.
osrmTrip()
computes the shortest trip between a set of unordered points (the traveling salesperson problem).
So I think you were right to use osrmRoute()
first.