@@ -19,7 +19,7 @@ local gen_config = {
1919-- ---------------------------------------------------------------------------
2020
2121-- If anything related to boundaries changes, this expire table gets an entry.
22- -- It used zoom level 0, so there will always be at most one entry which
22+ -- It uses zoom level 0, so there will always be at most one entry which
2323-- triggers re-calculation of all boundaries in the world.
2424local expire_boundaries = osm2pgsql .define_expire_output ({
2525 table = themepark .with_prefix (' expire_boundaries' )
@@ -101,27 +101,30 @@ end
101101
102102-- Storage of information from boundary relations for use by boundary ways
103103-- (two-stage processing).
104+
105+ -- Relation ids and minimum admin level of all relations that reference a way id
104106local rinfos = {}
105107
108+ -- Minimum admin level of all relations tagged boundary=disputed that
109+ -- reference a way id
110+ local min_disputed_admin_level = {}
111+
106112-- ---------------------------------------------------------------------------
107113
108- -- Check if this looks like a boundary and return admin_level as number
109- -- Return nil if this is not a valid boundary.
110- local function get_admin_level (tags )
111- local type = tags .type
114+ -- Shortbread is only interested in level 2 and level 4 admin boundaries.
115+ local function is_admin_boundary (tags )
116+ return (tags .type == ' boundary' or tags .type == ' multipolygon' )
117+ and tags .boundary == ' administrative'
118+ and (tags .admin_level == ' 2' or tags .admin_level == ' 4' )
119+ end
112120
113- if type == ' boundary' or type == ' multipolygon' then
114- local boundary = tags .boundary
115- if boundary == ' administrative' or boundary == ' disputed' then
116- return tonumber (tags .admin_level )
117- end
121+ -- Get numerical admin level from string, default to 1 if invalid
122+ local function get_admin_level (value )
123+ if not value or not string.match (value , ' ^[1-9][0-9]?$' ) then
124+ return 1
118125 end
119- end
120126
121- -- Check the (numeric) admin level. Change this depending on which admin
122- -- levels you want to process. Shortbread only shows 2 and 4.
123- local function valid_admin_level (level )
124- return level == 2 or level == 4
127+ return tonumber (value )
125128end
126129
127130-- ---------------------------------------------------------------------------
@@ -139,11 +142,20 @@ themepark:add_proc('way', function(object, data)
139142 table.sort (info .rel_ids )
140143
141144 local t = object .tags
145+
146+ -- Set disputed flag either from disputed tag on the way...
147+ local disputed = (t .disputed == ' yes' )
148+
149+ -- .. or from a parent relation with boundary=disputed
150+ if min_disputed_admin_level [object .id ] and min_disputed_admin_level [object .id ] <= info .admin_level then
151+ disputed = true
152+ end
153+
142154 local a = {
143155 relation_ids = ' {' .. table.concat (info .rel_ids , ' ,' ) .. ' }' ,
144156 admin_level = info .admin_level ,
145157 maritime = (t .maritime ~= nil and (t .maritime == ' yes' )),
146- disputed = ( info . disputed or ( t . disputed ~= nil and t . disputed == ' yes ' )) ,
158+ disputed = disputed ,
147159 closure_segment = (t .closure_segment ~= nil and t .closure_segment == ' yes' ),
148160 coastline = (t .natural ~= nil and t .natural == ' coastline' ),
149161 geom = object :as_linestring (),
@@ -153,41 +165,52 @@ themepark:add_proc('way', function(object, data)
153165end )
154166
155167themepark :add_proc (' select_relation_members' , function (relation )
156- if valid_admin_level ( get_admin_level ( relation .tags ) ) then
168+ if is_admin_boundary ( relation .tags ) then
157169 return { ways = osm2pgsql .way_member_ids (relation ) }
158170 end
159171end )
160172
161173themepark :add_proc (' relation' , function (object , data )
162- local t = object .tags
174+ if is_admin_boundary (object .tags ) then
175+ local t = object .tags
163176
164- local admin_level = get_admin_level (t )
177+ local admin_level = tonumber (t .admin_level )
178+
179+ for _ , id in ipairs (osm2pgsql .way_member_ids (object )) do
180+ if not rinfos [id ] then
181+ rinfos [id ] = { admin_level = admin_level , rel_ids = {} }
182+ elseif rinfos [id ].admin_level > admin_level then
183+ rinfos [id ].admin_level = admin_level
184+ end
185+ table.insert (rinfos [id ].rel_ids , object .id )
186+ end
187+
188+ local a = {
189+ admin_level = admin_level ,
190+ maritime = (t .maritime ~= nil and t .maritime == ' yes' ),
191+ disputed = (t .disputed ~= nil and t .disputed == ' yes' ),
192+ geom = object :as_multilinestring (),
193+ }
194+
195+ themepark .themes .core .add_name (a , object )
196+ themepark :insert (' boundaries_relations_interim' , a , t )
165197
166- if not valid_admin_level (admin_level ) then
167198 return
168199 end
169200
170- for _ , member in ipairs (object .members ) do
171- if member .type == ' w' then
172- if not rinfos [member .ref ] then
173- rinfos [member .ref ] = { admin_level = admin_level , rel_ids = {} }
174- elseif rinfos [member .ref ].admin_level > admin_level then
175- rinfos [member .ref ].admin_level = admin_level
201+ if object .tags .boundary == ' disputed' then
202+ -- Ways in relations tagged boundary=disputed are flagged as disputed
203+ -- if either the relation doesn't have an admin_level tag or the
204+ -- admin_level tag is <= the admin level the way got from the
205+ -- boundary=administrative relation(s).
206+ local admin_level = get_admin_level (object .tags .admin_level )
207+
208+ for _ , id in ipairs (osm2pgsql .way_member_ids (object )) do
209+ if not min_disputed_admin_level [id ] or min_disputed_admin_level [id ] > admin_level then
210+ min_disputed_admin_level [id ] = admin_level
176211 end
177- table.insert (rinfos [member .ref ].rel_ids , object .id )
178- rinfos [member .ref ].disputed = (t .boundary == ' disputed' )
179212 end
180213 end
181-
182- local a = {
183- admin_level = admin_level ,
184- maritime = (t .maritime ~= nil and t .maritime == ' yes' ),
185- disputed = (t .disputed ~= nil and t .disputed == ' yes' ),
186- geom = object :as_multilinestring (),
187- }
188-
189- themepark .themes .core .add_name (a , object )
190- themepark :insert (' boundaries_relations_interim' , a , t )
191214end )
192215
193216local function gen_commands (sql , level )
0 commit comments