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
224 if data.keys ~= nil then
225 for _, key in pairs(data.keys) do
226 if key:sub(1, 1) == '*' then
228 local suffix = key:sub(2)
229 tests[#tests + 1] = function (k, v)
230 return k:sub(-#suffix) == suffix
233 elseif key:sub(#key, #key) == '*' then
234 local prefix = key:sub(1, #key - 1)
235 tests[#tests + 1] = function (k, v)
236 return k:sub(1, #prefix) == prefix
239 tests[#tests + 1] = function (k, v)
246 if data.tags ~= nil then
248 for k, vlist in pairs(data.tags) do
250 for _, v in pairs(vlist) do
254 tests[#tests + 1] = function (k, v)
255 return tags[k] ~= nil and tags[k][v] ~= nil
259 return function (k, v)
260 for _, func in pairs(tests) do
270 -- Process functions for all data types
271 function osm2pgsql.process_node(object)
273 local function geom_func(o)
277 process_tags(Place.new(object, geom_func))
280 function osm2pgsql.process_way(object)
282 local function geom_func(o)
283 local geom = o:as_polygon()
285 if geom:is_null() then
286 geom = o:as_linestring()
292 process_tags(Place.new(object, geom_func))
295 function relation_as_multipolygon(o)
296 return o:as_multipolygon()
299 function relation_as_multiline(o)
300 return o:as_multilinestring():line_merge()
303 function osm2pgsql.process_relation(object)
304 local geom_func = RELATION_TYPES[object.tags.type]
306 if geom_func ~= nil then
307 process_tags(Place.new(object, geom_func))
311 function process_tags(o)
314 o:delete{match = PRE_DELETE}
315 o:grab_extratags{match = PRE_EXTRAS}
317 -- Exception for boundary/place double tagging
318 if o.object.tags.boundary == 'administrative' then
319 o:grab_extratags{match = function (k, v)
320 return k == 'place' and v:sub(1,3) ~= 'isl'
325 o:grab_address{match=function (k, v) return COUNTRY_TAGS(k, v) and #v == 2 end,
327 if o:grab_name{match=HOUSENAME_TAGS} > 0 then
328 fallback = {'place', 'house'}
330 if o:grab_address{match=HOUSENUMBER_TAGS, include_on_name = true} > 0 and fallback == nil then
331 fallback = {'place', 'house'}
333 if o:grab_address{match=POSTCODES, out_key='postcode'} > 0 and fallback == nil then
334 fallback = {'place', 'postcode'}
337 local is_interpolation = o:grab_address{match=INTERPOLATION_TAGS} > 0
339 if ADD_TIGER_COUNTY then
340 local v = o:grab_tag('tiger:county')
342 v, num = v:gsub(',.*', ' county')
346 o:set_address('tiger:county', v)
349 o:grab_address{match=ADDRESS_TAGS}
351 if is_interpolation then
352 o:write_place('place', 'houses', 'always', SAVE_EXTRA_MAINS)
357 o:grab_name{match = NAMES}
358 o:grab_name{match = REFS, include_on_name = false}
360 o:delete{match = POST_DELETE}
361 o:grab_extratags{match = POST_EXTRAS}
365 for k, v in pairs(o:tags()) do
366 num_mains = num_mains + o:write_place(k, v, MAIN_KEYS[k], SAVE_EXTRA_MAINS)
369 if num_mains == 0 then
370 for tag, mtype in pairs(MAIN_FALLBACK_KEYS) do
371 if o:write_place(tag, nil, mtype, SAVE_EXTRA_MAINS) > 0 then
376 if fallback ~= nil then
377 o:write_place(fallback[1], fallback[2], 'always', SAVE_EXTRA_MAINS)