如何在OSRM查询中获取OpenStreetMap的maxspeed标签信息?
Great question! You’re spot on that OSRM doesn’t expose raw OSM tags like maxspeed by default—it only retains the data it needs to compute routes efficiently. But custom Lua profiles are indeed the right approach here, and it’s straightforward to set up with a few small changes.
Here’s a step-by-step guide to make this work:
1. Modify Your Lua Profile to Capture the maxspeed Tag
First, open your target Lua profile (e.g., car.lua for car routing). You’ll need to add code to extract and store the raw maxspeed tag when processing OSM ways.
Find the process_way function in the profile and add this snippet:
function process_way(way, result) -- Keep all existing code in this function -- ... -- Extract and store the raw maxspeed tag local raw_maxspeed = way:get_value_by_key("maxspeed") if raw_maxspeed then result.maxspeed = raw_maxspeed end -- Keep any remaining existing code -- ... end
This grabs the unmodified maxspeed value from the OSM way and attaches it to the processed result for later use.
2. Expose maxspeed in Route Annotations
Next, you need to ensure this stored maxspeed value is included in the annotations returned by OSRM. Locate the annotate_way function (or similar annotation logic) in your profile and add:
function annotate_way(way, data, result) -- Keep existing annotation code (e.g., distance, duration) -- ... -- Add raw maxspeed to the annotation output if way.maxspeed then result.maxspeed = way.maxspeed end end
This tells OSRM to include the raw maxspeed tag in the annotation data when you enable annotations in your query.
3. Recompile OSRM Data with the Updated Profile
OSRM uses preprocessed data files, so you’ll need to re-run the extraction and contraction steps with your modified profile:
# Extract data using your custom profile osrm-extract --profile=car.lua your-osm-dataset.osm.pbf # Contract the extracted data for routing osrm-contract your-osm-dataset.osrm
Make sure to replace car.lua and your-osm-dataset.osm.pbf with your actual profile and OSM file paths.
4. Query OSRM with Annotations Enabled
Now when you make a route request with annotations=true, the raw maxspeed tag will appear in the annotations section of the response. For example, your API call might look like:
http://your-osrm-server/route/v1/car/start-lon,start-lat;end-lon,end-lat?annotations=true
In the response, you’ll see a maxspeed array under routes[].legs[].annotations with the raw OSM values for each segment of the route.
Additional Notes
- This approach works for other raw OSM tags too—just repeat the pattern for tags like
maxspeed:conditionalif you need conditional speed limits. - If you’re using OSRM via Docker, you’ll need to mount your custom Lua profile into the container and run the extraction/contraction commands inside the container to use the modified profile.
- Different transport profiles (e.g.,
bike.lua,foot.lua) can be modified using the exact same logic to capturemaxspeedor other tags relevant to those modes.
内容的提问来源于stack exchange,提问作者Marcos Roriz Junior

