This repository has been archived by the owner on Apr 15, 2018. It is now read-only.
forked from ClearTables/ClearTables
-
Notifications
You must be signed in to change notification settings - Fork 0
/
transportation.lua
185 lines (157 loc) · 6.28 KB
/
transportation.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
--[[
This file is part of ClearTables
@author Paul Norman <penorman@mac.com>
@copyright 2015-2016 Paul Norman, MIT license
]]--
--[[
Code for the transportation layers. These are some of the most complex. There's a few principles
- Separate tables are needed for roads and rail
- A way might appear in both if it's both a road and rail
- Non-moterized lines are processed to avoid highway=path insanity
]]--
require "common"
local highway = {
motorway = {class="motorway", oneway="yes", motor_access="yes", bicycle="no", ramp="false"},
trunk = {class="trunk", motor_access="yes", ramp="false"},
primary = {class="primary", motor_access="yes", bicycle="yes", ramp="false"},
secondary = {class="secondary", motor_access="yes", bicycle="yes", ramp="false"},
tertiary = {class="tertiary", motor_access="yes", bicycle="yes", ramp="false"},
unclassified = {class="minor", motor_access="yes", bicycle="yes", ramp="false"},
residential = {class="minor", motor_access="yes", bicycle="yes", ramp="false"},
road = {class="unknown", motor_access="yes"},
living_street = {class="minor", motor_access="yes"},
motorway_link = {class="motorway", oneway="yes", motor_access="yes", ramp="true"},
trunk_link = {class="trunk", oneway="yes", motor_access="yes", ramp="true"},
primary_link = {class="primary", oneway="yes", motor_access="yes", ramp="true"},
secondary_link = {class="secondary", oneway="yes", motor_access="yes", ramp="true"},
tertiary_link = {class="tertiary", oneway="yes", motor_access="yes", ramp="true"},
service = {class="service", motor_access="yes", bicycle="yes"},
track = {class="track"},
pedestrian = {class="path", motor_access="no"},
path = {class="path", motor_access="no"},
footway = {class="path", motor_access="no"},
cycleway = {class="path", motor_access="no", bicycle="yes"},
steps = {class="path", motor_access="no"}
}
local railway = {
rail = {z=44, class="rail"},
narrow_gauge = {z=42, class="rail"},
preserved = {z=42, class="rail"},
funicular = {z=42, class="rail"},
subway = {z=42, class="transit"},
light_rail = {z=42, class="transit"},
monorail = {z=42, class="transit"},
tram = {z=41, class="transit"}
}
-- Some regions have the annoying habit of using values like RO:urban rather than setting the maxspeed.
-- These are the most common ones
local regional_maxspeed = {
["RO:urban"] = "50",
["RU:urban"] = "60",
["RU:rural"] = "90",
["RO:rural"] = "90",
["RO:trunk"] = "100",
["RU:living_street"] = "20"
}
--- Normalizes speed
-- @param v Speed tag value
-- @return Speed in km/h
function speed (v)
if v == nil then
return nil
end
-- speeds in km/h
if string.find(v, "^%d+%.?%d*$") then
-- Cap speed at 1000 km/h
return v and tonumber(v) < 1000 and v or nil
end
if string.find(v, "^(%d+.?%d*) ?mph$") then
return tostring(tonumber(string.match(v, "^(%d+.?%d*) ?mph$"))*1.609)
end
if regional_maxspeed[v] then
-- calling speed() allows the value in the table to be in mph
return speed(regional_maxspeed[v])
end
return nil
end
--- Normalizes lane tags
-- @param v The lane tag value
-- @return An integer > 0 or nil for the lane tag
function lanes (v)
return v and string.find(v, "^%d+$") and tonumber(v) < 100 and tonumber(v) > 0 and v or nil
end
function brunnel (tags)
return isset(tags["bridge"]) and "bridge" or isset(tags["tunnel"]) and "tunnel" or nil
end
function accept_road (tags)
return highway[tags["highway"]]
end
function accept_rail (tags)
return railway[tags["railway"]]
end
function accept_road_point (tags)
return tags["highway"] and (
tags["highway"]== "crossing" or
tags["highway"]== "traffic_signals" or
tags["highway"]== "motorway_junction")
end
function transform_road_point (tags)
local cols = {}
cols.type = tags["highway"]
cols.name = tags["name"]
cols.names = names(tags)
cols.ref = tags["ref"]
return cols
end
function transform_road (tags)
local cols = {}
cols.name = tags["name"]
cols.names = names(tags)
cols.refs = split_list(tags["ref"])
if highway[tags["highway"]] then
cols.class = highway[tags["highway"]]["class"]
cols.ramp = highway[tags["highway"]]["ramp"]
cols.oneway = oneway(tags["oneway"] or highway[tags["highway"]]["oneway"] or (tags["junction"] == "roundabout" and "yes") or nil)
-- Build access tags, taking the first non-nil value from the access hiarchy, or if that fails, taking a sane default
cols.motor_access = access(tags["motor_vehicle"] or
tags["vehicle"] or tags["access"] or highway[tags["highway"]]["motor_access"])
cols.bicycle_access = access(tags["bicycle"] or
tags["vehicle"] or tags["access"] or highway[tags["highway"]]["bicycle_access"])
cols.maxspeed = speed(tags["maxspeed"])
cols.brunnel = brunnel(tags)
cols.layer = layer(tags["layer"])
end
return cols
end
function transform_rail (tags)
local cols = {}
cols.name = tags["name"]
cols.names = names(tags)
if railway[tags["railway"]] then
cols.class = railway[tags["railway"]]["class"]
cols.brunnel = brunnel(tags)
cols.layer = layer(tags["layer"])
end
return cols
end
function road_ways (tags, num_keys)
return generic_line_way(tags, accept_road, transform_road)
end
function road_area_ways (tags, num_keys)
return generic_polygon_way(tags, accept_road, transform_road) -- uses the same accept/transform functions as lines
end
function road_area_rels (tags, num_keys)
if (tags["type"] == "multipolygon" and accept_road(tags)) then
return 0, tags
end
return 1, {}
end
function road_area_rel_members (tags, member_tags, member_roles, membercount)
return generic_multipolygon_members(tags, member_tags, membercount, accept_road, transform_road)
end
function road_points (tags, num_keys)
return generic_node(tags, accept_road_point, transform_road_point)
end
function rail_ways (tags, num_keys)
return generic_line_way(tags, accept_rail, transform_rail)
end