]> git.openstreetmap.org Git - nominatim.git/blob - settings/flex-base.lua
CI: completely remove ubuntu 18
[nominatim.git] / settings / flex-base.lua
1 -- Core functions for Nominatim import flex style.
2 --
3
4 local module = {}
5
6 local PRE_DELETE = nil
7 local PRE_EXTRAS = nil
8 local MAIN_KEYS = nil
9 local NAMES = nil
10 local ADDRESS_TAGS = nil
11 local SAVE_EXTRA_MAINS = false
12 local POSTCODE_FALLBACK = true
13
14
15 -- The single place table.
16 local place_table = osm2pgsql.define_table{
17     name = "place",
18     ids = { type = 'any', id_column = 'osm_id', type_column = 'osm_type' },
19     columns = {
20         { column = 'class', type = 'text', not_null = true },
21         { column = 'type', type = 'text', not_null = true },
22         { column = 'admin_level', type = 'smallint' },
23         { column = 'name', type = 'hstore' },
24         { column = 'address', type = 'hstore' },
25         { column = 'extratags', type = 'hstore' },
26         { column = 'geometry', type = 'geometry', projection = 'WGS84', not_null = true },
27     },
28     indexes = {}
29 }
30
31 ------------ Geometry functions for relations ---------------------
32
33 function module.relation_as_multipolygon(o)
34     return o:as_multipolygon()
35 end
36
37 function module.relation_as_multiline(o)
38     return o:as_multilinestring():line_merge()
39 end
40
41
42 module.RELATION_TYPES = {
43     multipolygon = module.relation_as_multipolygon,
44     boundary = module.relation_as_multipolygon,
45     waterway = module.relation_as_multiline
46 }
47
48 ------------- Place class ------------------------------------------
49
50 local Place = {}
51 Place.__index = Place
52
53 function Place.new(object, geom_func)
54     local self = setmetatable({}, Place)
55     self.object = object
56     self.geom_func = geom_func
57
58     self.admin_level = tonumber(self.object:grab_tag('admin_level'))
59     if self.admin_level == nil
60        or self.admin_level <= 0 or self.admin_level > 15
61        or math.floor(self.admin_level) ~= self.admin_level then
62         self.admin_level = 15
63     end
64
65     self.num_entries = 0
66     self.has_name = false
67     self.names = {}
68     self.address = {}
69     self.extratags = {}
70
71     return self
72 end
73
74 function Place:clean(data)
75     for k, v in pairs(self.object.tags) do
76         if data.delete ~= nil and data.delete(k, v) then
77             self.object.tags[k] = nil
78         elseif data.extra ~= nil and data.extra(k, v) then
79             self.extratags[k] = v
80             self.object.tags[k] = nil
81         end
82     end
83 end
84
85 function Place:delete(data)
86     if data.match ~= nil then
87         for k, v in pairs(self.object.tags) do
88             if data.match(k, v) then
89                 self.object.tags[k] = nil
90             end
91         end
92     end
93 end
94
95 function Place:grab_extratags(data)
96     local count = 0
97
98     if data.match ~= nil then
99         for k, v in pairs(self.object.tags) do
100             if data.match(k, v) then
101                 self.object.tags[k] = nil
102                 self.extratags[k] = v
103                 count = count + 1
104             end
105         end
106     end
107
108     return count
109 end
110
111 local function strip_address_prefix(k)
112     if k:sub(1, 5) == 'addr:' then
113         return k:sub(6)
114     end
115
116     if k:sub(1, 6) == 'is_in:' then
117         return k:sub(7)
118     end
119
120     return k
121 end
122
123
124 function Place:grab_address_parts(data)
125     local count = 0
126
127     if data.groups ~= nil then
128         for k, v in pairs(self.object.tags) do
129             local atype = data.groups(k, v)
130
131             if atype ~= nil then
132                 if atype == 'main' then
133                     self.has_name = true
134                     self.address[strip_address_prefix(k)] = v
135                     count = count + 1
136                 elseif atype == 'extra' then
137                     self.address[strip_address_prefix(k)] = v
138                 else
139                     self.address[atype] = v
140                 end
141                 self.object.tags[k] = nil
142             end
143         end
144     end
145
146     return count
147 end
148
149
150 function Place:grab_name_parts(data)
151     local fallback = nil
152
153     if data.groups ~= nil then
154         for k, v in pairs(self.object.tags) do
155             local atype = data.groups(k, v)
156
157             if atype ~= nil then
158                 self.names[k] = v
159                 self.object.tags[k] = nil
160                 if atype == 'main' then
161                     self.has_name = true
162                 elseif atype == 'house' then
163                     self.has_name = true
164                     fallback = {'place', 'house', 'always'}
165                 end
166             end
167         end
168     end
169
170     return fallback
171 end
172
173
174 function Place:write_place(k, v, mtype, save_extra_mains)
175     if mtype == nil then
176         return 0
177     end
178
179     v = v or self.object.tags[k]
180     if v == nil then
181         return 0
182     end
183
184     if type(mtype) == 'table' then
185         mtype = mtype[v] or mtype[1]
186     end
187
188     if mtype == 'always' or (self.has_name and mtype == 'named') then
189         return self:write_row(k, v, save_extra_mains)
190     end
191
192     if mtype == 'named_with_key' then
193         local names = {}
194         local prefix = k .. ':name'
195         for namek, namev in pairs(self.object.tags) do
196             if namek:sub(1, #prefix) == prefix
197                and (#namek == #prefix
198                     or namek:sub(#prefix + 1, #prefix + 1) == ':') then
199                 names[namek:sub(#k + 2)] = namev
200             end
201         end
202
203         if next(names) ~= nil then
204             local saved_names = self.names
205             self.names = names
206
207             local results = self:write_row(k, v, save_extra_mains)
208
209             self.names = saved_names
210
211             return results
212         end
213     end
214
215     return 0
216 end
217
218 function Place:write_row(k, v, save_extra_mains)
219     if self.geometry == nil then
220         self.geometry = self.geom_func(self.object)
221     end
222     if self.geometry:is_null() then
223         return 0
224     end
225
226     if save_extra_mains ~= nil then
227         for extra_k, extra_v in pairs(self.object.tags) do
228             if extra_k ~= k and save_extra_mains(extra_k, extra_v) then
229                 self.extratags[extra_k] = extra_v
230             end
231         end
232     end
233
234     place_table:insert{
235         class = k,
236         type = v,
237         admin_level = self.admin_level,
238         name = next(self.names) and self.names,
239         address = next(self.address) and self.address,
240         extratags = next(self.extratags) and self.extratags,
241         geometry = self.geometry
242     }
243
244     if save_extra_mains then
245         for k, v in pairs(self.object.tags) do
246             if save_extra_mains(k, v) then
247                 self.extratags[k] = nil
248             end
249         end
250     end
251
252     self.num_entries = self.num_entries + 1
253
254     return 1
255 end
256
257
258 function module.tag_match(data)
259     if data == nil or next(data) == nil then
260         return nil
261     end
262
263     local fullmatches = {}
264     local key_prefixes = {}
265     local key_suffixes = {}
266
267     if data.keys ~= nil then
268         for _, key in pairs(data.keys) do
269             if key:sub(1, 1) == '*' then
270                 if #key > 1 then
271                     if key_suffixes[#key - 1] == nil then
272                         key_suffixes[#key - 1] = {}
273                     end
274                     key_suffixes[#key - 1][key:sub(2)] = true
275                 end
276             elseif key:sub(#key, #key) == '*' then
277                 if key_prefixes[#key - 1] == nil then
278                     key_prefixes[#key - 1] = {}
279                 end
280                 key_prefixes[#key - 1][key:sub(1, #key - 1)] = true
281             else
282                 fullmatches[key] = true
283             end
284         end
285     end
286
287     if data.tags ~= nil then
288         for k, vlist in pairs(data.tags) do
289             if fullmatches[k] == nil then
290                 fullmatches[k] = {}
291                 for _, v in pairs(vlist) do
292                     fullmatches[k][v] = true
293                 end
294             end
295         end
296     end
297
298     return function (k, v)
299         if fullmatches[k] ~= nil and (fullmatches[k] == true or fullmatches[k][v] ~= nil) then
300             return true
301         end
302
303         for slen, slist in pairs(key_suffixes) do
304             if #k >= slen and slist[k:sub(-slen)] ~= nil then
305                 return true
306             end
307         end
308
309         for slen, slist in pairs(key_prefixes) do
310             if #k >= slen and slist[k:sub(1, slen)] ~= nil then
311                 return true
312             end
313         end
314
315         return false
316     end
317 end
318
319
320 function module.tag_group(data)
321     if data == nil or next(data) == nil then
322         return nil
323     end
324
325     local fullmatches = {}
326     local key_prefixes = {}
327     local key_suffixes = {}
328
329     for group, tags in pairs(data) do
330         for _, key in pairs(tags) do
331             if key:sub(1, 1) == '*' then
332                 if #key > 1 then
333                     if key_suffixes[#key - 1] == nil then
334                         key_suffixes[#key - 1] = {}
335                     end
336                     key_suffixes[#key - 1][key:sub(2)] = group
337                 end
338             elseif key:sub(#key, #key) == '*' then
339                 if key_prefixes[#key - 1] == nil then
340                     key_prefixes[#key - 1] = {}
341                 end
342                 key_prefixes[#key - 1][key:sub(1, #key - 1)] = group
343             else
344                 fullmatches[key] = group
345             end
346         end
347     end
348
349     return function (k, v)
350         local val = fullmatches[k]
351         if val ~= nil then
352             return val
353         end
354
355         for slen, slist in pairs(key_suffixes) do
356             if #k >= slen then
357                 val = slist[k:sub(-slen)]
358                 if val ~= nil then
359                     return val
360                 end
361             end
362         end
363
364         for slen, slist in pairs(key_prefixes) do
365             if #k >= slen then
366                 val = slist[k:sub(1, slen)]
367                 if val ~= nil then
368                     return val
369                 end
370             end
371         end
372     end
373 end
374
375 -- Process functions for all data types
376 function module.process_node(object)
377
378     local function geom_func(o)
379         return o:as_point()
380     end
381
382     module.process_tags(Place.new(object, geom_func))
383 end
384
385 function module.process_way(object)
386
387     local function geom_func(o)
388         local geom = o:as_polygon()
389
390         if geom:is_null() then
391             geom = o:as_linestring()
392         end
393
394         return geom
395     end
396
397     module.process_tags(Place.new(object, geom_func))
398 end
399
400 function module.process_relation(object)
401     local geom_func = module.RELATION_TYPES[object.tags.type]
402
403     if geom_func ~= nil then
404         module.process_tags(Place.new(object, geom_func))
405     end
406 end
407
408 -- The process functions are used by default by osm2pgsql.
409 osm2pgsql.process_node = module.process_node
410 osm2pgsql.process_way = module.process_way
411 osm2pgsql.process_relation = module.process_relation
412
413 function module.process_tags(o)
414     o:clean{delete = PRE_DELETE, extra = PRE_EXTRAS}
415
416     -- Exception for boundary/place double tagging
417     if o.object.tags.boundary == 'administrative' then
418         o:grab_extratags{match = function (k, v)
419             return k == 'place' and v:sub(1,3) ~= 'isl'
420         end}
421     end
422
423     -- name keys
424     local fallback = o:grab_name_parts{groups=NAMES}
425
426     -- address keys
427     if o:grab_address_parts{groups=ADDRESS_TAGS} > 0 and fallback == nil then
428         fallback = {'place', 'house', 'always'}
429     end
430     if o.address.country ~= nil and #o.address.country ~= 2 then
431         o.address['country'] = nil
432     end
433     if POSTCODE_FALLBACK and fallback == nil and o.address.postcode ~= nil then
434         fallback = {'place', 'postcode', 'always'}
435     end
436
437     if o.address.interpolation ~= nil then
438         o:write_place('place', 'houses', 'always', SAVE_EXTRA_MAINS)
439         return
440     end
441
442     o:clean{delete = POST_DELETE}
443
444     -- collect main keys
445     for k, v in pairs(o.object.tags) do
446         local ktype = MAIN_KEYS[k]
447         if ktype == 'fallback' then
448             if o.has_name then
449                 fallback = {k, v, 'named'}
450             end
451         elseif ktype ~= nil then
452             o:write_place(k, v, MAIN_KEYS[k], SAVE_EXTRA_MAINS)
453         end
454     end
455
456     if fallback ~= nil and o.num_entries == 0 then
457         o:write_place(fallback[1], fallback[2], fallback[3], SAVE_EXTRA_MAINS)
458     end
459 end
460
461 --------- Convenience functions for simple style configuration -----------------
462
463
464 function module.set_prefilters(data)
465     PRE_DELETE = module.tag_match{keys = data.delete_keys, tags = data.delete_tags}
466     PRE_EXTRAS = module.tag_match{keys = data.extratag_keys,
467                                   tags = data.extratag_tags}
468 end
469
470 function module.set_main_tags(data)
471     MAIN_KEYS = data
472 end
473
474 function module.set_name_tags(data)
475     NAMES = module.tag_group(data)
476 end
477
478 function module.set_address_tags(data)
479     if data.postcode_fallback ~= nil then
480         POSTCODE_FALLBACK = data.postcode_fallback
481         data.postcode_fallback = nil
482     end
483
484     ADDRESS_TAGS = module.tag_group(data)
485 end
486
487 function module.set_unused_handling(data)
488     if data.extra_keys == nil and data.extra_tags == nil then
489         POST_DELETE = module.tag_match{keys = data.delete_keys, tags = data.delete_tags}
490         SAVE_EXTRA_MAINS = function() return true end
491     elseif data.delete_keys == nil and data.delete_tags == nil then
492         POST_DELETE = nil
493         SAVE_EXTRA_MAINS = module.tag_match{keys = data.extra_keys, tags = data.extra_tags}
494     else
495         error("unused handler can have only 'extra_keys' or 'delete_keys' set.")
496     end
497 end
498
499 function set_relation_types(data)
500     module.RELATION_TYPES = {}
501     for k, v in data do
502         if v == 'multipolygon' then
503             module.RELATION_TYPES[k] = module.relation_as_multipolygon
504         elseif v == 'multiline' then
505             module.RELATION_TYPES[k] = module.relation_as_multiline
506         end
507     end
508 end
509
510 return module