-
Notifications
You must be signed in to change notification settings - Fork 0
/
crossing.sql
215 lines (203 loc) · 4.25 KB
/
crossing.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
DROP TABLE IF EXISTS crossing CASCADE;
CREATE TABLE crossing
(
objectid serial,
id_origine character varying(50),
incidents smallint,
angle smallint,
geometrie geometry,
osm_highway character varying (150)
)
WITH (OIDS=TRUE);
CREATE INDEX gidx_crossing ON crossing USING GIST(geometrie);
DROP TABLE IF EXISTS tmp_crossing CASCADE;
CREATE TABLE tmp_crossing
AS
SELECT osm_id,
highway,
way geom
FROM planet_osm_point
WHERE highway = 'crossing';
DROP TABLE IF EXISTS tmp_ways_crossing CASCADE;
CREATE TABLE tmp_ways_crossing
AS
SELECT f.id way_id,
unnest(f.nodes) node,
p2.longueur
FROM planet_osm_ways f
JOIN (
SELECT DISTINCT id
FROM (
SELECT osm_id
FROM tmp_crossing
) b
JOIN (
SELECT id,
unnest(nodes) n
FROM planet_osm_ways
WHERE 'highway' = any(tags)
) w
ON b.osm_id = w.n
) j
ON f.id = j.id
JOIN (
SELECT wid,
count(*) longueur
FROM (
(
SELECT id wid,
unnest(nodes) node
FROM planet_osm_ways w
)p0
JOIN planet_osm_nodes n
ON n.id = p0.node
)p1
GROUP BY wid
)p2
ON p2.wid = f.id
LEFT OUTER JOIN planet_osm_polygon p
ON f.id = p.osm_id
WHERE p.osm_id IS NULL;
ALTER TABLE tmp_ways_crossing ADD COLUMN "order" serial;
DROP TABLE IF EXISTS tmp_ways_nodes_rang CASCADE;
CREATE TABLE tmp_ways_nodes_rang
AS
SELECT t.way_id,
t.node,
t.longueur,
rank() over(partition by t.way_id order by t."order") rang,
ST_SetSRID(ST_MakePoint(n.lon::decimal/10000000,n.lat::decimal/10000000),4326) geom
FROM tmp_ways_crossing t
JOIN planet_osm_nodes n
ON t.node = n.id;
DROP TABLE IF EXISTS tmp_nodes_encadrant CASCADE;
CREATE TABLE tmp_nodes_encadrant
AS
SELECT m.way_id,
m.node,
m.rang,
rank() over(partition by m.node order by m.way_id,m.rang) mini,
rank() over(partition by m.node order by m.way_id desc,m.rang desc)maxi
FROM (
SELECT way_id,
node,
rang-1 rang
FROM tmp_ways_nodes_rang
JOIN tmp_crossing
ON node = osm_id
WHERE rang > 1
UNION
SELECT way_id,
node,
rang+1
FROM tmp_ways_nodes_rang
JOIN tmp_crossing
ON node = osm_id
WHERE rang < longueur
) m;
ALTER TABLE tmp_nodes_encadrant ADD COLUMN "order" serial;
TRUNCATE crossing;
-- Barrières sur cul de sac
INSERT INTO crossing(id_origine,
incidents,
angle,
osm_highway,
geometrie)
SELECT id_crossing::text,
1,
(ST_azimuth(geom_crossing,geom_node_apres)/(2*pi()))*360,
highway,
geom_crossing
FROM (
SELECT n.node id_crossing,
g.highway,
g.geom geom_crossing,
nf.geom geom_node_apres,
ns.way_id,
ns.rang
FROM
(
SELECT node
FROM tmp_nodes_encadrant
GROUP BY 1
HAVING count(*) = 1
) n
JOIN tmp_crossing g
ON n.node = g.osm_id
JOIN tmp_nodes_encadrant ns
ON n.node = ns.node AND
ns.maxi = 1
JOIN tmp_ways_nodes_rang nf
ON ns.way_id = nf.way_id AND
ns.rang = nf.rang
)a;
-- Barrière sur un seul axe
INSERT INTO crossing(id_origine,
incidents,
angle,
osm_highway,
geometrie)
SELECT id_crossing::text,
2,
(ST_azimuth(geom_crossing,geom_node_avant)+ST_azimuth(geom_crossing,geom_node_apres))/(2*pi())*180,
highway,
geom_crossing
FROM (
SELECT n.node id_crossing,
g.highway,
g.geom geom_crossing,
nd.geom geom_node_avant,
nf.geom geom_node_apres
FROM
(
SELECT node
FROM tmp_nodes_encadrant
GROUP BY 1
HAVING count(*) = 2
) n
JOIN tmp_crossing g
ON n.node = g.osm_id
JOIN tmp_nodes_encadrant np
ON n.node = np.node AND
np.mini = 1
JOIN tmp_nodes_encadrant ns
ON n.node = ns.node AND
ns.maxi = 1
JOIN tmp_ways_nodes_rang nd
ON np.way_id = nd.way_id AND
np.rang = nd.rang
JOIN tmp_ways_nodes_rang nf
ON ns.way_id = nf.way_id AND
ns.rang = nf.rang
)a;
-- Barrière sur carrefour incident à + de 2 tronçons
INSERT INTO crossing(id_origine,
incidents,
angle,
osm_highway,
geometrie)
SELECT id_crossing::text,
3,
0,
highway,
geom_crossing
FROM (
SELECT n.node id_crossing,
g.highway,
g.geom geom_crossing
FROM
(
SELECT node
FROM tmp_nodes_encadrant
GROUP BY 1
HAVING count(*) > 2
) n
JOIN tmp_crossing g
ON n.node = g.osm_id
)a;
-- Ménage
/*DROP TABLE IF EXISTS tmp_barriers CASCADE;
DROP TABLE IF EXISTS tmp_ways_barriers CASCADE;
DROP TABLE IF EXISTS tmp_ways_nodes_rang CASCADE;
DROP TABLE IF EXISTS tmp_nodes_encadrant CASCADE;
*/