systemed / tilemaker

Make OpenStreetMap vector tiles without the stack
https://tilemaker.org/
Other
1.44k stars 229 forks source link

terminate called after throwing an instance of 'kaguya::LuaRuntimeError' #534

Closed larsschwarz closed 8 months ago

larsschwarz commented 1 year ago

Hey,

running into the above error using the latest Germany extract from Geofabrik and the latest tilemaker version.

Tried running osmium extract on the pbf with --strategy=smart, no change.

Error details

  what():  maybe...Argument mismatch:userdata,string,nil         candidate is:
                OsmLuaProcessing,std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >,std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >,
                OsmLuaProcessing,std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >,std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >,char,

Running osmium with check-refs on both files (the original extract the one processed with osmium) returns

There are 388819350 nodes, 63449058 ways, and 792987 relations in this file.
Nodes     in ways      missing: 0
Nodes     in relations missing: 25282
Ways      in relations missing: 383482
Relations in relations missing: 26922

Haven't changed anything in the processing script or config so I'm pretty sure the Geofabrik extract is the root cause, not sure why using osmium didn't change anything though?

larsschwarz commented 1 year ago

Just tried osmosis with clipIncompleteEntities=true same error :(

Running osmium with check-refs -r on the pfb processed by osmosis however reports no issues with the pbf

There are 388819350 nodes, 63449058 ways, and 790560 relations in this file.
Nodes     in ways      missing: 0
Nodes     in relations missing: 0
Ways      in relations missing: 0
Relations in relations missing: 0
systemed commented 1 year ago

Hmmmm. Using the latest Germany extract (downloaded this morning) it works ok for me:

tilemaker --input /media/data1/planet/germany-latest.osm.pbf --output ../tm_debug/germany.mbtiles
[...]
Block 56533/56635 ways 8000 relations 0        
[...]
Generated points: 23896147, lines: 32322159, polygons: 42387998
Zoom level 14, writing tile 272989 of 272989               
Filled the tileset with good things at ../tm_debug/germany.mbtiles

so I'm not sure what's going on here.

At what point is tilemaker exiting (i.e. what's shown in the console before it dies)? I would usually expect LuaRuntimeError to indicate that the profile is choking on something in the OSM data but I can't see any sign of that here.

larsschwarz commented 1 year ago

Thanks. Indeed it works fine with the OSM example script. Seems my issue was related to how I accessed tags in my custom script (pseudocode follows):

function way_function(way)

  local sometag = way:Find("sometag")
  if sometag~="" then
      way:Attribute("sometag",sometag)
  end

This caused the reported error

After rewriting it to

if way:Holds("sometag") then            
    way:Attribute("sometag", way:Find("sometag")) 
end

it works fine!

Not sure what's the actual root cause, because I've used this script for a long time. Probably some weird data in some of the tags I access. An issue search for the resulting error made me think it's related to the PBF.

systemed commented 1 year ago

Interesting. I might put a try/catch block around the Lua calls which would allow us to see which OSM object it is that's triggering the error - that would help in narrowing this sort of thing down.

larsschwarz commented 1 year ago

Is this something I can do (no Lua guy) or something only you can do when "internally" calling the way_function()?

systemed commented 1 year ago

Have a look at https://github.com/systemed/tilemaker/pull/535 . This should output the ID of the OSM object causing the issue; the Lua error that was encountered; and (if using Lua 5.2 or later) a full stack trace.

larsschwarz commented 1 year ago

Ok, it nows prints out the way ID. The used in relations: 99%) error output however looks kinda broken?

Layer place (z5-14)
Layer transportation (z5-14)
Layer transportation_name (z5-14)
Bounding box 5.86442, 47.2654, 15.0508, 55.1478
Sorting loaded shapes
Reading .pbf germany-latest.osm.pbf
Block 48601/56635 ways 0 relations 0
Sorting nodes
lua runtime error: used in relations: 99%)
`maybe...Argument mismatch:userdata,string,nil   candidate is:
                OsmLuaProcessing,std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >,std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >,
                OsmLuaProcessing,std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >,std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >,char,
'  `maybe...Argument mismatch:userdata,string,nil        candidate is:
                OsmLuaProcessing,std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >,std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >,
                OsmLuaProcessing,std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >,std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >,char,
'
Lua error on way 4058980
Segmentation fault
systemed commented 1 year ago

Don't worry about the used in relations: 99%) bit, that's just leftover output from the previous line which ends with a \r rather than a \n.

Can you share the relevant bit of your Lua script (i.e. what "sometag" actually is etc.)?

larsschwarz commented 1 year ago

Here's the relevant part of the previous script (all vars are declared I just left them out here). The error is caused somewhere in this section, when removing these lines the generation completes just fine:

if bicycle_road~="" then
    way:Attribute("bicycle_road",bicroad)
end
if cycleway~="" then
    way:Attribute("cycleway",cycleway)
end
if traffic_sign~="" then
    way:Attribute("traffic_sign",traffic_sign)
end
if bicycle~="" then
    way:Attribute("bicycle",bicycle)
end
if segregated~="" then
    way:Attribute("segregated",segregated)
end
if maxspeed~="" then
    way:Attribute("maxspeed",maxspeed)
end
if br_width~="" then
    way:Attribute("width",br_width)
end
if rcn~="" then
    way:Attribute("rcn",rcn)
end
if hazard~="" then
    way:Attribute("hazard",hazard)
end
if parking~="" then
    way:Attribute("parking:both",parking)
end
if parking_l~="" then
    way:Attribute("parking:left",parking_l)
end
if parking_r~="" then
    way:Attribute("parking:right",parking_r)
end
systemed commented 12 months ago

Always good to see tilemaker used for something bike-focused :)

if bicycle_road~="" then way:Attribute("bicycle_road",bicroad)

You have bicycle_road in the if statement but bicroad in the way:Attribute call - could that be the issue?

larsschwarz commented 12 months ago

bicroad actually was defined as local bicroad = way:Find("bicycle_road"), but this exact line vanished from the latest version of the script somehow 🤯 So does Argument mismatch mean undefined variable?

However, thanks for the quick fix Richard.

ps: Building a map that shows all bicycle roads in Germany. Using a low zoom level to display the roads so one can literally see all roads at once when on a zoom level like 5 and there's no need to only display them on a certain state- or city-like zoom level.

systemed commented 12 months ago

Sounds awesome!

I wonder if what was happening is that the Lua/C++ bridge was treating the undefined variable as null (aka nil in Lua), and then throwing the error because it expected a string but was getting null instead.