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