-
-
Notifications
You must be signed in to change notification settings - Fork 480
Expand file tree
/
Copy pathinit.lua
More file actions
199 lines (166 loc) · 4.71 KB
/
Copy pathinit.lua
File metadata and controls
199 lines (166 loc) · 4.71 KB
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
186
187
188
189
190
191
192
193
194
195
196
197
198
--
-- This will be compiled into osm2pgsql and run as initialization code in
-- the flex backend.
--
local math = require('math')
local _define_table_impl = function(_type, _name, _columns, _options)
_options = _options or {}
_options.name = _name
_options.ids = { type = _type, id_column = _type .. '_id' }
_options.columns = _columns
return osm2pgsql.define_table(_options)
end
function osm2pgsql.has_prefix(str, prefix)
if str == nil then
return nil
end
return str:sub(1, prefix:len()) == prefix
end
function osm2pgsql.has_suffix(str, suffix)
if str == nil then
return nil
end
if suffix == '' then
return true
end
return str:sub(- #suffix) == suffix
end
function osm2pgsql.define_node_table(_name, _columns, _options)
return _define_table_impl('node', _name, _columns, _options)
end
function osm2pgsql.define_way_table(_name, _columns, _options)
return _define_table_impl('way', _name, _columns, _options)
end
function osm2pgsql.define_relation_table(_name, _columns, _options)
return _define_table_impl('relation', _name, _columns, _options)
end
function osm2pgsql.define_area_table(_name, _columns, _options)
return _define_table_impl('area', _name, _columns, _options)
end
function osm2pgsql.node_member_ids(relation)
local ids = {}
for _, member in ipairs(relation.members) do
if member.type == 'n' then
ids[#ids + 1] = member.ref
end
end
return ids
end
function osm2pgsql.way_member_ids(relation)
local ids = {}
for _, member in ipairs(relation.members) do
if member.type == 'w' then
ids[#ids + 1] = member.ref
end
end
return ids
end
function osm2pgsql.clamp(value, low, high)
if value == nil then
return nil
end
return math.min(math.max(value, low), high)
end
function osm2pgsql.make_check_values_func(list, default)
local valid_values = {}
if default ~= nil then
local mt = {__index = function () return default end}
setmetatable(valid_values, mt)
end
for _, elem in ipairs(list) do
valid_values[elem] = elem
end
return function(value)
return valid_values[value]
end
end
function osm2pgsql.make_clean_tags_func(keys)
local keys_to_delete = {}
local prefixes_to_delete = {}
local suffixes_to_delete = {}
for _, k in ipairs(keys) do
if k:sub(-1) == '*' then
prefixes_to_delete[#prefixes_to_delete + 1] = k:sub(1, -2)
elseif k:sub(1, 1) == '*' then
suffixes_to_delete[#suffixes_to_delete + 1] = k:sub(2)
else
keys_to_delete[#keys_to_delete + 1] = k
end
end
return function(tags)
for _, k in ipairs(keys_to_delete) do
tags[k] = nil
end
if next(tags) == nil then
return true
end
for tag, _ in pairs(tags) do
for _, k in ipairs(prefixes_to_delete) do
if osm2pgsql.has_prefix(tag, k) then
tags[tag] = nil
break
end
end
end
for tag, _ in pairs(tags) do
for _, k in ipairs(suffixes_to_delete) do
if osm2pgsql.has_suffix(tag, k) then
tags[tag] = nil
break
end
end
end
return next(tags) == nil
end
end
-- from http://lua-users.org/wiki/StringTrim
function osm2pgsql.trim(str)
if str == nil then
return nil
end
local from = str:match("^%s*()")
return from > #str and "" or str:match(".*%S", from)
end
function osm2pgsql.split_unit(str, default_unit)
if str == nil then
return nil
end
local val, unit = string.match(str, "^(-?[0-9.]+) ?(%a*)$")
if val == nil then
return nil
end
if unit == '' then
unit = default_unit
end
val = tonumber(val)
return val, unit
end
function osm2pgsql.split_string(str, separator)
local result = {}
if str ~= nil then
local pattern = '([^' .. (separator or ';') .. ']+)'
for w in string.gmatch(str, pattern) do
result[#result + 1] = osm2pgsql.trim(w)
end
end
return result
end
if osm2pgsql.OSMObject then
osm2pgsql.OSMObject.__index.grab_tag = function(data, tag)
if not tag then
error("Missing tag key", 2)
end
local v = data.tags[tag]
data.tags[tag] = nil
return v
end
end
-- This is used to iterate over (multi)geometries.
function osm2pgsql.Geometry.geometries(geom)
local i = 0
local max = #geom
return function()
i = i + 1
if i <= max then return geom:geometry_n(i) end
end
end