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 121 - (show annotations) (download) (as text)
Sun Feb 15 14:30:11 2009 UTC (16 years, 1 month ago) by amb
File MIME type: text/x-csrc
File size: 9077 byte(s)
Store radians rather than degrees.

1 /***************************************
2 $Header: /home/amb/CVS/routino/src/router.c,v 1.35 2009-02-15 14:30:11 amb Exp $
3
4 OSM router.
5 ******************/ /******************
6 Written by Andrew M. Bishop
7
8 This file Copyright 2008,2009 Andrew M. Bishop
9 It may be distributed under the GNU Public License, version 2, or
10 any higher version. See section COPYING of the GNU Public license
11 for conditions under which this file may be redistributed.
12 ***************************************/
13
14
15 #include <stdio.h>
16 #include <string.h>
17 #include <stdlib.h>
18 #include <math.h>
19
20 #include "types.h"
21 #include "functions.h"
22 #include "profiles.h"
23 #include "nodes.h"
24 #include "segments.h"
25 #include "ways.h"
26
27
28 /*+ The option not to print any progress information. +*/
29 int option_quiet=0;
30
31 /*+ The option to calculate the quickest route insted of the shortest. +*/
32 int option_quickest=0;
33
34
35 int main(int argc,char** argv)
36 {
37 Nodes *OSMNodes;
38 Segments *OSMSegments;
39 Ways *OSMWays;
40 index_t start,finish;
41 int help_profile=0,all=0,only_super=0,no_print=0;
42 char *dirname=NULL,*prefix=NULL,*filename;
43 Transport transport=Transport_None;
44 Profile profile;
45 int i;
46
47 /* Parse the command line arguments */
48
49 if(argc<5)
50 {
51 usage:
52
53 fprintf(stderr,"Usage: router <start-lat> <start-lon> <finish-lat> <finish-lon>\n"
54 " [--help] [--help-profile]\n"
55 " [--dir=<name>] [--prefix=<name>]\n"
56 " [--shortest] [--quickest]\n"
57 " [--all] [--only-super]\n"
58 " [--no-print] [--quiet]\n"
59 " [--transport=<transport>]\n"
60 " [--not-highway=<highway> ...]\n"
61 " [--speed-<highway>=<speed> ...]\n"
62 " [--ignore-oneway]\n"
63 "\n"
64 "<transport> defaults to motorcar but can be set to:\n"
65 "%s"
66 "\n"
67 "<highway> can be selected from:\n"
68 "%s"
69 "\n"
70 "<speed> is a speed in km/hour\n"
71 "\n",
72 TransportList(),HighwayList());
73
74 return(1);
75 }
76
77 /* Get the transport type if specified and fill in the profile */
78
79 for(i=3;i<argc;i++)
80 if(!strncmp(argv[i],"--transport=",12))
81 {
82 transport=TransportType(&argv[i][12]);
83
84 if(transport==Transport_None)
85 goto usage;
86 }
87
88 if(transport==Transport_None)
89 transport=Transport_Motorcar;
90
91 profile=*GetProfile(transport);
92
93 /* Parse the other command line arguments */
94
95 while(--argc>=5)
96 {
97 if(!strcmp(argv[argc],"--help"))
98 goto usage;
99 else if(!strcmp(argv[argc],"--help-profile"))
100 help_profile=1;
101 else if(!strncmp(argv[argc],"--dir=",6))
102 dirname=&argv[argc][6];
103 else if(!strncmp(argv[argc],"--prefix=",9))
104 prefix=&argv[argc][9];
105 else if(!strcmp(argv[argc],"--all"))
106 all=1;
107 else if(!strcmp(argv[argc],"--shortest"))
108 option_quickest=0;
109 else if(!strcmp(argv[argc],"--quickest"))
110 option_quickest=1;
111 else if(!strcmp(argv[argc],"--only-super"))
112 only_super=1;
113 else if(!strcmp(argv[argc],"--no-print"))
114 no_print=1;
115 else if(!strcmp(argv[argc],"--quiet"))
116 option_quiet=1;
117 else if(!strncmp(argv[argc],"--transport=",12))
118 ; /* Done this already*/
119 else if(!strncmp(argv[argc],"--not-highway=",14))
120 {
121 Highway highway=HighwayType(&argv[argc][14]);
122
123 if(highway==Way_Unknown)
124 goto usage;
125
126 profile.highways[highway]=0;
127 }
128 else if(!strncmp(argv[argc],"--speed-",8))
129 {
130 Highway highway;
131 char *equal=strchr(argv[argc],'=');
132 char *string;
133
134 if(!equal)
135 goto usage;
136
137 string=strcpy((char*)malloc(strlen(argv[argc])),argv[argc]+8);
138 string[equal-argv[argc]]=0;
139
140 highway=HighwayType(string);
141
142 free(string);
143
144 if(highway==Way_Unknown)
145 goto usage;
146
147 profile.speed[highway]=atoi(equal+1);
148 }
149 else if(!strcmp(argv[argc],"-ignore-oneway"))
150 profile.oneway=0;
151 else
152 goto usage;
153 }
154
155 if(help_profile)
156 {
157 PrintProfile(&profile);
158
159 return(0);
160 }
161
162 /* Load in the data */
163
164 filename=(char*)malloc((dirname?strlen(dirname):0)+(prefix?strlen(prefix):0)+16);
165
166 sprintf(filename,"%s%s%s%snodes.mem",dirname?dirname:"",dirname?"/":"",prefix?prefix:"",prefix?"-":"");
167 OSMNodes=LoadNodeList(filename);
168
169 if(!OSMNodes)
170 {
171 fprintf(stderr,"Cannot open nodes file '%s'.\n",filename);
172 return(1);
173 }
174
175 sprintf(filename,"%s%s%s%ssegments.mem",dirname?dirname:"",dirname?"/":"",prefix?prefix:"",prefix?"-":"");
176 OSMSegments=LoadSegmentList(filename);
177
178 if(!OSMSegments)
179 {
180 fprintf(stderr,"Cannot open segments file '%s'.\n",filename);
181 return(1);
182 }
183
184 sprintf(filename,"%s%s%s%sways.mem",dirname?dirname:"",dirname?"/":"",prefix?prefix:"",prefix?"-":"");
185 OSMWays=LoadWayList(filename);
186
187 if(!OSMWays)
188 {
189 fprintf(stderr,"Cannot open ways file '%s'.\n",filename);
190 return(1);
191 }
192
193 /* Get the start and finish */
194
195 {
196 float lat_start =(M_PI/180)*atof(argv[1]);
197 float lon_start =(M_PI/180)*atof(argv[2]);
198 float lat_finish=(M_PI/180)*atof(argv[3]);
199 float lon_finish=(M_PI/180)*atof(argv[4]);
200
201 Node *start_node =FindNode(OSMNodes,lat_start ,lon_start );
202 Node *finish_node=FindNode(OSMNodes,lat_finish,lon_finish);
203
204 if(!start_node)
205 {
206 fprintf(stderr,"Cannot find start node.\n");
207 return(1);
208 }
209
210 if(!finish_node)
211 {
212 fprintf(stderr,"Cannot find finish node.\n");
213 return(1);
214 }
215
216 if(!option_quiet)
217 {
218 float lat,lon;
219 distance_t dist;
220
221 GetLatLong(OSMNodes,start_node,&lat,&lon);
222 dist=Distance(lat_start,lon_start,lat,lon);
223
224 printf("Start node : %3.6f %4.6f = %2.3f km\n",(180/M_PI)*lat,(180/M_PI)*lon,distance_to_km(dist));
225
226 GetLatLong(OSMNodes,finish_node,&lat,&lon);
227 dist=Distance(lat_finish,lon_finish,lat,lon);
228
229 printf("Finish node: %3.6f %4.6f = %2.3f km\n",(180/M_PI)*lat,(180/M_PI)*lon,distance_to_km(dist));
230 }
231
232 start =IndexNode(OSMNodes,start_node );
233 finish=IndexNode(OSMNodes,finish_node);
234 }
235
236 /* Calculate the route. */
237
238 if(all)
239 {
240 Results *results;
241
242 /* Calculate the route */
243
244 results=FindRoute(OSMNodes,OSMSegments,OSMWays,start,finish,&profile,all);
245
246 /* Print the route */
247
248 if(!results)
249 {
250 fprintf(stderr,"Cannot find route compatible with profile.\n");
251 return(1);
252 }
253 else if(!no_print)
254 PrintRoute(results,OSMNodes,OSMSegments,OSMWays,start,finish,&profile);
255 }
256 else
257 {
258 Results *begin,*end;
259
260 /* Calculate the beginning of the route */
261
262 if(IsSuperNode(LookupNode(OSMNodes,start)))
263 {
264 Result *result;
265
266 begin=NewResultsList(1);
267
268 result=InsertResult(begin,start);
269
270 result->node=start;
271 result->prev=0;
272 result->next=0;
273 result->distance=0;
274 result->duration=0;
275 }
276 else
277 {
278 begin=FindRoutes(OSMNodes,OSMSegments,OSMWays,start,&profile);
279
280 if(!begin)
281 {
282 fprintf(stderr,"Cannot find initial section of route compatible with profile.\n");
283 return(1);
284 }
285 }
286
287 if(FindResult(begin,finish))
288 {
289 /* Print the route */
290
291 if(!no_print)
292 PrintRoute(begin,OSMNodes,OSMSegments,OSMWays,start,finish,&profile);
293 }
294 else
295 {
296 Results *superresults;
297
298 /* Calculate the end of the route */
299
300 if(IsSuperNode(LookupNode(OSMNodes,finish)))
301 {
302 Result *result;
303
304 end=NewResultsList(1);
305
306 result=InsertResult(end,finish);
307
308 result->node=finish;
309 result->prev=0;
310 result->next=0;
311 result->distance=0;
312 result->duration=0;
313 }
314 else
315 {
316 end=FindReverseRoutes(OSMNodes,OSMSegments,OSMWays,finish,&profile);
317
318 if(!end)
319 {
320 fprintf(stderr,"Cannot find final section of route compatible with profile.\n");
321 return(1);
322 }
323 }
324
325 /* Calculate the middle of the route */
326
327 superresults=FindRoute3(OSMNodes,OSMSegments,OSMWays,start,finish,begin,end,&profile);
328
329 /* Print the route */
330
331 if(!superresults)
332 {
333 fprintf(stderr,"Cannot find route compatible with profile.\n");
334 return(1);
335 }
336 else if(!no_print)
337 {
338 if(only_super)
339 {
340 PrintRoute(superresults,OSMNodes,OSMSegments,OSMWays,start,finish,&profile);
341 }
342 else
343 {
344 Results *results=CombineRoutes(superresults,OSMNodes,OSMSegments,OSMWays,start,finish,&profile);
345
346 PrintRoute(results,OSMNodes,OSMSegments,OSMWays,start,finish,&profile);
347 }
348 }
349 }
350 }
351
352 return(0);
353 }

Properties

Name Value
cvs:description Router.