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 ~= 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
238 admin_level = self.admin_level,
239 name = next(self.names) and self.names,
240 address = next(self.address) and self.address,
241 extratags = next(self.extratags) and self.extratags,
242 geometry = self.geometry
245 if save_extra_mains then
246 for k, v in pairs(self.object.tags) do
247 if save_extra_mains(k, v) then
248 self.extratags[k] = nil
253 self.num_entries = self.num_entries + 1
259 function module.tag_match(data)
260 if data == nil or next(data) == nil then
264 local fullmatches = {}
265 local key_prefixes = {}
266 local key_suffixes = {}
268 if data.keys ~= nil then
269 for _, key in pairs(data.keys) do
270 if key:sub(1, 1) == '*' then
272 if key_suffixes[#key - 1] == nil then
273 key_suffixes[#key - 1] = {}
275 key_suffixes[#key - 1][key:sub(2)] = true
277 elseif key:sub(#key, #key) == '*' then
278 if key_prefixes[#key - 1] == nil then
279 key_prefixes[#key - 1] = {}
281 key_prefixes[#key - 1][key:sub(1, #key - 1)] = true
283 fullmatches[key] = true
288 if data.tags ~= nil then
289 for k, vlist in pairs(data.tags) do
290 if fullmatches[k] == nil then
292 for _, v in pairs(vlist) do
293 fullmatches[k][v] = true
299 return function (k, v)
300 if fullmatches[k] ~= nil and (fullmatches[k] == true or fullmatches[k][v] ~= nil) then
304 for slen, slist in pairs(key_suffixes) do
305 if #k >= slen and slist[k:sub(-slen)] ~= nil then
310 for slen, slist in pairs(key_prefixes) do
311 if #k >= slen and slist[k:sub(1, slen)] ~= nil then
321 function module.tag_group(data)
322 if data == nil or next(data) == nil then
326 local fullmatches = {}
327 local key_prefixes = {}
328 local key_suffixes = {}
330 for group, tags in pairs(data) do
331 for _, key in pairs(tags) do
332 if key:sub(1, 1) == '*' then
334 if key_suffixes[#key - 1] == nil then
335 key_suffixes[#key - 1] = {}
337 key_suffixes[#key - 1][key:sub(2)] = group
339 elseif key:sub(#key, #key) == '*' then
340 if key_prefixes[#key - 1] == nil then
341 key_prefixes[#key - 1] = {}
343 key_prefixes[#key - 1][key:sub(1, #key - 1)] = group
345 fullmatches[key] = group
350 return function (k, v)
351 local val = fullmatches[k]
356 for slen, slist in pairs(key_suffixes) do
358 val = slist[k:sub(-slen)]
365 for slen, slist in pairs(key_prefixes) do
367 val = slist[k:sub(1, slen)]
376 -- Process functions for all data types
377 function module.process_node(object)
379 local function geom_func(o)
383 module.process_tags(Place.new(object, geom_func))
386 function module.process_way(object)
388 local function geom_func(o)
389 local geom = o:as_polygon()
391 if geom:is_null() then
392 geom = o:as_linestring()
398 module.process_tags(Place.new(object, geom_func))
401 function module.process_relation(object)
402 local geom_func = module.RELATION_TYPES[object.tags.type]
404 if geom_func ~= nil then
405 module.process_tags(Place.new(object, geom_func))
409 -- The process functions are used by default by osm2pgsql.
410 osm2pgsql.process_node = module.process_node
411 osm2pgsql.process_way = module.process_way
412 osm2pgsql.process_relation = module.process_relation
414 function module.process_tags(o)
415 o:clean{delete = PRE_DELETE, extra = PRE_EXTRAS}
417 -- Exception for boundary/place double tagging
418 if o.object.tags.boundary == 'administrative' then
419 o:grab_extratags{match = function (k, v)
420 return k == 'place' and v:sub(1,3) ~= 'isl'
425 local fallback = o:grab_name_parts{groups=NAMES}
428 if o:grab_address_parts{groups=ADDRESS_TAGS} > 0 and fallback == nil then
429 fallback = {'place', 'house', 'always'}
431 if o.address.country ~= nil and #o.address.country ~= 2 then
432 o.address['country'] = nil
434 if POSTCODE_FALLBACK and fallback == nil and o.address.postcode ~= nil then
435 fallback = {'place', 'postcode', 'always'}
438 if o.address.interpolation ~= nil then
439 o:write_place('place', 'houses', 'always', SAVE_EXTRA_MAINS)
443 o:clean{delete = POST_DELETE}
446 for k, v in pairs(o.object.tags) do
447 local ktype = MAIN_KEYS[k]
448 if ktype == 'fallback' then
450 fallback = {k, v, 'named'}
452 elseif ktype ~= nil then
453 o:write_place(k, v, MAIN_KEYS[k], SAVE_EXTRA_MAINS)
457 if fallback ~= nil and o.num_entries == 0 then
458 o:write_place(fallback[1], fallback[2], fallback[3], SAVE_EXTRA_MAINS)
462 --------- Convenience functions for simple style configuration -----------------
465 function module.set_prefilters(data)
466 PRE_DELETE = module.tag_match{keys = data.delete_keys, tags = data.delete_tags}
467 PRE_EXTRAS = module.tag_match{keys = data.extra_keys,
468 tags = data.extra_tags}
471 function module.set_main_tags(data)
475 function module.set_name_tags(data)
476 NAMES = module.tag_group(data)
479 function module.set_address_tags(data)
480 if data.postcode_fallback ~= nil then
481 POSTCODE_FALLBACK = data.postcode_fallback
482 data.postcode_fallback = nil
485 ADDRESS_TAGS = module.tag_group(data)
488 function module.set_unused_handling(data)
489 if data.extra_keys == nil and data.extra_tags == nil then
490 POST_DELETE = module.tag_match{keys = data.delete_keys, tags = data.delete_tags}
491 SAVE_EXTRA_MAINS = function() return true end
492 elseif data.delete_keys == nil and data.delete_tags == nil then
494 SAVE_EXTRA_MAINS = module.tag_match{keys = data.extra_keys, tags = data.extra_tags}
496 error("unused handler can have only 'extra_keys' or 'delete_keys' set.")
500 function set_relation_types(data)
501 module.RELATION_TYPES = {}
503 if v == 'multipolygon' then
504 module.RELATION_TYPES[k] = module.relation_as_multipolygon
505 elseif v == 'multiline' then
506 module.RELATION_TYPES[k] = module.relation_as_multiline