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 179 - (show annotations) (download) (as text)
Fri May 29 17:45:24 2009 UTC (15 years, 10 months ago) by amb
File MIME type: text/x-csrc
File size: 12031 byte(s)
Make sure that the chosen "nearest point" is a highway that the profile allows.

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

Properties

Name Value
cvs:description Router.