-
Notifications
You must be signed in to change notification settings - Fork 2
/
prepare_milestones.sql
101 lines (94 loc) · 3.39 KB
/
prepare_milestones.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
-- SPDX-License-Identifier: GPL-2.0-or-later
-- Prepare the database for querying milestones
CREATE OR REPLACE FUNCTION railway_api_valid_float(value TEXT) RETURNS FLOAT AS $$
BEGIN
IF value ~ '^-?[0-9]+(\.[0-9]+)$' THEN
RETURN value::FLOAT;
END IF;
RETURN NULL;
END;
$$ LANGUAGE plpgsql;
CREATE MATERIALIZED VIEW IF NOT EXISTS openrailwaymap_milestones AS
SELECT DISTINCT ON (osm_id) osm_id, position, precision, railway, name, ref, tags, geom
FROM (
SELECT osm_id, position, precision, railway, name, ref, tags, geom
FROM (
SELECT
osm_id,
railway_api_valid_float(unnest(string_to_array(tags->'railway:position', ';'))) AS position,
1::SMALLINT AS precision,
railway,
name,
ref,
tags,
way AS geom
FROM planet_osm_point
WHERE
(
railway IS NOT NULL
OR tags?'disused:railway'
OR tags?'abandoned:railway'
OR tags?'construction:railway'
OR tags?'proposed:railway'
OR tags?'razed:railway'
) AND tags?'railway:position'
UNION ALL
SELECT
osm_id,
railway_api_valid_float(unnest(string_to_array(tags->'railway:position:exact', ';'))) AS position,
3::SMALLINT AS precision,
railway,
name,
ref,
tags,
way AS geom
FROM planet_osm_point
WHERE
(
railway IS NOT NULL
OR tags?'disused:railway'
OR tags?'abandoned:railway'
OR tags?'construction:railway'
OR tags?'proposed:railway'
OR tags?'razed:railway'
) AND tags?'railway:position:exact'
) AS features_with_position
WHERE position IS NOT NULL
ORDER BY osm_id ASC, precision DESC
) AS duplicates_merged;
CREATE INDEX IF NOT EXISTS openrailwaymap_milestones_geom_idx
ON openrailwaymap_milestones
USING gist(geom);
CREATE INDEX IF NOT EXISTS openrailwaymap_milestones_position_idx
ON openrailwaymap_milestones
USING gist(geom);
CREATE OR REPLACE VIEW openrailwaymap_tracks_with_ref AS
SELECT
osm_id,
railway,
name,
ref,
tags,
way AS geom
FROM planet_osm_line
WHERE
railway IN ('rail', 'narrow_gauge', 'subway', 'light_rail', 'tram', 'construction', 'proposed', 'disused', 'abandoned', 'razed')
AND (NOT (tags?'service') OR tags->'usage' IN ('industrial', 'military', 'test'))
AND ref IS NOT NULL
AND osm_id > 0;
CREATE INDEX IF NOT EXISTS planet_osm_line_ref_geom_idx
ON planet_osm_line
USING gist(way)
WHERE
railway IN ('rail', 'narrow_gauge', 'subway', 'light_rail', 'tram', 'construction', 'proposed', 'disused', 'abandoned', 'razed')
AND (NOT tags?'service' OR tags->'usage' IN ('industrial', 'military', 'test'))
AND ref IS NOT NULL
AND osm_id > 0;
CREATE INDEX IF NOT EXISTS planet_osm_line_ref_idx
ON planet_osm_line
USING btree(ref)
WHERE
railway IN ('rail', 'narrow_gauge', 'subway', 'light_rail', 'tram', 'construction', 'proposed', 'disused', 'abandoned', 'razed')
AND (NOT tags?'service' OR tags->'usage' IN ('industrial', 'military', 'test'))
AND ref IS NOT NULL
AND osm_id > 0;