Routino SVN Repository Browser

Check out the latest version of Routino: svn co http://routino.org/svn/trunk routino

ViewVC logotype

Contents of /trunk/web/www/routino/router.pl

Parent Directory Parent Directory | Revision Log Revision Log


Revision 629 - (show annotations) (download) (as text)
Fri Feb 11 10:12:14 2011 UTC (14 years, 1 month ago) by amb
File MIME type: text/x-perl
File size: 12651 byte(s)
Print a message if routed OK, allow web users to see router output (now logged
to file).

1 #
2 # Routino generic router Perl script
3 #
4 # Part of the Routino routing software.
5 #
6 # This file Copyright 2008-2011 Andrew M. Bishop
7 #
8 # This program is free software: you can redistribute it and/or modify
9 # it under the terms of the GNU Affero General Public License as published by
10 # the Free Software Foundation, either version 3 of the License, or
11 # (at your option) any later version.
12 #
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU Affero General Public License for more details.
17 #
18 # You should have received a copy of the GNU Affero General Public License
19 # along with this program. If not, see <http://www.gnu.org/licenses/>.
20 #
21
22 # Use the directory paths script
23 require "paths.pl";
24
25 # Use the perl Time::HiRes module
26 use Time::HiRes qw(gettimeofday tv_interval);
27
28 $t0 = [gettimeofday];
29
30 # Filename prefix
31
32 $data_prefix="";
33
34 ################################################################################
35 ########################### Routino default profile ############################
36 ################################################################################
37
38 $routino={ # contains all default Routino options (generated using "--help-profile-perl").
39
40 # Default transport type
41 transport => 'motorcar',
42
43 # Transport types
44 transports => { foot => 1, horse => 2, wheelchair => 3, bicycle => 4, moped => 5, motorbike => 6, motorcar => 7, goods => 8, hgv => 9, psv => 10 },
45
46 # Highway types
47 highways => { motorway => 1, trunk => 2, primary => 3, secondary => 4, tertiary => 5, unclassified => 6, residential => 7, service => 8, track => 9, cycleway => 10, path => 11, steps => 12, ferry => 13 },
48
49 # Property types
50 properties => { paved => 1, multilane => 2, bridge => 3, tunnel => 4, footroute => 5, bicycleroute => 6 },
51
52 # Restriction types
53 restrictions => { oneway => 1, turns => 2, weight => 3, height => 4, width => 5, length => 6 },
54
55 # Allowed highways
56 profile_highway => {
57 motorway => { foot => 0, horse => 0, wheelchair => 0, bicycle => 0, moped => 0, motorbike => 100, motorcar => 100, goods => 100, hgv => 100, psv => 100 },
58 trunk => { foot => 40, horse => 25, wheelchair => 40, bicycle => 30, moped => 90, motorbike => 100, motorcar => 100, goods => 100, hgv => 100, psv => 100 },
59 primary => { foot => 50, horse => 50, wheelchair => 50, bicycle => 70, moped => 100, motorbike => 90, motorcar => 90, goods => 90, hgv => 90, psv => 90 },
60 secondary => { foot => 60, horse => 50, wheelchair => 60, bicycle => 80, moped => 90, motorbike => 80, motorcar => 80, goods => 80, hgv => 80, psv => 80 },
61 tertiary => { foot => 70, horse => 75, wheelchair => 70, bicycle => 90, moped => 80, motorbike => 70, motorcar => 70, goods => 70, hgv => 70, psv => 70 },
62 unclassified => { foot => 80, horse => 75, wheelchair => 80, bicycle => 90, moped => 70, motorbike => 60, motorcar => 60, goods => 60, hgv => 60, psv => 60 },
63 residential => { foot => 90, horse => 75, wheelchair => 90, bicycle => 90, moped => 60, motorbike => 50, motorcar => 50, goods => 50, hgv => 50, psv => 50 },
64 service => { foot => 90, horse => 75, wheelchair => 90, bicycle => 90, moped => 80, motorbike => 80, motorcar => 80, goods => 80, hgv => 80, psv => 80 },
65 track => { foot => 95, horse => 100, wheelchair => 95, bicycle => 90, moped => 0, motorbike => 0, motorcar => 0, goods => 0, hgv => 0, psv => 0 },
66 cycleway => { foot => 95, horse => 90, wheelchair => 95, bicycle => 100, moped => 0, motorbike => 0, motorcar => 0, goods => 0, hgv => 0, psv => 0 },
67 path => { foot => 100, horse => 100, wheelchair => 100, bicycle => 90, moped => 0, motorbike => 0, motorcar => 0, goods => 0, hgv => 0, psv => 0 },
68 steps => { foot => 80, horse => 0, wheelchair => 0, bicycle => 0, moped => 0, motorbike => 0, motorcar => 0, goods => 0, hgv => 0, psv => 0 },
69 ferry => { foot => 20, horse => 20, wheelchair => 20, bicycle => 20, moped => 20, motorbike => 20, motorcar => 20, goods => 20, hgv => 20, psv => 20 }
70 },
71
72 # Speed limits
73 profile_speed => {
74 motorway => { foot => 0, horse => 0, wheelchair => 0, bicycle => 0, moped => 48, motorbike => 112, motorcar => 112, goods => 96, hgv => 89, psv => 89 },
75 trunk => { foot => 4, horse => 8, wheelchair => 4, bicycle => 20, moped => 48, motorbike => 96, motorcar => 96, goods => 96, hgv => 80, psv => 80 },
76 primary => { foot => 4, horse => 8, wheelchair => 4, bicycle => 20, moped => 48, motorbike => 96, motorcar => 96, goods => 96, hgv => 80, psv => 80 },
77 secondary => { foot => 4, horse => 8, wheelchair => 4, bicycle => 20, moped => 48, motorbike => 88, motorcar => 88, goods => 88, hgv => 80, psv => 80 },
78 tertiary => { foot => 4, horse => 8, wheelchair => 4, bicycle => 20, moped => 48, motorbike => 80, motorcar => 80, goods => 80, hgv => 80, psv => 80 },
79 unclassified => { foot => 4, horse => 8, wheelchair => 4, bicycle => 20, moped => 48, motorbike => 64, motorcar => 64, goods => 64, hgv => 64, psv => 64 },
80 residential => { foot => 4, horse => 8, wheelchair => 4, bicycle => 20, moped => 48, motorbike => 48, motorcar => 48, goods => 48, hgv => 48, psv => 48 },
81 service => { foot => 4, horse => 8, wheelchair => 4, bicycle => 20, moped => 32, motorbike => 32, motorcar => 32, goods => 32, hgv => 32, psv => 32 },
82 track => { foot => 4, horse => 8, wheelchair => 4, bicycle => 20, moped => 16, motorbike => 16, motorcar => 16, goods => 16, hgv => 16, psv => 16 },
83 cycleway => { foot => 4, horse => 8, wheelchair => 4, bicycle => 20, moped => 0, motorbike => 0, motorcar => 0, goods => 0, hgv => 0, psv => 0 },
84 path => { foot => 4, horse => 8, wheelchair => 4, bicycle => 20, moped => 0, motorbike => 0, motorcar => 0, goods => 0, hgv => 0, psv => 0 },
85 steps => { foot => 4, horse => 0, wheelchair => 4, bicycle => 0, moped => 0, motorbike => 0, motorcar => 0, goods => 0, hgv => 0, psv => 0 },
86 ferry => { foot => 10, horse => 10, wheelchair => 10, bicycle => 10, moped => 10, motorbike => 10, motorcar => 10, goods => 10, hgv => 10, psv => 10 }
87 },
88
89 # Highway properties
90 profile_property => {
91 paved => { foot => 50, horse => 20, wheelchair => 90, bicycle => 50, moped => 100, motorbike => 100, motorcar => 100, goods => 100, hgv => 100, psv => 100 },
92 multilane => { foot => 25, horse => 25, wheelchair => 25, bicycle => 25, moped => 35, motorbike => 60, motorcar => 60, goods => 60, hgv => 60, psv => 60 },
93 bridge => { foot => 50, horse => 50, wheelchair => 50, bicycle => 50, moped => 50, motorbike => 50, motorcar => 50, goods => 50, hgv => 50, psv => 50 },
94 tunnel => { foot => 50, horse => 50, wheelchair => 50, bicycle => 50, moped => 50, motorbike => 50, motorcar => 50, goods => 50, hgv => 50, psv => 50 },
95 footroute => { foot => 55, horse => 50, wheelchair => 55, bicycle => 50, moped => 50, motorbike => 50, motorcar => 45, goods => 45, hgv => 45, psv => 45 },
96 bicycleroute => { foot => 55, horse => 50, wheelchair => 55, bicycle => 60, moped => 50, motorbike => 50, motorcar => 45, goods => 45, hgv => 45, psv => 45 }
97 },
98
99 # Restrictions
100 profile_restrictions => {
101 oneway => { foot => 0, horse => 1, wheelchair => 0, bicycle => 1, moped => 1, motorbike => 1, motorcar => 1, goods => 1, hgv => 1, psv => 1 },
102 turns => { foot => 0, horse => 1, wheelchair => 1, bicycle => 1, moped => 1, motorbike => 1, motorcar => 1, goods => 1, hgv => 1, psv => 1 },
103 weight => { foot => 0.0, horse => 0.0, wheelchair => 0.0, bicycle => 0.0, moped => 0.0, motorbike => 0.0, motorcar => 0.0, goods => 5.0, hgv => 10.0, psv => 15.0 },
104 height => { foot => 0.0, horse => 0.0, wheelchair => 0.0, bicycle => 0.0, moped => 0.0, motorbike => 0.0, motorcar => 0.0, goods => 2.5, hgv => 3.0, psv => 3.0 },
105 width => { foot => 0.0, horse => 0.0, wheelchair => 0.0, bicycle => 0.0, moped => 0.0, motorbike => 0.0, motorcar => 0.0, goods => 2.0, hgv => 2.5, psv => 2.5 },
106 length => { foot => 0.0, horse => 0.0, wheelchair => 0.0, bicycle => 0.0, moped => 0.0, motorbike => 0.0, motorcar => 0.0, goods => 5.0, hgv => 6.0, psv => 6.0 }
107 }
108
109 }; # end of routino variable
110
111
112 #
113 # Fill in the default parameters using the ones above (don't use executable compiled in defaults)
114 #
115
116 sub FillInDefaults
117 {
118 my(%params)=@_;
119
120 $params{transport}=$routino->{transport} if(!defined $params{transport});
121
122 my($transport)=$params{transport};
123
124 foreach $highway (keys %{$routino->{highways}})
125 {
126 $key="highway-$highway";
127 $value=$routino->{profile_highway}->{$highway}->{$transport};
128 $params{$key}=$value if(!defined $params{$key});
129
130 $key="speed-$highway";
131 $value=$routino->{profile_speed}->{$highway}->{$transport};
132 $params{$key}=$value if(!defined $params{$key});
133 }
134
135 foreach $property (keys %{$routino->{properties}})
136 {
137 $key="property-$property";
138 $value=$routino->{profile_property}->{$property}->{$transport};
139 $params{$key}=$value if(!defined $params{$key});
140 }
141
142 $params{oneway} =~ s/(true|on)/1/;
143 $params{oneway} =~ s/(false|off)/0/;
144
145 $params{turns} =~ s/(true|on)/1/;
146 $params{turns} =~ s/(false|off)/0/;
147
148 foreach $restriction (keys %{$routino->{restrictions}})
149 {
150 $key="$restriction";
151 $value=$routino->{profile_restrictions}->{$restriction}->{$transport};
152 $params{$key}=$value if(!defined $params{$key});
153 }
154
155 return %params;
156 }
157
158
159 #
160 # Run the router
161 #
162
163 sub RunRouter
164 {
165 my($optimise,%params)=@_;
166
167 # Combine all of the parameters together
168
169 my($params)="--$optimise";
170
171 foreach $key (keys %params)
172 {
173 $params.=" --$key=$params{$key}";
174 }
175
176 # Change directory
177
178 mkdir $results_dir,0755 if(! -d $results_dir);
179 chdir $results_dir;
180
181 # Create a unique output directory
182
183 chomp($uuid=`echo '$params' $$ | md5sum | cut -f1 '-d '`);
184
185 mkdir $uuid;
186 chmod 0775, $uuid;
187 chdir $uuid;
188
189 # Run the router
190
191 $params.=" --dir=$data_dir" if($data_dir);
192 $params.=" --prefix=$data_prefix" if($data_prefix);
193 $params.=" --loggable";
194
195 system "$bin_dir/$router_exe $params > router.log 2>&1";
196
197 (undef,undef,$cuser,$csystem) = times;
198 $time=sprintf "time: %.3f CPU / %.3f elapsed",$cuser+$csystem,tv_interval($t0);
199
200 $message="";
201
202 if($? != 0)
203 {
204 $message=`tail -1 router.log`;
205 }
206
207 $result="";
208
209 if(-f "$optimise.txt")
210 {
211 $result=`tail -1 $optimise.txt`;
212 @result=split(/\t/,$result);
213 $result = $result[4]." , ".$result[5];
214 }
215
216 # Return the results
217
218 return($uuid,$time,$result,$message);
219 }
220
221
222 #
223 # Return the output file
224 #
225
226 # Possible file formats
227
228 %suffixes=(
229 "html" => ".html",
230 "gpx-route" => "-route.gpx",
231 "gpx-track" => "-track.gpx",
232 "text" => ".txt",
233 "text-all" => "-all.txt",
234 "log" => ".log"
235 );
236
237 # Possible MIME types
238
239 %mimetypes=(
240 "html" => "text/html",
241 "gpx-route" => "text/xml",
242 "gpx-track" => "text/xml",
243 "text" => "text/plain",
244 "text-all" => "text/plain",
245 "log" => "text/plain"
246 );
247
248 sub ReturnOutput
249 {
250 my($uuid,$type,$format)=@_;
251
252 if($type eq "router") { $format="log" }
253
254 $suffix=$suffixes{$format};
255 $mime =$mimetypes{$format};
256
257 $file="$results_dir/$uuid/$type$suffix";
258
259 # Return the output
260
261 if(!$type || !$uuid || !$format || ! -f $file)
262 {
263 print header('text/plain','404 Not found');
264 print "Not Found!\n";
265 }
266 else
267 {
268 print header($mime);
269
270 system "cat $file";
271 }
272 }
273
274 1;