Skip to content

Commit

Permalink
osm2pgsql style: simplify computation of extra tags
Browse files Browse the repository at this point in the history
Now implemented as a simple filter function which can also be
customized by the user.
  • Loading branch information
lonvia committed Dec 6, 2024
1 parent c0a2089 commit b7e3d07
Showing 1 changed file with 49 additions and 31 deletions.
80 changes: 49 additions & 31 deletions lib-lua/themes/nominatim/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,11 @@

local module = {}

local POST_DELETE = nil
local MAIN_KEYS = {admin_level = {'delete'}}
local PRE_FILTER = {prefix = {}, suffix = {}}
local NAMES = nil
local ADDRESS_TAGS = nil
local SAVE_EXTRA_MAINS = false
local EXTRATAGS_FILTER = function (p) return p.extratags end
local POSTCODE_FALLBACK = true

-- This file can also be directly require'd instead of running it under
Expand Down Expand Up @@ -371,36 +370,33 @@ function Place:grab_name_parts(data)
end


function Place:write_place(k, v, mfunc, save_extra_mains)
function Place:write_place(k, v, mfunc)
v = v or self.intags[k]
if v == nil then
return 0
end

local place = mfunc(self, k, v)
if place then
local res = place:write_row(k, v, save_extra_mains)
local res = place:write_row(k, v)
self.num_entries = self.num_entries + res
return res
end

return 0
end

function Place:write_row(k, v, save_extra_mains)
function Place:write_row(k, v)
if self.geometry == nil then
self.geometry = self.geom_func(self.object)
end
if self.geometry:is_null() then
return 0
end

if save_extra_mains ~= nil then
for extra_k, extra_v in pairs(self.intags) do
if extra_k ~= k and save_extra_mains(extra_k, extra_v) then
self.extratags[extra_k] = extra_v
end
end
local extratags = EXTRATAGS_FILTER(self, k, v)
if not (extratags and next(extratags)) then
extratags = nil
end

insert_row{
Expand All @@ -409,18 +405,10 @@ function Place:write_row(k, v, save_extra_mains)
admin_level = self.admin_level,
name = next(self.names) and self.names,
address = next(self.address) and self.address,
extratags = next(self.extratags) and self.extratags,
extratags = extratags,
geometry = self.geometry
}

if save_extra_mains then
for tk, tv in pairs(self.intags) do
if save_extra_mains(tk, tv) then
self.extratags[tk] = nil
end
end
end

return 1
end

Expand Down Expand Up @@ -531,7 +519,7 @@ function module.tag_group(data)
end
end

return function (k, v)
return function (k)
local val = fullmatches[k]
if val ~= nil then
return val
Expand Down Expand Up @@ -639,27 +627,25 @@ function module.process_tags(o)
end

if o.address.interpolation ~= nil then
o:write_place('place', 'houses', PlaceTransform.always, SAVE_EXTRA_MAINS)
o:write_place('place', 'houses', PlaceTransform.always)
return
end

o:clean{delete = POST_DELETE}

-- collect main keys
for k, v in pairs(o.intags) do
local ktable = MAIN_KEYS[k]
if ktable then
local ktype = ktable[v] or ktable[1]
if type(ktype) == 'function' then
o:write_place(k, v, ktype, SAVE_EXTRA_MAINS)
o:write_place(k, v, ktype)
elseif ktype == 'fallback' and o.has_name then
fallback = {k, v, PlaceTransform.named}
end
end
end

if fallback ~= nil and o.num_entries == 0 then
o:write_place(fallback[1], fallback[2], fallback[3], SAVE_EXTRA_MAINS)
o:write_place(fallback[1], fallback[2], fallback[3])
end
end

Expand Down Expand Up @@ -771,12 +757,44 @@ end


function module.set_unused_handling(data)
if data.extra_keys == nil and data.extra_tags == nil then
POST_DELETE = module.tag_match{keys = data.delete_keys, tags = data.delete_tags}
SAVE_EXTRA_MAINS = function() return true end
if type(data) == 'function' then
EXTRATAGS_FILTER = data
elseif data == nil then
EXTRATAGS_FILTER = function (p) return p.extratags end
elseif data.extra_keys == nil and data.extra_tags == nil then
local delfilter = module.tag_match{keys = data.delete_keys, tags = data.delete_tags}
EXTRATAGS_FILTER = function (p, k)
local extra = {}
for kin, vin in pairs(p.intags) do
if kin ~= k and not delfilter(kin, vin) then
extra[kin] = vin
end
end
if next(extra) == nil then
return p.extratags
end
for kextra, vextra in pairs(p.extratags) do
extra[kextra] = vextra
end
return extra
end
elseif data.delete_keys == nil and data.delete_tags == nil then
POST_DELETE = nil
SAVE_EXTRA_MAINS = module.tag_match{keys = data.extra_keys, tags = data.extra_tags}
local incfilter = module.tag_match{keys = data.extra_keys, tags = data.extra_tags}
EXTRATAGS_FILTER = function (p, k)
local extra = {}
for kin, vin in pairs(p.intags) do
if kin ~= k and incfilter(kin, vin) then
extra[kin] = vin
end
end
if next(extra) == nil then
return p.extratags
end
for kextra, vextra in pairs(p.extratags) do
extra[kextra] = vextra
end
return extra
end
else
error("unused handler can have only 'extra_keys' or 'delete_keys' set.")
end
Expand Down

0 comments on commit b7e3d07

Please sign in to comment.