Skip to content

Commit d462268

Browse files
authored
Postgres 17 (#122)
See https://www.postgresql.org/about/news/postgresql-17-released-2936/ See https://hub.docker.com/layers/postgis/postgis/17-3.4-alpine/images/sha256-5a1dbedac34e0e6663f8b7190d393339571f1cb3ecb2ab2f724524b4f3c7956e?context=explore The redefinition of a column with a function call no longer works in Postgres 17. The function call to determine the largest speed has been moved to Lua code.
1 parent 257feb3 commit d462268

File tree

5 files changed

+52
-83
lines changed

5 files changed

+52
-83
lines changed

api/Dockerfile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
FROM postgis/postgis:16-3.4-alpine as import
1+
FROM postgis/postgis:17-3.4-alpine as import
22

33
COPY api.sql /sql/import.sql
44
COPY facility_functions.sql /sql/facility_functions.sql
@@ -12,7 +12,7 @@ ENV PGDATA /var/lib/postgresql/postgres-data
1212

1313
HEALTHCHECK CMD ["pg_isready", "--host", "localhost", "--user", "postgres", "--dbname", "gis", "--port", "5432"]
1414

15-
FROM postgis/postgis:16-3.4-alpine as runtime
15+
FROM postgis/postgis:17-3.4-alpine as runtime
1616

1717
RUN apk add --no-cache \
1818
curl \

db/Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
FROM postgis/postgis:16-3.4-alpine
1+
FROM postgis/postgis:17-3.4-alpine
22

33
COPY tune-postgis.sh /docker-entrypoint-initdb.d/tune-postgis.sh

import/openrailwaymap.lua

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,40 @@ function strip_prefix(value, prefix)
2727
end
2828
end
2929

30+
-- Convert a speed number from text to integer but not convert units
31+
function speed_int_noconvert(value)
32+
local _, _, match = value:find('^(%d+%.?%d*)$')
33+
if match then
34+
return tonumber(match)
35+
end
36+
37+
local _, _, match = value:find('^(%d+%.?%d*) ?mph$')
38+
if match then
39+
return tonumber(match)
40+
end
41+
42+
return nil
43+
end
44+
45+
-- Get the largest speed from a list of speed values (common at light speed signals)
46+
function largest_speed_noconvert(value)
47+
if not value then
48+
return nil
49+
end
50+
51+
local largest_speed = nil
52+
for elem in string.gmatch(value, '[^;]+') do
53+
if elem then
54+
local speed = speed_int_noconvert(elem)
55+
if speed ~= nil and (largest_speed == nil or largest_speed < speed) then
56+
largest_speed = speed
57+
end
58+
end
59+
end
60+
61+
return largest_speed
62+
end
63+
3064
local railway_line = osm2pgsql.define_table({
3165
name = 'railway_line',
3266
ids = { type = 'way', id_column = 'osm_id' },
@@ -414,8 +448,17 @@ function osm2pgsql.process_node(object)
414448
["{% tag %}"] = tags['{% tag %}'],
415449
{% end %}
416450
{% for tag in speed_railway_signals.tags %}
451+
{% unless tag | matches("railway:signal:speed_limit:speed") %}
452+
{% unless tag | matches("railway:signal:speed_limit_distant:speed") %}
417453
["{% tag %}"] = tags['{% tag %}'],
418454
{% end %}
455+
{% end %}
456+
{% end %}
457+
-- We cast the highest speed to text to make it possible to only select those speeds
458+
-- we have an icon for. Otherwise we might render an icon for 40 kph if
459+
-- 42 is tagged (but invalid tagging).
460+
["railway:signal:speed_limit:speed"] = tags['railway:signal:speed_limit'] and largest_speed_noconvert(tags['railway:signal:speed_limit:speed']) or tags['railway:signal:speed_limit:speed'],
461+
["railway:signal:speed_limit_distant:speed"] = tags['railway:signal:speed_limit_distant'] and largest_speed_noconvert(tags['railway:signal:speed_limit_distant:speed']) or tags['railway:signal:speed_limit_distant:speed'],
419462
{% for tag in electrification_signals.tags %}
420463
["{% tag %}"] = tags['{% tag %}'],
421464
{% end %}

import/sql/functions.sql

Lines changed: 0 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -132,51 +132,6 @@ $$ LANGUAGE plpgsql
132132
LEAKPROOF
133133
PARALLEL SAFE;
134134

135-
-- Convert a speed number from text to integer but not convert units
136-
CREATE OR REPLACE FUNCTION railway_speed_int_noconvert(value TEXT) RETURNS INTEGER AS $$
137-
BEGIN
138-
IF value ~ '^[0-9]+(\.[0-9]+)?$' THEN
139-
RETURN value::DOUBLE PRECISION;
140-
END IF;
141-
IF value ~ '^[0-9]+(\.[0-9]+)? ?mph$' THEN
142-
RETURN substring(value FROM '^([0-9]+(\.[0-9]+)?)')::DOUBLE PRECISION;
143-
END IF;
144-
RETURN NULL;
145-
END;
146-
$$ LANGUAGE plpgsql
147-
IMMUTABLE
148-
LEAKPROOF
149-
PARALLEL SAFE;
150-
151-
-- Get the largest speed from a list of speed values (common at light speed signals)
152-
CREATE OR REPLACE FUNCTION railway_largest_speed_noconvert(value TEXT) RETURNS INTEGER AS $$
153-
DECLARE
154-
parts TEXT[];
155-
elem TEXT;
156-
largest_value INTEGER := NULL;
157-
this_value INTEGER;
158-
BEGIN
159-
IF value IS NULL OR value = '' THEN
160-
RETURN NULL;
161-
END IF;
162-
parts := regexp_split_to_array(value, ';');
163-
FOREACH elem IN ARRAY parts
164-
LOOP
165-
IF elem = '' THEN
166-
CONTINUE;
167-
END IF;
168-
this_value := railway_speed_int_noconvert(elem);
169-
IF largest_value IS NULL OR largest_value < this_value THEN
170-
largest_value := this_value;
171-
END IF;
172-
END LOOP;
173-
RETURN largest_value;
174-
END;
175-
$$ LANGUAGE plpgsql
176-
IMMUTABLE
177-
LEAKPROOF
178-
PARALLEL SAFE;
179-
180135
-- Get dominant speed for coloring
181136
CREATE OR REPLACE FUNCTION railway_dominant_speed(preferred_direction TEXT, speed TEXT, forward_speed TEXT, backward_speed TEXT) RETURNS INTEGER AS $$
182137
BEGIN

import/sql/signals_with_azimuth.sql

Lines changed: 6 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
CREATE OR REPLACE VIEW signals_with_azimuth_view AS
44
-- TODO investigate signals with null features
55
SELECT
6-
fs.*,
6+
s.*,
77
degrees(ST_Azimuth(
8-
st_lineinterpolatepoint(sl.way, greatest(0, st_linelocatepoint(sl.way, ST_ClosestPoint(sl.way, fs.way)) - 0.01)),
9-
st_lineinterpolatepoint(sl.way, least(1, st_linelocatepoint(sl.way, ST_ClosestPoint(sl.way, fs.way)) + 0.01))
8+
st_lineinterpolatepoint(sl.way, greatest(0, st_linelocatepoint(sl.way, ST_ClosestPoint(sl.way, s.way)) - 0.01)),
9+
st_lineinterpolatepoint(sl.way, least(1, st_linelocatepoint(sl.way, ST_ClosestPoint(sl.way, s.way)) + 0.01))
1010
)) + (CASE WHEN signal_direction = 'backward' THEN 180.0 ELSE 0.0 END) as azimuth,
1111
CASE
1212

@@ -72,41 +72,12 @@ CREATE OR REPLACE VIEW signals_with_azimuth_view AS
7272

7373
{% end %}
7474
END as electrification_feature
75-
FROM (
76-
SELECT
77-
id,
78-
way,
79-
railway,
80-
rank,
81-
ref,
82-
ref_multiline,
83-
signal_direction,
84-
deactivated,
85-
{% for tag in signals_railway_signals.tags %}
86-
"{% tag %}",
87-
{% end %}
88-
{% for tag in speed_railway_signals.tags %}
89-
{% unless tag | matches("railway:signal:speed_limit:speed") %}
90-
{% unless tag | matches("railway:signal:speed_limit_distant:speed") %}
91-
"{% tag %}",
92-
{% end %}
93-
{% end %}
94-
{% end %}
95-
{% for tag in electrification_signals.tags %}
96-
"{% tag %}",
97-
{% end %}
98-
-- We cast the highest speed to text to make it possible to only select those speeds
99-
-- we have an icon for. Otherwise we might render an icon for 40 kph if
100-
-- 42 is tagged (but invalid tagging).
101-
CASE WHEN "railway:signal:speed_limit" IS NOT NULL THEN railway_largest_speed_noconvert("railway:signal:speed_limit:speed")::text ELSE "railway:signal:speed_limit:speed" END AS "railway:signal:speed_limit:speed",
102-
CASE WHEN "railway:signal:speed_limit_distant" IS NOT NULL THEN railway_largest_speed_noconvert("railway:signal:speed_limit_distant:speed")::text ELSE "railway:signal:speed_limit_distant:speed" END AS "railway:signal:speed_limit_distant:speed"
103-
FROM signals s
104-
) AS fs
75+
FROM signals s
10576
LEFT JOIN LATERAL (
10677
SELECT line.way as way
10778
FROM railway_line line
108-
WHERE st_dwithin(fs.way, line.way, 10) AND line.railway IN ('rail', 'tram', 'light_rail', 'subway', 'narrow_gauge', 'construction', 'preserved', 'monorail', 'miniature') -- TODO use feature
109-
ORDER BY fs.way <-> line.way
79+
WHERE st_dwithin(s.way, line.way, 10) AND line.railway IN ('rail', 'tram', 'light_rail', 'subway', 'narrow_gauge', 'construction', 'preserved', 'monorail', 'miniature') -- TODO use feature
80+
ORDER BY s.way <-> line.way
11081
LIMIT 1
11182
) as sl ON true
11283
WHERE

0 commit comments

Comments
 (0)