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 },
20 ------------- Place class ------------------------------------------
25 function Place.new(object, geom_func)
26 local self = setmetatable({}, Place)
28 self.geom_func = geom_func
30 self.admin_level = tonumber(self.object:grab_tag('admin_level'))
31 if self.admin_level == nil
32 or self.admin_level <= 0 or self.admin_level > 15
33 or math.floor(self.admin_level) ~= self.admin_level then
46 function Place:delete(data)
47 if data.match ~= nil then
48 for k, v in pairs(self.object.tags) do
49 if data.match(k, v) then
50 self.object.tags[k] = nil
56 function Place:grab_extratags(data)
59 if data.match ~= nil then
60 for k, v in pairs(self.object.tags) do
61 if data.match(k, v) then
62 self.object.tags[k] = nil
72 function Place:grab_address(data)
75 if data.match ~= nil then
76 for k, v in pairs(self.object.tags) do
77 if data.match(k, v) then
78 self.object.tags[k] = nil
80 if data.include_on_name == true then
84 if data.out_key ~= nil then
85 self.address[data.out_key] = v
89 if k:sub(1, 5) == 'addr:' then
90 self.address[k:sub(6)] = v
91 elseif k:sub(1, 6) == 'is_in:' then
92 self.address[k:sub(7)] = v
104 function Place:set_address(key, value)
105 self.address[key] = value
108 function Place:grab_name(data)
111 if data.match ~= nil then
112 for k, v in pairs(self.object.tags) do
113 if data.match(k, v) then
114 self.object.tags[k] = nil
116 if data.include_on_name ~= false then
127 function Place:grab_tag(key)
128 return self.object:grab_tag(key)
131 function Place:tags()
132 return self.object.tags
135 function Place:write_place(k, v, mtype, save_extra_mains)
140 v = v or self.object.tags[k]
145 if type(mtype) == 'table' then
146 mtype = mtype[v] or mtype[1]
149 if mtype == 'always' or (self.has_name and mtype == 'named') then
150 return self:write_row(k, v, save_extra_mains)
153 if mtype == 'named_with_key' then
155 local prefix = k .. ':name'
156 for namek, namev in pairs(self.object.tags) do
157 if namek:sub(1, #prefix) == prefix
158 and (#namek == #prefix
159 or namek:sub(#prefix + 1, #prefix + 1) == ':') then
160 names[namek:sub(#k + 2)] = namev
164 if next(names) ~= nil then
165 local saved_names = self.names
168 local results = self:write_row(k, v, save_extra_mains)
170 self.names = saved_names
179 function Place:write_row(k, v, save_extra_mains)
180 if self.geometry == nil then
181 self.geometry = self.geom_func(self.object)
183 if self.geometry:is_null() then
187 if save_extra_mains then
188 for extra_k, extra_v in pairs(self.object.tags) do
190 self.extratags[extra_k] = extra_v
198 admin_level = self.admin_level,
199 name = next(self.names) and self.names,
200 address = next(self.address) and self.address,
201 extratags = next(self.extratags) and self.extratags,
202 geometry = self.geometry
205 if save_extra_mains then
206 for k, v in pairs(self.object.tags) do
207 self.extratags[k] = nil
211 self.num_entries = self.num_entries + 1
217 function tag_match(data)
218 if data == nil or next(data) == nil then
222 local fullmatches = {}
223 local key_prefixes = {}
224 local key_suffixes = {}
226 if data.keys ~= nil then
227 for _, key in pairs(data.keys) do
228 if key:sub(1, 1) == '*' then
230 if key_suffixes[#key - 1] == nil then
231 key_suffixes[#key - 1] = {}
233 key_suffixes[#key - 1][key:sub(2)] = true
235 elseif key:sub(#key, #key) == '*' then
236 if key_prefixes[#key - 1] == nil then
237 key_prefixes[#key - 1] = {}
239 key_prefixes[#key - 1][key:sub(1, #key - 1)] = true
241 fullmatches[key] = true
246 if data.tags ~= nil then
247 for k, vlist in pairs(data.tags) do
248 if fullmatches[k] == nil then
250 for _, v in pairs(vlist) do
251 fullmatches[k][v] = true
257 return function (k, v)
258 if fullmatches[k] ~= nil and (fullmatches[k] == true or fullmatches[k][v] ~= nil) then
262 for slen, slist in pairs(key_suffixes) do
263 if #k >= slen and slist[k:sub(-slen)] ~= nil then
268 for slen, slist in pairs(key_prefixes) do
269 if #k >= slen and slist[k:sub(1, slen)] ~= nil then
279 -- Process functions for all data types
280 function osm2pgsql.process_node(object)
282 local function geom_func(o)
286 process_tags(Place.new(object, geom_func))
289 function osm2pgsql.process_way(object)
291 local function geom_func(o)
292 local geom = o:as_polygon()
294 if geom:is_null() then
295 geom = o:as_linestring()
301 process_tags(Place.new(object, geom_func))
304 function relation_as_multipolygon(o)
305 return o:as_multipolygon()
308 function relation_as_multiline(o)
309 return o:as_multilinestring():line_merge()
312 function osm2pgsql.process_relation(object)
313 local geom_func = RELATION_TYPES[object.tags.type]
315 if geom_func ~= nil then
316 process_tags(Place.new(object, geom_func))
320 function process_tags(o)
323 o:delete{match = PRE_DELETE}
324 o:grab_extratags{match = PRE_EXTRAS}
326 -- Exception for boundary/place double tagging
327 if o.object.tags.boundary == 'administrative' then
328 o:grab_extratags{match = function (k, v)
329 return k == 'place' and v:sub(1,3) ~= 'isl'
334 o:grab_address{match=COUNTRY_TAGS, out_key='country'}
335 if o.address.country ~= nil and #o.address.country ~= 2 then
336 o.address['country'] = nil
338 if o:grab_name{match=HOUSENAME_TAGS} > 0 then
339 fallback = {'place', 'house'}
341 if o:grab_address{match=HOUSENUMBER_TAGS, include_on_name = true} > 0 and fallback == nil then
342 fallback = {'place', 'house'}
344 if o:grab_address{match=POSTCODES, out_key='postcode'} > 0 and fallback == nil then
345 fallback = {'place', 'postcode'}
348 local is_interpolation = o:grab_address{match=INTERPOLATION_TAGS} > 0
350 o:grab_address{match=ADDRESS_TAGS}
352 if is_interpolation then
353 o:write_place('place', 'houses', 'always', SAVE_EXTRA_MAINS)
358 o:grab_name{match = NAMES}
359 o:grab_name{match = REFS, include_on_name = false}
361 o:delete{match = POST_DELETE}
362 o:grab_extratags{match = POST_EXTRAS}
366 for k, v in pairs(o:tags()) do
367 num_mains = num_mains + o:write_place(k, v, MAIN_KEYS[k], SAVE_EXTRA_MAINS)
370 if num_mains == 0 then
371 for tag, mtype in pairs(MAIN_FALLBACK_KEYS) do
372 if o:write_place(tag, nil, mtype, SAVE_EXTRA_MAINS) > 0 then
377 if fallback ~= nil then
378 o:write_place(fallback[1], fallback[2], 'always', SAVE_EXTRA_MAINS)