Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parse gauges and store as array #89

Merged
merged 2 commits into from
Aug 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions import/openrailwaymap.lua
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,8 @@ local railway_line = osm2pgsql.define_table({
{ column = 'electrification_state', type = 'text' },
{ column = 'future_frequency', type = 'real' },
{ column = 'future_voltage', type = 'integer' },
{ column = 'gauge', type = 'text' },
{ column = 'gauges', sql_type = 'text[]' },
{ column = 'construction_railway', type = 'text' },
{ column = 'construction_gauge', type = 'text' },
{ column = 'proposed_railway', type = 'text' },
{ column = 'disused_railway', type = 'text' },
{ column = 'abandoned_railway', type = 'text' },
Expand Down Expand Up @@ -438,6 +437,17 @@ function osm2pgsql.process_way(object)

local current_electrification_state, voltage, frequency = electrification_state(tags, true)
local _, future_voltage, future_frequency = electrification_state(tags, false)

local gauges = {}
local gauge_tag = tags['gauge'] or tags['construction:gauge']
if gauge_tag then
for gauge in string.gmatch(gauge_tag, '[^;]+') do
if gauge then
table.insert(gauges, gauge)
end
end
end

railway_line:insert({
way = object:as_linestring(),
railway = tags['railway'],
Expand All @@ -463,9 +473,8 @@ function osm2pgsql.process_way(object)
voltage = voltage,
future_frequency = future_frequency,
future_voltage = future_voltage,
gauge = tags['gauge'],
gauges = '{' .. table.concat(gauges, ',') .. '}',
construction_railway = tags['construction:railway'],
construction_gauge = tags['construction:gauge'],
proposed_railway = tags['proposed:railway'],
disused_railway = tags['disused:railway'],
abandoned_railway = tags['abandoned:railway'],
Expand Down
26 changes: 0 additions & 26 deletions import/sql/functions.sql
Original file line number Diff line number Diff line change
Expand Up @@ -321,29 +321,3 @@ BEGIN
RETURN volt_text;
END;
$$ LANGUAGE plpgsql;

-- Get label for gauge
CREATE OR REPLACE FUNCTION railway_gauge_label(gauge TEXT) RETURNS TEXT AS $$
BEGIN
IF gauge IS NOT NULL AND gauge ~ '^[0-9;]+$' THEN
RETURN regexp_replace(gauge, ';', ' | ', 'g');
END IF;
RETURN NULL;
END;
$$ LANGUAGE plpgsql;

-- Get the desired value from listed values (e.g. gauge)
CREATE OR REPLACE FUNCTION railway_desired_value_from_list(desired_nr INTEGER, listed_values TEXT) RETURNS TEXT AS $$
DECLARE
value_array TEXT[];
BEGIN
IF listed_values IS NULL OR listed_values = '' OR desired_nr <= 0 THEN
RETURN NULL;
END IF;
value_array := regexp_split_to_array(listed_values, ';');
IF desired_nr > array_length(value_array, 1) THEN
RETURN NULL;
END IF;
RETURN value_array[desired_nr];
END;
$$ LANGUAGE plpgsql;
8 changes: 4 additions & 4 deletions import/sql/tile_views.sql
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,10 @@ CREATE OR REPLACE VIEW railway_line_high AS
railway_electrification_label(COALESCE(voltage, future_voltage), COALESCE(frequency, future_frequency)) AS electrification_label,
future_voltage,
future_frequency,
railway_desired_value_from_list(1, COALESCE(gauge, construction_gauge)) AS gauge0,
railway_desired_value_from_list(2, COALESCE(gauge, construction_gauge)) AS gauge1,
railway_desired_value_from_list(3, COALESCE(gauge, construction_gauge)) AS gauge2,
railway_gauge_label(gauge) AS gauge_label
gauges[1] AS gauge0,
gauges[2] AS gauge1,
gauges[3] AS gauge2,
(select string_agg(gauge, ' | ') from unnest(gauges) as gauge where gauge ~ '^[0-9]+$') as gauge_label
FROM railway_line
WHERE railway IN ('rail', 'tram', 'light_rail', 'subway', 'narrow_gauge', 'disused', 'abandoned', 'razed', 'construction', 'proposed', 'preserved')
) AS r
Expand Down