1 -- Core functions for Nominatim import flex style.
5 -- The single place table.
6 place_table = osm2pgsql.define_table{
8 ids = { type = 'any', id_column = 'osm_id', type_column = 'osm_type' },
10 { column = 'class', type = 'text', not_null = true },
11 { column = 'type', type = 'text', not_null = true },
12 { column = 'admin_level', type = 'smallint' },
13 { column = 'name', type = 'hstore' },
14 { column = 'address', type = 'hstore' },
15 { column = 'extratags', type = 'hstore' },
16 { column = 'geometry', type = 'geometry', projection = 'WGS84', not_null = true },
21 ------------- Place class ------------------------------------------
26 function Place.new(object, geom_func)
27 local self = setmetatable({}, Place)
29 self.geom_func = geom_func
31 self.admin_level = tonumber(self.object:grab_tag('admin_level'))
32 if self.admin_level == nil
33 or self.admin_level <= 0 or self.admin_level > 15
34 or math.floor(self.admin_level) ~= self.admin_level then
47 function Place:delete(data)
48 if data.match ~= nil then
49 for k, v in pairs(self.object.tags) do
50 if data.match(k, v) then
51 self.object.tags[k] = nil
57 function Place:grab_extratags(data)
60 if data.match ~= nil then
61 for k, v in pairs(self.object.tags) do
62 if data.match(k, v) then
63 self.object.tags[k] = nil
73 function Place:grab_address(data)
76 if data.match ~= nil then
77 for k, v in pairs(self.object.tags) do
78 if data.match(k, v) then
79 self.object.tags[k] = nil
81 if data.include_on_name == true then
85 if data.out_key ~= nil then
86 self.address[data.out_key] = v
90 if k:sub(1, 5) == 'addr:' then
91 self.address[k:sub(6)] = v
92 elseif k:sub(1, 6) == 'is_in:' then
93 self.address[k:sub(7)] = v
105 local function strip_address_prefix(k)
106 if k:sub(1, 5) == 'addr:' then
110 if k:sub(1, 6) == 'is_in:' then
118 function Place:grab_address_parts(data)
121 if data.groups ~= nil then
122 for k, v in pairs(self.object.tags) do
123 local atype = data.groups(k, v)
125 if atype == 'main' then
127 self.address[strip_address_prefix(k)] = v
129 elseif atype == 'extra' then
130 self.address[strip_address_prefix(k)] = v
131 elseif atype ~= nil then
132 self.address[atype] = v
140 function Place:grab_name(data)
143 if data.match ~= nil then
144 for k, v in pairs(self.object.tags) do
145 if data.match(k, v) then
146 self.object.tags[k] = nil
148 if data.include_on_name ~= false then
159 function Place:grab_tag(key)
160 return self.object:grab_tag(key)
163 function Place:write_place(k, v, mtype, save_extra_mains)
168 v = v or self.object.tags[k]
173 if type(mtype) == 'table' then
174 mtype = mtype[v] or mtype[1]
177 if mtype == 'always' or (self.has_name and mtype == 'named') then
178 return self:write_row(k, v, save_extra_mains)
181 if mtype == 'named_with_key' then
183 local prefix = k .. ':name'
184 for namek, namev in pairs(self.object.tags) do
185 if namek:sub(1, #prefix) == prefix
186 and (#namek == #prefix
187 or namek:sub(#prefix + 1, #prefix + 1) == ':') then
188 names[namek:sub(#k + 2)] = namev
192 if next(names) ~= nil then
193 local saved_names = self.names
196 local results = self:write_row(k, v, save_extra_mains)
198 self.names = saved_names
207 function Place:write_row(k, v, save_extra_mains)
208 if self.geometry == nil then
209 self.geometry = self.geom_func(self.object)
211 if self.geometry:is_null() then
215 if save_extra_mains then
216 for extra_k, extra_v in pairs(self.object.tags) do
218 self.extratags[extra_k] = extra_v
226 admin_level = self.admin_level,
227 name = next(self.names) and self.names,
228 address = next(self.address) and self.address,
229 extratags = next(self.extratags) and self.extratags,
230 geometry = self.geometry
233 if save_extra_mains then
234 for k, v in pairs(self.object.tags) do
235 self.extratags[k] = nil
239 self.num_entries = self.num_entries + 1
245 function tag_match(data)
246 if data == nil or next(data) == nil then
250 local fullmatches = {}
251 local key_prefixes = {}
252 local key_suffixes = {}
254 if data.keys ~= nil then
255 for _, key in pairs(data.keys) do
256 if key:sub(1, 1) == '*' then
258 if key_suffixes[#key - 1] == nil then
259 key_suffixes[#key - 1] = {}
261 key_suffixes[#key - 1][key:sub(2)] = true
263 elseif key:sub(#key, #key) == '*' then
264 if key_prefixes[#key - 1] == nil then
265 key_prefixes[#key - 1] = {}
267 key_prefixes[#key - 1][key:sub(1, #key - 1)] = true
269 fullmatches[key] = true
274 if data.tags ~= nil then
275 for k, vlist in pairs(data.tags) do
276 if fullmatches[k] == nil then
278 for _, v in pairs(vlist) do
279 fullmatches[k][v] = true
285 return function (k, v)
286 if fullmatches[k] ~= nil and (fullmatches[k] == true or fullmatches[k][v] ~= nil) then
290 for slen, slist in pairs(key_suffixes) do
291 if #k >= slen and slist[k:sub(-slen)] ~= nil then
296 for slen, slist in pairs(key_prefixes) do
297 if #k >= slen and slist[k:sub(1, slen)] ~= nil then
307 function key_group(data)
308 if data == nil or next(data) == nil then
312 local fullmatches = {}
313 local key_prefixes = {}
314 local key_suffixes = {}
316 for group, tags in pairs(data) do
317 for _, key in pairs(tags) 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)] = group
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)] = group
331 fullmatches[key] = group
336 return function (k, v)
337 local val = fullmatches[k]
342 for slen, slist in pairs(key_suffixes) do
344 val = slist[k:sub(-slen)]
351 for slen, slist in pairs(key_prefixes) do
353 val = slist[k:sub(1, slen)]
362 -- Process functions for all data types
363 function osm2pgsql.process_node(object)
365 local function geom_func(o)
369 process_tags(Place.new(object, geom_func))
372 function osm2pgsql.process_way(object)
374 local function geom_func(o)
375 local geom = o:as_polygon()
377 if geom:is_null() then
378 geom = o:as_linestring()
384 process_tags(Place.new(object, geom_func))
387 function relation_as_multipolygon(o)
388 return o:as_multipolygon()
391 function relation_as_multiline(o)
392 return o:as_multilinestring():line_merge()
395 function osm2pgsql.process_relation(object)
396 local geom_func = RELATION_TYPES[object.tags.type]
398 if geom_func ~= nil then
399 process_tags(Place.new(object, geom_func))
403 function process_tags(o)
406 o:delete{match = PRE_DELETE}
407 o:grab_extratags{match = PRE_EXTRAS}
409 -- Exception for boundary/place double tagging
410 if o.object.tags.boundary == 'administrative' then
411 o:grab_extratags{match = function (k, v)
412 return k == 'place' and v:sub(1,3) ~= 'isl'
417 if o:grab_name{match=HOUSENAME_TAGS} > 0 then
418 fallback = {'place', 'house', 'always'}
420 if o:grab_address_parts{groups=ADDRESS_TAGS} > 0 and fallback == nil then
421 fallback = {'place', 'house', 'always'}
423 if o.address.country ~= nil and #o.address.country ~= 2 then
424 o.address['country'] = nil
426 if fallback == nil and o.address.postcode ~= nil then
427 fallback = {'place', 'postcode', 'always'}
430 if o.address.interpolation ~= nil then
431 o:write_place('place', 'houses', 'always', SAVE_EXTRA_MAINS)
436 o:grab_name{match = NAMES}
437 o:grab_name{match = REFS, include_on_name = false}
439 o:delete{match = POST_DELETE}
440 o:grab_extratags{match = 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)