Routino SVN Repository Browser

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

ViewVC logotype

Contents of /trunk/src/router.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 213 - (show annotations) (download) (as text)
Thu Jul 2 16:33:31 2009 UTC (15 years, 8 months ago) by amb
File MIME type: text/x-csrc
File size: 12063 byte(s)
Removed unused header files, change assert statements, tidy some code.

1 /***************************************
2 $Header: /home/amb/CVS/routino/src/router.c,v 1.54 2009-07-02 16:33:31 amb Exp $
3
4 OSM router.
5
6 Part of the Routino routing software.
7 ******************/ /******************
8 This file Copyright 2008,2009 Andrew M. Bishop
9
10 This program is free software: you can redistribute it and/or modify
11 it under the terms of the GNU Affero General Public License as published by
12 the Free Software Foundation, either version 3 of the License, or
13 (at your option) any later version.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU Affero General Public License for more details.
19
20 You should have received a copy of the GNU Affero General Public License
21 along with this program. If not, see <http://www.gnu.org/licenses/>.
22 ***************************************/
23
24
25 #include <stdio.h>
26 #include <string.h>
27 #include <stdlib.h>
28 #include <ctype.h>
29
30 #include "types.h"
31 #include "functions.h"
32 #include "profiles.h"
33 #include "nodes.h"
34 #include "segments.h"
35 #include "ways.h"
36
37
38 /*+ The option not to print any progress information. +*/
39 int option_quiet=0;
40
41 /*+ The option to calculate the quickest route insted of the shortest. +*/
42 int option_quickest=0;
43
44
45 int main(int argc,char** argv)
46 {
47 Nodes *OSMNodes;
48 Segments *OSMSegments;
49 Ways *OSMWays;
50 Results *results[10]={NULL};
51 int point_used[10]={0};
52 float point_lon[10],point_lat[10];
53 int help_profile=0,help_profile_js=0,help_profile_pl=0;
54 int option_all=0,option_super=0,option_no_output=0;
55 char *dirname=NULL,*prefix=NULL,*filename;
56 Transport transport=Transport_None;
57 Profile profile;
58 index_t start=NO_NODE,finish=NO_NODE;
59 int arg,node;
60
61 /* Parse the command line arguments */
62
63 if(argc<2)
64 {
65 usage:
66
67 fprintf(stderr,"Usage: router [--lon1=]<longitude> [--lat1=]<latitude>\n"
68 " [--lon2=]<longitude> [--lon2=]<latitude>\n"
69 " [ ... [--lon9=]<longitude> [--lon9=]<latitude> ]\n"
70 " [--help | --help-profile | --help-profile-js | --help-profile-pl]\n"
71 " [--dir=<name>] [--prefix=<name>]\n"
72 " [--shortest | --quickest]\n"
73 " [--all | --super]\n"
74 " [--no-output] [--quiet]\n"
75 " [--transport=<transport>]\n"
76 " [--highway-<highway>=<preference> ...]\n"
77 " [--speed-<highway>=<speed> ...]\n"
78 " [--oneway=[0|1]]\n"
79 " [--weight=<weight>]\n"
80 " [--height=<height>] [--width=<width>] [--length=<length>]\n"
81 "\n"
82 "<transport> defaults to motorcar but can be set to:\n"
83 "%s"
84 "\n"
85 "<highway> can be selected from:\n"
86 "%s"
87 "\n"
88 "<preference> is a preference expressed as a percentage\n"
89 "<speed> is a speed in km/hour\n"
90 "<weight> is a weight in tonnes\n"
91 "<height>, <width>, <length> are dimensions in metres\n"
92 "\n",
93 TransportList(),HighwayList());
94
95 return(1);
96 }
97
98 /* Get the transport type if specified and fill in the default profile */
99
100 for(arg=1;arg<argc;arg++)
101 if(!strncmp(argv[arg],"--transport=",12))
102 {
103 transport=TransportType(&argv[arg][12]);
104
105 if(transport==Transport_None)
106 goto usage;
107 }
108
109 if(transport==Transport_None)
110 transport=Transport_Motorcar;
111
112 profile=*GetProfile(transport);
113
114 /* Parse the other command line arguments */
115
116 for(arg=1;arg<argc;arg++)
117 {
118 if(isdigit(argv[arg][0]) ||
119 ((argv[arg][0]=='-' || argv[arg][0]=='+') && isdigit(argv[arg][1])))
120 {
121 for(node=1;node<sizeof(point_used)/sizeof(point_used[0]);node++)
122 if(point_used[node]!=3)
123 {
124 if(point_used[node]==0)
125 {
126 point_lon[node]=degrees_to_radians(atof(argv[arg]));
127 point_used[node]=1;
128 }
129 else /* if(point_used[node]==1) */
130 {
131 point_lat[node]=degrees_to_radians(atof(argv[arg]));
132 point_used[node]=3;
133 }
134 break;
135 }
136 }
137 else if(!strncmp(argv[arg],"--lon",5) && isdigit(argv[arg][5]) && argv[arg][6]=='=')
138 {
139 node=atoi(&argv[arg][5]);
140 if(point_used[node]&1)
141 goto usage;
142 point_lon[node]=degrees_to_radians(atof(&argv[arg][7]));
143 point_used[node]+=1;
144 }
145 else if(!strncmp(argv[arg],"--lat",5) && isdigit(argv[arg][5]) && argv[arg][6]=='=')
146 {
147 node=atoi(&argv[arg][5]);
148 if(point_used[node]&2)
149 goto usage;
150 point_lat[node]=degrees_to_radians(atof(&argv[arg][7]));
151 point_used[node]+=2;
152 }
153 else if(!strcmp(argv[arg],"--help"))
154 goto usage;
155 else if(!strcmp(argv[arg],"--help-profile"))
156 help_profile=1;
157 else if(!strcmp(argv[arg],"--help-profile-js"))
158 help_profile_js=1;
159 else if(!strcmp(argv[arg],"--help-profile-pl"))
160 help_profile_pl=1;
161 else if(!strncmp(argv[arg],"--dir=",6))
162 dirname=&argv[arg][6];
163 else if(!strncmp(argv[arg],"--prefix=",9))
164 prefix=&argv[arg][9];
165 else if(!strcmp(argv[arg],"--shortest"))
166 option_quickest=0;
167 else if(!strcmp(argv[arg],"--quickest"))
168 option_quickest=1;
169 else if(!strcmp(argv[arg],"--all"))
170 option_all=1;
171 else if(!strcmp(argv[arg],"--super"))
172 option_super=1;
173 else if(!strcmp(argv[arg],"--no-output"))
174 option_no_output=1;
175 else if(!strcmp(argv[arg],"--quiet"))
176 option_quiet=1;
177 else if(!strncmp(argv[arg],"--transport=",12))
178 ; /* Done this already*/
179 else if(!strncmp(argv[arg],"--highway-",10))
180 {
181 Highway highway;
182 char *equal=strchr(argv[arg],'=');
183 char *string;
184
185 if(!equal)
186 goto usage;
187
188 string=strcpy((char*)malloc(strlen(argv[arg])),argv[arg]+10);
189 string[equal-argv[arg]-10]=0;
190
191 highway=HighwayType(string);
192
193 free(string);
194
195 if(highway==Way_Unknown)
196 goto usage;
197
198 profile.highway[highway]=atof(equal+1);
199 }
200 else if(!strncmp(argv[arg],"--speed-",8))
201 {
202 Highway highway;
203 char *equal=strchr(argv[arg],'=');
204 char *string;
205
206 if(!equal)
207 goto usage;
208
209 string=strcpy((char*)malloc(strlen(argv[arg])),argv[arg]+8);
210 string[equal-argv[arg]-8]=0;
211
212 highway=HighwayType(string);
213
214 free(string);
215
216 if(highway==Way_Unknown)
217 goto usage;
218
219 profile.speed[highway]=kph_to_speed(atof(equal+1));
220 }
221 else if(!strncmp(argv[arg],"--oneway=",9))
222 profile.oneway=!!atoi(&argv[arg][9]);
223 else if(!strncmp(argv[arg],"--weight=",9))
224 profile.weight=tonnes_to_weight(atof(&argv[arg][9]));
225 else if(!strncmp(argv[arg],"--height=",9))
226 profile.height=metres_to_height(atof(&argv[arg][9]));
227 else if(!strncmp(argv[arg],"--width=",8))
228 profile.width=metres_to_width(atof(&argv[arg][8]));
229 else if(!strncmp(argv[arg],"--length=",9))
230 profile.length=metres_to_length(atof(&argv[arg][9]));
231 else
232 goto usage;
233 }
234
235 for(node=0;node<sizeof(point_used)/sizeof(point_used[0]);node++)
236 if(point_used[node]==1 || point_used[node]==2)
237 goto usage;
238
239 if(help_profile)
240 {
241 PrintProfile(&profile);
242
243 return(0);
244 }
245 else if(help_profile_js)
246 {
247 PrintProfilesJS();
248
249 return(0);
250 }
251 else if(help_profile_pl)
252 {
253 PrintProfilesPerl();
254
255 return(0);
256 }
257
258 UpdateProfile(&profile);
259
260 /* Load in the data */
261
262 OSMNodes=LoadNodeList(filename=FileName(dirname,prefix,"nodes.mem"));
263
264 if(!OSMNodes)
265 {
266 fprintf(stderr,"Cannot open nodes file '%s'.\n",filename);
267 return(1);
268 }
269
270 OSMSegments=LoadSegmentList(filename=FileName(dirname,prefix,"segments.mem"));
271
272 if(!OSMSegments)
273 {
274 fprintf(stderr,"Cannot open segments file '%s'.\n",filename);
275 return(1);
276 }
277
278 OSMWays=LoadWayList(filename=FileName(dirname,prefix,"ways.mem"));
279
280 if(!OSMWays)
281 {
282 fprintf(stderr,"Cannot open ways file '%s'.\n",filename);
283 return(1);
284 }
285
286 /* Loop through all pairs of nodes */
287
288 for(node=1;node<sizeof(point_used)/sizeof(point_used[0]);node++)
289 {
290 distance_t dist=km_to_distance(10);
291
292 if(point_used[node]!=3)
293 continue;
294
295 /* Find the node */
296
297 start=finish;
298
299 finish=FindNode(OSMNodes,OSMSegments,OSMWays,point_lat[node],point_lon[node],&dist,&profile);
300
301 if(finish==NO_NODE)
302 {
303 fprintf(stderr,"Cannot find node close to specified point %d.\n",node);
304 return(1);
305 }
306
307 if(!option_quiet)
308 {
309 float lat,lon;
310
311 GetLatLong(OSMNodes,finish,&lat,&lon);
312
313 printf("Node %d: %3.6f %4.6f = %2.3f km\n",node,radians_to_degrees(lon),radians_to_degrees(lat),distance_to_km(dist));
314 }
315
316 if(start==NO_NODE)
317 continue;
318
319 /* Find the route segment */
320
321 if(option_super && !IsSuperNode(OSMNodes,start) && !IsSuperNode(OSMNodes,finish))
322 {
323 fprintf(stderr,"Start and/or finish nodes are not super-nodes.\n");
324 return(1);
325 }
326
327 /* Calculate the route. */
328
329 if(option_all)
330 {
331 /* Calculate the route */
332
333 results[node]=FindRoute(OSMNodes,OSMSegments,OSMWays,start,finish,&profile,option_all);
334
335 if(!results[node])
336 {
337 fprintf(stderr,"Cannot find route compatible with profile.\n");
338 return(1);
339 }
340 }
341 else
342 {
343 Results *begin,*end;
344
345 /* Calculate the beginning of the route */
346
347 if(IsSuperNode(OSMNodes,start))
348 {
349 Result *result;
350
351 begin=NewResultsList(1);
352
353 begin->start=start;
354
355 result=InsertResult(begin,start);
356
357 ZeroResult(result);
358 }
359 else
360 {
361 begin=FindStartRoutes(OSMNodes,OSMSegments,OSMWays,start,&profile);
362
363 if(!begin)
364 {
365 fprintf(stderr,"Cannot find initial section of route compatible with profile.\n");
366 return(1);
367 }
368 }
369
370 if(FindResult(begin,finish))
371 {
372 results[node]=begin;
373
374 results[node]->finish=finish;
375 }
376 else
377 {
378 Results *superresults;
379
380 /* Calculate the end of the route */
381
382 if(IsSuperNode(OSMNodes,finish))
383 {
384 Result *result;
385
386 end=NewResultsList(1);
387
388 end->finish=finish;
389
390 result=InsertResult(end,finish);
391
392 ZeroResult(result);
393 }
394 else
395 {
396 end=FindFinishRoutes(OSMNodes,OSMSegments,OSMWays,finish,&profile);
397
398 if(!end)
399 {
400 fprintf(stderr,"Cannot find final section of route compatible with profile.\n");
401 return(1);
402 }
403 }
404
405 /* Calculate the middle of the route */
406
407 superresults=FindRoute3(OSMNodes,OSMSegments,OSMWays,begin,end,&profile);
408
409 if(!superresults)
410 {
411 fprintf(stderr,"Cannot find route compatible with profile.\n");
412 return(1);
413 }
414
415 if(option_super)
416 results[node]=superresults;
417 else
418 results[node]=CombineRoutes(superresults,OSMNodes,OSMSegments,OSMWays,&profile);
419 }
420 }
421 }
422
423 if(!option_no_output)
424 {
425 PrintRouteHead(FileName(dirname,prefix,"copyright.txt"));
426
427 for(node=1;node<sizeof(point_used)/sizeof(point_used[0]);node++)
428 if(results[node])
429 PrintRoute(results[node],OSMNodes,OSMSegments,OSMWays,&profile);
430
431 PrintRouteTail();
432 }
433
434 return(0);
435 }

Properties

Name Value
cvs:description Router.