|
| 1 | +SELECT * |
| 2 | + FROM osm.pgosm_flex |
| 3 | +; |
| 4 | + |
| 5 | + |
| 6 | +CREATE EXTENSION IF NOT EXISTS pgrouting; |
| 7 | +CREATE SCHEMA IF NOT EXISTS routing; |
| 8 | + |
| 9 | + |
| 10 | +SELECT postgis_full_version(), pgr_version(); |
| 11 | + |
| 12 | + |
| 13 | + |
| 14 | +CREATE TABLE routing.road_line AS |
| 15 | +WITH a AS ( |
| 16 | +-- Remove as many multi-linestrings as possible with ST_LineMerge() |
| 17 | +SELECT osm_id, osm_type, maxspeed, oneway, layer, |
| 18 | + route_foot, route_cycle, route_motor, access, |
| 19 | + ST_LineMerge(geom) AS geom |
| 20 | + FROM osm.road_line |
| 21 | +), extra_cleanup AS ( |
| 22 | +-- Pull out those that are still multi, use ST_Dump() to pull out parts |
| 23 | +SELECT osm_id, osm_type, maxspeed, oneway, layer, |
| 24 | + route_foot, route_cycle, route_motor, access, |
| 25 | + (ST_Dump(geom)).geom AS geom |
| 26 | + FROM a |
| 27 | + WHERE ST_GeometryType(geom) = 'ST_MultiLineString' |
| 28 | +), combined AS ( |
| 29 | +-- Combine two sources |
| 30 | +SELECT osm_id, osm_type, maxspeed, oneway, layer, |
| 31 | + route_foot, route_cycle, route_motor, access, |
| 32 | + geom |
| 33 | + FROM a |
| 34 | + WHERE ST_GeometryType(geom) != 'ST_MultiLineString' |
| 35 | +UNION |
| 36 | +SELECT osm_id, osm_type, maxspeed, oneway, layer, |
| 37 | + route_foot, route_cycle, route_motor, access, |
| 38 | + geom |
| 39 | + FROM extra_cleanup |
| 40 | + -- Some data may be lost here if multi-linestring somehow |
| 41 | + -- persists through the extra_cleanup query |
| 42 | + WHERE ST_GeometryType(geom) != 'ST_MultiLineString' |
| 43 | +) |
| 44 | +-- Calculate a new surrogate ID for key |
| 45 | +SELECT ROW_NUMBER() OVER (ORDER BY geom) AS id, * |
| 46 | + FROM combined |
| 47 | + ORDER BY geom |
| 48 | +; |
| 49 | + |
| 50 | + |
| 51 | + |
| 52 | +COMMENT ON COLUMN routing.road_line.id IS 'Surrogate ID, cannot rely on osm_id being unique after converting multi-linestrings to linestrings.'; |
| 53 | +ALTER TABLE routing.road_line |
| 54 | + ADD CONSTRAINT pk_routing_road_line PRIMARY KEY (id) |
| 55 | +; |
| 56 | +CREATE INDEX ix_routing_road_line_osm_id |
| 57 | + ON routing.road_line (osm_id) |
| 58 | +; |
| 59 | + |
| 60 | + |
| 61 | +SELECT COUNT(*) FROM routing.road_line WHERE route_motor |
| 62 | + |
| 63 | + |
| 64 | +--SELECT pgr_nodeNetwork('routing.road_line', 0.1, 'id', 'geom'); |
| 65 | +/* |
| 66 | + * Seperate Crossing splits at intersections with actual crossings where both lines |
| 67 | + * extend to the other side. |
| 68 | + * It does NOT split long lines at intersections where one section extends past |
| 69 | + * another section. AKA - T-Intersections. |
| 70 | + * Unless Routing functions have become more flexible, this will NOT work with |
| 71 | + * most common traffic routing use cases. |
| 72 | + * |
| 73 | + * The ID comes through the new table with a new sub_id value. |
| 74 | + * Only creates records where splitting was done. |
| 75 | + * |
| 76 | + * Fort Collins sub-region took ~30 seconds |
| 77 | + */ |
| 78 | +DROP TABLE IF EXISTS routing.road_separate_crossing; |
| 79 | +--started at 8:20:30 |
| 80 | +CREATE TABLE routing.road_separate_crossing AS |
| 81 | +SELECT * |
| 82 | +FROM pgr_separateCrossing('SELECT id, geom FROM routing.road_line WHERE route_motor', dryrun => false) |
| 83 | +; |
| 84 | + |
| 85 | +/* |
| 86 | + * Takes much longer than pgr_separateCrossing |
| 87 | + * |
| 88 | + * Seperate Crossing splits at touch points where two lines intersect |
| 89 | + * This DOES split long lines at intersections where one section intersects another |
| 90 | + * in the middle, AKA t-intesections |
| 91 | + * |
| 92 | + * The ID comes through the new table with a new sub_id value. |
| 93 | + * Only creates records where splitting was done. Will need to merge with |
| 94 | + * unsplit lines from source table. |
| 95 | + * |
| 96 | + * Fort Collins sub-region took 9 minutes (25k inputs, 21k outputs) |
| 97 | + * |
| 98 | + * NOTE: Only seq, id, sub_id, and geom columns make it to final table. Does not help |
| 99 | + * to pass in SELECT * thinking you'll get all the columns in the final table. |
| 100 | + * (I tried to be lazier in later steps) |
| 101 | + */ |
| 102 | +DROP TABLE IF EXISTS routing.road_separate_touching; |
| 103 | +CREATE TABLE routing.road_separate_touching AS |
| 104 | +SELECT * |
| 105 | +FROM pgr_separateTouching('SELECT id, geom FROM routing.road_line WHERE route_motor', dryrun => false) |
| 106 | +; |
| 107 | + |
| 108 | + |
| 109 | + |
| 110 | +SELECT * FROM routing.road_line |
| 111 | +; |
| 112 | +SELECT * FROM routing.road_separate_touching |
| 113 | +; |
| 114 | + |
| 115 | +DROP TABLE IF EXISTS routing.road_motor_edges; |
| 116 | +CREATE TABLE routing.road_motor_edges AS |
| 117 | +WITH split_lines AS ( |
| 118 | +SELECT r.id AS parent_id, spl.sub_id, r.osm_id, r.osm_type, r.maxspeed, r.oneway, r.layer |
| 119 | + , r.access, spl.geom |
| 120 | + FROM routing.road_line r |
| 121 | + INNER JOIN routing.road_separate_touching spl |
| 122 | + ON r.id = spl.id |
| 123 | + WHERE route_motor |
| 124 | +), unsplit_lines AS ( |
| 125 | +SELECT r.id AS parent_id, 1::INT AS sub_id, r.osm_id, r.osm_type, r.maxspeed, r.oneway, r.layer |
| 126 | + , r.access, r.geom |
| 127 | + FROM routing.road_line r |
| 128 | + LEFT JOIN routing.road_separate_touching spl |
| 129 | + ON r.id = spl.id |
| 130 | + WHERE spl.id IS NULL |
| 131 | + AND r.route_motor |
| 132 | +) |
| 133 | +SELECT * |
| 134 | + FROM split_lines |
| 135 | +UNION |
| 136 | +SELECT * |
| 137 | + FROM unsplit_lines |
| 138 | +; |
| 139 | + |
| 140 | +COMMENT ON TABLE routing.road_motor_edges IS 'OSM road data setup for edges for routing for motorized travel'; |
| 141 | +ALTER TABLE routing.road_motor_edges |
| 142 | + ADD edge_id BIGINT NOT NULL GENERATED ALWAYS AS IDENTITY PRIMARY KEY; |
| 143 | +ALTER TABLE routing.road_motor_edges |
| 144 | + ADD source BIGINT; |
| 145 | +ALTER TABLE routing.road_motor_edges |
| 146 | + ADD target BIGINT; |
| 147 | +; |
| 148 | +ALTER TABLE routing.road_motor_edges |
| 149 | + ADD CONSTRAINT uq_routing_road_motor_edges_parent_id_sub_id |
| 150 | + UNIQUE (parent_id, sub_id) |
| 151 | +; |
| 152 | + |
| 153 | + |
| 154 | +----------------------------------------------------------------------- |
| 155 | +-- SELECT pgr_createTopology('routing.road_line_noded', 0.1, 'geom'); |
| 156 | +----------------------------------------------------------------------- |
| 157 | +DROP TABLE IF EXISTS routing.road_motor_vertices; |
| 158 | +CREATE TABLE routing.road_motor_vertices AS |
| 159 | +SELECT * FROM pgr_extractVertices( |
| 160 | + 'SELECT edge_id AS id, geom FROM routing.road_motor_edges') |
| 161 | +; |
| 162 | + |
| 163 | + |
| 164 | + |
| 165 | + |
| 166 | +SELECT * |
| 167 | +FROM routing.road_motor_vertices |
| 168 | +WHERE in_edges IS NOT NULL |
| 169 | + OR out_edges IS NOT NULL |
| 170 | +; |
| 171 | + |
| 172 | + |
| 173 | + |
| 174 | +------------------------------------------------- |
| 175 | +--SELECT pgr_analyzeGraph('routing.road_line_noded', 0.1, 'geom'); |
| 176 | + |
| 177 | +-- Update source column from out_edges |
| 178 | + |
| 179 | +WITH outgoing AS ( |
| 180 | + SELECT id AS source |
| 181 | + , unnest(out_edges) AS edge_id |
| 182 | + --, x, y |
| 183 | + FROM routing.road_motor_vertices |
| 184 | +) |
| 185 | +UPDATE routing.road_motor_edges e |
| 186 | +SET source = o.source--, x1 = x, y1 = y |
| 187 | +FROM outgoing o |
| 188 | +WHERE e.edge_id = o.edge_id |
| 189 | + AND e.source IS NULL |
| 190 | +; |
| 191 | + |
| 192 | +-- Update target colum from in_edges |
| 193 | +WITH incoming AS ( |
| 194 | + SELECT id AS target |
| 195 | + , unnest(in_edges) AS edge_id |
| 196 | + --, x, y |
| 197 | + FROM routing.road_motor_vertices |
| 198 | +) |
| 199 | +UPDATE routing.road_motor_edges e |
| 200 | +SET target = i.target--, x1 = x, y1 = y |
| 201 | +FROM incoming i |
| 202 | +WHERE e.edge_id = i.edge_id |
| 203 | + AND e.target IS NULL |
| 204 | +; |
| 205 | + |
| 206 | + |
| 207 | +SELECT * |
| 208 | + FROM routing.road_motor_edges |
| 209 | +; |
| 210 | + |
| 211 | + |
| 212 | + |
| 213 | +---------------------- |
| 214 | +-- TRYING TO CONTINUE HERE! |
| 215 | +ALTER TABLE routing.road_motor_edges |
| 216 | + ADD cost_length DOUBLE PRECISION NOT NULL |
| 217 | + GENERATED ALWAYS AS (ST_Length(geom)) |
| 218 | + STORED; |
| 219 | + |
| 220 | + |
| 221 | +/* |
| 222 | + * v_start: 12181 |
| 223 | +* v_end: 10402 |
| 224 | + */ |
| 225 | + |
| 226 | + |
| 227 | +SELECT * FROM routing.road_motor_edges; |
| 228 | +SELECT * FROM routing.road_motor_vertices; |
| 229 | + |
| 230 | + |
| 231 | +SELECT d.*, n.geom AS node_geom, e.geom AS edge_geom |
| 232 | + FROM pgr_dijkstra( |
| 233 | + 'SELECT edge_id AS id, source, target, cost_length AS cost, |
| 234 | + geom |
| 235 | + FROM routing.road_motor_edges |
| 236 | + ', |
| 237 | + :start_id, :end_id, directed := False |
| 238 | + ) d |
| 239 | + INNER JOIN routing.road_motor_vertices n ON d.node = n.id |
| 240 | + LEFT JOIN routing.road_motor_edges e ON d.edge = e.edge_id |
| 241 | +; |
| 242 | + |
| 243 | + |
| 244 | + |
0 commit comments