Skip to content

Commit 415c252

Browse files
committed
Make shortbread theme work with prefix
1 parent 79d4d0a commit 415c252

File tree

5 files changed

+60
-55
lines changed

5 files changed

+60
-55
lines changed

lua/themepark.lua

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -439,10 +439,15 @@ end
439439

440440
-- ---------------------------------------------------------------------------
441441

442-
function themepark.expand_template(command)
443-
local res = string.gsub(command, '{schema}', themepark.options.schema)
442+
function themepark.expand_template(str)
443+
str = string.gsub(str, '{schema}', themepark.options.schema)
444+
str = string.gsub(str, '{prefix}', themepark.options.prefix or '')
444445
-- ignore second return value of gsub
445-
return res
446+
return str
447+
end
448+
449+
function themepark.with_prefix(name)
450+
return (themepark.options.prefix or '') .. name
446451
end
447452

448453
-- ---------------------------------------------------------------------------

themes/shortbread_v1_gen/topics/boundaries.lua

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ local gen_config = {
2222
-- It used zoom level 0, so there will always be at most one entry which
2323
-- triggers re-calculation of all boundaries in the world.
2424
local expire_boundaries = osm2pgsql.define_expire_output({
25-
table = 'expire_boundaries'
25+
table = themepark.with_prefix('expire_boundaries')
2626
})
2727

2828
-- This table contains all the ways that are members of a boundary relation.
@@ -193,27 +193,27 @@ end)
193193
local function gen_commands(sql, level)
194194
local c = gen_config[level]
195195

196-
table.insert(sql, 'CREATE TABLE {schema}.boundaries_' .. level ..
197-
'_new (LIKE {schema}.boundaries_' .. level .. ' INCLUDING IDENTITY)')
196+
table.insert(sql, 'CREATE TABLE {schema}.{prefix}boundaries_' .. level ..
197+
'_new (LIKE {schema}.{prefix}boundaries_' .. level .. ' INCLUDING IDENTITY)')
198198

199199
table.insert(sql, [[
200200
WITH simplified AS (
201201
SELECT way_ids, relation_ids, admin_level, maritime, disputed, ST_SimplifyVW(geom, ]] .. c.simplify .. [[) AS geom
202-
FROM {schema}.boundaries ]] .. c.condition .. [[
202+
FROM {schema}.{prefix}boundaries ]] .. c.condition .. [[
203203
)
204-
INSERT INTO {schema}.boundaries_]] .. level .. [[_new (way_ids, relation_ids, admin_level, maritime, disputed, geom)
204+
INSERT INTO {schema}.{prefix}boundaries_]] .. level .. [[_new (way_ids, relation_ids, admin_level, maritime, disputed, geom)
205205
SELECT way_ids, relation_ids, admin_level, maritime, disputed, geom
206206
FROM simplified WHERE ST_Length(geom) > ]] .. c.minlength)
207207

208-
table.insert(sql, 'ANALYZE {schema}.boundaries_' .. level .. '_new')
209-
table.insert(sql, 'CREATE INDEX ON {schema}.boundaries_' .. level .. '_new USING GIST (geom)')
210-
table.insert(sql, 'DROP TABLE {schema}.boundaries_' .. level)
211-
table.insert(sql, 'ALTER TABLE {schema}.boundaries_' .. level .. '_new RENAME TO boundaries_' .. level)
208+
table.insert(sql, 'ANALYZE {schema}.{prefix}boundaries_' .. level .. '_new')
209+
table.insert(sql, 'CREATE INDEX ON {schema}.{prefix}boundaries_' .. level .. '_new USING GIST (geom)')
210+
table.insert(sql, 'DROP TABLE {schema}.{prefix}boundaries_' .. level)
211+
table.insert(sql, 'ALTER TABLE {schema}.{prefix}boundaries_' .. level .. '_new RENAME TO {prefix}boundaries_' .. level)
212212
end
213213

214214
themepark:add_proc('gen', function(data)
215215
local sql = {
216-
'CREATE TABLE {schema}.boundaries_new (LIKE {schema}.boundaries INCLUDING IDENTITY)',
216+
'CREATE TABLE {schema}.{prefix}boundaries_new (LIKE {schema}.{prefix}boundaries INCLUDING IDENTITY)',
217217
[[
218218
WITH multigeom AS (
219219
SELECT array_agg(way_id ORDER BY way_id) AS way_ids,
@@ -222,24 +222,24 @@ SELECT array_agg(way_id ORDER BY way_id) AS way_ids,
222222
(maritime OR coastline) AS maritime,
223223
disputed,
224224
ST_LineMerge(ST_Collect(geom)) AS geom
225-
FROM {schema}.boundaries_ways_interim
225+
FROM {schema}.{prefix}boundaries_ways_interim
226226
WHERE closure_segment IS FALSE
227227
GROUP BY relation_ids, maritime OR coastline, disputed
228228
)
229-
INSERT INTO {schema}.boundaries_new (way_ids, relation_ids, admin_level, maritime, disputed, geom)
229+
INSERT INTO {schema}.{prefix}boundaries_new (way_ids, relation_ids, admin_level, maritime, disputed, geom)
230230
SELECT way_ids, relation_ids, admin_level, maritime, disputed, (ST_Dump(geom)).geom AS geom
231231
FROM multigeom ]],
232-
'ANALYZE {schema}.boundaries_new',
233-
'CREATE INDEX ON {schema}.boundaries_new USING GIST (geom)',
234-
'DROP TABLE {schema}.boundaries',
235-
'ALTER TABLE {schema}.boundaries_new RENAME TO boundaries'
232+
'ANALYZE {schema}.{prefix}boundaries_new',
233+
'CREATE INDEX ON {schema}.{prefix}boundaries_new USING GIST (geom)',
234+
'DROP TABLE {schema}.{prefix}boundaries',
235+
'ALTER TABLE {schema}.{prefix}boundaries_new RENAME TO {prefix}boundaries'
236236
}
237237

238238
gen_commands(sql, 'l');
239239
gen_commands(sql, 'm');
240240
gen_commands(sql, 's');
241241

242-
table.insert(sql, 'TRUNCATE {schema}.expire_boundaries')
242+
table.insert(sql, 'TRUNCATE {schema}.{prefix}expire_boundaries')
243243

244244
local expanded_sql = {}
245245
for _, s in ipairs(sql) do
@@ -248,7 +248,7 @@ SELECT way_ids, relation_ids, admin_level, maritime, disputed, (ST_Dump(geom)).g
248248

249249
osm2pgsql.run_sql({
250250
description = 'Merge boundary lines for small zoom levels',
251-
if_has_rows = themepark.expand_template('SELECT 1 FROM {schema}.expire_boundaries LIMIT 1'),
251+
if_has_rows = themepark.expand_template('SELECT 1 FROM {schema}.{prefix}expire_boundaries LIMIT 1'),
252252
transaction = true,
253253
sql = expanded_sql
254254
})

themes/shortbread_v1_gen/topics/land.lua

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ local expire_outputs = {}
1717
for _, zoom in ipairs(gen_zoom_levels) do
1818
expire_outputs[zoom] = osm2pgsql.define_expire_output({
1919
maxzoom = zoom,
20-
table = 'expire_land_z' .. zoom
20+
table = themepark.with_prefix('expire_land_z' .. zoom)
2121
})
2222

2323
themepark:add_table{
@@ -171,8 +171,8 @@ themepark:add_proc('gen', function(data)
171171
schema = themepark.options.schema,
172172
name = 'land_z' .. zoom,
173173
debug = false,
174-
src_table = 'land',
175-
dest_table = 'land_z' .. zoom,
174+
src_table = themepark.with_prefix('land'),
175+
dest_table = themepark.with_prefix('land_z' .. zoom),
176176
zoom = zoom,
177177
geom_column = 'geom',
178178
group_by_column = 'kind',

themes/shortbread_v1_gen/topics/streets.lua

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -441,23 +441,23 @@ themepark:add_proc('gen', function(data)
441441
transaction = true,
442442
sql = {
443443
themepark.expand_template([[
444-
CREATE TABLE {schema}.streets_med_new
445-
(LIKE {schema}.streets_med INCLUDING IDENTITY)]]),
444+
CREATE TABLE {schema}.{prefix}streets_med_new
445+
(LIKE {schema}.{prefix}streets_med INCLUDING IDENTITY)]]),
446446
themepark.expand_template([[
447447
CREATE OR REPLACE FUNCTION osm2pgsql_shortbread_streets_med() RETURNS void AS $$
448448
DECLARE
449449
cell geometry;
450450
BEGIN
451451
FOR cell IN
452452
SELECT ST_TileEnvelope(6, x, y) FROM generate_series(0, 63) x, generate_series(0, 63) y
453-
WHERE ST_TileEnvelope(6, x, y) && ST_EstimatedExtent('{schema}', 'streets_med_interim', 'geom')
453+
WHERE ST_TileEnvelope(6, x, y) && ST_EstimatedExtent('{schema}', '{prefix}streets_med_interim', 'geom')
454454
LOOP
455455
WITH
456456
merged AS
457457
(SELECT kind, link, rail, tunnel, bridge, tracktype, surface,
458458
service, layer, ref, ref_rows, ref_cols, z_order, minzoom,
459459
ST_LineMerge(ST_Collect(geom)) AS geom
460-
FROM {schema}.streets_med_interim
460+
FROM {schema}.{prefix}streets_med_interim
461461
WHERE geom && cell
462462
GROUP BY kind, link, rail, tunnel, bridge, tracktype, surface,
463463
service, layer, ref, ref_rows, ref_cols, z_order, minzoom),
@@ -467,7 +467,7 @@ BEGIN
467467
ref_rows, ref_cols, z_order, minzoom,
468468
ST_Simplify((ST_Dump(geom)).geom, 20) AS geom
469469
FROM merged)
470-
INSERT INTO {schema}.streets_med_new (way_id, kind, link, rail, tunnel, bridge,
470+
INSERT INTO {schema}.{prefix}streets_med_new (way_id, kind, link, rail, tunnel, bridge,
471471
tracktype, surface, service, layer, ref,
472472
ref_rows, ref_cols, z_order, minzoom, geom)
473473
SELECT * FROM simplified WHERE geom IS NOT NULL;
@@ -476,10 +476,10 @@ END;
476476
$$ LANGUAGE plpgsql]]),
477477
themepark.expand_template('SELECT osm2pgsql_shortbread_streets_med()'),
478478
themepark.expand_template('DROP FUNCTION osm2pgsql_shortbread_streets_med()'),
479-
themepark.expand_template('ANALYZE {schema}.streets_med_new'),
480-
themepark.expand_template('CREATE INDEX ON {schema}.streets_med_new USING GIST (geom)'),
481-
themepark.expand_template('DROP TABLE {schema}.streets_med'),
482-
themepark.expand_template('ALTER TABLE {schema}.streets_med_new RENAME TO streets_med'),
479+
themepark.expand_template('ANALYZE {schema}.{prefix}streets_med_new'),
480+
themepark.expand_template('CREATE INDEX ON {schema}.{prefix}streets_med_new USING GIST (geom)'),
481+
themepark.expand_template('DROP TABLE {schema}.{prefix}streets_med'),
482+
themepark.expand_template('ALTER TABLE {schema}.{prefix}streets_med_new RENAME TO streets_med'),
483483
}
484484
})
485485

@@ -488,22 +488,22 @@ $$ LANGUAGE plpgsql]]),
488488
transaction = true,
489489
sql = {
490490
themepark.expand_template([[
491-
CREATE TABLE {schema}.streets_low_new
492-
(LIKE {schema}.streets_low INCLUDING IDENTITY)]]),
491+
CREATE TABLE {schema}.{prefix}streets_low_new
492+
(LIKE {schema}.{prefix}streets_low INCLUDING IDENTITY)]]),
493493
themepark.expand_template([[
494494
WITH
495495
merged AS
496496
(SELECT kind, ref, rail, minzoom, ST_LineMerge(ST_Collect(geom)) AS geom
497-
FROM {schema}.streets_low_interim GROUP BY kind, ref, rail, minzoom),
497+
FROM {schema}.{prefix}streets_low_interim GROUP BY kind, ref, rail, minzoom),
498498
simplified AS
499499
(SELECT 1, kind, ref, rail, minzoom,
500500
ST_Simplify((ST_Dump(geom)).geom, 20) AS geom FROM merged)
501-
INSERT INTO {schema}.streets_low_new (way_id, kind, ref, rail, minzoom, geom)
501+
INSERT INTO {schema}.{prefix}streets_low_new (way_id, kind, ref, rail, minzoom, geom)
502502
SELECT * FROM simplified WHERE geom IS NOT NULL]]),
503-
themepark.expand_template('ANALYZE {schema}.streets_low_new'),
504-
themepark.expand_template('CREATE INDEX ON {schema}.streets_low_new USING GIST (geom)'),
505-
themepark.expand_template('DROP TABLE {schema}.streets_low'),
506-
themepark.expand_template('ALTER TABLE {schema}.streets_low_new RENAME TO streets_low'),
503+
themepark.expand_template('ANALYZE {schema}.{prefix}streets_low_new'),
504+
themepark.expand_template('CREATE INDEX ON {schema}.{prefix}streets_low_new USING GIST (geom)'),
505+
themepark.expand_template('DROP TABLE {schema}.{prefix}streets_low'),
506+
themepark.expand_template('ALTER TABLE {schema}.{prefix}streets_low_new RENAME TO streets_low'),
507507
}
508508
})
509509
end)

themes/shortbread_v1_gen/topics/water.lua

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ local expire_levels = {}
2323
for _, level in ipairs(gen_levels) do
2424
expire_levels[level] = osm2pgsql.define_expire_output({
2525
maxzoom = gen_config[level].genzoom,
26-
table = 'expire_water_polygons_' .. level
26+
table = themepark.with_prefix('expire_water_polygons_' .. level)
2727
})
2828

2929
themepark:add_table{
@@ -94,7 +94,7 @@ themepark:add_table{
9494
}
9595

9696
local expire_lines = osm2pgsql.define_expire_output({
97-
table = 'expire_water_lines'
97+
table = themepark.with_prefix('expire_water_lines')
9898
})
9999

100100
-- Used as basis for generalized data
@@ -252,8 +252,8 @@ themepark:add_proc('gen', function(data)
252252
schema = themepark.options.schema,
253253
name = 'water_polygons_' .. level,
254254
debug = false,
255-
src_table = 'water_polygons',
256-
dest_table = 'water_polygons_' .. level,
255+
src_table = themepark.with_prefix('water_polygons'),
256+
dest_table = themepark.with_prefix('water_polygons_' .. level),
257257
zoom = gen_config[level].genzoom,
258258
geom_column = 'geom',
259259
group_by_column = 'kind',
@@ -265,28 +265,28 @@ themepark:add_proc('gen', function(data)
265265

266266
osm2pgsql.run_sql({
267267
description = 'Merge water lines for small zoom levels',
268-
if_has_rows = themepark.expand_template('SELECT 1 FROM {schema}.expire_water_lines LIMIT 1'),
268+
if_has_rows = themepark.expand_template('SELECT 1 FROM {schema}.{prefix}expire_water_lines LIMIT 1'),
269269
transaction = true,
270270
sql = {
271271
themepark.expand_template([[
272-
CREATE TABLE {schema}.water_lines_gen_new
273-
(LIKE {schema}.water_lines_gen INCLUDING IDENTITY)]]),
272+
CREATE TABLE {schema}.{prefix}water_lines_gen_new
273+
(LIKE {schema}.{prefix}water_lines_gen INCLUDING IDENTITY)]]),
274274
themepark.expand_template([[
275275
WITH merged AS
276276
(SELECT ]] .. name_list .. [[, kind, tunnel, bridge, ST_LineMerge(ST_Collect(geom)) AS geom
277-
FROM {schema}.water_lines_gen_interim
277+
FROM {schema}.{prefix}water_lines_gen_interim
278278
GROUP BY kind, ]] .. name_list .. [[, tunnel, bridge),
279279
simplified AS
280280
(SELECT ]] .. name_list .. [[, kind, tunnel, bridge,
281281
ST_Simplify((ST_Dump(geom)).geom, 20) AS geom FROM merged)
282-
INSERT INTO {schema}.water_lines_gen_new (]] .. name_list .. [[, kind, tunnel, bridge, geom)
282+
INSERT INTO {schema}.{prefix}water_lines_gen_new (]] .. name_list .. [[, kind, tunnel, bridge, geom)
283283
SELECT * FROM simplified WHERE geom IS NOT NULL
284284
]]),
285-
themepark.expand_template('ANALYZE {schema}.water_lines_gen_new'),
286-
themepark.expand_template('CREATE INDEX ON {schema}.water_lines_gen_new USING GIST (geom)'),
287-
themepark.expand_template('DROP TABLE {schema}.water_lines_gen'),
288-
themepark.expand_template('ALTER TABLE {schema}.water_lines_gen_new RENAME TO water_lines_gen'),
289-
themepark.expand_template('TRUNCATE {schema}.expire_water_lines'),
285+
themepark.expand_template('ANALYZE {schema}.{prefix}water_lines_gen_new'),
286+
themepark.expand_template('CREATE INDEX ON {schema}.{prefix}water_lines_gen_new USING GIST (geom)'),
287+
themepark.expand_template('DROP TABLE {schema}.{prefix}water_lines_gen'),
288+
themepark.expand_template('ALTER TABLE {schema}.{prefix}water_lines_gen_new RENAME TO water_lines_gen'),
289+
themepark.expand_template('TRUNCATE {schema}.{prefix}expire_water_lines'),
290290
}
291291
})
292292
end)

0 commit comments

Comments
 (0)