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 function Place:set_address(key, value)
106 self.address[key] = value
109 function Place:grab_name(data)
112 if data.match ~= nil then
113 for k, v in pairs(self.object.tags) do
114 if data.match(k, v) then
115 self.object.tags[k] = nil
117 if data.include_on_name ~= false then
128 function Place:grab_tag(key)
129 return self.object:grab_tag(key)
132 function Place:tags()
133 return self.object.tags
136 function Place:write_place(k, v, mtype, save_extra_mains)
141 v = v or self.object.tags[k]
146 if type(mtype) == 'table' then
147 mtype = mtype[v] or mtype[1]
150 if mtype == 'always' or (self.has_name and mtype == 'named') then
151 return self:write_row(k, v, save_extra_mains)
154 if mtype == 'named_with_key' then
156 local prefix = k .. ':name'
157 for namek, namev in pairs(self.object.tags) do
158 if namek:sub(1, #prefix) == prefix
159 and (#namek == #prefix
160 or namek:sub(#prefix + 1, #prefix + 1) == ':') then
161 names[namek:sub(#k + 2)] = namev
165 if next(names) ~= nil then
166 local saved_names = self.names
169 local results = self:write_row(k, v, save_extra_mains)
171 self.names = saved_names
180 function Place:write_row(k, v, save_extra_mains)
181 if self.geometry == nil then
182 self.geometry = self.geom_func(self.object)
184 if self.geometry:is_null() then
188 if save_extra_mains then
189 for extra_k, extra_v in pairs(self.object.tags) do
191 self.extratags[extra_k] = extra_v
199 admin_level = self.admin_level,
200 name = next(self.names) and self.names,
201 address = next(self.address) and self.address,
202 extratags = next(self.extratags) and self.extratags,
203 geometry = self.geometry
206 if save_extra_mains then
207 for k, v in pairs(self.object.tags) do
208 self.extratags[k] = nil
212 self.num_entries = self.num_entries + 1
218 function tag_match(data)
219 if data == nil or next(data) == nil then
223 local fullmatches = {}
224 local key_prefixes = {}
225 local key_suffixes = {}
227 if data.keys ~= nil then
228 for _, key in pairs(data.keys) do
229 if key:sub(1, 1) == '*' then
231 if key_suffixes[#key - 1] == nil then
232 key_suffixes[#key - 1] = {}
234 key_suffixes[#key - 1][key:sub(2)] = true
236 elseif key:sub(#key, #key) == '*' then
237 if key_prefixes[#key - 1] == nil then
238 key_prefixes[#key - 1] = {}
240 key_prefixes[#key - 1][key:sub(1, #key - 1)] = true
242 fullmatches[key] = true
247 if data.tags ~= nil then
248 for k, vlist in pairs(data.tags) do
249 if fullmatches[k] == nil then
251 for _, v in pairs(vlist) do
252 fullmatches[k][v] = true
258 return function (k, v)
259 if fullmatches[k] ~= nil and (fullmatches[k] == true or fullmatches[k][v] ~= nil) then
263 for slen, slist in pairs(key_suffixes) do
264 if #k >= slen and slist[k:sub(-slen)] ~= nil then
269 for slen, slist in pairs(key_prefixes) do
270 if #k >= slen and slist[k:sub(1, slen)] ~= nil then
280 -- Process functions for all data types
281 function osm2pgsql.process_node(object)
283 local function geom_func(o)
287 process_tags(Place.new(object, geom_func))
290 function osm2pgsql.process_way(object)
292 local function geom_func(o)
293 local geom = o:as_polygon()
295 if geom:is_null() then
296 geom = o:as_linestring()
302 process_tags(Place.new(object, geom_func))
305 function relation_as_multipolygon(o)
306 return o:as_multipolygon()
309 function relation_as_multiline(o)
310 return o:as_multilinestring():line_merge()
313 function osm2pgsql.process_relation(object)
314 local geom_func = RELATION_TYPES[object.tags.type]
316 if geom_func ~= nil then
317 process_tags(Place.new(object, geom_func))
321 function process_tags(o)
324 o:delete{match = PRE_DELETE}
325 o:grab_extratags{match = PRE_EXTRAS}
327 -- Exception for boundary/place double tagging
328 if o.object.tags.boundary == 'administrative' then
329 o:grab_extratags{match = function (k, v)
330 return k == 'place' and v:sub(1,3) ~= 'isl'
335 o:grab_address{match=COUNTRY_TAGS, out_key='country'}
336 if o.address.country ~= nil and #o.address.country ~= 2 then
337 o.address['country'] = nil
339 if o:grab_name{match=HOUSENAME_TAGS} > 0 then
340 fallback = {'place', 'house'}
342 if o:grab_address{match=HOUSENUMBER_TAGS, include_on_name = true} > 0 and fallback == nil then
343 fallback = {'place', 'house'}
345 if o:grab_address{match=POSTCODES, out_key='postcode'} > 0 and fallback == nil then
346 fallback = {'place', 'postcode'}
349 local is_interpolation = o:grab_address{match=INTERPOLATION_TAGS} > 0
351 o:grab_address{match=ADDRESS_TAGS}
353 if is_interpolation then
354 o:write_place('place', 'houses', 'always', SAVE_EXTRA_MAINS)
359 o:grab_name{match = NAMES}
360 o:grab_name{match = REFS, include_on_name = false}
362 o:delete{match = POST_DELETE}
363 o:grab_extratags{match = POST_EXTRAS}
367 for k, v in pairs(o:tags()) do
368 num_mains = num_mains + o:write_place(k, v, MAIN_KEYS[k], SAVE_EXTRA_MAINS)
371 if num_mains == 0 then
372 for tag, mtype in pairs(MAIN_FALLBACK_KEYS) do
373 if o:write_place(tag, nil, mtype, SAVE_EXTRA_MAINS) > 0 then
378 if fallback ~= nil then
379 o:write_place(fallback[1], fallback[2], 'always', SAVE_EXTRA_MAINS)