1 -- Core functions for Nominatim import flex style.
10 local ADDRESS_TAGS = nil
11 local SAVE_EXTRA_MAINS = false
12 local POSTCODE_FALLBACK = true
14 -- tables required for taginfo
15 module.TAGINFO_MAIN = {keys = {}, delete_tags = {}}
16 module.TAGINFO_NAME_KEYS = {}
17 module.TAGINFO_ADDRESS_KEYS = {}
20 -- The single place table.
21 local place_table = osm2pgsql.define_table{
23 ids = { type = 'any', id_column = 'osm_id', type_column = 'osm_type' },
25 { column = 'class', type = 'text', not_null = true },
26 { column = 'type', type = 'text', not_null = true },
27 { column = 'admin_level', type = 'smallint' },
28 { column = 'name', type = 'hstore' },
29 { column = 'address', type = 'hstore' },
30 { column = 'extratags', type = 'hstore' },
31 { column = 'geometry', type = 'geometry', projection = 'WGS84', not_null = true },
33 data_tablespace = os.getenv("NOMINATIM_TABLESPACE_PLACE_DATA"),
34 index_tablespace = os.getenv("NOMINATIM_TABLESPACE_PLACE_INDEX"),
38 ------------ Geometry functions for relations ---------------------
40 function module.relation_as_multipolygon(o)
41 return o:as_multipolygon()
44 function module.relation_as_multiline(o)
45 return o:as_multilinestring():line_merge()
49 module.RELATION_TYPES = {
50 multipolygon = module.relation_as_multipolygon,
51 boundary = module.relation_as_multipolygon,
52 waterway = module.relation_as_multiline
55 ------------- Place class ------------------------------------------
60 function Place.new(object, geom_func)
61 local self = setmetatable({}, Place)
63 self.geom_func = geom_func
65 self.admin_level = tonumber(self.object:grab_tag('admin_level'))
66 if self.admin_level == nil
67 or self.admin_level <= 0 or self.admin_level > 15
68 or math.floor(self.admin_level) ~= self.admin_level then
81 function Place:clean(data)
82 for k, v in pairs(self.object.tags) do
83 if data.delete ~= nil and data.delete(k, v) then
84 self.object.tags[k] = nil
85 elseif data.extra ~= nil and data.extra(k, v) then
87 self.object.tags[k] = nil
92 function Place:delete(data)
93 if data.match ~= nil then
94 for k, v in pairs(self.object.tags) do
95 if data.match(k, v) then
96 self.object.tags[k] = nil
102 function Place:grab_extratags(data)
105 if data.match ~= nil then
106 for k, v in pairs(self.object.tags) do
107 if data.match(k, v) then
108 self.object.tags[k] = nil
109 self.extratags[k] = v
118 local function strip_address_prefix(k)
119 if k:sub(1, 5) == 'addr:' then
123 if k:sub(1, 6) == 'is_in:' then
131 function Place:grab_address_parts(data)
134 if data.groups ~= nil then
135 for k, v in pairs(self.object.tags) do
136 local atype = data.groups(k, v)
139 if atype == 'main' then
141 self.address[strip_address_prefix(k)] = v
143 elseif atype == 'extra' then
144 self.address[strip_address_prefix(k)] = v
146 self.address[atype] = v
148 self.object.tags[k] = nil
157 function Place:grab_name_parts(data)
160 if data.groups ~= nil then
161 for k, v in pairs(self.object.tags) do
162 local atype = data.groups(k, v)
166 self.object.tags[k] = nil
167 if atype == 'main' then
169 elseif atype == 'house' then
171 fallback = {'place', 'house', 'always'}
181 function Place:write_place(k, v, mtype, save_extra_mains)
186 v = v or self.object.tags[k]
191 if type(mtype) == 'table' then
192 mtype = mtype[v] or mtype[1]
195 if mtype == 'always' or (self.has_name and mtype == 'named') then
196 return self:write_row(k, v, save_extra_mains)
199 if mtype == 'named_with_key' then
201 local prefix = k .. ':name'
202 for namek, namev in pairs(self.object.tags) do
203 if namek:sub(1, #prefix) == prefix
204 and (#namek == #prefix
205 or namek:sub(#prefix + 1, #prefix + 1) == ':') then
206 names[namek:sub(#k + 2)] = namev
210 if next(names) ~= nil then
211 local saved_names = self.names
214 local results = self:write_row(k, v, save_extra_mains)
216 self.names = saved_names
225 function Place:write_row(k, v, save_extra_mains)
226 if self.geometry == nil then
227 self.geometry = self.geom_func(self.object)
229 if self.geometry:is_null() then
233 if save_extra_mains ~= nil then
234 for extra_k, extra_v in pairs(self.object.tags) do
235 if extra_k ~= k and save_extra_mains(extra_k, extra_v) then
236 self.extratags[extra_k] = extra_v
244 admin_level = self.admin_level,
245 name = next(self.names) and self.names,
246 address = next(self.address) and self.address,
247 extratags = next(self.extratags) and self.extratags,
248 geometry = self.geometry
251 if save_extra_mains then
252 for k, v in pairs(self.object.tags) do
253 if save_extra_mains(k, v) then
254 self.extratags[k] = nil
259 self.num_entries = self.num_entries + 1
265 function module.tag_match(data)
266 if data == nil or next(data) == nil then
270 local fullmatches = {}
271 local key_prefixes = {}
272 local key_suffixes = {}
274 if data.keys ~= nil then
275 for _, key in pairs(data.keys) do
276 if key:sub(1, 1) == '*' then
278 if key_suffixes[#key - 1] == nil then
279 key_suffixes[#key - 1] = {}
281 key_suffixes[#key - 1][key:sub(2)] = true
283 elseif key:sub(#key, #key) == '*' then
284 if key_prefixes[#key - 1] == nil then
285 key_prefixes[#key - 1] = {}
287 key_prefixes[#key - 1][key:sub(1, #key - 1)] = true
289 fullmatches[key] = true
294 if data.tags ~= nil then
295 for k, vlist in pairs(data.tags) do
296 if fullmatches[k] == nil then
298 for _, v in pairs(vlist) do
299 fullmatches[k][v] = true
305 return function (k, v)
306 if fullmatches[k] ~= nil and (fullmatches[k] == true or fullmatches[k][v] ~= nil) then
310 for slen, slist in pairs(key_suffixes) do
311 if #k >= slen and slist[k:sub(-slen)] ~= nil then
316 for slen, slist in pairs(key_prefixes) do
317 if #k >= slen and slist[k:sub(1, slen)] ~= nil then
327 function module.tag_group(data)
328 if data == nil or next(data) == nil then
332 local fullmatches = {}
333 local key_prefixes = {}
334 local key_suffixes = {}
336 for group, tags in pairs(data) do
337 for _, key in pairs(tags) do
338 if key:sub(1, 1) == '*' then
340 if key_suffixes[#key - 1] == nil then
341 key_suffixes[#key - 1] = {}
343 key_suffixes[#key - 1][key:sub(2)] = group
345 elseif key:sub(#key, #key) == '*' then
346 if key_prefixes[#key - 1] == nil then
347 key_prefixes[#key - 1] = {}
349 key_prefixes[#key - 1][key:sub(1, #key - 1)] = group
351 fullmatches[key] = group
356 return function (k, v)
357 local val = fullmatches[k]
362 for slen, slist in pairs(key_suffixes) do
364 val = slist[k:sub(-slen)]
371 for slen, slist in pairs(key_prefixes) do
373 val = slist[k:sub(1, slen)]
382 -- Returns prefix part of the keys, and reject suffix matching keys
383 local function process_key(key)
384 if key:sub(1, 1) == '*' then
387 if key:sub(#key, #key) == '*' then
388 return key:sub(1, #key - 2)
393 -- Process functions for all data types
394 function module.process_node(object)
396 local function geom_func(o)
400 module.process_tags(Place.new(object, geom_func))
403 function module.process_way(object)
405 local function geom_func(o)
406 local geom = o:as_polygon()
408 if geom:is_null() then
409 geom = o:as_linestring()
415 module.process_tags(Place.new(object, geom_func))
418 function module.process_relation(object)
419 local geom_func = module.RELATION_TYPES[object.tags.type]
421 if geom_func ~= nil then
422 module.process_tags(Place.new(object, geom_func))
426 -- The process functions are used by default by osm2pgsql.
427 osm2pgsql.process_node = module.process_node
428 osm2pgsql.process_way = module.process_way
429 osm2pgsql.process_relation = module.process_relation
431 function module.process_tags(o)
432 o:clean{delete = PRE_DELETE, extra = PRE_EXTRAS}
434 -- Exception for boundary/place double tagging
435 if o.object.tags.boundary == 'administrative' then
436 o:grab_extratags{match = function (k, v)
437 return k == 'place' and v:sub(1,3) ~= 'isl'
442 local fallback = o:grab_name_parts{groups=NAMES}
445 if o:grab_address_parts{groups=ADDRESS_TAGS} > 0 and fallback == nil then
446 fallback = {'place', 'house', 'always'}
448 if o.address.country ~= nil and #o.address.country ~= 2 then
449 o.address['country'] = nil
451 if POSTCODE_FALLBACK and fallback == nil and o.address.postcode ~= nil then
452 fallback = {'place', 'postcode', 'always'}
455 if o.address.interpolation ~= nil then
456 o:write_place('place', 'houses', 'always', SAVE_EXTRA_MAINS)
460 o:clean{delete = POST_DELETE}
463 for k, v in pairs(o.object.tags) do
464 local ktype = MAIN_KEYS[k]
465 if ktype == 'fallback' then
467 fallback = {k, v, 'named'}
469 elseif ktype ~= nil then
470 o:write_place(k, v, MAIN_KEYS[k], SAVE_EXTRA_MAINS)
474 if fallback ~= nil and o.num_entries == 0 then
475 o:write_place(fallback[1], fallback[2], fallback[3], SAVE_EXTRA_MAINS)
479 --------- Convenience functions for simple style configuration -----------------
482 function module.set_prefilters(data)
483 PRE_DELETE = module.tag_match{keys = data.delete_keys, tags = data.delete_tags}
484 PRE_EXTRAS = module.tag_match{keys = data.extra_keys,
485 tags = data.extra_tags}
486 module.TAGINFO_MAIN.delete_tags = data.delete_tags
489 function module.set_main_tags(data)
492 for k, _ in pairs(data) do
493 table.insert(keys, k)
495 module.TAGINFO_MAIN.keys = keys
498 function module.set_name_tags(data)
499 NAMES = module.tag_group(data)
501 for _, lst in pairs(data) do
502 for _, k in ipairs(lst) do
503 local key = process_key(k)
505 module.TAGINFO_NAME_KEYS[key] = true
511 function module.set_address_tags(data)
512 if data.postcode_fallback ~= nil then
513 POSTCODE_FALLBACK = data.postcode_fallback
514 data.postcode_fallback = nil
516 ADDRESS_TAGS = module.tag_group(data)
518 for _, lst in pairs(data) do
520 for _, k in ipairs(lst) do
521 local key = process_key(k)
523 module.TAGINFO_ADDRESS_KEYS[key] = true
530 function module.set_unused_handling(data)
531 if data.extra_keys == nil and data.extra_tags == nil then
532 POST_DELETE = module.tag_match{keys = data.delete_keys, tags = data.delete_tags}
533 SAVE_EXTRA_MAINS = function() return true end
534 elseif data.delete_keys == nil and data.delete_tags == nil then
536 SAVE_EXTRA_MAINS = module.tag_match{keys = data.extra_keys, tags = data.extra_tags}
538 error("unused handler can have only 'extra_keys' or 'delete_keys' set.")
542 function set_relation_types(data)
543 module.RELATION_TYPES = {}
545 if v == 'multipolygon' then
546 module.RELATION_TYPES[k] = module.relation_as_multipolygon
547 elseif v == 'multiline' then
548 module.RELATION_TYPES[k] = module.relation_as_multiline