|
| 1 | +#include "luatagtransform.h" |
| 2 | + |
| 3 | +#include <stdexcept> |
| 4 | +#include <boost/format.hpp> |
| 5 | + |
| 6 | +using namespace valhalla::mjolnir; |
| 7 | + |
| 8 | +LuaTagTransform::LuaTagTransform() |
| 9 | +{ |
| 10 | + // Create a new lua state |
| 11 | + luastate_ = luaL_newstate(); |
| 12 | + |
| 13 | +} |
| 14 | + |
| 15 | +LuaTagTransform::~LuaTagTransform(){ |
| 16 | + if (luastate_ != NULL) { |
| 17 | + |
| 18 | + lua_close(luastate_); |
| 19 | + |
| 20 | + } |
| 21 | +} |
| 22 | + |
| 23 | +void LuaTagTransform::Init() const { |
| 24 | + |
| 25 | + luaL_openlibs(luastate_); |
| 26 | + |
| 27 | + //TODO:add lua script to options. |
| 28 | + std::string tag_transform_script = "/data/valhalla/import/osm2pgsql/edges.lua"; |
| 29 | + luaL_dofile(luastate_, tag_transform_script.c_str()); |
| 30 | + CheckLuaFuncExists("ways_proc"); |
| 31 | + |
| 32 | + tag_transform_script = "/data/valhalla/import/osm2pgsql/vertices.lua"; |
| 33 | + luaL_dofile(luastate_, tag_transform_script.c_str()); |
| 34 | + CheckLuaFuncExists("nodes_proc"); |
| 35 | +} |
| 36 | + |
| 37 | +void LuaTagTransform::SetLuaWayFunc(std::string luawayfunc) { |
| 38 | + luawayfunc_ = luawayfunc; |
| 39 | +} |
| 40 | + |
| 41 | +void LuaTagTransform::SetLuaNodeFunc(std::string luanodefunc) { |
| 42 | + luanodefunc_ = luanodefunc; |
| 43 | +} |
| 44 | + |
| 45 | +std::string LuaTagTransform::GetLuaWayFunc() const { |
| 46 | + return luawayfunc_; |
| 47 | +} |
| 48 | + |
| 49 | +std::string LuaTagTransform::GetLuaNodeFunc() const { |
| 50 | + return luanodefunc_; |
| 51 | +} |
| 52 | + |
| 53 | +void LuaTagTransform::CheckLuaFuncExists(const std::string &func_name) const { |
| 54 | + |
| 55 | + lua_getglobal(luastate_, func_name.c_str()); |
| 56 | + |
| 57 | + if (!lua_isfunction (luastate_, -1)) { |
| 58 | + throw std::runtime_error((boost::format("Lua script does not contain a function %1%") |
| 59 | + % func_name).str()); |
| 60 | + } |
| 61 | + lua_pop(luastate_,1); |
| 62 | +} |
| 63 | + |
| 64 | +Tags LuaTagTransform::TransformInLua(bool isWay, const Tags &maptags) { |
| 65 | + |
| 66 | + Tags result; |
| 67 | + |
| 68 | + int filter; |
| 69 | + int count = 0; |
| 70 | + struct keyval *item; |
| 71 | + const char * key, * value; |
| 72 | + |
| 73 | + int polygon = 0; |
| 74 | + int roads = 0; |
| 75 | + |
| 76 | + //TODO::add boost options. |
| 77 | + if (isWay) |
| 78 | + lua_getglobal(luastate_,"ways_proc"); |
| 79 | + else |
| 80 | + lua_getglobal(luastate_,"nodes_proc"); |
| 81 | + |
| 82 | + lua_newtable(luastate_); /* key value table */ |
| 83 | + |
| 84 | + for (auto tag : maptags) { |
| 85 | + lua_pushstring(luastate_, tag.first.c_str()); |
| 86 | + lua_pushstring(luastate_, tag.second.c_str()); |
| 87 | + lua_rawset(luastate_, -3); |
| 88 | + count++; |
| 89 | + } |
| 90 | + |
| 91 | + lua_pushinteger(luastate_, count); |
| 92 | + |
| 93 | + if (lua_pcall(luastate_,2,4,0)) { |
| 94 | + fprintf(stderr, "Failed to execute lua function for basic tag processing: %s\n", lua_tostring(luastate_, -1)); |
| 95 | + /* lua function failed */ |
| 96 | + } |
| 97 | + |
| 98 | + roads = lua_tointeger(luastate_, -1); |
| 99 | + lua_pop(luastate_,1); |
| 100 | + polygon = lua_tointeger(luastate_, -1); |
| 101 | + lua_pop(luastate_,1); |
| 102 | + |
| 103 | + lua_pushnil(luastate_); |
| 104 | + while (lua_next(luastate_,-2) != 0) { |
| 105 | + key = lua_tostring(luastate_,-2); |
| 106 | + value = lua_tostring(luastate_,-1); |
| 107 | + |
| 108 | + result[key] = value; |
| 109 | + |
| 110 | + lua_pop(luastate_,1); |
| 111 | + } |
| 112 | + |
| 113 | + filter = lua_tointeger(luastate_, -2); |
| 114 | + |
| 115 | + lua_pop(luastate_,2); |
| 116 | + |
| 117 | + return result; |
| 118 | + |
| 119 | +} |
| 120 | + |
0 commit comments