X-Git-Url: https://git.openstreetmap.org./nominatim.git/blobdiff_plain/63a9bc94f767bc6aea2ba0eac52c018a9d09443a..a915815e4d13007c7bf8b04e9bdf410e58cb9125:/settings/flex-base.lua diff --git a/settings/flex-base.lua b/settings/flex-base.lua index 19f4e27b..f508b98a 100644 --- a/settings/flex-base.lua +++ b/settings/flex-base.lua @@ -1,6 +1,7 @@ -- Core functions for Nominatim import flex style. -- +local module = {} -- The single place table. place_table = osm2pgsql.define_table{ @@ -14,7 +15,8 @@ place_table = osm2pgsql.define_table{ { column = 'address', type = 'hstore' }, { column = 'extratags', type = 'hstore' }, { column = 'geometry', type = 'geometry', projection = 'WGS84', not_null = true }, - } + }, + indexes = {} } ------------- Place class ------------------------------------------ @@ -43,6 +45,19 @@ function Place.new(object, geom_func) return self end +function Place:clean(data) + if data.delete ~= nil or data.extra ~= nil then + for k, v in pairs(self.object.tags) do + if data.delete ~= nil and data.delete(k, v) then + self.object.tags[k] = nil + elseif data.extra ~= nil and data.extra(k, v) then + self.extratags[k] = v + self.object.tags[k] = nil + end + end + end +end + function Place:delete(data) if data.match ~= nil then for k, v in pairs(self.object.tags) do @@ -101,8 +116,42 @@ function Place:grab_address(data) return count end -function Place:set_address(key, value) - self.address[key] = value +local function strip_address_prefix(k) + if k:sub(1, 5) == 'addr:' then + return k:sub(6) + end + + if k:sub(1, 6) == 'is_in:' then + return k:sub(7) + end + + return k +end + + +function Place:grab_address_parts(data) + local count = 0 + + if data.groups ~= nil then + for k, v in pairs(self.object.tags) do + local atype = data.groups(k, v) + + if atype ~= nil then + if atype == 'main' then + self.has_name = true + self.address[strip_address_prefix(k)] = v + count = count + 1 + elseif atype == 'extra' then + self.address[strip_address_prefix(k)] = v + else + self.address[atype] = v + end + self.object.tags[k] = nil + end + end + end + + return count end function Place:grab_name(data) @@ -124,12 +173,30 @@ function Place:grab_name(data) return count end -function Place:grab_tag(key) - return self.object:grab_tag(key) +function Place:grab_name_parts(data) + local fallback = nil + + if data.groups ~= nil then + for k, v in pairs(self.object.tags) do + local atype = data.groups(k, v) + + if atype ~= nil then + self.names[k] = v + if atype == 'main' then + self.has_name = true + elseif atype == 'house' then + self.has_name = true + fallback = {'place', 'house', 'always'} + end + end + end + end + + return fallback end -function Place:tags() - return self.object.tags +function Place:grab_tag(key) + return self.object:grab_tag(key) end function Place:write_place(k, v, mtype, save_extra_mains) @@ -214,7 +281,7 @@ function Place:write_row(k, v, save_extra_mains) end -function tag_match(data) +function module.tag_match(data) if data == nil or next(data) == nil then return nil end @@ -276,6 +343,61 @@ function tag_match(data) end +function module.tag_group(data) + if data == nil or next(data) == nil then + return nil + end + + local fullmatches = {} + local key_prefixes = {} + local key_suffixes = {} + + for group, tags in pairs(data) do + for _, key in pairs(tags) do + if key:sub(1, 1) == '*' then + if #key > 1 then + if key_suffixes[#key - 1] == nil then + key_suffixes[#key - 1] = {} + end + key_suffixes[#key - 1][key:sub(2)] = group + end + elseif key:sub(#key, #key) == '*' then + if key_prefixes[#key - 1] == nil then + key_prefixes[#key - 1] = {} + end + key_prefixes[#key - 1][key:sub(1, #key - 1)] = group + else + fullmatches[key] = group + end + end + end + + return function (k, v) + local val = fullmatches[k] + if val ~= nil then + return val + end + + for slen, slist in pairs(key_suffixes) do + if #k >= slen then + val = slist[k:sub(-slen)] + if val ~= nil then + return val + end + end + end + + for slen, slist in pairs(key_prefixes) do + if #k >= slen then + val = slist[k:sub(1, slen)] + if val ~= nil then + return val + end + end + end + end +end + -- Process functions for all data types function osm2pgsql.process_node(object) @@ -283,7 +405,7 @@ function osm2pgsql.process_node(object) return o:as_point() end - process_tags(Place.new(object, geom_func)) + module.process_tags(Place.new(object, geom_func)) end function osm2pgsql.process_way(object) @@ -298,30 +420,27 @@ function osm2pgsql.process_way(object) return geom end - process_tags(Place.new(object, geom_func)) + module.process_tags(Place.new(object, geom_func)) end -function relation_as_multipolygon(o) +function module.relation_as_multipolygon(o) return o:as_multipolygon() end -function relation_as_multiline(o) +function module.relation_as_multiline(o) return o:as_multilinestring():line_merge() end function osm2pgsql.process_relation(object) - local geom_func = RELATION_TYPES[object.tags.type] + local geom_func = module.RELATION_TYPES[object.tags.type] if geom_func ~= nil then - process_tags(Place.new(object, geom_func)) + module.process_tags(Place.new(object, geom_func)) end end -function process_tags(o) - local fallback - - o:delete{match = PRE_DELETE} - o:grab_extratags{match = PRE_EXTRAS} +function module.process_tags(o) + o:clean{delete = module.PRE_DELETE, extra = module.PRE_EXTRAS} -- Exception for boundary/place double tagging if o.object.tags.boundary == 'administrative' then @@ -330,64 +449,51 @@ function process_tags(o) end} end + -- name keys + local fallback = o:grab_name_parts{groups=module.NAMES} + -- address keys - o:grab_address{match=COUNTRY_TAGS, out_key='country'} + if o:grab_address_parts{groups=module.ADDRESS_TAGS} > 0 and fallback == nil then + fallback = {'place', 'house', 'always'} + end if o.address.country ~= nil and #o.address.country ~= 2 then o.address['country'] = nil end - if o:grab_name{match=HOUSENAME_TAGS} > 0 then - fallback = {'place', 'house'} - end - if o:grab_address{match=HOUSENUMBER_TAGS, include_on_name = true} > 0 and fallback == nil then - fallback = {'place', 'house'} + if fallback == nil and o.address.postcode ~= nil then + fallback = {'place', 'postcode', 'always'} end - if o:grab_address{match=POSTCODES, out_key='postcode'} > 0 and fallback == nil then - fallback = {'place', 'postcode'} - end - - local is_interpolation = o:grab_address{match=INTERPOLATION_TAGS} > 0 - if ADD_TIGER_COUNTY then - local v = o:grab_tag('tiger:county') - if v ~= nil then - v, num = v:gsub(',.*', ' county') - if num == 0 then - v = v .. ' county' - end - o:set_address('tiger:county', v) - end - end - o:grab_address{match=ADDRESS_TAGS} - - if is_interpolation then - o:write_place('place', 'houses', 'always', SAVE_EXTRA_MAINS) + if o.address.interpolation ~= nil then + o:write_place('place', 'houses', 'always', module.SAVE_EXTRA_MAINS) return end - -- name keys - o:grab_name{match = NAMES} - o:grab_name{match = REFS, include_on_name = false} - - o:delete{match = POST_DELETE} - o:grab_extratags{match = POST_EXTRAS} + o:clean{delete = module.POST_DELETE, extra = module.POST_EXTRAS} -- collect main keys - local num_mains = 0 - for k, v in pairs(o:tags()) do - num_mains = num_mains + o:write_place(k, v, MAIN_KEYS[k], SAVE_EXTRA_MAINS) - end - - if num_mains == 0 then - for tag, mtype in pairs(MAIN_FALLBACK_KEYS) do - if o:write_place(tag, nil, mtype, SAVE_EXTRA_MAINS) > 0 then - return + for k, v in pairs(o.object.tags) do + local ktype = module.MAIN_KEYS[k] + if ktype == 'fallback' then + if o.has_name then + fallback = {k, v, 'named'} end + elseif ktype ~= nil then + o:write_place(k, v,module.MAIN_KEYS[k], module.SAVE_EXTRA_MAINS) end + end - if fallback ~= nil then - o:write_place(fallback[1], fallback[2], 'always', SAVE_EXTRA_MAINS) - end + if fallback ~= nil and o.num_entries == 0 then + o:write_place(fallback[1], fallback[2], fallback[3], module.SAVE_EXTRA_MAINS) end end +------ defaults -------------- + +module.RELATION_TYPES = { + multipolygon = module.relation_as_multipolygon, + boundary = module.relation_as_multipolygon, + waterway = module.relation_as_multiline +} + +return module