Bienvenue sur PostGIS.fr

Bienvenue sur PostGIS.fr , le site de la communauté des utilisateurs francophones de PostGIS.

PostGIS ajoute le support d'objets géographique à la base de données PostgreSQL. En effet, PostGIS "spatialise" le serverur PostgreSQL, ce qui permet de l'utiliser comme une base de données SIG.

Maintenu à jour, en fonction de nos disponibilités et des diverses sorties des outils que nous testons, nous vous proposons l'ensemble de nos travaux publiés en langue française.

source: trunk/workshop-routing-foss4g/chapters/advanced.rst @ 69

Revision 63, 6.4 KB checked in by djay, 13 years ago (diff)

Initial import of pgROuting workshop for translation. Section 1 to 3 translated, pleae review.

RevLine 
[63]1==============================================================================================================
2Advanced Routing Queries
3==============================================================================================================
4
5As explained in the previous chapter a shortest path query usualy looks like this:
6
7.. code-block:: sql
8
9        SELECT * FROM shortest_path_shooting_star(
10                'SELECT gid as id, source, target, length as cost, x1, y1, x2, y2, rule,
11                to_cost, reverse_cost FROM ways', 6585, 8247, true, true);
12       
13This is usually called **shortest** path, which means that a length of an edge is its cost. But cost doesn't need to be length, cost can be almost anything, for example time, slope, surface, road type, etc.. Or it can be a combination of multiple parameters ("Weighted costs").
14
15.. note::
16
17        If you want to proceed with a routing database containing pgRouting functions, sample data and all required attributes, you can load the following database dump file.
18
19.. code-block:: bash
20
21        # Optional: Drop database
22        dropdb -U postgres pgrouting-workshop
23
24        # Load database dump file
25        psql -U postgres -f ~/Desktop/pgrouting-workshop/data/sampledata_routing.sql
26
27-------------------------------------------------------------------------------------------------------------
28Weighted costs
29-------------------------------------------------------------------------------------------------------------
30
31In real networks there are different limitations or preferences for different road types for example. In other words, we don't want to get the *shortest* but the **cheapest** path - a path with a minimal cost. There is no limitation in what we take as costs.
32
33When we convert data from OSM format using the osm2pgrouting tool, we get two additional tables for road ``types`` and road ``classes``:
34
35.. note::
36
37        We switch now to the database we previously generated with osm2pgrouting. From within PostgreSQL shell this is possible with the ``\c routing`` command.
38
39.. rubric:: Run: ``SELECT * FROM types;``
40
41.. code-block:: sql
42
43          id |   name   
44        -----+------------
45           2 | cycleway
46           1 | highway
47           4 | junction
48           3 | tracktype
49   
50.. rubric:: Run: ``SELECT * FROM classes;``
51
52.. code-block:: sql
53
54         id  | type_id |        name        |  cost
55        -----+---------+--------------------+--------
56         201 |       2 | lane               |     
57         204 |       2 | opposite           |     
58         203 |       2 | opposite_lane      |   
59         202 |       2 | track              |     
60         117 |       1 | bridleway          |     
61         113 |       1 | bus_guideway       |     
62         118 |       1 | byway              |     
63         115 |       1 | cicleway           |     
64         116 |       1 | footway            |     
65         108 |       1 | living_street      |     
66         101 |       1 | motorway           |   
67         103 |       1 | motorway_junction  |     
68         102 |       1 | motorway_link      |     
69         114 |       1 | path               |     
70         111 |       1 | pedestrian         |     
71         106 |       1 | primary            |     
72         107 |       1 | primary_link       |     
73         107 |       1 | residential        |     
74         100 |       1 | road               |     
75         100 |       1 | unclassified       |     
76         106 |       1 | secondary          |   
77         109 |       1 | service            |     
78         112 |       1 | services           |     
79         119 |       1 | steps              |     
80         107 |       1 | tertiary           |     
81         110 |       1 | track              |     
82         104 |       1 | trunk              |     
83         105 |       1 | trunk_link         |     
84         401 |       4 | roundabout         |     
85         301 |       3 | grade1             |     
86         302 |       3 | grade2             |     
87         303 |       3 | grade3             |     
88         304 |       3 | grade4             |     
89         305 |       3 | grade5             |     
90
91The road class is linked with the ways table by ``class_id`` field. After importing data the ``cost`` attribute is not set yet. Its values can be changed with an ``UPDATE`` query. In this example cost values for the classes table are assigned arbitrary, so we execute:
92
93.. code-block:: sql
94
95        UPDATE classes SET cost=1 ;
96        UPDATE classes SET cost=2.0 WHERE name IN ('pedestrian','steps','footway');
97        UPDATE classes SET cost=1.5 WHERE name IN ('cicleway','living_street','path');
98        UPDATE classes SET cost=0.8 WHERE name IN ('secondary','tertiary');
99        UPDATE classes SET cost=0.6 WHERE name IN ('primary','primary_link');
100        UPDATE classes SET cost=0.4 WHERE name IN ('trunk','trunk_link');
101        UPDATE classes SET cost=0.3 WHERE name IN ('motorway','motorway_junction','motorway_link');
102
103For better performance, especially if the network data is large, it is better to create an index on the ``class_id`` field of the ways table and eventually on the ``id`` field of the ``types`` table.
104
105.. code-block:: sql
106
107        CREATE INDEX ways_class_idx ON ways (class_id);
108        CREATE INDEX classes_idx ON classes (id);
109
110The idea behind these two tables is to specify a factor to be multiplied with the cost of each link (usually length):
111
112.. code-block:: sql
113
114        SELECT * FROM shortest_path_shooting_star(
115                'SELECT gid as id, class_id, source, target, length*c.cost as cost,
116                        x1, y1, x2, y2, rule, to_cost, reverse_cost*c.cost as reverse_cost
117                FROM ways w, classes c
118                WHERE class_id=c.id', 6585, 8247, true, true);
119
120-------------------------------------------------------------------------------------------------------------
121Restricted access
122-------------------------------------------------------------------------------------------------------------
123
124Another possibility is to restrict access to roads of a certain type by either setting a very high cost for road links with a certain attribute or by not selecting certain road links at all:
125
126.. code-block:: sql
127
128        UPDATE classes SET cost=100000 WHERE name LIKE 'motorway%';
129
130Through subqueries you can "mix" your costs as you like and this will change the results of your routing request immediately. Cost changes will affect the next shortest path search, and there is no need to rebuild your network.
131
132Of course certain road classes can be excluded in the ``WHERE`` clause of the query as well, for example exclude "living_street" class:
133
134.. code-block:: sql
135
136        SELECT * FROM shortest_path_shooting_star(
137                'SELECT gid as id, class_id, source, target, length*c.cost as cost,
138                        x1, y1, x2, y2, rule, to_cost, reverse_cost*c.cost as reverse_cost
139                FROM ways w, classes c
140                WHERE class_id=c.id AND class_id != 111', 6585, 8247, true, true);
141
142Of course pgRouting allows you all kind of SQL that is possible with PostgreSQL/PostGIS.
143 
Note: See TracBrowser for help on using the repository browser.