1 -- Core functions for Nominatim import flex style.
8 local POST_DELETE = nil
11 local ADDRESS_TAGS = nil
12 local SAVE_EXTRA_MAINS = false
13 local POSTCODE_FALLBACK = true
15 -- tables required for taginfo
16 module.TAGINFO_MAIN = {keys = {}, delete_tags = {}}
17 module.TAGINFO_NAME_KEYS = {}
18 module.TAGINFO_ADDRESS_KEYS = {}
21 -- The single place table.
22 local place_table = osm2pgsql.define_table{
24 ids = { type = 'any', id_column = 'osm_id', type_column = 'osm_type' },
26 { column = 'class', type = 'text', not_null = true },
27 { column = 'type', type = 'text', not_null = true },
28 { column = 'admin_level', type = 'smallint' },
29 { column = 'name', type = 'hstore' },
30 { column = 'address', type = 'hstore' },
31 { column = 'extratags', type = 'hstore' },
32 { column = 'geometry', type = 'geometry', projection = 'WGS84', not_null = true },
34 data_tablespace = os.getenv("NOMINATIM_TABLESPACE_PLACE_DATA"),
35 index_tablespace = os.getenv("NOMINATIM_TABLESPACE_PLACE_INDEX"),
39 ------------ Geometry functions for relations ---------------------
41 function module.relation_as_multipolygon(o)
42 return o:as_multipolygon()
45 function module.relation_as_multiline(o)
46 return o:as_multilinestring():line_merge()
50 module.RELATION_TYPES = {
51 multipolygon = module.relation_as_multipolygon,
52 boundary = module.relation_as_multipolygon,
53 waterway = module.relation_as_multiline
56 ------------- Place class ------------------------------------------
61 function Place.new(object, geom_func)
62 local self = setmetatable({}, Place)
64 self.geom_func = geom_func
66 self.admin_level = tonumber(self.object:grab_tag('admin_level'))
67 if self.admin_level == nil
68 or self.admin_level <= 0 or self.admin_level > 15
69 or math.floor(self.admin_level) ~= self.admin_level then
82 function Place:clean(data)
83 for k, v in pairs(self.object.tags) do
84 if data.delete ~= nil and data.delete(k, v) then
85 self.object.tags[k] = nil
86 elseif data.extra ~= nil and data.extra(k, v) then
88 self.object.tags[k] = nil
93 function Place:delete(data)
94 if data.match ~= nil then
95 for k, v in pairs(self.object.tags) do
96 if data.match(k, v) then
97 self.object.tags[k] = nil
103 function Place:grab_extratags(data)
106 if data.match ~= nil then
107 for k, v in pairs(self.object.tags) do
108 if data.match(k, v) then
109 self.object.tags[k] = nil
110 self.extratags[k] = v
119 local function strip_address_prefix(k)
120 if k:sub(1, 5) == 'addr:' then
124 if k:sub(1, 6) == 'is_in:' then
132 function Place:grab_address_parts(data)
135 if data.groups ~= nil then
136 for k, v in pairs(self.object.tags) do
137 local atype = data.groups(k, v)
140 if atype == 'main' then
142 self.address[strip_address_prefix(k)] = v
144 elseif atype == 'extra' then
145 self.address[strip_address_prefix(k)] = v
147 self.address[atype] = v
149 self.object.tags[k] = nil
158 function Place:grab_name_parts(data)
161 if data.groups ~= nil then
162 for k, v in pairs(self.object.tags) do
163 local atype = data.groups(k, v)
167 self.object.tags[k] = nil
168 if atype == 'main' then
170 elseif atype == 'house' then
172 fallback = {'place', 'house', 'always'}
182 function Place:write_place(k, v, mtype, save_extra_mains)
187 v = v or self.object.tags[k]
192 if type(mtype) == 'table' then
193 mtype = mtype[v] or mtype[1]
196 if mtype == 'always' or (self.has_name and mtype == 'named') then
197 return self:write_row(k, v, save_extra_mains)
200 if mtype == 'named_with_key' then
202 local prefix = k .. ':name'
203 for namek, namev in pairs(self.object.tags) do
204 if namek:sub(1, #prefix) == prefix
205 and (#namek == #prefix
206 or namek:sub(#prefix + 1, #prefix + 1) == ':') then
207 names[namek:sub(#k + 2)] = namev
211 if next(names) ~= nil then
212 local saved_names = self.names
215 local results = self:write_row(k, v, save_extra_mains)
217 self.names = saved_names
226 function Place:write_row(k, v, save_extra_mains)
227 if self.geometry == nil then
228 self.geometry = self.geom_func(self.object)
230 if self.geometry == nil or self.geometry:is_null() then
234 if save_extra_mains ~= nil then
235 for extra_k, extra_v in pairs(self.object.tags) do
236 if extra_k ~= k and save_extra_mains(extra_k, extra_v) then
237 self.extratags[extra_k] = extra_v
245 admin_level = self.admin_level,
246 name = next(self.names) and self.names,
247 address = next(self.address) and self.address,
248 extratags = next(self.extratags) and self.extratags,
249 geometry = self.geometry
252 if save_extra_mains then
253 for tk, tv in pairs(self.object.tags) do
254 if save_extra_mains(tk, tv) then
255 self.extratags[tk] = nil
260 self.num_entries = self.num_entries + 1
266 function module.tag_match(data)
267 if data == nil or next(data) == nil then
271 local fullmatches = {}
272 local key_prefixes = {}
273 local key_suffixes = {}
275 if data.keys ~= nil then
276 for _, key in pairs(data.keys) do
277 if key:sub(1, 1) == '*' then
279 if key_suffixes[#key - 1] == nil then
280 key_suffixes[#key - 1] = {}
282 key_suffixes[#key - 1][key:sub(2)] = true
284 elseif key:sub(#key, #key) == '*' then
285 if key_prefixes[#key - 1] == nil then
286 key_prefixes[#key - 1] = {}
288 key_prefixes[#key - 1][key:sub(1, #key - 1)] = true
290 fullmatches[key] = true
295 if data.tags ~= nil then
296 for k, vlist in pairs(data.tags) do
297 if fullmatches[k] == nil then
299 for _, v in pairs(vlist) do
300 fullmatches[k][v] = true
306 return function (k, v)
307 if fullmatches[k] ~= nil and (fullmatches[k] == true or fullmatches[k][v] ~= nil) then
311 for slen, slist in pairs(key_suffixes) do
312 if #k >= slen and slist[k:sub(-slen)] ~= nil then
317 for slen, slist in pairs(key_prefixes) do
318 if #k >= slen and slist[k:sub(1, slen)] ~= nil then
328 function module.tag_group(data)
329 if data == nil or next(data) == nil then
333 local fullmatches = {}
334 local key_prefixes = {}
335 local key_suffixes = {}
337 for group, tags in pairs(data) do
338 for _, key in pairs(tags) do
339 if key:sub(1, 1) == '*' then
341 if key_suffixes[#key - 1] == nil then
342 key_suffixes[#key - 1] = {}
344 key_suffixes[#key - 1][key:sub(2)] = group
346 elseif key:sub(#key, #key) == '*' then
347 if key_prefixes[#key - 1] == nil then
348 key_prefixes[#key - 1] = {}
350 key_prefixes[#key - 1][key:sub(1, #key - 1)] = group
352 fullmatches[key] = group
357 return function (k, v)
358 local val = fullmatches[k]
363 for slen, slist in pairs(key_suffixes) do
365 val = slist[k:sub(-slen)]
372 for slen, slist in pairs(key_prefixes) do
374 val = slist[k:sub(1, slen)]
383 -- Returns prefix part of the keys, and reject suffix matching keys
384 local function process_key(key)
385 if key:sub(1, 1) == '*' then
388 if key:sub(#key, #key) == '*' then
389 return key:sub(1, #key - 2)
394 -- Process functions for all data types
395 function module.process_node(object)
397 local function geom_func(o)
401 module.process_tags(Place.new(object, geom_func))
404 function module.process_way(object)
406 local function geom_func(o)
407 local geom = o:as_polygon()
409 if geom:is_null() then
410 geom = o:as_linestring()
411 if not geom:is_null() and geom:length() > 30 then
419 module.process_tags(Place.new(object, geom_func))
422 function module.process_relation(object)
423 local geom_func = module.RELATION_TYPES[object.tags.type]
425 if geom_func ~= nil then
426 module.process_tags(Place.new(object, geom_func))
430 -- The process functions are used by default by osm2pgsql.
431 osm2pgsql.process_node = module.process_node
432 osm2pgsql.process_way = module.process_way
433 osm2pgsql.process_relation = module.process_relation
435 function module.process_tags(o)
436 o:clean{delete = PRE_DELETE, extra = PRE_EXTRAS}
438 -- Exception for boundary/place double tagging
439 if o.object.tags.boundary == 'administrative' then
440 o:grab_extratags{match = function (k, v)
441 return k == 'place' and v:sub(1,3) ~= 'isl'
446 local fallback = o:grab_name_parts{groups=NAMES}
449 if o:grab_address_parts{groups=ADDRESS_TAGS} > 0 and fallback == nil then
450 fallback = {'place', 'house', 'always'}
452 if o.address.country ~= nil and #o.address.country ~= 2 then
453 o.address['country'] = nil
455 if POSTCODE_FALLBACK and fallback == nil and o.address.postcode ~= nil then
456 fallback = {'place', 'postcode', 'always'}
459 if o.address.interpolation ~= nil then
460 o:write_place('place', 'houses', 'always', SAVE_EXTRA_MAINS)
464 o:clean{delete = POST_DELETE}
467 for k, v in pairs(o.object.tags) do
468 local ktype = MAIN_KEYS[k]
469 if ktype == 'fallback' then
471 fallback = {k, v, 'named'}
473 elseif ktype ~= nil then
474 o:write_place(k, v, MAIN_KEYS[k], SAVE_EXTRA_MAINS)
478 if fallback ~= nil and o.num_entries == 0 then
479 o:write_place(fallback[1], fallback[2], fallback[3], SAVE_EXTRA_MAINS)
483 --------- Convenience functions for simple style configuration -----------------
486 function module.set_prefilters(data)
487 PRE_DELETE = module.tag_match{keys = data.delete_keys, tags = data.delete_tags}
488 PRE_EXTRAS = module.tag_match{keys = data.extra_keys,
489 tags = data.extra_tags}
490 module.TAGINFO_MAIN.delete_tags = data.delete_tags
493 function module.set_main_tags(data)
496 for k, _ in pairs(data) do
497 table.insert(keys, k)
499 module.TAGINFO_MAIN.keys = keys
502 function module.set_name_tags(data)
503 NAMES = module.tag_group(data)
505 for _, lst in pairs(data) do
506 for _, k in ipairs(lst) do
507 local key = process_key(k)
509 module.TAGINFO_NAME_KEYS[key] = true
515 function module.set_address_tags(data)
516 if data.postcode_fallback ~= nil then
517 POSTCODE_FALLBACK = data.postcode_fallback
518 data.postcode_fallback = nil
520 ADDRESS_TAGS = module.tag_group(data)
522 for _, lst in pairs(data) do
524 for _, k in ipairs(lst) do
525 local key = process_key(k)
527 module.TAGINFO_ADDRESS_KEYS[key] = true
534 function module.set_unused_handling(data)
535 if data.extra_keys == nil and data.extra_tags == nil then
536 POST_DELETE = module.tag_match{keys = data.delete_keys, tags = data.delete_tags}
537 SAVE_EXTRA_MAINS = function() return true end
538 elseif data.delete_keys == nil and data.delete_tags == nil then
540 SAVE_EXTRA_MAINS = module.tag_match{keys = data.extra_keys, tags = data.extra_tags}
542 error("unused handler can have only 'extra_keys' or 'delete_keys' set.")
546 function module.set_relation_types(data)
547 module.RELATION_TYPES = {}
549 if v == 'multipolygon' then
550 module.RELATION_TYPES[k] = module.relation_as_multipolygon
551 elseif v == 'multiline' then
552 module.RELATION_TYPES[k] = module.relation_as_multiline