1 -- Core functions for Nominatim import flex style.
10 local ADDRESS_TAGS = nil
13 -- The single place table.
14 place_table = osm2pgsql.define_table{
16 ids = { type = 'any', id_column = 'osm_id', type_column = 'osm_type' },
18 { column = 'class', type = 'text', not_null = true },
19 { column = 'type', type = 'text', not_null = true },
20 { column = 'admin_level', type = 'smallint' },
21 { column = 'name', type = 'hstore' },
22 { column = 'address', type = 'hstore' },
23 { column = 'extratags', type = 'hstore' },
24 { column = 'geometry', type = 'geometry', projection = 'WGS84', not_null = true },
29 ------------ Geometry functions for relations ---------------------
31 function module.relation_as_multipolygon(o)
32 return o:as_multipolygon()
35 function module.relation_as_multiline(o)
36 return o:as_multilinestring():line_merge()
40 module.RELATION_TYPES = {
41 multipolygon = module.relation_as_multipolygon,
42 boundary = module.relation_as_multipolygon,
43 waterway = module.relation_as_multiline
46 ------------- Place class ------------------------------------------
51 function Place.new(object, geom_func)
52 local self = setmetatable({}, Place)
54 self.geom_func = geom_func
56 self.admin_level = tonumber(self.object:grab_tag('admin_level'))
57 if self.admin_level == nil
58 or self.admin_level <= 0 or self.admin_level > 15
59 or math.floor(self.admin_level) ~= self.admin_level then
72 function Place:clean(data)
73 for k, v in pairs(self.object.tags) do
74 if data.delete ~= nil and data.delete(k, v) then
75 self.object.tags[k] = nil
76 elseif data.extra ~= nil and data.extra(k, v) then
78 self.object.tags[k] = nil
83 function Place:delete(data)
84 if data.match ~= nil then
85 for k, v in pairs(self.object.tags) do
86 if data.match(k, v) then
87 self.object.tags[k] = nil
93 function Place:grab_extratags(data)
96 if data.match ~= nil then
97 for k, v in pairs(self.object.tags) do
98 if data.match(k, v) then
99 self.object.tags[k] = nil
100 self.extratags[k] = v
109 function Place:grab_address(data)
112 if data.match ~= nil then
113 for k, v in pairs(self.object.tags) do
114 if data.match(k, v) then
115 self.object.tags[k] = nil
117 if data.include_on_name == true then
121 if data.out_key ~= nil then
122 self.address[data.out_key] = v
126 if k:sub(1, 5) == 'addr:' then
127 self.address[k:sub(6)] = v
128 elseif k:sub(1, 6) == 'is_in:' then
129 self.address[k:sub(7)] = v
141 local function strip_address_prefix(k)
142 if k:sub(1, 5) == 'addr:' then
146 if k:sub(1, 6) == 'is_in:' then
154 function Place:grab_address_parts(data)
157 if data.groups ~= nil then
158 for k, v in pairs(self.object.tags) do
159 local atype = data.groups(k, v)
162 if atype == 'main' then
164 self.address[strip_address_prefix(k)] = v
166 elseif atype == 'extra' then
167 self.address[strip_address_prefix(k)] = v
169 self.address[atype] = v
171 self.object.tags[k] = nil
179 function Place:grab_name(data)
182 if data.match ~= nil then
183 for k, v in pairs(self.object.tags) do
184 if data.match(k, v) then
185 self.object.tags[k] = nil
187 if data.include_on_name ~= false then
198 function Place:grab_name_parts(data)
201 if data.groups ~= nil then
202 for k, v in pairs(self.object.tags) do
203 local atype = data.groups(k, v)
207 self.object.tags[k] = nil
208 if atype == 'main' then
210 elseif atype == 'house' then
212 fallback = {'place', 'house', 'always'}
221 function Place:grab_tag(key)
222 return self.object:grab_tag(key)
225 function Place:write_place(k, v, mtype, save_extra_mains)
230 v = v or self.object.tags[k]
235 if type(mtype) == 'table' then
236 mtype = mtype[v] or mtype[1]
239 if mtype == 'always' or (self.has_name and mtype == 'named') then
240 return self:write_row(k, v, save_extra_mains)
243 if mtype == 'named_with_key' then
245 local prefix = k .. ':name'
246 for namek, namev in pairs(self.object.tags) do
247 if namek:sub(1, #prefix) == prefix
248 and (#namek == #prefix
249 or namek:sub(#prefix + 1, #prefix + 1) == ':') then
250 names[namek:sub(#k + 2)] = namev
254 if next(names) ~= nil then
255 local saved_names = self.names
258 local results = self:write_row(k, v, save_extra_mains)
260 self.names = saved_names
269 function Place:write_row(k, v, save_extra_mains)
270 if self.geometry == nil then
271 self.geometry = self.geom_func(self.object)
273 if self.geometry:is_null() then
277 if save_extra_mains then
278 for extra_k, extra_v in pairs(self.object.tags) do
280 self.extratags[extra_k] = extra_v
288 admin_level = self.admin_level,
289 name = next(self.names) and self.names,
290 address = next(self.address) and self.address,
291 extratags = next(self.extratags) and self.extratags,
292 geometry = self.geometry
295 if save_extra_mains then
296 for k, v in pairs(self.object.tags) do
297 self.extratags[k] = nil
301 self.num_entries = self.num_entries + 1
307 function module.tag_match(data)
308 if data == nil or next(data) == nil then
312 local fullmatches = {}
313 local key_prefixes = {}
314 local key_suffixes = {}
316 if data.keys ~= nil then
317 for _, key in pairs(data.keys) do
318 if key:sub(1, 1) == '*' then
320 if key_suffixes[#key - 1] == nil then
321 key_suffixes[#key - 1] = {}
323 key_suffixes[#key - 1][key:sub(2)] = true
325 elseif key:sub(#key, #key) == '*' then
326 if key_prefixes[#key - 1] == nil then
327 key_prefixes[#key - 1] = {}
329 key_prefixes[#key - 1][key:sub(1, #key - 1)] = true
331 fullmatches[key] = true
336 if data.tags ~= nil then
337 for k, vlist in pairs(data.tags) do
338 if fullmatches[k] == nil then
340 for _, v in pairs(vlist) do
341 fullmatches[k][v] = true
347 return function (k, v)
348 if fullmatches[k] ~= nil and (fullmatches[k] == true or fullmatches[k][v] ~= nil) then
352 for slen, slist in pairs(key_suffixes) do
353 if #k >= slen and slist[k:sub(-slen)] ~= nil then
358 for slen, slist in pairs(key_prefixes) do
359 if #k >= slen and slist[k:sub(1, slen)] ~= nil then
369 function module.tag_group(data)
370 if data == nil or next(data) == nil then
374 local fullmatches = {}
375 local key_prefixes = {}
376 local key_suffixes = {}
378 for group, tags in pairs(data) do
379 for _, key in pairs(tags) do
380 if key:sub(1, 1) == '*' then
382 if key_suffixes[#key - 1] == nil then
383 key_suffixes[#key - 1] = {}
385 key_suffixes[#key - 1][key:sub(2)] = group
387 elseif key:sub(#key, #key) == '*' then
388 if key_prefixes[#key - 1] == nil then
389 key_prefixes[#key - 1] = {}
391 key_prefixes[#key - 1][key:sub(1, #key - 1)] = group
393 fullmatches[key] = group
398 return function (k, v)
399 local val = fullmatches[k]
404 for slen, slist in pairs(key_suffixes) do
406 val = slist[k:sub(-slen)]
413 for slen, slist in pairs(key_prefixes) do
415 val = slist[k:sub(1, slen)]
424 -- Process functions for all data types
425 function osm2pgsql.process_node(object)
427 local function geom_func(o)
431 module.process_tags(Place.new(object, geom_func))
434 function osm2pgsql.process_way(object)
436 local function geom_func(o)
437 local geom = o:as_polygon()
439 if geom:is_null() then
440 geom = o:as_linestring()
446 module.process_tags(Place.new(object, geom_func))
449 function osm2pgsql.process_relation(object)
450 local geom_func = module.RELATION_TYPES[object.tags.type]
452 if geom_func ~= nil then
453 module.process_tags(Place.new(object, geom_func))
457 function module.process_tags(o)
458 o:clean{delete = PRE_DELETE, extra = PRE_EXTRAS}
460 -- Exception for boundary/place double tagging
461 if o.object.tags.boundary == 'administrative' then
462 o:grab_extratags{match = function (k, v)
463 return k == 'place' and v:sub(1,3) ~= 'isl'
468 local fallback = o:grab_name_parts{groups=NAMES}
471 if o:grab_address_parts{groups=ADDRESS_TAGS} > 0 and fallback == nil then
472 fallback = {'place', 'house', 'always'}
474 if o.address.country ~= nil and #o.address.country ~= 2 then
475 o.address['country'] = nil
477 if fallback == nil and o.address.postcode ~= nil then
478 fallback = {'place', 'postcode', 'always'}
481 if o.address.interpolation ~= nil then
482 o:write_place('place', 'houses', 'always', SAVE_EXTRA_MAINS)
486 o:clean{delete = POST_DELETE, extra = POST_EXTRAS}
489 for k, v in pairs(o.object.tags) do
490 local ktype = MAIN_KEYS[k]
491 if ktype == 'fallback' then
493 fallback = {k, v, 'named'}
495 elseif ktype ~= nil then
496 o:write_place(k, v, MAIN_KEYS[k], SAVE_EXTRA_MAINS)
500 if fallback ~= nil and o.num_entries == 0 then
501 o:write_place(fallback[1], fallback[2], fallback[3], SAVE_EXTRA_MAINS)
505 --------- Convenience functions for simple style configuration -----------------
508 function module.set_prefilters(data)
509 PRE_DELETE = module.tag_match{keys = data.delete_keys, tags = data.delete_tags}
510 PRE_EXTRAS = module.tag_match{keys = data.extratag_keys,
511 tags = data.extratag_tags}
514 function module.set_main_tags(data)
518 function module.set_name_tags(data)
519 NAMES = module.tag_group(data)
522 function module.set_address_tags(data)
523 ADDRESS_TAGS = module.tag_group(data)
526 function module.set_unused_handling(data)
527 if data.extra_keys == nil and data.extra_tags == nil then
528 POST_DELETE = module.tag_match{data.delete_keys, tags = data.delete_tags}
530 SAVE_EXTRA_MAINS = true
531 elseif data.delete_keys == nil and data.delete_tags == nil then
533 POST_EXTRAS = module.tag_match{data.extra_keys, tags = data.extra_tags}
534 SAVE_EXTRA_MAINS = false
536 error("unused handler can have only 'extra_keys' or 'delete_keys' set.")
540 function set_relation_types(data)
541 module.RELATION_TYPES = {}
543 if v == 'multipolygon' then
544 module.RELATION_TYPES[k] = module.relation_as_multipolygon
545 elseif v == 'multiline' then
546 module.RELATION_TYPES[k] = module.relation_as_multiline