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