Skip to content

Commit

Permalink
Improve boundary processing in shortbread topic
Browse files Browse the repository at this point in the history
More consistent processing of boundaries in shortbread_v1. Deciding when
something is a boundary is now done only in one place. And the code is
more robust, checking that the way info is there before using it.

(This is now the same as in the shortbread_v1_gen code which already had
those changes.)

See #2
  • Loading branch information
joto committed Dec 21, 2023
1 parent a789b0e commit e5d39e6
Showing 1 changed file with 38 additions and 17 deletions.
55 changes: 38 additions & 17 deletions themes/shortbread_v1/topics/boundaries.lua
Expand Up @@ -33,13 +33,38 @@ local rinfos = {}

-- ---------------------------------------------------------------------------

-- Check if this looks like a boundary and return admin_level as number
-- Return nil if this is not a valid boundary.
local function get_admin_level(tags)
local type = tags.type

if type == 'boundary' or type == 'multipolygon' then
local boundary = tags.boundary
if boundary == 'administrative' or boundary == 'disputed' then
return tonumber(tags.admin_level)
end
end
end

-- Check the (numeric) admin level. Change this depending on which admin
-- levels you want to process. Shortbread only shows 2 and 4.
local function valid_admin_level(level)
return level == 2 or level == 4
end

-- ---------------------------------------------------------------------------

themepark:add_proc('way', function(object, data)
if osm2pgsql.stage == 1 then
return
end

local t = object.tags
local info = rinfos[object.id]
if not info then
return
end

local t = object.tags
local a = {
admin_level = info.admin_level,
maritime = (t.maritime and (t.maritime == 'yes' or t.natural == 'coastline')),
Expand All @@ -51,32 +76,28 @@ themepark:add_proc('way', function(object, data)
end)

themepark:add_proc('select_relation_members', function(relation)
local t = relation.tags
-- Only interested in relations with type=boundary, boundary=administrative
if t.type == 'boundary' and t.boundary == 'administrative'
and (t.admin_level == '2' or t.admin_level == '4') then
if valid_admin_level(get_admin_level(relation.tags)) then
return { ways = osm2pgsql.way_member_ids(relation) }
end
end)

themepark:add_proc('relation', function(object, data)
local t = object.tags
if t.type ~= 'boundary' then

local admin_level = get_admin_level(t)

if not valid_admin_level(admin_level) then
return
end

if (t.boundary == 'administrative' or t.boundary == 'disputed')
and (t.admin_level == '2' or t.admin_level == '4') then
local admin_level = tonumber(t.admin_level)
for _, member in ipairs(object.members) do
if member.type == 'w' then
if not rinfos[member.ref] then
rinfos[member.ref] = { admin_level = admin_level }
elseif rinfos[member.ref].admin_level > admin_level then
rinfos[member.ref].admin_level = admin_level
end
rinfos[member.ref].disputed = t.boundary == 'disputed'
for _, member in ipairs(object.members) do
if member.type == 'w' then
if not rinfos[member.ref] then
rinfos[member.ref] = { admin_level = admin_level }
elseif rinfos[member.ref].admin_level > admin_level then
rinfos[member.ref].admin_level = admin_level
end
rinfos[member.ref].disputed = (t.boundary == 'disputed')
end
end
end)
Expand Down

0 comments on commit e5d39e6

Please sign in to comment.