Ticket #177: osm2pgrouting.sql

File osm2pgrouting.sql, 3.6 KB (added by milovanderlinden, 14 months ago)

Script to create tables from the openstreetmap tables for routing

Line 
1drop view topology;
2/* ways */
3drop table ways;
4create table ways as (
5select
6id as gid,
7length_spheroid( planet_osm_line.way, 'SPHEROID["WGS 84",6378137,298.257223563,AUTHORITY["EPSG","7030"]]') as length,
8name,
9way as the_geom,
10nodes[array_lower(nodes,1)] as sourcenode,
11nodes[array_upper(nodes,1)] as targetnode
12from
13planet_osm_ways,
14planet_osm_line
15where
16planet_osm_line.osm_id = planet_osm_ways.id
17and not planet_osm_line.highway is null);
18
19alter table ways ADD CONSTRAINT ways_pk PRIMARY KEY (gid);
20alter table ways ADD CONSTRAINT enforce_dims_the_geom CHECK (ndims(the_geom) = 2);
21alter table ways ADD CONSTRAINT enforce_geotype_the_geom CHECK (geometrytype(the_geom) = 'LINESTRING'::text OR the_geom IS NULL);
22alter table ways ADD CONSTRAINT enforce_srid_the_geom CHECK (srid(the_geom) = 4326);
23
24ALTER TABLE ways ADD COLUMN source integer;
25ALTER TABLE ways ADD COLUMN target integer;
26ALTER TABLE ways ADD COLUMN x1 double precision;
27ALTER TABLE ways ADD COLUMN y1 double precision;
28ALTER TABLE ways ADD COLUMN x2 double precision;
29ALTER TABLE ways ADD COLUMN y2 double precision;
30UPDATE ways SET x1 = x(startpoint(the_geom));
31UPDATE ways SET y1 = y(startpoint(the_geom));
32UPDATE ways SET x2 = x(endpoint(the_geom));
33UPDATE ways SET y2 = y(endpoint(the_geom));
34ALTER TABLE ways ADD COLUMN reverse_cost double precision;
35UPDATE ways SET reverse_cost = length;
36ALTER TABLE ways ADD COLUMN to_cost double precision;
37ALTER TABLE ways ADD COLUMN rule text;
38
39CREATE INDEX source_idx ON ways(source);
40CREATE INDEX target_idx ON ways(target);
41CREATE INDEX geom_idx ON ways USING GIST(the_geom GIST_GEOMETRY_OPS);
42
43drop table vertices_tmp;
44SELECT assign_vertex_id('ways', 0.00001, 'the_geom', 'gid');
45
46/* nodes */
47DROP TABLE nodes;
48CREATE TABLE nodes as
49select id as gid, geomfromtext('POINT(' || cast(lon as float) / 10000000 || ' ' || cast(lat as float) / 10000000 || ')', 4326) as the_geom from planet_osm_nodes;
50alter table nodes ADD CONSTRAINT nodes_pk PRIMARY KEY (gid);
51alter table nodes ADD CONSTRAINT enforce_dims_the_geom CHECK (ndims(the_geom) = 2);
52alter table nodes ADD CONSTRAINT enforce_geotype_the_geom CHECK (geometrytype(the_geom) = 'POINT'::text OR the_geom IS NULL);
53alter table nodes ADD CONSTRAINT enforce_srid_the_geom CHECK (srid(the_geom) = 4326);
54
55select probe_geometry_columns();
56
57
58/* latency */
59drop table latency;
60create table latency as select id, the_geom,
61(select count(*) from ways where vertices_tmp.id = source or vertices_tmp.id = target) as count from vertices_tmp order by count;
62alter table latency add constraint latency_pk PRIMARY KEY (id);
63
64/* topology */
65
66create view topology as select
67        gid,
68        --source,
69        --slcy.count as source_latency,
70        --target,
71        --tlcy.count as target_latency,
72        ways.the_geom,
73        CASE
74        WHEN slcy.count = 1 AND tlcy.count = 1 THEN 'Detached'
75        WHEN slcy.count = 1 AND tlcy.count > 1 THEN 'Cul de Sac'
76        WHEN slcy.count > 1 AND tlcy.count = 1 THEN 'Cul de Sac'
77        WHEN slcy.count > 1 AND tlcy.count > 1 THEN NULL
78        WHEN source = target THEN 'Loop'
79        END as network_type
80from ways, latency as slcy, latency as tlcy
81where source = slcy.id and target = tlcy.id order by network_type;
82
83update ways set reverse_cost = length;
84update ways set to_cost = length;
85update ways set reverse_cost = 999999 from planet_osm_line where planet_osm_line.osm_id = ways.gid AND lower(oneway) = 'yes';
86update ways set reverse_cost = 999999 from planet_osm_line where planet_osm_line.osm_id = ways.gid AND lower(oneway) = 'yes';
87update ways set reverse_cost = 999999 from planet_osm_line where planet_osm_line.osm_id = ways.gid AND lower(oneway) = 'true';
88update ways set to_cost = 999999 from planet_osm_line where planet_osm_line.osm_id = ways.gid AND lower(oneway) = '-1';