Project-OSRM / osrm-backend

Open Source Routing Machine - C++ backend
http://map.project-osrm.org
BSD 2-Clause "Simplified" License
6.34k stars 3.36k forks source link

Using traffic volume data in routing #1371

Closed bjtaylor1 closed 8 years ago

bjtaylor1 commented 9 years ago

This isn't strictly a bug in osrm so apologies if it's out of place but I hope it is of interest.

One of my users gave me the idea of integrating traffic volume data into routing so that you could generate cycle routes on the least busy roads. This sounds like a fantastic idea if it could work.

So I set about trying to integrate traffic volume flow data into the OSM raw data, so I could then put a rule in the LUA file to reduce the speed of roads that have high volumes and vice versa. That way it would prefer quiet roads.

Now this data is available from the Department for Transport, but the hard part is marrying up the format it is in, to ways in OSM data. The data it provides is a coordinate (it's in easting/northing but it's easy to convert to lat long), the road identifier (e.g. M1, A57, etc, often referred to in the US I think as 'shield number', and this usually matches the "ref" tag on a way), and then daily traffic flow volumes for cars, lorries, buses, etc. There are about 11,300 rows of this.

Now, my idea was to do this, for each row: 1) Generate a bounding box from the row's coordinate 2) Generate a translation.xml containing the road's flow volumes 3) Run osmosis selecting data from the country file with the bounding box and the tag transform plugin matching on the road reference, and applying the translation. Restricting to a 50m bounding box from the coordinate but also filtering on the road reference seems to be a pretty reliable way of matching, as it doesn't select parts of the same road that are miles away, but doesn't select minor roads that are in the vicinity either - but it does seem to pretty much guarantee to catch the way of interest. 4) This generates an output file where the way has got tags with the traffic volume figures in it. 5) I could then apply an xslt transform to these to make them into patch files to merge the data back into the country file.

so a typical translation looks something like: osmosis --read-pbf file="great-britain-latest.osm.pbf" --bb left=-2.3030 bottom=53.4006 right=-2.3015 top=53.4015 --tag-transform translation.9833.xml --write-xml translated.9833.osm With a tag filter of "A560.*", this catches 4 ways on the A560 in manchester: 4086000, 229688387, 300113826, 300113827. Which are the exact ones I think that row of the traffic volume data should catch.

however this single osmosis invocation takes about a minute, so doing this 11,000 times would take about a month.

I was just wondering if anybody knows a much quicker way of doing this - can osmosis read the file in only once but apply all the translations in one go, for example?

emiltin commented 9 years ago

an easier and more flexible way to use external data is to query postgis from lua. for an example, see https://github.com/Project-OSRM/osrm-backend/blob/master/profiles/examples/postgis.lua. it slows down processing a bit, but i would say surprisingly little.

the hard part is the map matching. especially for smaller roads it is not trivial.

bjtaylor1 commented 9 years ago

OK thanks i'll explore that avenue. I thought I'd got the matching nailed, by maching on a tag AND a coordinate. The coordinate always seems to be on the road, so I only have to expand it by about 50 metres to get a bounding box that intersects with the way. So as long as PostGIS can match on a tag AND a geospatial index then we're golden. But we'll see!

emiltin commented 9 years ago

postgis can do almost anything. remember to define spatial indexes to speed up queries.

bjtaylor1 commented 9 years ago

Ah. I was thinking I could access the geometry of the way in the lua so I would only have to import the (smaller) traffic volume's data and query by passing the geometry into the query.

Although that example appears to suggest all the ways are imported into the database first. This probably takes a bit of time but probably not prohibitively so.... however it does still mean passing the way's id in and thus relies on being able to solve my other question about accessing that? (see https://github.com/Project-OSRM/osrm-backend/issues/1369 )

emiltin commented 9 years ago

yes, the example keep all the ways in postgis, so you can query just using the id and then get the geometry form postgis. not sure wether you can access the geometry from lua.

TheMarex commented 9 years ago

Getting the way id is currently not possible. That is a regression from previous OSRM versions. Should be easy to add though as far as I can see. Ah okay just saw. issue #1369

daniel-j-h commented 8 years ago

Getting the way id is currently not possible.

Accessing way ids is possible since https://github.com/Project-OSRM/osrm-backend/pull/1375 was merged in. Closing here, nothing actionable.