Imported Upstream version 1.5.1
[routino] / web / www / routino / router.pl
1 #
2 # Routino generic router Perl script
3 #
4 # Part of the Routino routing software.
5 #
6 # This file Copyright 2008-2010 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, weight => 2, height => 3, width => 4, length => 5 },
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           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 },
103           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 },
104            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 },
105           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 }
106      }
107
108 }; # end of routino variable
109
110
111 #
112 # Fill in the default parameters using the ones above (don't use executable compiled in defaults)
113 #
114
115 sub FillInDefaults
116   {
117    my(%params)=@_;
118
119    $params{transport}=$routino->{transport} if(!defined $params{transport});
120
121    my($transport)=$params{transport};
122
123    foreach $highway (keys %{$routino->{highways}})
124      {
125       $key="highway-$highway";
126       $value=$routino->{profile_highway}->{$highway}->{$transport};
127       $params{$key}=$value if(!defined $params{$key});
128
129       $key="speed-$highway";
130       $value=$routino->{profile_speed}->{$highway}->{$transport};
131       $params{$key}=$value if(!defined $params{$key});
132      }
133
134    foreach $property (keys %{$routino->{properties}})
135      {
136       $key="property-$property";
137       $value=$routino->{profile_property}->{$property}->{$transport};
138       $params{$key}=$value if(!defined $params{$key});
139      }
140
141    $params{oneway} =~ s/(true|on)/1/;
142    $params{oneway} =~ s/(false|off)/0/;
143
144    foreach $restriction (keys %{$routino->{restrictions}})
145      {
146       $key="$restriction";
147       $value=$routino->{profile_restrictions}->{$restriction}->{$transport};
148       $params{$key}=$value if(!defined $params{$key});
149      }
150
151    return %params;
152   }
153
154
155 #
156 # Run the router
157 #
158
159 sub RunRouter
160   {
161    my($optimise,%params)=@_;
162
163    # Combine all of the parameters together
164
165    my($params)="--$optimise";
166
167    foreach $key (keys %params)
168      {
169       $params.=" --$key=$params{$key}";
170      }
171
172    # Change directory
173
174    mkdir $results_dir,0755 if(! -d $results_dir);
175    chdir $results_dir;
176
177    # Create a unique output directory
178
179    chomp($uuid=`echo '$params' $$ | md5sum | cut -f1 '-d '`);
180
181    mkdir $uuid;
182    chmod 0775, $uuid;
183    chdir $uuid;
184
185    # Run the router
186
187    $params.=" --dir=$data_dir" if($data_dir);
188    $params.=" --prefix=$data_prefix" if($data_prefix);
189    $params.=" --quiet";
190
191    $message=`$bin_dir/$router_exe $params 2>&1`;
192
193    (undef,undef,$cuser,$csystem) = times;
194    $time=sprintf "time: %.3f CPU / %.3f elapsed",$cuser+$csystem,tv_interval($t0);
195
196    if(-f "$optimise.txt")
197      {
198       $result=`tail -1 $optimise.txt`;
199       @result=split(/\t/,$result);
200       $result = $result[4]." , ".$result[5];
201      }
202
203    # Return the results
204
205    return($uuid,$time,$result,$message);
206   }
207
208
209 #
210 # Return the output file
211 #
212
213 # Possible file formats
214
215 %suffixes=(
216            "html"      => ".html",
217            "gpx-route" => "-route.gpx",
218            "gpx-track" => "-track.gpx",
219            "text"      => ".txt",
220            "text-all"  => "-all.txt"
221           );
222
223 # Possible MIME types
224
225 %mimetypes=(
226             "html"      => "text/html",
227             "gpx-route" => "text/xml",
228             "gpx-track" => "text/xml",
229             "text"      => "text/plain",
230             "text-all"  => "text/plain"
231            );
232
233 sub ReturnOutput
234   {
235    my($uuid,$type,$format)=@_;
236
237    $suffix=$suffixes{$format};
238    $mime  =$mimetypes{$format};
239
240    $file="$results_dir/$uuid/$type$suffix";
241
242    # Return the output
243
244    if(!$type || !$uuid || !$format || ! -f $file)
245      {
246       print header('text/plain','404 Not found');
247       print "Not Found!\n";
248      }
249    else
250      {
251       print header($mime);
252
253       system "cat $file";
254      }
255   }
256
257 1;