Imported Upstream version 1.4.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 $routino={ # contains all default Routino options (generated using "--help-profile-pl").
35
36   # Default transport type
37   transport => 'motorcar',
38
39   # Transport types
40   transports => {foot => 1, horse => 2, wheelchair => 3, bicycle => 4, moped => 5, motorbike => 6, motorcar => 7, goods => 8, hgv => 9, psv => 10},
41
42   # Highway types
43   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},
44
45   # Property types
46   properties => {paved => 1, multilane => 2, bridge => 3, tunnel => 4},
47
48   # Restriction types
49   restrictions => {oneway => 1, weight => 2, height => 3, width => 4, length => 5},
50
51   # Allowed highways
52   profile_highway => {
53       motorway => { foot =>   0,  horse =>   0,  wheelchair =>   0,  bicycle =>   0,  moped =>   0,  motorbike => 100,  motorcar => 100,  goods => 100,  hgv => 100,  psv => 100},
54          trunk => { foot =>  40,  horse =>  25,  wheelchair =>  40,  bicycle =>  30,  moped =>  90,  motorbike => 100,  motorcar => 100,  goods => 100,  hgv => 100,  psv => 100},
55        primary => { foot =>  50,  horse =>  50,  wheelchair =>  50,  bicycle =>  70,  moped => 100,  motorbike =>  90,  motorcar =>  90,  goods =>  90,  hgv =>  90,  psv =>  90},
56      secondary => { foot =>  60,  horse =>  50,  wheelchair =>  60,  bicycle =>  80,  moped =>  90,  motorbike =>  80,  motorcar =>  80,  goods =>  80,  hgv =>  80,  psv =>  80},
57       tertiary => { foot =>  70,  horse =>  75,  wheelchair =>  70,  bicycle =>  90,  moped =>  80,  motorbike =>  70,  motorcar =>  70,  goods =>  70,  hgv =>  70,  psv =>  70},
58   unclassified => { foot =>  80,  horse =>  75,  wheelchair =>  80,  bicycle =>  90,  moped =>  70,  motorbike =>  60,  motorcar =>  60,  goods =>  60,  hgv =>  60,  psv =>  60},
59    residential => { foot =>  90,  horse =>  75,  wheelchair =>  90,  bicycle =>  90,  moped =>  60,  motorbike =>  50,  motorcar =>  50,  goods =>  50,  hgv =>  50,  psv =>  50},
60        service => { foot =>  90,  horse =>  75,  wheelchair =>  90,  bicycle =>  90,  moped =>  80,  motorbike =>  80,  motorcar =>  80,  goods =>  80,  hgv =>  80,  psv =>  80},
61          track => { foot =>  95,  horse => 100,  wheelchair =>  95,  bicycle =>  90,  moped =>   0,  motorbike =>   0,  motorcar =>   0,  goods =>   0,  hgv =>   0,  psv =>   0},
62       cycleway => { foot =>  95,  horse =>  90,  wheelchair =>  95,  bicycle => 100,  moped =>   0,  motorbike =>   0,  motorcar =>   0,  goods =>   0,  hgv =>   0,  psv =>   0},
63           path => { foot => 100,  horse => 100,  wheelchair => 100,  bicycle =>  90,  moped =>   0,  motorbike =>   0,  motorcar =>   0,  goods =>   0,  hgv =>   0,  psv =>   0},
64          steps => { foot =>  80,  horse =>   0,  wheelchair =>   0,  bicycle =>   0,  moped =>   0,  motorbike =>   0,  motorcar =>   0,  goods =>   0,  hgv =>   0,  psv =>   0}
65      },
66
67   # Speed limits
68   profile_speed => {
69       motorway => { foot =>   0,  horse =>   0,  wheelchair =>   0,  bicycle =>   0,  moped =>  48,  motorbike => 112,  motorcar => 112,  goods =>  96,  hgv =>  89,  psv =>  89},
70          trunk => { foot =>   4,  horse =>   8,  wheelchair =>   4,  bicycle =>  20,  moped =>  48,  motorbike =>  96,  motorcar =>  96,  goods =>  96,  hgv =>  80,  psv =>  80},
71        primary => { foot =>   4,  horse =>   8,  wheelchair =>   4,  bicycle =>  20,  moped =>  48,  motorbike =>  96,  motorcar =>  96,  goods =>  96,  hgv =>  80,  psv =>  80},
72      secondary => { foot =>   4,  horse =>   8,  wheelchair =>   4,  bicycle =>  20,  moped =>  48,  motorbike =>  88,  motorcar =>  88,  goods =>  88,  hgv =>  80,  psv =>  80},
73       tertiary => { foot =>   4,  horse =>   8,  wheelchair =>   4,  bicycle =>  20,  moped =>  48,  motorbike =>  80,  motorcar =>  80,  goods =>  80,  hgv =>  80,  psv =>  80},
74   unclassified => { foot =>   4,  horse =>   8,  wheelchair =>   4,  bicycle =>  20,  moped =>  48,  motorbike =>  64,  motorcar =>  64,  goods =>  64,  hgv =>  64,  psv =>  64},
75    residential => { foot =>   4,  horse =>   8,  wheelchair =>   4,  bicycle =>  20,  moped =>  48,  motorbike =>  48,  motorcar =>  48,  goods =>  48,  hgv =>  48,  psv =>  48},
76        service => { foot =>   4,  horse =>   8,  wheelchair =>   4,  bicycle =>  20,  moped =>  32,  motorbike =>  32,  motorcar =>  32,  goods =>  32,  hgv =>  32,  psv =>  32},
77          track => { foot =>   4,  horse =>   8,  wheelchair =>   4,  bicycle =>  20,  moped =>  16,  motorbike =>  16,  motorcar =>  16,  goods =>  16,  hgv =>  16,  psv =>  16},
78       cycleway => { foot =>   4,  horse =>   8,  wheelchair =>   4,  bicycle =>  20,  moped =>   0,  motorbike =>   0,  motorcar =>   0,  goods =>   0,  hgv =>   0,  psv =>   0},
79           path => { foot =>   4,  horse =>   8,  wheelchair =>   4,  bicycle =>  20,  moped =>   0,  motorbike =>   0,  motorcar =>   0,  goods =>   0,  hgv =>   0,  psv =>   0},
80          steps => { foot =>   4,  horse =>   0,  wheelchair =>   4,  bicycle =>   0,  moped =>   0,  motorbike =>   0,  motorcar =>   0,  goods =>   0,  hgv =>   0,  psv =>   0}
81      },
82
83   # Highway properties
84   profile_property => {
85          paved => { foot =>  50,  horse =>  20,  wheelchair =>  90,  bicycle =>  50,  moped => 100,  motorbike => 100,  motorcar => 100,  goods => 100,  hgv => 100,  psv => 100},
86      multilane => { foot =>  25,  horse =>  25,  wheelchair =>  25,  bicycle =>  25,  moped =>  25,  motorbike =>  75,  motorcar =>  75,  goods =>  75,  hgv =>  75,  psv =>  75},
87         bridge => { foot =>  50,  horse =>  50,  wheelchair =>  50,  bicycle =>  50,  moped =>  50,  motorbike =>  50,  motorcar =>  50,  goods =>  50,  hgv =>  50,  psv =>  50},
88         tunnel => { foot =>  50,  horse =>  50,  wheelchair =>  50,  bicycle =>  50,  moped =>  50,  motorbike =>  50,  motorcar =>  50,  goods =>  50,  hgv =>  50,  psv =>  50}
89      },
90
91   # Restrictions
92   profile_restrictions => {
93           oneway => { foot =>    0,  horse =>    1,  wheelchair =>    0,  bicycle =>    1,  moped =>    1,  motorbike =>    1,  motorcar =>    1,  goods =>    1,  hgv =>    1,  psv =>    1},
94           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},
95           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},
96            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},
97           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}
98      },
99
100 }; # end of routino variable
101
102
103 #
104 # Fill in the default parameters using the ones above (don't use executable compiled in defaults)
105 #
106
107 sub FillInDefaults
108   {
109    my(%params)=@_;
110
111    $params{transport}=$routino->{transport} if(!defined $params{transport});
112
113    my($transport)=$params{transport};
114
115    foreach $highway (keys %{$routino->{highways}})
116      {
117       $key="highway-$highway";
118       $value=$routino->{profile_highway}->{$highway}->{$transport};
119       $params{$key}=$value if(!defined $params{$key});
120
121       $key="speed-$highway";
122       $value=$routino->{profile_speed}->{$highway}->{$transport};
123       $params{$key}=$value if(!defined $params{$key});
124      }
125
126    foreach $property (keys %{$routino->{properties}})
127      {
128       $key="property-$property";
129       $value=$routino->{profile_property}->{$property}->{$transport};
130       $params{$key}=$value if(!defined $params{$key});
131      }
132
133    $params{oneway} =~ s/(true|on)/1/;
134    $params{oneway} =~ s/(false|off)/0/;
135
136    foreach $restriction (keys %{$routino->{restrictions}})
137      {
138       $key="$restriction";
139       $value=$routino->{profile_restrictions}->{$restriction}->{$transport};
140       $params{$key}=$value if(!defined $params{$key});
141      }
142
143    return %params;
144   }
145
146
147 #
148 # Run the router
149 #
150
151 sub RunRouter
152   {
153    my($optimise,%params)=@_;
154
155    # Combine all of the parameters together
156
157    my($params)="--$optimise";
158
159    foreach $key (keys %params)
160      {
161       $params.=" --$key=$params{$key}";
162      }
163
164    # Change directory
165
166    mkdir $results_dir,0755 if(! -d $results_dir);
167    chdir $results_dir;
168
169    # Create a unique output directory
170
171    chomp($uuid=`echo '$params' $$ | md5sum | cut -f1 '-d '`);
172
173    mkdir $uuid;
174    chmod 0775, $uuid;
175    chdir $uuid;
176
177    # Run the router
178
179    $params.=" --dir=$data_dir" if($data_dir);
180    $params.=" --prefix=$data_prefix" if($data_prefix);
181    $params.=" --quiet";
182
183    $message=`$bin_dir/router $params 2>&1`;
184
185    (undef,undef,$cuser,$csystem) = times;
186    $time=sprintf "time: %.3f CPU / %.3f elapsed",$cuser+$csystem,tv_interval($t0);
187
188    if(-f "$optimise.txt")
189      {
190       $result=`tail -1 $optimise.txt`;
191       @result=split(/\t/,$result);
192       $result = $result[4]." , ".$result[5];
193      }
194
195    # Return the results
196
197    return($uuid,$time,$result,$message);
198   }
199
200
201 #
202 # Return the output file
203 #
204
205 # Possible file formats
206
207 %suffixes=(
208            "html"      => ".html",
209            "gpx-route" => "-route.gpx",
210            "gpx-track" => "-track.gpx",
211            "text"      => ".txt",
212            "text-all"  => "-all.txt"
213           );
214
215 # Possible MIME types
216
217 %mimetypes=(
218             "html"      => "text/html",
219             "gpx-route" => "text/xml",
220             "gpx-track" => "text/xml",
221             "text"      => "text/plain",
222             "text-all"  => "text/plain"
223            );
224
225 sub ReturnOutput
226   {
227    my($uuid,$type,$format)=@_;
228
229    $suffix=$suffixes{$format};
230    $mime  =$mimetypes{$format};
231
232    $file="$results_dir/$uuid/$type$suffix";
233
234    # Return the output
235
236    if(!$type || !$uuid || !$format || ! -f $file)
237      {
238       print header('text/plain','404 Not found');
239       print "Not Found!\n";
240      }
241    else
242      {
243       print header($mime);
244
245       system "cat $file";
246      }
247   }
248
249 1;