diff --git a/features/options/extract/lua.feature b/features/options/extract/lua.feature index 0a0c8d11f..113a93228 100644 --- a/features/options/extract/lua.feature +++ b/features/options/extract/lua.feature @@ -37,7 +37,8 @@ Feature: osrm-extract lua ways:get_nodes() """ functions = require('testbot') - function way_function(profile, way, result, relations, location_data) + function way_function(profile, way, result, relations) + location_data = way:get_location_tags() assert(location_data) for k, v in pairs(location_data) do print (k .. ' ' .. tostring(v)) end result.forward_mode = mode.driving @@ -67,7 +68,8 @@ Feature: osrm-extract lua ways:get_nodes() """ functions = require('testbot') - function way_function(profile, way, result, relations, location_data) + function way_function(profile, way, result, relations) + location_data = way:get_location_tags() assert(location_data) print('ISO3166-1 ' .. (location_data['ISO3166-1'] or 'none')) result.forward_mode = mode.driving @@ -101,7 +103,8 @@ Feature: osrm-extract lua ways:get_nodes() """ functions = require('testbot') - function way_function(profile, way, result, relations, location_data) + function way_function(profile, way, result, relations) + location_data = way:get_location_tags() assert(location_data) for k, v in pairs(location_data) do print (k .. ' ' .. tostring(v)) end result.forward_mode = mode.driving diff --git a/include/extractor/location_dependent_data.hpp b/include/extractor/location_dependent_data.hpp index e8dca02cc..b9ddaecf1 100644 --- a/include/extractor/location_dependent_data.hpp +++ b/include/extractor/location_dependent_data.hpp @@ -40,8 +40,6 @@ struct LocationDependentData properties_t operator()(const point_t &point) const; - properties_t operator()(const osmium::Way &way) const; - private: void loadLocationDependentData(const boost::filesystem::path &file_path); diff --git a/profiles/bicycle.lua b/profiles/bicycle.lua index 2cd2e0dad..4593a9141 100644 --- a/profiles/bicycle.lua +++ b/profiles/bicycle.lua @@ -550,7 +550,7 @@ function process_way(profile, way, result) WayHandlers.weights } - WayHandlers.run(profile,way,result,data,handlers) + WayHandlers.run(profile, way, result, data, handlers) end function process_turn(profile, turn) diff --git a/profiles/car.lua b/profiles/car.lua index 6febb4607..867c74c01 100644 --- a/profiles/car.lua +++ b/profiles/car.lua @@ -303,7 +303,7 @@ function process_node(profile, node, result) end end -function process_way(profile, way, result, relations, location_data) +function process_way(profile, way, result, relations) -- the intial filtering of ways based on presence of tags -- affects processing times significantly, because all ways -- have to be checked. @@ -387,8 +387,7 @@ function process_way(profile, way, result, relations, location_data) WayHandlers.weights } - print (profile, way, result, data, handlers, relations, location_data) - WayHandlers.run(profile, way, result, data, handlers, relations, location_data) + WayHandlers.run(profile, way, result, data, handlers, relations) end function process_turn(profile, turn) diff --git a/profiles/foot.lua b/profiles/foot.lua index 687c4631f..3d604619b 100644 --- a/profiles/foot.lua +++ b/profiles/foot.lua @@ -243,7 +243,7 @@ function process_way(profile, way, result) WayHandlers.weights } - WayHandlers.run(profile,way,result,data,handlers) + WayHandlers.run(profile, way, result, data, handlers) end function process_turn (profile, turn) diff --git a/profiles/lib/way_handlers.lua b/profiles/lib/way_handlers.lua index 2ea4e1d07..8def08788 100644 --- a/profiles/lib/way_handlers.lua +++ b/profiles/lib/way_handlers.lua @@ -552,16 +552,19 @@ function WayHandlers.blocked_ways(profile,way,result,data) end end -function WayHandlers.driving_side(profile, way, result, data, relations, location_data) +function WayHandlers.driving_side(profile, way, result, data) local driving_side = way:get_value_by_key("driving_side") if driving_side == 'left' then result.is_left_hand_driving = true elseif driving_side == 'right' then result.is_left_hand_driving = false - elseif location_data then - result.is_left_hand_driving = location_data['driving_side'] == 'left' - elseif profile.left_hand_driving then - result.is_left_hand_driving = true + else + location_data = way:get_location_tags() + if location_data and location_data['driving_side'] then + result.is_left_hand_driving = location_data['driving_side'] == 'left' + else + result.is_left_hand_driving = profile.left_hand_driving + end end end @@ -580,13 +583,13 @@ end -- WayHandlers.run(handlers,way,result,data,profile) -- -- Each method in the list will be called on the WayHandlers object. --- All handlers must accept the parameteres (profile, way, result, data[, location_data]) and return false +-- All handlers must accept the parameteres (profile, way, result, data, relations) and return false -- if the handler chain should be aborted. -- To ensure the correct order of method calls, use a Sequence of handler names. -function WayHandlers.run(profile, way, result, data, handlers, relatons, location_data) +function WayHandlers.run(profile, way, result, data, handlers, relations) for i,handler in ipairs(handlers) do - if handler(profile, way, result, data, relatons, location_data) == false then + if handler(profile, way, result, data, relations) == false then return false end end diff --git a/src/extractor/location_dependent_data.cpp b/src/extractor/location_dependent_data.cpp index b9f9311ea..366d57004 100644 --- a/src/extractor/location_dependent_data.cpp +++ b/src/extractor/location_dependent_data.cpp @@ -265,20 +265,5 @@ LocationDependentData::properties_t LocationDependentData::operator()(const poin return result; } - -LocationDependentData::properties_t LocationDependentData::operator()(const osmium::Way &way) const -{ - // HEURISTIC: use a single node (last) of the way to localize the way - // For more complicated scenarios a proper merging of multiple tags - // at one or many locations must be provided - const auto &nodes = way.nodes(); - - // TODO: the next call requires an OSM file preprocessed with a command - // osmium add-locations-to-ways --keep-untagged-nodes - const auto &location = nodes.back().location(); - const point_t point(location.lon(), location.lat()); - - return operator()(point); -} } } diff --git a/src/extractor/scripting_environment_lua.cpp b/src/extractor/scripting_environment_lua.cpp index 775e459fe..564434439 100644 --- a/src/extractor/scripting_environment_lua.cpp +++ b/src/extractor/scripting_environment_lua.cpp @@ -42,6 +42,8 @@ namespace osrm namespace extractor { +namespace +{ template auto get_value_by_key(T const &object, const char *key) -> decltype(object.get_value_by_key(key)) { @@ -80,6 +82,28 @@ template double lonToDouble(T const &object) return static_cast(util::toFloating(object.lon)); } +// boost::variant visitor that inserts a key-value pair in a Lua table +struct table_setter : public boost::static_visitor<> +{ + table_setter(sol::table &table, const std::string &key) : table(table), key(key) {} + template void operator()(const T &value) const { table.set(key, value); } + void operator()(const boost::blank &) const { /* ignore */} + + sol::table &table; + const std::string &key; +}; + +// Converts a properties map into a Lua table +sol::table toLua(sol::state &state, const LocationDependentData::properties_t &properties) +{ + auto table = sol::table(state, sol::create); + std::for_each(properties.begin(), properties.end(), [&table](const auto &property) { + boost::apply_visitor(table_setter(table, property.first), property.second); + }); + return table; +} +} + Sol2ScriptingEnvironment::Sol2ScriptingEnvironment( const std::string &file_name, const std::vector &location_dependent_data_paths) @@ -285,10 +309,20 @@ void Sol2ScriptingEnvironment::InitContext(LuaScriptingContext &context) &get_value_by_key, "id", &osmium::Way::id, + "version", + &osmium::Way::version, "get_nodes", [](const osmium::Way &way) { return sol::as_table(way.nodes()); }, - "version", - &osmium::Way::version); + "get_location_tags", + [&context](const osmium::Way &way) { + // HEURISTIC: use a single node (last) of the way to localize the way + // For more complicated scenarios a proper merging of multiple tags + // at one or many locations must be provided + const auto &nodes = way.nodes(); + const auto &location = nodes.back().location(); + return toLua(context.state, + context.location_dependent_data({location.lon(), location.lat()})); + }); context.state.new_usertype( "RelationMember", @@ -978,30 +1012,6 @@ void LuaScriptingContext::ProcessNode(const osmium::Node &node, } } -namespace -{ -// boost::variant visitor that inserts a key-value pair in a Lua table -struct table_setter : public boost::static_visitor<> -{ - table_setter(sol::table &table, const std::string &key) : table(table), key(key) {} - template void operator()(const T &value) const { table.set(key, value); } - void operator()(const boost::blank &) const { /* ignore */} - - sol::table &table; - const std::string &key; -}; - -// Converts a properties map into a Lua table -sol::table toLua(sol::state &state, const LocationDependentData::properties_t properties) -{ - auto table = sol::table(state, sol::create); - std::for_each(properties.begin(), properties.end(), [&table](const auto &property) { - boost::apply_visitor(table_setter(table, property.first), property.second); - }); - return table; -} -} - void LuaScriptingContext::ProcessWay(const osmium::Way &way, ExtractionWay &result, const ExtractionRelationContainer::RelationList &relations) @@ -1011,15 +1021,7 @@ void LuaScriptingContext::ProcessWay(const osmium::Way &way, switch (api_version) { case 3: - if (location_dependent_data.empty()) - { - way_function(profile_table, way, result, relations); - } - else - { - way_function( - profile_table, way, result, relations, toLua(state, location_dependent_data(way))); - } + way_function(profile_table, way, result, relations); break; case 2: way_function(profile_table, way, result);