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:clean(data)
48 if data.delete ~= nil or data.extra ~= nil then
49 for k, v in pairs(self.object.tags) do
50 if data.delete ~= nil and data.delete(k, v) then
51 self.object.tags[k] = nil
52 elseif data.extra ~= nil and data.extra(k, v) then
54 self.object.tags[k] = nil
60 function Place:delete(data)
61 if data.match ~= nil then
62 for k, v in pairs(self.object.tags) do
63 if data.match(k, v) then
64 self.object.tags[k] = nil
70 function Place:grab_extratags(data)
73 if data.match ~= nil then
74 for k, v in pairs(self.object.tags) do
75 if data.match(k, v) then
76 self.object.tags[k] = nil
86 function Place:grab_address(data)
89 if data.match ~= nil then
90 for k, v in pairs(self.object.tags) do
91 if data.match(k, v) then
92 self.object.tags[k] = nil
94 if data.include_on_name == true then
98 if data.out_key ~= nil then
99 self.address[data.out_key] = v
103 if k:sub(1, 5) == 'addr:' then
104 self.address[k:sub(6)] = v
105 elseif k:sub(1, 6) == 'is_in:' then
106 self.address[k:sub(7)] = 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)
138 if atype == 'main' then
140 self.address[strip_address_prefix(k)] = v
142 elseif atype == 'extra' then
143 self.address[strip_address_prefix(k)] = v
144 elseif atype ~= nil then
145 self.address[atype] = v
153 function Place:grab_name(data)
156 if data.match ~= nil then
157 for k, v in pairs(self.object.tags) do
158 if data.match(k, v) then
159 self.object.tags[k] = nil
161 if data.include_on_name ~= false then
172 function Place:grab_name_parts(data)
175 if data.groups ~= nil then
176 for k, v in pairs(self.object.tags) do
177 local atype = data.groups(k, v)
181 if atype == 'main' then
183 elseif atype == 'house' then
185 fallback = {'place', 'house', 'always'}
194 function Place:grab_tag(key)
195 return self.object:grab_tag(key)
198 function Place:write_place(k, v, mtype, save_extra_mains)
203 v = v or self.object.tags[k]
208 if type(mtype) == 'table' then
209 mtype = mtype[v] or mtype[1]
212 if mtype == 'always' or (self.has_name and mtype == 'named') then
213 return self:write_row(k, v, save_extra_mains)
216 if mtype == 'named_with_key' then
218 local prefix = k .. ':name'
219 for namek, namev in pairs(self.object.tags) do
220 if namek:sub(1, #prefix) == prefix
221 and (#namek == #prefix
222 or namek:sub(#prefix + 1, #prefix + 1) == ':') then
223 names[namek:sub(#k + 2)] = namev
227 if next(names) ~= nil then
228 local saved_names = self.names
231 local results = self:write_row(k, v, save_extra_mains)
233 self.names = saved_names
242 function Place:write_row(k, v, save_extra_mains)
243 if self.geometry == nil then
244 self.geometry = self.geom_func(self.object)
246 if self.geometry:is_null() then
250 if save_extra_mains then
251 for extra_k, extra_v in pairs(self.object.tags) do
253 self.extratags[extra_k] = extra_v
261 admin_level = self.admin_level,
262 name = next(self.names) and self.names,
263 address = next(self.address) and self.address,
264 extratags = next(self.extratags) and self.extratags,
265 geometry = self.geometry
268 if save_extra_mains then
269 for k, v in pairs(self.object.tags) do
270 self.extratags[k] = nil
274 self.num_entries = self.num_entries + 1
280 function tag_match(data)
281 if data == nil or next(data) == nil then
285 local fullmatches = {}
286 local key_prefixes = {}
287 local key_suffixes = {}
289 if data.keys ~= nil then
290 for _, key in pairs(data.keys) do
291 if key:sub(1, 1) == '*' then
293 if key_suffixes[#key - 1] == nil then
294 key_suffixes[#key - 1] = {}
296 key_suffixes[#key - 1][key:sub(2)] = true
298 elseif key:sub(#key, #key) == '*' then
299 if key_prefixes[#key - 1] == nil then
300 key_prefixes[#key - 1] = {}
302 key_prefixes[#key - 1][key:sub(1, #key - 1)] = true
304 fullmatches[key] = true
309 if data.tags ~= nil then
310 for k, vlist in pairs(data.tags) do
311 if fullmatches[k] == nil then
313 for _, v in pairs(vlist) do
314 fullmatches[k][v] = true
320 return function (k, v)
321 if fullmatches[k] ~= nil and (fullmatches[k] == true or fullmatches[k][v] ~= nil) then
325 for slen, slist in pairs(key_suffixes) do
326 if #k >= slen and slist[k:sub(-slen)] ~= nil then
331 for slen, slist in pairs(key_prefixes) do
332 if #k >= slen and slist[k:sub(1, slen)] ~= nil then
342 function key_group(data)
343 if data == nil or next(data) == nil then
347 local fullmatches = {}
348 local key_prefixes = {}
349 local key_suffixes = {}
351 for group, tags in pairs(data) do
352 for _, key in pairs(tags) do
353 if key:sub(1, 1) == '*' then
355 if key_suffixes[#key - 1] == nil then
356 key_suffixes[#key - 1] = {}
358 key_suffixes[#key - 1][key:sub(2)] = group
360 elseif key:sub(#key, #key) == '*' then
361 if key_prefixes[#key - 1] == nil then
362 key_prefixes[#key - 1] = {}
364 key_prefixes[#key - 1][key:sub(1, #key - 1)] = group
366 fullmatches[key] = group
371 return function (k, v)
372 local val = fullmatches[k]
377 for slen, slist in pairs(key_suffixes) do
379 val = slist[k:sub(-slen)]
386 for slen, slist in pairs(key_prefixes) do
388 val = slist[k:sub(1, slen)]
397 -- Process functions for all data types
398 function osm2pgsql.process_node(object)
400 local function geom_func(o)
404 process_tags(Place.new(object, geom_func))
407 function osm2pgsql.process_way(object)
409 local function geom_func(o)
410 local geom = o:as_polygon()
412 if geom:is_null() then
413 geom = o:as_linestring()
419 process_tags(Place.new(object, geom_func))
422 function relation_as_multipolygon(o)
423 return o:as_multipolygon()
426 function relation_as_multiline(o)
427 return o:as_multilinestring():line_merge()
430 function osm2pgsql.process_relation(object)
431 local geom_func = RELATION_TYPES[object.tags.type]
433 if geom_func ~= nil then
434 process_tags(Place.new(object, geom_func))
438 function process_tags(o)
439 o:clean{delete = PRE_DELETE, extra = PRE_EXTRAS}
441 -- Exception for boundary/place double tagging
442 if o.object.tags.boundary == 'administrative' then
443 o:grab_extratags{match = function (k, v)
444 return k == 'place' and v:sub(1,3) ~= 'isl'
449 local fallback = o:grab_name_parts{groups=NAMES}
452 if o:grab_address_parts{groups=ADDRESS_TAGS} > 0 and fallback == nil then
453 fallback = {'place', 'house', 'always'}
455 if o.address.country ~= nil and #o.address.country ~= 2 then
456 o.address['country'] = nil
458 if fallback == nil and o.address.postcode ~= nil then
459 fallback = {'place', 'postcode', 'always'}
462 if o.address.interpolation ~= nil then
463 o:write_place('place', 'houses', 'always', SAVE_EXTRA_MAINS)
467 o:clean{delete = POST_DELETE, extra = POST_EXTRAS}
470 for k, v in pairs(o.object.tags) do
471 local ktype = MAIN_KEYS[k]
472 if ktype == 'fallback' then
474 fallback = {k, v, 'named'}
476 elseif ktype ~= nil then
477 o:write_place(k, v, MAIN_KEYS[k], SAVE_EXTRA_MAINS)
481 if fallback ~= nil and o.num_entries == 0 then
482 o:write_place(fallback[1], fallback[2], fallback[3], SAVE_EXTRA_MAINS)