Skip to content

Commit fc9a89e

Browse files
deniskoronchikPatrick Niklaus
authored andcommitted
Support some cases for supperrelations in car.lua
1 parent 832cdbf commit fc9a89e

File tree

3 files changed

+81
-33
lines changed

3 files changed

+81
-33
lines changed

profiles/car.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -406,7 +406,7 @@ function process_way(profile, way, result, relations)
406406
local rel_id_list = relations:get_relations(way)
407407
for i, rel_id in ipairs(rel_id_list) do
408408
local rel = relations:relation(rel_id)
409-
parsed_rel_list[i] = Relations.parse_route_relation(rel, way)
409+
parsed_rel_list[i] = Relations.parse_route_relation(rel, way, relations)
410410
end
411411

412412
-- now process relations data

profiles/lib/relations.lua

Lines changed: 61 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ Utils = require('lib/utils')
77

88
Relations = {}
99

10+
function is_direction(role)
11+
return (role == 'north' or role == 'south' or role == 'west' or role == 'east')
12+
end
13+
1014
-- match ref values to relations data
1115
function Relations.match_to_ref(relations, ref)
1216

@@ -91,37 +95,71 @@ function Relations.match_to_ref(relations, ref)
9195
return result
9296
end
9397

94-
function Relations.parse_route_relation(relation, obj)
95-
local t = relation:get_value_by_key("type")
96-
local role = relation:get_role(obj)
98+
function get_direction_from_superrel(rel, relations)
99+
local result = nil
100+
local result_id = nil
101+
local rel_id_list = relations:get_relations(rel)
102+
103+
function set_result(direction, current_rel)
104+
if (result ~= nil) and (direction ~= nil) then
105+
print('WARNING: relation ' .. rel:id() .. ' is a part of more then one supperrelations ' .. result_id .. ' and ' .. current_rel:id())
106+
else
107+
result = direction
108+
result_id = current_rel:id()
109+
end
110+
end
111+
112+
for i, rel_id in ipairs(rel_id_list) do
113+
local parent_rel = relations:relation(rel_id)
114+
if parent_rel:get_value_by_key('type') == 'route' then
115+
local role = parent_rel:get_role(rel)
116+
117+
if is_direction(role) then
118+
set_result(role, parent_rel)
119+
else
120+
local dir = parent_rel:get_value_by_key('direction')
121+
if is_direction(dir) then
122+
set_result(dir, parent_rel)
123+
end
124+
end
125+
end
126+
-- TODO: support forward/backward
127+
end
128+
129+
return result
130+
end
131+
132+
function Relations.parse_route_relation(rel, way, relations)
133+
local t = rel:get_value_by_key("type")
134+
local role = rel:get_role(way)
97135
local result = {}
98136

99137
function add_extra_data(m)
100-
local name = relation:get_value_by_key("name")
138+
local name = rel:get_value_by_key("name")
101139
if name then
102140
result['route_name'] = name
103141
end
104142

105-
local ref = relation:get_value_by_key("ref")
143+
local ref = rel:get_value_by_key("ref")
106144
if ref then
107145
result['route_ref'] = ref
108146
end
109147
end
110148

111149
if t == 'route' then
112-
local route = relation:get_value_by_key("route")
150+
local route = rel:get_value_by_key("route")
113151
if route == 'road' then
114152
-- process case, where directions set as role
115-
if role == 'north' or role == 'south' or role == 'west' or role == 'east' then
153+
if is_direction(role) then
116154
result['route_direction'] = role
117155
add_extra_data(m)
118156
end
119157
end
120158

121-
local direction = relation:get_value_by_key('direction')
159+
local direction = rel:get_value_by_key('direction')
122160
if direction then
123161
direction = string.lower(direction)
124-
if direction == 'north' or direction == 'south' or direction == 'west' or direction == 'east' then
162+
if is_direction(direction) then
125163
if role == 'forward' then
126164
result['route_direction'] = direction
127165
add_extra_data(m)
@@ -130,6 +168,20 @@ function Relations.parse_route_relation(relation, obj)
130168
end
131169
end
132170

171+
-- process superrelations
172+
local super_dir = get_direction_from_superrel(rel, relations)
173+
174+
-- check if there are data error
175+
local dir = result['route_direction']
176+
if (dir ~= nil) and (super_dir ~= nil) and (dir ~= super_dir) then
177+
print('ERROR: conflicting relation directions found for way ' .. way:id() ..
178+
' relation direction is ' .. dir .. ' superrelation direction is ' .. super_dir)
179+
end
180+
181+
if (dir == nil) and (super_dir ~= nil) then
182+
result['route_direction'] = super_dir
183+
end
184+
133185
return result
134186
end
135187

src/extractor/scripting_environment_lua.cpp

Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -417,24 +417,23 @@ void Sol2ScriptingEnvironment::InitContext(LuaScriptingContext &context)
417417
sol::property([](const ExtractionWay &way) { return way.is_left_hand_driving; },
418418
[](ExtractionWay &way, bool flag) { way.is_left_hand_driving = flag; }));
419419

420-
auto getTypedRefBySol = [](const sol::object & obj) -> ExtractionRelation::OsmIDTyped
421-
{
420+
auto getTypedRefBySol = [](const sol::object &obj) -> ExtractionRelation::OsmIDTyped {
422421
if (obj.is<osmium::Way>())
423422
{
424-
osmium::Way * way = obj.as<osmium::Way*>();
425-
return { way->id(), osmium::item_type::way };
423+
osmium::Way *way = obj.as<osmium::Way *>();
424+
return {way->id(), osmium::item_type::way};
426425
}
427426

428-
if (obj.is<osmium::Relation>())
427+
if (obj.is<ExtractionRelation>())
429428
{
430-
osmium::Relation * rel = obj.as<osmium::Relation*>();
431-
return { rel->id(), osmium::item_type::relation };
429+
ExtractionRelation *rel = obj.as<ExtractionRelation *>();
430+
return rel->id;
432431
}
433432

434433
if (obj.is<osmium::Node>())
435434
{
436-
osmium::Node * node = obj.as<osmium::Node*>();
437-
return { node->id(), osmium::item_type::node };
435+
osmium::Node *node = obj.as<osmium::Node *>();
436+
return {node->id(), osmium::item_type::node};
438437
}
439438

440439
return ExtractionRelation::OsmIDTyped(0, osmium::item_type::undefined);
@@ -449,27 +448,25 @@ void Sol2ScriptingEnvironment::InitContext(LuaScriptingContext &context)
449448

450449
context.state.new_usertype<ExtractionRelation>(
451450
"ExtractionRelation",
451+
"id",
452+
[](ExtractionRelation &rel) { return rel.id.GetID(); },
452453
"get_value_by_key",
453-
[](ExtractionRelation &rel, const char * key) -> const char * { return rel.GetAttr(key); },
454+
[](ExtractionRelation &rel, const char *key) -> const char * { return rel.GetAttr(key); },
454455
"get_role",
455-
[&getTypedRefBySol](ExtractionRelation &rel, const sol::object & obj) -> const char *
456-
{
456+
[&getTypedRefBySol](ExtractionRelation &rel, const sol::object &obj) -> const char * {
457457
return rel.GetRole(getTypedRefBySol(obj));
458458
});
459459

460460
context.state.new_usertype<ExtractionRelationContainer>(
461461
"ExtractionRelationContainer",
462462
"get_relations",
463-
[&getTypedRefBySol](ExtractionRelationContainer &cont, const sol::object & obj)
464-
-> const ExtractionRelationContainer::RelationIDList &
465-
{
466-
return cont.GetRelations(getTypedRefBySol(obj));
467-
},
463+
[&getTypedRefBySol](ExtractionRelationContainer &cont, const sol::object &obj)
464+
-> const ExtractionRelationContainer::RelationIDList & {
465+
return cont.GetRelations(getTypedRefBySol(obj));
466+
},
468467
"relation",
469-
[](ExtractionRelationContainer &cont, const ExtractionRelation::OsmIDTyped & rel_id) -> const ExtractionRelation &
470-
{
471-
return cont.GetRelationData(rel_id);
472-
});
468+
[](ExtractionRelationContainer &cont, const ExtractionRelation::OsmIDTyped &rel_id)
469+
-> const ExtractionRelation & { return cont.GetRelationData(rel_id); });
473470

474471
context.state.new_usertype<ExtractionSegment>("ExtractionSegment",
475472
"source",
@@ -717,7 +714,7 @@ LuaScriptingContext &Sol2ScriptingEnvironment::GetSol2Context()
717714
auto &ref = script_contexts.local(initialized);
718715
if (!initialized)
719716
{
720-
ref = std::make_unique<LuaScriptingContext>(location_dependent_data);
717+
ref = std::make_unique<LuaScriptingContext>();
721718
InitContext(*ref);
722719
}
723720

@@ -1037,7 +1034,6 @@ void LuaScriptingContext::ProcessWay(const osmium::Way &way,
10371034
case 1:
10381035
case 0:
10391036
way_function(way, result);
1040-
result.is_left_hand_driving = properties.left_hand_driving;
10411037
break;
10421038
}
10431039
}

0 commit comments

Comments
 (0)