Debugging Lua scripts/profiles

Posted by Richard on 13 November 2017 in English.

osm2pgsql and OSRM can both make use of Lua scripting for tag processing, which in many cases is the best way to make sense of the often conflicting and confusing tag soup in OSM.

Firing up an osm2pgsql/OSRM run each time is, however, not the fastest way of debugging your Lua. So here’s a little script that reads your Lua osm2pgsql code, passes it the tags from a way, and returns the result:

# Test osm2pgsql Lua scripting with Ruby

require 'rufus-lua'
require 'overpass_api_ruby'
require 'pp'

# Initialise
overpass =
lua =
lua.eval('require "name_of_my_lua_script"')

# Get tags for the way
way_id = ARGV[0]
response = overpass.query("way(#{way_id}); (._; > ;);out;")
tags = response[:elements].find { |h| h[:type]=='way' }[:tags]
pp tags

# Convert keys from symbols to strings
lua['way'] = Hash[{|(k,v)| [k.to_s,v]}]

# Run Lua code
cmd = "res = {}; filter,tags,poly,roads=filter_tags_way(way,nil); return tags "
pp lua.eval(cmd).to_h

Call it with a way ID like this:

ruby test_lua.rb 35222450

When I do that on my osm2pgsql script, I get:

{:access=>"private", :highway=>"service", :source=>"OS_OpenData_StreetView"}

In other words, the input tags followed by the output tags.

It’s fairly easily adaptable for OSRM profiles, or for other functions in your osm2pgsql Lua script, or whatever.

I chose to write it in Ruby because there’s a ready-made Overpass gem and I’m generally a bit more comfortable in Ruby than Lua, but you could of course do the whole thing in Lua if you were so inclined.


Login to leave a comment