1-- Helpers for searching and parsing tags
2
3local Tags = {}
4
5-- return [forward,backward] values for a specific tag.
6-- e.g. for maxspeed search forward:
7--   maxspeed:forward
8--   maxspeed
9-- and backward:
10--   maxspeed:backward
11--   maxspeed
12
13function Tags.get_forward_backward_by_key(way,data,key)
14  local forward = way:get_value_by_key(key .. ':forward')
15  local backward = way:get_value_by_key(key .. ':backward')
16
17  if not forward or not backward then
18     local common = way:get_value_by_key(key)
19
20     if (data.oneway) then
21        if data.is_forward_oneway then
22           forward = forward or common
23        end
24        if data.is_reverse_oneway then
25           backward = backward or common
26        end
27     else
28        forward = forward or common
29        backward = backward or common
30     end
31  end
32
33  return forward, backward
34end
35
36-- return [forward,backward] values, searching a
37-- prioritized sequence of tags
38-- e.g. for the sequence [maxspeed,advisory] search forward:
39--   maxspeed:forward
40--   maxspeed
41--   advisory:forward
42--   advisory
43-- and for backward:
44--   maxspeed:backward
45--   maxspeed
46--   advisory:backward
47--   advisory
48
49function Tags.get_forward_backward_by_set(way,data,keys)
50  local forward, backward
51  for i,key in ipairs(keys) do
52    if not forward then
53      forward = way:get_value_by_key(key .. ':forward')
54    end
55    if not backward then
56      backward = way:get_value_by_key(key .. ':backward')
57    end
58    if not forward or not backward then
59      local common = way:get_value_by_key(key)
60      forward = forward or common
61      backward = backward or common
62    end
63    if forward and backward then
64      break
65    end
66  end
67
68  return forward, backward
69end
70
71-- look through a sequence of keys combined with a prefix
72-- e.g. for the sequence [motorcar,motor_vehicle,vehicle] and the prefix 'oneway' search for:
73-- oneway:motorcar
74-- oneway:motor_vehicle
75-- oneway:vehicle
76
77function Tags.get_value_by_prefixed_sequence(way,seq,prefix)
78  local v
79  for i,key in ipairs(seq) do
80    v = way:get_value_by_key(prefix .. ':' .. key)
81    if v then
82      return v
83    end
84  end
85end
86
87-- look through a sequence of keys combined with a postfix
88-- e.g. for the sequence [motorcar,motor_vehicle,vehicle] and the postfix 'oneway' search for:
89-- motorcar:oneway
90-- motor_vehicle:oneway
91-- vehicle:oneway
92
93function Tags.get_value_by_postfixed_sequence(way,seq,postfix)
94  local v
95  for i,key in ipairs(seq) do
96    v = way:get_value_by_key(key .. ':' .. postfix)
97    if v then
98      return v
99    end
100  end
101end
102
103-- check if key-value pairs are set in a way and return a
104-- corresponding constant if it is. e.g. for this input:
105--
106-- local speeds = {
107--  highway = {
108--    residential = 20,
109--    primary = 40
110--  },
111--  amenity = {
112--    parking = 10
113--  }
114-- }
115--
116-- we would check whether the following key-value combinations
117-- are set, and return the corresponding constant:
118--
119-- highway = residential      => 20
120-- highway = primary          => 40
121-- amenity = parking          => 10
122
123function Tags.get_constant_by_key_value(way,lookup)
124  for key,set in pairs(lookup) do
125    local way_value = way:get_value_by_key(key)
126    for value,t in pairs(set) do
127      if way_value == value then
128        return key,value,t
129      end
130    end
131  end
132end
133
134return Tags
135