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_name_parts(data)
162 if data.groups ~= nil then
163 for k, v in pairs(self.object.tags) do
164 local atype = data.groups(k, v)
168 if atype == 'main' then
170 elseif atype == 'house' then
172 fallback = {'place', 'house', 'always'}
181 function Place:grab_tag(key)
182 return self.object:grab_tag(key)
185 function Place:write_place(k, v, mtype, save_extra_mains)
190 v = v or self.object.tags[k]
195 if type(mtype) == 'table' then
196 mtype = mtype[v] or mtype[1]
199 if mtype == 'always' or (self.has_name and mtype == 'named') then
200 return self:write_row(k, v, save_extra_mains)
203 if mtype == 'named_with_key' then
205 local prefix = k .. ':name'
206 for namek, namev in pairs(self.object.tags) do
207 if namek:sub(1, #prefix) == prefix
208 and (#namek == #prefix
209 or namek:sub(#prefix + 1, #prefix + 1) == ':') then
210 names[namek:sub(#k + 2)] = namev
214 if next(names) ~= nil then
215 local saved_names = self.names
218 local results = self:write_row(k, v, save_extra_mains)
220 self.names = saved_names
229 function Place:write_row(k, v, save_extra_mains)
230 if self.geometry == nil then
231 self.geometry = self.geom_func(self.object)
233 if self.geometry:is_null() then
237 if save_extra_mains then
238 for extra_k, extra_v in pairs(self.object.tags) do
240 self.extratags[extra_k] = extra_v
248 admin_level = self.admin_level,
249 name = next(self.names) and self.names,
250 address = next(self.address) and self.address,
251 extratags = next(self.extratags) and self.extratags,
252 geometry = self.geometry
255 if save_extra_mains then
256 for k, v in pairs(self.object.tags) do
257 self.extratags[k] = nil
261 self.num_entries = self.num_entries + 1
267 function tag_match(data)
268 if data == nil or next(data) == nil then
272 local fullmatches = {}
273 local key_prefixes = {}
274 local key_suffixes = {}
276 if data.keys ~= nil then
277 for _, key in pairs(data.keys) do
278 if key:sub(1, 1) == '*' then
280 if key_suffixes[#key - 1] == nil then
281 key_suffixes[#key - 1] = {}
283 key_suffixes[#key - 1][key:sub(2)] = true
285 elseif key:sub(#key, #key) == '*' then
286 if key_prefixes[#key - 1] == nil then
287 key_prefixes[#key - 1] = {}
289 key_prefixes[#key - 1][key:sub(1, #key - 1)] = true
291 fullmatches[key] = true
296 if data.tags ~= nil then
297 for k, vlist in pairs(data.tags) do
298 if fullmatches[k] == nil then
300 for _, v in pairs(vlist) do
301 fullmatches[k][v] = true
307 return function (k, v)
308 if fullmatches[k] ~= nil and (fullmatches[k] == true or fullmatches[k][v] ~= nil) then
312 for slen, slist in pairs(key_suffixes) do
313 if #k >= slen and slist[k:sub(-slen)] ~= nil then
318 for slen, slist in pairs(key_prefixes) do
319 if #k >= slen and slist[k:sub(1, slen)] ~= nil then
329 function key_group(data)
330 if data == nil or next(data) == nil then
334 local fullmatches = {}
335 local key_prefixes = {}
336 local key_suffixes = {}
338 for group, tags in pairs(data) do
339 for _, key in pairs(tags) do
340 if key:sub(1, 1) == '*' then
342 if key_suffixes[#key - 1] == nil then
343 key_suffixes[#key - 1] = {}
345 key_suffixes[#key - 1][key:sub(2)] = group
347 elseif key:sub(#key, #key) == '*' then
348 if key_prefixes[#key - 1] == nil then
349 key_prefixes[#key - 1] = {}
351 key_prefixes[#key - 1][key:sub(1, #key - 1)] = group
353 fullmatches[key] = group
358 return function (k, v)
359 local val = fullmatches[k]
364 for slen, slist in pairs(key_suffixes) do
366 val = slist[k:sub(-slen)]
373 for slen, slist in pairs(key_prefixes) do
375 val = slist[k:sub(1, slen)]
384 -- Process functions for all data types
385 function osm2pgsql.process_node(object)
387 local function geom_func(o)
391 process_tags(Place.new(object, geom_func))
394 function osm2pgsql.process_way(object)
396 local function geom_func(o)
397 local geom = o:as_polygon()
399 if geom:is_null() then
400 geom = o:as_linestring()
406 process_tags(Place.new(object, geom_func))
409 function relation_as_multipolygon(o)
410 return o:as_multipolygon()
413 function relation_as_multiline(o)
414 return o:as_multilinestring():line_merge()
417 function osm2pgsql.process_relation(object)
418 local geom_func = RELATION_TYPES[object.tags.type]
420 if geom_func ~= nil then
421 process_tags(Place.new(object, geom_func))
425 function process_tags(o)
426 o:delete{match = PRE_DELETE}
427 o:grab_extratags{match = PRE_EXTRAS}
429 -- Exception for boundary/place double tagging
430 if o.object.tags.boundary == 'administrative' then
431 o:grab_extratags{match = function (k, v)
432 return k == 'place' and v:sub(1,3) ~= 'isl'
437 local fallback = o:grab_name_parts{groups=NAMES}
440 if o:grab_address_parts{groups=ADDRESS_TAGS} > 0 and fallback == nil then
441 fallback = {'place', 'house', 'always'}
443 if o.address.country ~= nil and #o.address.country ~= 2 then
444 o.address['country'] = nil
446 if fallback == nil and o.address.postcode ~= nil then
447 fallback = {'place', 'postcode', 'always'}
450 if o.address.interpolation ~= nil then
451 o:write_place('place', 'houses', 'always', SAVE_EXTRA_MAINS)
455 o:delete{match = POST_DELETE}
456 o:grab_extratags{match = POST_EXTRAS}
459 for k, v in pairs(o.object.tags) do
460 local ktype = MAIN_KEYS[k]
461 if ktype == 'fallback' then
463 fallback = {k, v, 'named'}
465 elseif ktype ~= nil then
466 o:write_place(k, v, MAIN_KEYS[k], SAVE_EXTRA_MAINS)
470 if fallback ~= nil and o.num_entries == 0 then
471 o:write_place(fallback[1], fallback[2], fallback[3], SAVE_EXTRA_MAINS)