root/branches/workshop/FOSS4G2008/Docs/06_pgrouting.topology.chapter

Revision 231, 4.5 KB (checked in by daniel, 2 years ago)

workshop: topology chapter and installation updates

Line 
1= Load your network data and create a network topology =
2
3Some network data already comes with a network topology that can be used with
4pgRouting immediately. But usually the data is in a different format than we
5need for pgRouting. Often network data is stored in the Shape file format (.shp)
6and we can use PostGIS' shape2postgresql converter to import the data into the
7database. OpenStreetMap stores its data as XML and it has its own importing
8tools for PostgreSQL database.
9
10Later we will use the osm2pgrouting converter. But it does much more than the
11basic steps for simple routing, so we will start this workshop with the minimum
12required attributes.
13
14== Load the network data ==
15
16After creating the workshop database and adding the PostGIS and pgRouting
17functions to this database (see previous chapter), we load the sample data to
18our database:
19{{{
20psql -U postgres routing
21\i /home/foss4g/ways_without_topology.sql
22}}}
23
24Note: The SQL dump file was made from a database which already had PostGIS
25functions loaded, so it will report errors during import that these functions
26already exist. You can ignore these errors.
27
28Let's see witch tables have been created:
29{{{
30\d
31
32              List of relations
33 Schema |       Name       | Type  |  Owner   
34--------+------------------+-------+----------
35 public | geometry_columns | table | postgres
36 public | spatial_ref_sys  | table | postgres
37 public | ways             | table | postgres
38(3 rows)
39
40\d ways
41
42           Table "public.ways"
43  Column  |       Type       | Modifiers
44----------+------------------+-----------
45 gid      | integer          | not null
46 length   | double precision |
47 name     | character(200)   |
48 the_geom | geometry         |
49Indexes:
50    "ways_pkey" PRIMARY KEY, btree (gid)
51Check constraints:
52    "enforce_dims_the_geom" CHECK (ndims(the_geom) = 2)
53    "enforce_geotype_the_geom" CHECK (geometrytype(the_geom) = 'MULTILINESTRING'::text OR the_geom IS NULL)
54    "enforce_srid_the_geom" CHECK (srid(the_geom) = 4326)
55}}}
56
57== Create network topology ==
58
59Having your data imported into a PostgreSQL database usually requires one more
60step for pgRouting. You have to make sure that your data provides a correct
61network topology, which consists of links with source and target ID each.
62
63If your network data doesn't have such network topology information already
64you need to run the "assign_vertex_id" function. This function assigns a source
65and a target ID to each link and it can "snap" nearby vertices within a
66certain tolerance.
67
68{{{
69assign_vertex_id('<table>', float tolerance, '<geometry column', '<gid>')
70}}}
71
72First we have to add source and target column, then we run the assign_vertex_id
73function ... and wait.
74{{{
75ALTER TABLE ways ADD COLUMN source integer;
76ALTER TABLE ways ADD COLUMN target integer;
77SELECT assign_vertex_id('ways', 0.00001, 'the_geom', 'gid');
78}}}
79
80Note: The dimension of the tolerance parameter depends on your data projection.
81Usually it's either "degrees" or "meters". Because OSM data has a very good
82quality for Cape town we can choose a very small "snapping" tolerance:
830.00001 degrees
84
85== Add indices ==
86
87Fortunately we didn't need to wait too long because the data is small. But your
88network data might be very large, so it's a good idea to add an index on source,
89target and geometry column.
90{{{
91CREATE INDEX source_idx ON ways(source);
92CREATE INDEX target_idx ON ways(target);
93CREATE INDEX geom_idx ON ways USING GIST(the_geom GIST_GEOMETRY_OPS);
94}}}
95
96After these steps our routing database look like this:
97{{{
98\d
99
100                 List of relations
101 Schema |        Name         |   Type   |  Owner   
102--------+---------------------+----------+----------
103 public | geometry_columns    | table    | postgres
104 public | spatial_ref_sys     | table    | postgres
105 public | vertices_tmp        | table    | postgres
106 public | vertices_tmp_id_seq | sequence | postgres
107 public | ways                | table    | postgres
108(5 rows)
109
110\d ways
111
112           Table "public.ways"
113  Column  |       Type       | Modifiers
114----------+------------------+-----------
115 gid      | integer          | not null
116 length   | double precision |
117 name     | character(200)   |
118 the_geom | geometry         |
119 source   | integer          |
120 target   | integer          |
121Indexes:
122    "ways_pkey" PRIMARY KEY, btree (gid)
123Check constraints:
124    "enforce_dims_the_geom" CHECK (ndims(the_geom) = 2)
125    "enforce_geotype_the_geom" CHECK (geometrytype(the_geom) = 'MULTILINESTRING'::text OR the_geom IS NULL)
126    "enforce_srid_the_geom" CHECK (srid(the_geom) = 4326)
127}}}
128
129Now we are ready for routing with Dijkstra algorithm!
Note: See TracBrowser for help on using the browser.