Skip to content

Commit

Permalink
Make shortbread theme work with prefix
Browse files Browse the repository at this point in the history
  • Loading branch information
joto committed Sep 24, 2024
1 parent 79d4d0a commit 415c252
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 55 deletions.
11 changes: 8 additions & 3 deletions lua/themepark.lua
Original file line number Diff line number Diff line change
Expand Up @@ -439,10 +439,15 @@ end

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

function themepark.expand_template(command)
local res = string.gsub(command, '{schema}', themepark.options.schema)
function themepark.expand_template(str)
str = string.gsub(str, '{schema}', themepark.options.schema)
str = string.gsub(str, '{prefix}', themepark.options.prefix or '')
-- ignore second return value of gsub
return res
return str
end

function themepark.with_prefix(name)
return (themepark.options.prefix or '') .. name
end

-- ---------------------------------------------------------------------------
Expand Down
36 changes: 18 additions & 18 deletions themes/shortbread_v1_gen/topics/boundaries.lua
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ local gen_config = {
-- It used zoom level 0, so there will always be at most one entry which
-- triggers re-calculation of all boundaries in the world.
local expire_boundaries = osm2pgsql.define_expire_output({
table = 'expire_boundaries'
table = themepark.with_prefix('expire_boundaries')
})

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

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

table.insert(sql, [[
WITH simplified AS (
SELECT way_ids, relation_ids, admin_level, maritime, disputed, ST_SimplifyVW(geom, ]] .. c.simplify .. [[) AS geom
FROM {schema}.boundaries ]] .. c.condition .. [[
FROM {schema}.{prefix}boundaries ]] .. c.condition .. [[
)
INSERT INTO {schema}.boundaries_]] .. level .. [[_new (way_ids, relation_ids, admin_level, maritime, disputed, geom)
INSERT INTO {schema}.{prefix}boundaries_]] .. level .. [[_new (way_ids, relation_ids, admin_level, maritime, disputed, geom)
SELECT way_ids, relation_ids, admin_level, maritime, disputed, geom
FROM simplified WHERE ST_Length(geom) > ]] .. c.minlength)

table.insert(sql, 'ANALYZE {schema}.boundaries_' .. level .. '_new')
table.insert(sql, 'CREATE INDEX ON {schema}.boundaries_' .. level .. '_new USING GIST (geom)')
table.insert(sql, 'DROP TABLE {schema}.boundaries_' .. level)
table.insert(sql, 'ALTER TABLE {schema}.boundaries_' .. level .. '_new RENAME TO boundaries_' .. level)
table.insert(sql, 'ANALYZE {schema}.{prefix}boundaries_' .. level .. '_new')
table.insert(sql, 'CREATE INDEX ON {schema}.{prefix}boundaries_' .. level .. '_new USING GIST (geom)')
table.insert(sql, 'DROP TABLE {schema}.{prefix}boundaries_' .. level)
table.insert(sql, 'ALTER TABLE {schema}.{prefix}boundaries_' .. level .. '_new RENAME TO {prefix}boundaries_' .. level)
end

themepark:add_proc('gen', function(data)
local sql = {
'CREATE TABLE {schema}.boundaries_new (LIKE {schema}.boundaries INCLUDING IDENTITY)',
'CREATE TABLE {schema}.{prefix}boundaries_new (LIKE {schema}.{prefix}boundaries INCLUDING IDENTITY)',
[[
WITH multigeom AS (
SELECT array_agg(way_id ORDER BY way_id) AS way_ids,
Expand All @@ -222,24 +222,24 @@ SELECT array_agg(way_id ORDER BY way_id) AS way_ids,
(maritime OR coastline) AS maritime,
disputed,
ST_LineMerge(ST_Collect(geom)) AS geom
FROM {schema}.boundaries_ways_interim
FROM {schema}.{prefix}boundaries_ways_interim
WHERE closure_segment IS FALSE
GROUP BY relation_ids, maritime OR coastline, disputed
)
INSERT INTO {schema}.boundaries_new (way_ids, relation_ids, admin_level, maritime, disputed, geom)
INSERT INTO {schema}.{prefix}boundaries_new (way_ids, relation_ids, admin_level, maritime, disputed, geom)
SELECT way_ids, relation_ids, admin_level, maritime, disputed, (ST_Dump(geom)).geom AS geom
FROM multigeom ]],
'ANALYZE {schema}.boundaries_new',
'CREATE INDEX ON {schema}.boundaries_new USING GIST (geom)',
'DROP TABLE {schema}.boundaries',
'ALTER TABLE {schema}.boundaries_new RENAME TO boundaries'
'ANALYZE {schema}.{prefix}boundaries_new',
'CREATE INDEX ON {schema}.{prefix}boundaries_new USING GIST (geom)',
'DROP TABLE {schema}.{prefix}boundaries',
'ALTER TABLE {schema}.{prefix}boundaries_new RENAME TO {prefix}boundaries'
}

gen_commands(sql, 'l');
gen_commands(sql, 'm');
gen_commands(sql, 's');

table.insert(sql, 'TRUNCATE {schema}.expire_boundaries')
table.insert(sql, 'TRUNCATE {schema}.{prefix}expire_boundaries')

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

osm2pgsql.run_sql({
description = 'Merge boundary lines for small zoom levels',
if_has_rows = themepark.expand_template('SELECT 1 FROM {schema}.expire_boundaries LIMIT 1'),
if_has_rows = themepark.expand_template('SELECT 1 FROM {schema}.{prefix}expire_boundaries LIMIT 1'),
transaction = true,
sql = expanded_sql
})
Expand Down
6 changes: 3 additions & 3 deletions themes/shortbread_v1_gen/topics/land.lua
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ local expire_outputs = {}
for _, zoom in ipairs(gen_zoom_levels) do
expire_outputs[zoom] = osm2pgsql.define_expire_output({
maxzoom = zoom,
table = 'expire_land_z' .. zoom
table = themepark.with_prefix('expire_land_z' .. zoom)
})

themepark:add_table{
Expand Down Expand Up @@ -171,8 +171,8 @@ themepark:add_proc('gen', function(data)
schema = themepark.options.schema,
name = 'land_z' .. zoom,
debug = false,
src_table = 'land',
dest_table = 'land_z' .. zoom,
src_table = themepark.with_prefix('land'),
dest_table = themepark.with_prefix('land_z' .. zoom),
zoom = zoom,
geom_column = 'geom',
group_by_column = 'kind',
Expand Down
34 changes: 17 additions & 17 deletions themes/shortbread_v1_gen/topics/streets.lua
Original file line number Diff line number Diff line change
Expand Up @@ -441,23 +441,23 @@ themepark:add_proc('gen', function(data)
transaction = true,
sql = {
themepark.expand_template([[
CREATE TABLE {schema}.streets_med_new
(LIKE {schema}.streets_med INCLUDING IDENTITY)]]),
CREATE TABLE {schema}.{prefix}streets_med_new
(LIKE {schema}.{prefix}streets_med INCLUDING IDENTITY)]]),
themepark.expand_template([[
CREATE OR REPLACE FUNCTION osm2pgsql_shortbread_streets_med() RETURNS void AS $$
DECLARE
cell geometry;
BEGIN
FOR cell IN
SELECT ST_TileEnvelope(6, x, y) FROM generate_series(0, 63) x, generate_series(0, 63) y
WHERE ST_TileEnvelope(6, x, y) && ST_EstimatedExtent('{schema}', 'streets_med_interim', 'geom')
WHERE ST_TileEnvelope(6, x, y) && ST_EstimatedExtent('{schema}', '{prefix}streets_med_interim', 'geom')
LOOP
WITH
merged AS
(SELECT kind, link, rail, tunnel, bridge, tracktype, surface,
service, layer, ref, ref_rows, ref_cols, z_order, minzoom,
ST_LineMerge(ST_Collect(geom)) AS geom
FROM {schema}.streets_med_interim
FROM {schema}.{prefix}streets_med_interim
WHERE geom && cell
GROUP BY kind, link, rail, tunnel, bridge, tracktype, surface,
service, layer, ref, ref_rows, ref_cols, z_order, minzoom),
Expand All @@ -467,7 +467,7 @@ BEGIN
ref_rows, ref_cols, z_order, minzoom,
ST_Simplify((ST_Dump(geom)).geom, 20) AS geom
FROM merged)
INSERT INTO {schema}.streets_med_new (way_id, kind, link, rail, tunnel, bridge,
INSERT INTO {schema}.{prefix}streets_med_new (way_id, kind, link, rail, tunnel, bridge,
tracktype, surface, service, layer, ref,
ref_rows, ref_cols, z_order, minzoom, geom)
SELECT * FROM simplified WHERE geom IS NOT NULL;
Expand All @@ -476,10 +476,10 @@ END;
$$ LANGUAGE plpgsql]]),
themepark.expand_template('SELECT osm2pgsql_shortbread_streets_med()'),
themepark.expand_template('DROP FUNCTION osm2pgsql_shortbread_streets_med()'),
themepark.expand_template('ANALYZE {schema}.streets_med_new'),
themepark.expand_template('CREATE INDEX ON {schema}.streets_med_new USING GIST (geom)'),
themepark.expand_template('DROP TABLE {schema}.streets_med'),
themepark.expand_template('ALTER TABLE {schema}.streets_med_new RENAME TO streets_med'),
themepark.expand_template('ANALYZE {schema}.{prefix}streets_med_new'),
themepark.expand_template('CREATE INDEX ON {schema}.{prefix}streets_med_new USING GIST (geom)'),
themepark.expand_template('DROP TABLE {schema}.{prefix}streets_med'),
themepark.expand_template('ALTER TABLE {schema}.{prefix}streets_med_new RENAME TO streets_med'),
}
})

Expand All @@ -488,22 +488,22 @@ $$ LANGUAGE plpgsql]]),
transaction = true,
sql = {
themepark.expand_template([[
CREATE TABLE {schema}.streets_low_new
(LIKE {schema}.streets_low INCLUDING IDENTITY)]]),
CREATE TABLE {schema}.{prefix}streets_low_new
(LIKE {schema}.{prefix}streets_low INCLUDING IDENTITY)]]),
themepark.expand_template([[
WITH
merged AS
(SELECT kind, ref, rail, minzoom, ST_LineMerge(ST_Collect(geom)) AS geom
FROM {schema}.streets_low_interim GROUP BY kind, ref, rail, minzoom),
FROM {schema}.{prefix}streets_low_interim GROUP BY kind, ref, rail, minzoom),
simplified AS
(SELECT 1, kind, ref, rail, minzoom,
ST_Simplify((ST_Dump(geom)).geom, 20) AS geom FROM merged)
INSERT INTO {schema}.streets_low_new (way_id, kind, ref, rail, minzoom, geom)
INSERT INTO {schema}.{prefix}streets_low_new (way_id, kind, ref, rail, minzoom, geom)
SELECT * FROM simplified WHERE geom IS NOT NULL]]),
themepark.expand_template('ANALYZE {schema}.streets_low_new'),
themepark.expand_template('CREATE INDEX ON {schema}.streets_low_new USING GIST (geom)'),
themepark.expand_template('DROP TABLE {schema}.streets_low'),
themepark.expand_template('ALTER TABLE {schema}.streets_low_new RENAME TO streets_low'),
themepark.expand_template('ANALYZE {schema}.{prefix}streets_low_new'),
themepark.expand_template('CREATE INDEX ON {schema}.{prefix}streets_low_new USING GIST (geom)'),
themepark.expand_template('DROP TABLE {schema}.{prefix}streets_low'),
themepark.expand_template('ALTER TABLE {schema}.{prefix}streets_low_new RENAME TO streets_low'),
}
})
end)
Expand Down
28 changes: 14 additions & 14 deletions themes/shortbread_v1_gen/topics/water.lua
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ local expire_levels = {}
for _, level in ipairs(gen_levels) do
expire_levels[level] = osm2pgsql.define_expire_output({
maxzoom = gen_config[level].genzoom,
table = 'expire_water_polygons_' .. level
table = themepark.with_prefix('expire_water_polygons_' .. level)
})

themepark:add_table{
Expand Down Expand Up @@ -94,7 +94,7 @@ themepark:add_table{
}

local expire_lines = osm2pgsql.define_expire_output({
table = 'expire_water_lines'
table = themepark.with_prefix('expire_water_lines')
})

-- Used as basis for generalized data
Expand Down Expand Up @@ -252,8 +252,8 @@ themepark:add_proc('gen', function(data)
schema = themepark.options.schema,
name = 'water_polygons_' .. level,
debug = false,
src_table = 'water_polygons',
dest_table = 'water_polygons_' .. level,
src_table = themepark.with_prefix('water_polygons'),
dest_table = themepark.with_prefix('water_polygons_' .. level),
zoom = gen_config[level].genzoom,
geom_column = 'geom',
group_by_column = 'kind',
Expand All @@ -265,28 +265,28 @@ themepark:add_proc('gen', function(data)

osm2pgsql.run_sql({
description = 'Merge water lines for small zoom levels',
if_has_rows = themepark.expand_template('SELECT 1 FROM {schema}.expire_water_lines LIMIT 1'),
if_has_rows = themepark.expand_template('SELECT 1 FROM {schema}.{prefix}expire_water_lines LIMIT 1'),
transaction = true,
sql = {
themepark.expand_template([[
CREATE TABLE {schema}.water_lines_gen_new
(LIKE {schema}.water_lines_gen INCLUDING IDENTITY)]]),
CREATE TABLE {schema}.{prefix}water_lines_gen_new
(LIKE {schema}.{prefix}water_lines_gen INCLUDING IDENTITY)]]),
themepark.expand_template([[
WITH merged AS
(SELECT ]] .. name_list .. [[, kind, tunnel, bridge, ST_LineMerge(ST_Collect(geom)) AS geom
FROM {schema}.water_lines_gen_interim
FROM {schema}.{prefix}water_lines_gen_interim
GROUP BY kind, ]] .. name_list .. [[, tunnel, bridge),
simplified AS
(SELECT ]] .. name_list .. [[, kind, tunnel, bridge,
ST_Simplify((ST_Dump(geom)).geom, 20) AS geom FROM merged)
INSERT INTO {schema}.water_lines_gen_new (]] .. name_list .. [[, kind, tunnel, bridge, geom)
INSERT INTO {schema}.{prefix}water_lines_gen_new (]] .. name_list .. [[, kind, tunnel, bridge, geom)
SELECT * FROM simplified WHERE geom IS NOT NULL
]]),
themepark.expand_template('ANALYZE {schema}.water_lines_gen_new'),
themepark.expand_template('CREATE INDEX ON {schema}.water_lines_gen_new USING GIST (geom)'),
themepark.expand_template('DROP TABLE {schema}.water_lines_gen'),
themepark.expand_template('ALTER TABLE {schema}.water_lines_gen_new RENAME TO water_lines_gen'),
themepark.expand_template('TRUNCATE {schema}.expire_water_lines'),
themepark.expand_template('ANALYZE {schema}.{prefix}water_lines_gen_new'),
themepark.expand_template('CREATE INDEX ON {schema}.{prefix}water_lines_gen_new USING GIST (geom)'),
themepark.expand_template('DROP TABLE {schema}.{prefix}water_lines_gen'),
themepark.expand_template('ALTER TABLE {schema}.{prefix}water_lines_gen_new RENAME TO water_lines_gen'),
themepark.expand_template('TRUNCATE {schema}.{prefix}expire_water_lines'),
}
})
end)
Expand Down

0 comments on commit 415c252

Please sign in to comment.