This repository has been archived by the owner on Jul 2, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathprepare-graph-vehicular-speeds.sql
178 lines (166 loc) · 7.97 KB
/
prepare-graph-vehicular-speeds.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
-- Create a node lookup using the startNode and endNode references.
--
-- Table: osmm_highways_route.node_table
DROP TABLE IF EXISTS osmm_highways_route.node_table;
CREATE TABLE osmm_highways_route.node_table AS
SELECT row_number() OVER (ORDER BY foo.p) AS id,
foo.p AS node,
foo.geom
FROM (
SELECT DISTINCT CONCAT(a.startnode, a.startgradeseparation) AS p, ST_Startpoint(geom) AS geom FROM osmm_highways.roadlink a
UNION
SELECT DISTINCT CONCAT(a.endnode, a.endgradeseparation) AS p, ST_Endpoint(geom) AS geom FROM osmm_highways.roadlink a
) foo
GROUP BY foo.p, foo.geom;
CREATE UNIQUE INDEX node_table_id_idx ON osmm_highways_route.node_table (id);
CREATE INDEX node_table_node_idx ON osmm_highways_route.node_table (node);
CREATE INDEX node_table_geom_idx
ON osmm_highways_route.node_table
USING gist
(geom);
-- Create a directed graph which can be used for routing.
-- Uses Basemap average speed data for calculating the fastest route.
--
-- Table: osmm_highways_route.edge_table
DROP TABLE IF EXISTS osmm_highways_route.edge_table;
CREATE TABLE osmm_highways_route.edge_table AS
SELECT row_number() OVER (ORDER BY a.ogc_fid) AS id,
a.id AS fid,
a.roadname AS name,
a.alternatename AS alt_name,
a.roadclassificationnumber AS ref,
a.roadclassification,
a.routehierarchy,
a.formofway,
a.operationalstate,
a.directionality,
a.length,
b.id AS source,
c.id AS target,
CASE
WHEN directionality = 'in opposite direction' THEN -1
ELSE a.length
END AS cost_distance,
CASE
WHEN directionality = 'in direction' THEN -1
ELSE a.length
END AS reverse_cost_distance,
CASE
WHEN directionality = 'in opposite direction' THEN -1
ELSE ((a.length / 1000) / (COALESCE(d.peakam0709monfria + 0.01, 48)) * 60)
END AS cost_time_peakam0709monfri,
CASE
WHEN directionality = 'in direction' THEN -1
WHEN directionality = 'in opposite direction' THEN ((a.length / 1000) / (COALESCE(d.peakam0709monfria + 0.01, 48)) * 60)
ELSE ((a.length / 1000) / (COALESCE(d.peakam0709monfrib + 0.01, 48)) * 60)
END AS reverse_cost_time_peakam0709monfri,
CASE
WHEN directionality = 'in opposite direction' THEN -1
ELSE ((a.length / 1000) / (COALESCE(d.peakpm1619monfria + 0.01, 48)) * 60)
END AS cost_time_peakpm1619monfri,
CASE
WHEN directionality = 'in direction' THEN -1
WHEN directionality = 'in opposite direction' THEN ((a.length / 1000) / (COALESCE(d.peakpm1619monfria + 0.01, 48)) * 60)
ELSE ((a.length / 1000) / (COALESCE(d.peakpm1619monfrib + 0.01, 48)) * 60)
END AS reverse_cost_time_peakpm1619monfri,
CASE
WHEN directionality = 'in opposite direction' THEN -1
ELSE ((a.length / 1000) / (COALESCE(d.offpeak1016monfria + 0.01, 48)) * 60)
END AS cost_time_offpeak1016monfri,
CASE
WHEN directionality = 'in direction' THEN -1
WHEN directionality = 'in opposite direction' THEN ((a.length / 1000) / (COALESCE(d.offpeak1016monfria + 0.01, 48)) * 60)
ELSE ((a.length / 1000) / (COALESCE(d.offpeak1016monfrib + 0.01, 48)) * 60)
END AS reverse_cost_time_offpeak1016monfri,
CASE
WHEN directionality = 'in opposite direction' THEN -1
ELSE ((a.length / 1000) / (COALESCE(d.eveningspeed1923everydaya + 0.01, 48)) * 60)
END AS cost_time_eveningspeed1923everyday,
CASE
WHEN directionality = 'in direction' THEN -1
WHEN directionality = 'in opposite direction' THEN ((a.length / 1000) / (COALESCE(d.eveningspeed1923everydaya + 0.01, 48)) * 60)
ELSE ((a.length / 1000) / (COALESCE(d.eveningspeed1923everydayb + 0.01, 48)) * 60)
END AS reverse_cost_time_eveningspeed1923everyday,
CASE
WHEN directionality = 'in opposite direction' THEN -1
ELSE ((a.length / 1000) / (COALESCE(d.nighttime0004everydaya + 0.01, 48)) * 60)
END AS cost_time_nighttime0004everyday,
CASE
WHEN directionality = 'in direction' THEN -1
WHEN directionality = 'in opposite direction' THEN ((a.length / 1000) / (COALESCE(d.nighttime0004everydaya + 0.01, 48)) * 60)
ELSE ((a.length / 1000) / (COALESCE(d.nighttime0004everydayb + 0.01, 48)) * 60)
END AS reverse_cost_time_nighttime0004everyday,
CASE
WHEN directionality = 'in opposite direction' THEN -1
ELSE ((a.length / 1000) / (COALESCE(d.weekend0719a + 0.01, 48)) * 60)
END AS cost_time_weekend0719,
CASE
WHEN directionality = 'in direction' THEN -1
WHEN directionality = 'in opposite direction' THEN ((a.length / 1000) / (COALESCE(d.weekend0719a + 0.01, 48)) * 60)
ELSE ((a.length / 1000) / (COALESCE(d.weekend0719b + 0.01, 48)) * 60)
END AS reverse_cost_time_weekend0719,
ST_X(ST_StartPoint(a.geom)) AS x1,
ST_Y(ST_StartPoint(a.geom)) AS y1,
ST_X(ST_EndPoint(a.geom)) AS x2,
ST_Y(ST_EndPoint(a.geom)) AS y2,
a.geom
FROM osmm_highways.roadlink a
JOIN osmm_highways_route.node_table AS b ON CONCAT(a.startnode, a.startgradeseparation) = b.node
JOIN osmm_highways_route.node_table AS c ON CONCAT(a.endnode, a.endgradeseparation) = c.node
LEFT OUTER JOIN osmm_highways_speeds.average_speeds AS d ON a.id = d.roadlinkid;
CREATE UNIQUE INDEX edge_table_id_idx ON osmm_highways_route.edge_table (id);
CREATE INDEX edge_table_source_idx ON osmm_highways_route.edge_table (source);
CREATE INDEX edge_table_target_idx ON osmm_highways_route.edge_table (target);
CREATE INDEX edge_table_routehierarchy_idx ON osmm_highways_route.edge_table (routehierarchy);
CREATE INDEX edge_table_operationalstate_idx ON osmm_highways_route.edge_table (operationalstate);
CREATE INDEX edge_table_geom_idx
ON osmm_highways_route.edge_table
USING gist
(geom);
-- [OPTIONAL] Add vehicular ferry routes to the directed graph.
--
-- Table: osmm_highways_route.ferryterminal
CREATE TABLE osmm_highways_route.ferryterminal AS
SELECT id,
array_agg(identifier) AS element_href
FROM osmm_highways.ferryterminal_element
WHERE id IN (SELECT id FROM osmm_highways.ferryterminal_element WHERE role = 'RoadNode')
GROUP BY id;
INSERT INTO osmm_highways_route.edge_table
SELECT row_number() OVER (ORDER BY a.ogc_fid) + 6000000 AS id,
a.id AS fid,
array[]::text[] AS name,
array[]::text[] AS alt_name,
'' AS ref,
'' AS roadclassification,
'Ferry Route' AS routehierarchy,
'' AS formofway,
'Open' AS operationalstate,
'both directions' AS directionality,
ST_Length(a.geom) AS length,
(SELECT id FROM osmm_highways_route.node_table WHERE node = CONCAT(b.element_href[2], 0)) AS source,
(SELECT id FROM osmm_highways_route.node_table WHERE node = CONCAT(c.element_href[2], 0)) AS target,
ROUND(ST_Length(a.geom)::numeric, 2) AS cost_distance,
ROUND(ST_Length(a.geom)::numeric, 2) AS reverse_cost_distance,
1 AS cost_time_peakam0709monfri,
1 AS reverse_cost_time_peakam0709monfri,
1 AS cost_time_peakpm1619monfri,
1 AS reverse_cost_time_peakpm1619monfri,
1 AS cost_time_offpeak1016monfri,
1 AS reverse_cost_time_offpeak1016monfri,
1 AS cost_time_eveningspeed1923everyday,
1 AS reverse_cost_time_eveningspeed1923everyday,
1 AS cost_time_nighttime0004everyday,
1 AS reverse_cost_time_nighttime0004everyday,
1 AS cost_time_weekend0719,
1 AS reverse_cost_time_weekend0719,
ST_X(ST_StartPoint(a.geom)) AS x1,
ST_Y(ST_StartPoint(a.geom)) AS y1,
ST_X(ST_EndPoint(a.geom)) AS x2,
ST_Y(ST_EndPoint(a.geom)) AS y2,
a.geom
FROM osmm_highways.ferrylink a
JOIN osmm_highways_route.ferryterminal AS b ON a.startnode = b.element_href[1]
JOIN osmm_highways_route.ferryterminal AS c ON a.endnode = c.element_href[1];
REINDEX TABLE osmm_highways_route.edge_table
DROP TABLE osmm_highways_route.ferryterminal;