Skip to content

Using location dependent data in profiles

Michael Krasnyk edited this page Oct 4, 2017 · 1 revision

Prepare location-dependent data in GeoJSON format, e.g. country names using query-overpass:

echo '[out:json];rel["boundary"="administrative"]["admin_level"="2"]["ISO3166-1:alpha3"];out geom;' | \
  query-overpass --flat-properties > countries.geojson
grep -oP '"ISO3166-1:alpha3":"[A-Z]{3}"' countries.geojson  | sort > ISO3166-1:alpha3.txt
wc -l ISO3166-1:alpha3.txt
217 ISO3166-1:alpha3.txt

Add location-dependent handling in a profile, e.g. print way ids and country names in process_way:

   print ('Way ' .. way:id() .. ' in ' .. way:get_location_tag('name') ..
             ' ['  .. way:get_location_tag('ISO3166-1:alpha3') .. ']')

Running OSRM with --location-dependent-data command line argument will produce output:

wget http://download.geofabrik.de/europe/monaco-latest.osm.pbf
osrm-extract -p ../profiles/car.lua --location-dependent-data=countries.geojson monaco-latest.osm.pbf
...
Way 4097656 in Monaco [MCO]
Way 4098197 in Monaco [MCO]
Way 4212554 in France [FRA]
Way 4224972 in Monaco [MCO]
...
Way 4230116 in Monaco [MCO]
Way 4230117 in Monaco [MCO]
Way 4230118 in Monaco [MCO]
Way 4230119 in Monaco [MCO]
Way 4230122 in Monaco [MCO]
Way 4230126 in France [FRA]
Way 4230182 in France [FRA]
Way 4230185 in Monaco [MCO]
Way 4230186 in Monaco [MCO]
Way 4230187 in Monaco [MCO]
Way 4230891 in Monaco [MCO]
Way 4534918 in France [FRA]
Way 4978262 in France [FRA]
...

⚠️ The current implementation of ways representative nodes is naïve: it is always the last node of a way. Also in the case of overlapping polygons the first found tag value will be returned. If this behavior is oversimplified, please open an issue with the desired use case and a possible conflict resolution (merging) of values.

⚠️ The internal locations cache implicitly assumes that entities are ordered in OSM PBF files: nodes must precede ways. There is no internal consistency check in the extractor code. If the ordering is not guaranteed, please open an issue.

To speed up data extraction with location-dependent tags, one can pre-process OSM files by adding locations to ways with osmium and avoid using the internal locations cache:

osmium add-locations-to-ways --keep-untagged-nodes monaco-latest.osm.pbf -o monaco-latest-loc.osm.pbf
osrm-extract -p ../profiles/car.lua --location-dependent-data=countries.geojson \
    --use-locations-cache=no monaco-latest-loc.osm.pbf

The pre-processing step adds node locations directly into OSM ways, so the internal cache is not need. This reduces memory consumption by osrm-extract, but increases OSM PBF file size.

⚠️ If the option --use-locations-cache=no is used with an OSM file that was not pre-processed then an exception will be thrown

osrm-extract -p ../profiles/car.lua --location-dependent-data=countries.geojson \
    --use-locations-cache=no monaco-latest.osm.pbf
...
terminate called after throwing an instance of 'sol::error'
  what():  lua: error: invalid location