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