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