1 -- Core functions for Nominatim import flex style.
10 local ADDRESS_TAGS = nil
11 local SAVE_EXTRA_MAINS = false
12 local POSTCODE_FALLBACK = true
15 -- The single place table.
16 local place_table = osm2pgsql.define_table{
18 ids = { type = 'any', id_column = 'osm_id', type_column = 'osm_type' },
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 },
31 ------------ Geometry functions for relations ---------------------
33 function module.relation_as_multipolygon(o)
34 return o:as_multipolygon()
37 function module.relation_as_multiline(o)
38 return o:as_multilinestring():line_merge()
42 module.RELATION_TYPES = {
43 multipolygon = module.relation_as_multipolygon,
44 boundary = module.relation_as_multipolygon,
45 waterway = module.relation_as_multiline
48 ------------- Place class ------------------------------------------
53 function Place.new(object, geom_func)
54 local self = setmetatable({}, Place)
56 self.geom_func = geom_func
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
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
80 self.object.tags[k] = nil
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
95 function Place:grab_extratags(data)
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
111 local function strip_address_prefix(k)
112 if k:sub(1, 5) == 'addr:' then
116 if k:sub(1, 6) == 'is_in:' then
124 function Place:grab_address_parts(data)
127 if data.groups ~= nil then
128 for k, v in pairs(self.object.tags) do
129 local atype = data.groups(k, v)
132 if atype == 'main' then
134 self.address[strip_address_prefix(k)] = v
136 elseif atype == 'extra' then
137 self.address[strip_address_prefix(k)] = v
139 self.address[atype] = v
141 self.object.tags[k] = nil
150 function Place:grab_name_parts(data)
153 if data.groups ~= nil then
154 for k, v in pairs(self.object.tags) do
155 local atype = data.groups(k, v)
159 self.object.tags[k] = nil
160 if atype == 'main' then
162 elseif atype == 'house' then
164 fallback = {'place', 'house', 'always'}
174 function Place:write_place(k, v, mtype, save_extra_mains)
179 v = v or self.object.tags[k]
184 if type(mtype) == 'table' then
185 mtype = mtype[v] or mtype[1]
188 if mtype == 'always' or (self.has_name and mtype == 'named') then
189 return self:write_row(k, v, save_extra_mains)
192 if mtype == 'named_with_key' then
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
203 if next(names) ~= nil then
204 local saved_names = self.names
207 local results = self:write_row(k, v, save_extra_mains)
209 self.names = saved_names
218 function Place:write_row(k, v, save_extra_mains)
219 if self.geometry == nil then
220 self.geometry = self.geom_func(self.object)
222 if self.geometry:is_null() then
226 if save_extra_mains then
227 for extra_k, extra_v in pairs(self.object.tags) do
229 self.extratags[extra_k] = extra_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
244 if save_extra_mains then
245 for k, v in pairs(self.object.tags) do
246 self.extratags[k] = nil
250 self.num_entries = self.num_entries + 1
256 function module.tag_match(data)
257 if data == nil or next(data) == nil then
261 local fullmatches = {}
262 local key_prefixes = {}
263 local key_suffixes = {}
265 if data.keys ~= nil then
266 for _, key in pairs(data.keys) do
267 if key:sub(1, 1) == '*' then
269 if key_suffixes[#key - 1] == nil then
270 key_suffixes[#key - 1] = {}
272 key_suffixes[#key - 1][key:sub(2)] = true
274 elseif key:sub(#key, #key) == '*' then
275 if key_prefixes[#key - 1] == nil then
276 key_prefixes[#key - 1] = {}
278 key_prefixes[#key - 1][key:sub(1, #key - 1)] = true
280 fullmatches[key] = true
285 if data.tags ~= nil then
286 for k, vlist in pairs(data.tags) do
287 if fullmatches[k] == nil then
289 for _, v in pairs(vlist) do
290 fullmatches[k][v] = true
296 return function (k, v)
297 if fullmatches[k] ~= nil and (fullmatches[k] == true or fullmatches[k][v] ~= nil) then
301 for slen, slist in pairs(key_suffixes) do
302 if #k >= slen and slist[k:sub(-slen)] ~= nil then
307 for slen, slist in pairs(key_prefixes) do
308 if #k >= slen and slist[k:sub(1, slen)] ~= nil then
318 function module.tag_group(data)
319 if data == nil or next(data) == nil then
323 local fullmatches = {}
324 local key_prefixes = {}
325 local key_suffixes = {}
327 for group, tags in pairs(data) do
328 for _, key in pairs(tags) do
329 if key:sub(1, 1) == '*' then
331 if key_suffixes[#key - 1] == nil then
332 key_suffixes[#key - 1] = {}
334 key_suffixes[#key - 1][key:sub(2)] = group
336 elseif key:sub(#key, #key) == '*' then
337 if key_prefixes[#key - 1] == nil then
338 key_prefixes[#key - 1] = {}
340 key_prefixes[#key - 1][key:sub(1, #key - 1)] = group
342 fullmatches[key] = group
347 return function (k, v)
348 local val = fullmatches[k]
353 for slen, slist in pairs(key_suffixes) do
355 val = slist[k:sub(-slen)]
362 for slen, slist in pairs(key_prefixes) do
364 val = slist[k:sub(1, slen)]
373 -- Process functions for all data types
374 function module.process_node(object)
376 local function geom_func(o)
380 module.process_tags(Place.new(object, geom_func))
383 function module.process_way(object)
385 local function geom_func(o)
386 local geom = o:as_polygon()
388 if geom:is_null() then
389 geom = o:as_linestring()
395 module.process_tags(Place.new(object, geom_func))
398 function module.process_relation(object)
399 local geom_func = module.RELATION_TYPES[object.tags.type]
401 if geom_func ~= nil then
402 module.process_tags(Place.new(object, geom_func))
406 -- The process functions are used by default by osm2pgsql.
407 osm2pgsql.process_node = module.process_node
408 osm2pgsql.process_way = module.process_way
409 osm2pgsql.process_relation = module.process_relation
411 function module.process_tags(o)
412 o:clean{delete = PRE_DELETE, extra = PRE_EXTRAS}
414 -- Exception for boundary/place double tagging
415 if o.object.tags.boundary == 'administrative' then
416 o:grab_extratags{match = function (k, v)
417 return k == 'place' and v:sub(1,3) ~= 'isl'
422 local fallback = o:grab_name_parts{groups=NAMES}
425 if o:grab_address_parts{groups=ADDRESS_TAGS} > 0 and fallback == nil then
426 fallback = {'place', 'house', 'always'}
428 if o.address.country ~= nil and #o.address.country ~= 2 then
429 o.address['country'] = nil
431 if POSTCODE_FALLBACK and fallback == nil and o.address.postcode ~= nil then
432 fallback = {'place', 'postcode', 'always'}
435 if o.address.interpolation ~= nil then
436 o:write_place('place', 'houses', 'always', SAVE_EXTRA_MAINS)
440 o:clean{delete = POST_DELETE, extra = POST_EXTRAS}
443 for k, v in pairs(o.object.tags) do
444 local ktype = MAIN_KEYS[k]
445 if ktype == 'fallback' then
447 fallback = {k, v, 'named'}
449 elseif ktype ~= nil then
450 o:write_place(k, v, MAIN_KEYS[k], SAVE_EXTRA_MAINS)
454 if fallback ~= nil and o.num_entries == 0 then
455 o:write_place(fallback[1], fallback[2], fallback[3], SAVE_EXTRA_MAINS)
459 --------- Convenience functions for simple style configuration -----------------
462 function module.set_prefilters(data)
463 PRE_DELETE = module.tag_match{keys = data.delete_keys, tags = data.delete_tags}
464 PRE_EXTRAS = module.tag_match{keys = data.extratag_keys,
465 tags = data.extratag_tags}
468 function module.set_main_tags(data)
472 function module.set_name_tags(data)
473 NAMES = module.tag_group(data)
476 function module.set_address_tags(data)
477 if data.postcode_fallback ~= nil then
478 POSTCODE_FALLBACK = data.postcode_fallback
479 data.postcode_fallback = nil
482 ADDRESS_TAGS = module.tag_group(data)
485 function module.set_unused_handling(data)
486 if data.extra_keys == nil and data.extra_tags == nil then
487 POST_DELETE = module.tag_match{data.delete_keys, tags = data.delete_tags}
489 SAVE_EXTRA_MAINS = true
490 elseif data.delete_keys == nil and data.delete_tags == nil then
492 POST_EXTRAS = module.tag_match{data.extra_keys, tags = data.extra_tags}
493 SAVE_EXTRA_MAINS = false
495 error("unused handler can have only 'extra_keys' or 'delete_keys' set.")
499 function set_relation_types(data)
500 module.RELATION_TYPES = {}
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