Add location_dependent_data unit tests

This commit is contained in:
Michael Krasnyk
2017-08-30 22:11:26 +02:00
parent 4eac861eae
commit b15288e0ea
5 changed files with 186 additions and 37 deletions
+23 -30
View File
@@ -18,6 +18,11 @@ namespace osrm
namespace extractor
{
LocationDependentData::LocationDependentData(const boost::filesystem::path &path)
{
loadLocationDependentData(path);
}
LocationDependentData::LocationDependentData(const std::vector<boost::filesystem::path> &file_paths)
{
for (const auto &path : file_paths)
@@ -139,37 +144,13 @@ void LocationDependentData::loadLocationDependentData(const boost::filesystem::p
<< polygons.size() << " GeoJSON polygons";
}
namespace
LocationDependentData::properties_t LocationDependentData::operator()(const point_t &point) const
{
struct table_setter : public boost::static_visitor<>
{
table_setter(sol::table &table, const std::string &key) : table(table), key(key) {}
template <typename T> void operator()(const T &value) const { table.set(key, value); }
void operator()(const boost::blank &) const { /* ignore */}
properties_t result;
sol::table &table;
const std::string &key;
};
}
sol::table LocationDependentData::operator()(sol::state &state, const osmium::Way &way) const
{
if (rtree.empty())
return sol::make_object(state, sol::nil);
// 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();
const point_t point(location.lon(), location.lat());
auto table = sol::table(state, sol::create);
auto merger = [this, &table](const rtree_t::value_type &rtree_entry) {
for (const auto &key_value : properties[polygons[rtree_entry.second].second])
{
boost::apply_visitor(table_setter(table, key_value.first), key_value.second);
}
auto merger = [this, &result](const rtree_t::value_type &rtree_entry) {
const auto &polygon_properties = properties[polygons[rtree_entry.second].second];
result.insert(polygon_properties.begin(), polygon_properties.end());
};
// Search the R-tree and collect a Lua table of tags that correspond to the location
@@ -179,7 +160,19 @@ sol::table LocationDependentData::operator()(sol::state &state, const osmium::Wa
}),
boost::make_function_output_iterator(std::ref(merger)));
return table;
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();
const auto &location = nodes.back().location();
const point_t point(location.lon(), location.lat());
return operator()(point);
}
}
}
+34 -2
View File
@@ -978,6 +978,31 @@ 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 <typename T> 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)
@@ -987,10 +1012,17 @@ void LuaScriptingContext::ProcessWay(const osmium::Way &way,
switch (api_version)
{
case 3:
way_function(profile_table, way, result, relations);
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)));
}
break;
case 2:
way_function(profile_table, way, result, location_dependent_data(state, way));
way_function(profile_table, way, result);
break;
case 1:
case 0: