Check out the latest version of Routino: svn co http://routino.org/svn/trunk routino
Contents of /trunk/src/planetsplitter.c
Parent Directory
|
Revision Log
Revision 1100 -
(show annotations)
(download)
(as text)
Sat Oct 20 16:52:20 2012 UTC (12 years, 5 months ago) by amb
File MIME type: text/x-csrc
File size: 17374 byte(s)
Sat Oct 20 16:52:20 2012 UTC (12 years, 5 months ago) by amb
File MIME type: text/x-csrc
File size: 17374 byte(s)
Move the compacting of the ways back to the top, delete the unused ways at this point and also call the function again after pruning segments.
1 | /*************************************** |
2 | OSM planet file splitter. |
3 | |
4 | Part of the Routino routing software. |
5 | ******************/ /****************** |
6 | This file Copyright 2008-2012 Andrew M. Bishop |
7 | |
8 | This program is free software: you can redistribute it and/or modify |
9 | it under the terms of the GNU Affero General Public License as published by |
10 | the Free Software Foundation, either version 3 of the License, or |
11 | (at your option) any later version. |
12 | |
13 | This program is distributed in the hope that it will be useful, |
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16 | GNU Affero General Public License for more details. |
17 | |
18 | You should have received a copy of the GNU Affero General Public License |
19 | along with this program. If not, see <http://www.gnu.org/licenses/>. |
20 | ***************************************/ |
21 | |
22 | |
23 | #include <stdio.h> |
24 | #include <stdlib.h> |
25 | #include <string.h> |
26 | #include <errno.h> |
27 | #include <sys/time.h> |
28 | |
29 | #include "types.h" |
30 | #include "ways.h" |
31 | |
32 | #include "typesx.h" |
33 | #include "nodesx.h" |
34 | #include "segmentsx.h" |
35 | #include "waysx.h" |
36 | #include "relationsx.h" |
37 | #include "superx.h" |
38 | #include "prunex.h" |
39 | |
40 | #include "files.h" |
41 | #include "logging.h" |
42 | #include "functions.h" |
43 | #include "osmparser.h" |
44 | #include "tagging.h" |
45 | |
46 | |
47 | /* Global variables */ |
48 | |
49 | /*+ The name of the temporary directory. +*/ |
50 | char *option_tmpdirname=NULL; |
51 | |
52 | /*+ The amount of RAM to use for filesorting. +*/ |
53 | size_t option_filesort_ramsize=0; |
54 | |
55 | /*+ The number of threads to use for filesorting. +*/ |
56 | int option_filesort_threads=1; |
57 | |
58 | |
59 | /* Local functions */ |
60 | |
61 | static void print_usage(int detail,const char *argerr,const char *err); |
62 | |
63 | |
64 | /*++++++++++++++++++++++++++++++++++++++ |
65 | The main program for the planetsplitter. |
66 | ++++++++++++++++++++++++++++++++++++++*/ |
67 | |
68 | int main(int argc,char** argv) |
69 | { |
70 | struct timeval start_time; |
71 | NodesX *Nodes; |
72 | SegmentsX *Segments,*SuperSegments=NULL,*MergedSegments=NULL; |
73 | WaysX *Ways; |
74 | RelationsX *Relations; |
75 | int iteration=0,quit=0; |
76 | int max_iterations=5; |
77 | char *dirname=NULL,*prefix=NULL,*tagging=NULL,*errorlog=NULL; |
78 | int option_parse_only=0,option_process_only=0; |
79 | int option_filenames=0; |
80 | int option_prune_isolated=500,option_prune_short=5,option_prune_straight=3; |
81 | int arg; |
82 | |
83 | gettimeofday(&start_time,NULL); |
84 | |
85 | /* Parse the command line arguments */ |
86 | |
87 | for(arg=1;arg<argc;arg++) |
88 | { |
89 | if(!strcmp(argv[arg],"--help")) |
90 | print_usage(1,NULL,NULL); |
91 | else if(!strncmp(argv[arg],"--sort-ram-size=",16)) |
92 | option_filesort_ramsize=atoi(&argv[arg][16]); |
93 | #if defined(USE_PTHREADS) && USE_PTHREADS |
94 | else if(!strncmp(argv[arg],"--sort-threads=",15)) |
95 | option_filesort_threads=atoi(&argv[arg][15]); |
96 | #endif |
97 | else if(!strncmp(argv[arg],"--dir=",6)) |
98 | dirname=&argv[arg][6]; |
99 | else if(!strncmp(argv[arg],"--tmpdir=",9)) |
100 | option_tmpdirname=&argv[arg][9]; |
101 | else if(!strncmp(argv[arg],"--prefix=",9)) |
102 | prefix=&argv[arg][9]; |
103 | else if(!strcmp(argv[arg],"--parse-only")) |
104 | option_parse_only=1; |
105 | else if(!strcmp(argv[arg],"--process-only")) |
106 | option_process_only=1; |
107 | else if(!strcmp(argv[arg],"--loggable")) |
108 | option_loggable=1; |
109 | else if(!strcmp(argv[arg],"--logtime")) |
110 | option_logtime=1; |
111 | else if(!strcmp(argv[arg],"--errorlog")) |
112 | errorlog="error.log"; |
113 | else if(!strncmp(argv[arg],"--errorlog=",11)) |
114 | errorlog=&argv[arg][11]; |
115 | else if(!strncmp(argv[arg],"--max-iterations=",17)) |
116 | max_iterations=atoi(&argv[arg][17]); |
117 | else if(!strncmp(argv[arg],"--tagging=",10)) |
118 | tagging=&argv[arg][10]; |
119 | else if(!strncmp(argv[arg],"--prune",7)) |
120 | { |
121 | if(!strcmp(&argv[arg][7],"-none")) |
122 | option_prune_isolated=option_prune_short=option_prune_straight=0; |
123 | else if(!strncmp(&argv[arg][7],"-isolated=",10)) |
124 | option_prune_isolated=atoi(&argv[arg][17]); |
125 | else if(!strncmp(&argv[arg][7],"-short=",7)) |
126 | option_prune_short=atoi(&argv[arg][14]); |
127 | else if(!strncmp(&argv[arg][7],"-straight=",10)) |
128 | option_prune_straight=atoi(&argv[arg][17]); |
129 | else |
130 | print_usage(0,argv[arg],NULL); |
131 | } |
132 | else if(argv[arg][0]=='-' && argv[arg][1]=='-') |
133 | print_usage(0,argv[arg],NULL); |
134 | else |
135 | option_filenames++; |
136 | } |
137 | |
138 | /* Check the specified command line options */ |
139 | |
140 | if(option_parse_only && option_process_only) |
141 | print_usage(0,NULL,"Cannot use '--parse-only' and '--process-only' at the same time."); |
142 | |
143 | if(option_filenames && option_process_only) |
144 | print_usage(0,NULL,"Cannot use '--process-only' and filenames at the same time."); |
145 | |
146 | if(!option_filesort_ramsize) |
147 | { |
148 | #if SLIM |
149 | option_filesort_ramsize=64*1024*1024; |
150 | #else |
151 | option_filesort_ramsize=256*1024*1024; |
152 | #endif |
153 | } |
154 | else |
155 | option_filesort_ramsize*=1024*1024; |
156 | |
157 | if(!option_tmpdirname) |
158 | { |
159 | if(!dirname) |
160 | option_tmpdirname="."; |
161 | else |
162 | option_tmpdirname=dirname; |
163 | } |
164 | |
165 | |
166 | if(!option_process_only) |
167 | { |
168 | if(tagging) |
169 | { |
170 | if(!ExistsFile(tagging)) |
171 | { |
172 | fprintf(stderr,"Error: The '--tagging' option specifies a file that does not exist.\n"); |
173 | return(1); |
174 | } |
175 | } |
176 | else |
177 | { |
178 | if(ExistsFile(FileName(dirname,prefix,"tagging.xml"))) |
179 | tagging=FileName(dirname,prefix,"tagging.xml"); |
180 | else if(ExistsFile(FileName(DATADIR,NULL,"tagging.xml"))) |
181 | tagging=FileName(DATADIR,NULL,"tagging.xml"); |
182 | else |
183 | { |
184 | fprintf(stderr,"Error: The '--tagging' option was not used and the default 'tagging.xml' does not exist.\n"); |
185 | return(1); |
186 | } |
187 | } |
188 | |
189 | if(ParseXMLTaggingRules(tagging)) |
190 | { |
191 | fprintf(stderr,"Error: Cannot read the tagging rules in the file '%s'.\n",tagging); |
192 | return(1); |
193 | } |
194 | } |
195 | |
196 | /* Create new node, segment, way and relation variables */ |
197 | |
198 | Nodes=NewNodeList(option_parse_only||option_process_only); |
199 | |
200 | Segments=NewSegmentList(option_parse_only||option_process_only); |
201 | |
202 | Ways=NewWayList(option_parse_only||option_process_only); |
203 | |
204 | Relations=NewRelationList(option_parse_only||option_process_only); |
205 | |
206 | /* Create the error log file */ |
207 | |
208 | if(errorlog) |
209 | open_errorlog(FileName(dirname,prefix,errorlog),option_parse_only||option_process_only); |
210 | |
211 | /* Parse the file */ |
212 | |
213 | if(!option_process_only) |
214 | { |
215 | if(option_filenames) |
216 | { |
217 | for(arg=1;arg<argc;arg++) |
218 | { |
219 | FILE *file; |
220 | |
221 | if(argv[arg][0]=='-' && argv[arg][1]=='-') |
222 | continue; |
223 | |
224 | file=fopen(argv[arg],"rb"); |
225 | |
226 | if(!file) |
227 | { |
228 | fprintf(stderr,"Cannot open file '%s' for reading [%s].\n",argv[arg],strerror(errno)); |
229 | exit(EXIT_FAILURE); |
230 | } |
231 | |
232 | printf("\nParse OSM Data [%s]\n==============\n\n",argv[arg]); |
233 | fflush(stdout); |
234 | |
235 | if(ParseOSM(file,Nodes,Segments,Ways,Relations)) |
236 | exit(EXIT_FAILURE); |
237 | |
238 | fclose(file); |
239 | } |
240 | } |
241 | else |
242 | { |
243 | printf("\nParse OSM Data\n==============\n\n"); |
244 | fflush(stdout); |
245 | |
246 | if(ParseOSM(stdin,Nodes,Segments,Ways,Relations)) |
247 | exit(EXIT_FAILURE); |
248 | } |
249 | |
250 | DeleteXMLTaggingRules(); |
251 | } |
252 | |
253 | if(option_parse_only) |
254 | { |
255 | FreeNodeList(Nodes,1); |
256 | FreeSegmentList(Segments,1); |
257 | FreeWayList(Ways,1); |
258 | FreeRelationList(Relations,1); |
259 | |
260 | return(0); |
261 | } |
262 | |
263 | /* Process the data */ |
264 | |
265 | printf("\nProcess OSM Data\n================\n\n"); |
266 | fflush(stdout); |
267 | |
268 | /* Sort the nodes, segments, ways and relations */ |
269 | |
270 | SortNodeList(Nodes); |
271 | |
272 | SortSegmentList(Segments); |
273 | |
274 | SortWayList(Ways); |
275 | |
276 | SortRelationList(Relations); |
277 | |
278 | /* Remove bad segments (must be after sorting the nodes and segments) */ |
279 | |
280 | RemoveBadSegments(Nodes,Segments); |
281 | |
282 | /* Remove non-highway nodes (must be after removing the bad segments) */ |
283 | |
284 | RemoveNonHighwayNodes(Nodes,Segments); |
285 | |
286 | /* Process the route relations and first part of turn relations (must be before compacting the ways) */ |
287 | |
288 | ProcessRouteRelations(Relations,Ways); |
289 | |
290 | ProcessTurnRelations1(Relations,Nodes,Ways); |
291 | |
292 | /* Measure the segments and replace node/way id with index (must be after removing non-highway nodes) */ |
293 | |
294 | MeasureSegments(Segments,Nodes,Ways); |
295 | |
296 | /* Index the segments */ |
297 | |
298 | IndexSegments(Segments,Nodes,Ways); |
299 | |
300 | /* Convert the turn relations from ways into nodes */ |
301 | |
302 | ProcessTurnRelations2(Relations,Nodes,Segments,Ways); |
303 | |
304 | /* Compact the ways (must be after turn relations 2) */ |
305 | |
306 | CompactWayList(Ways,Segments); |
307 | |
308 | /* Index the segments */ |
309 | |
310 | IndexSegments(Segments,Nodes,Ways); |
311 | |
312 | /* Prune unwanted nodes/segments. */ |
313 | |
314 | if(option_prune_straight || option_prune_isolated || option_prune_short) |
315 | { |
316 | printf("\nPrune Unneeded Data\n===================\n\n"); |
317 | fflush(stdout); |
318 | |
319 | StartPruning(Nodes,Segments,Ways); |
320 | |
321 | if(option_prune_straight) |
322 | PruneStraightHighwayNodes(Nodes,Segments,Ways,option_prune_straight); |
323 | |
324 | if(option_prune_isolated) |
325 | PruneIsolatedRegions(Nodes,Segments,Ways,option_prune_isolated); |
326 | |
327 | if(option_prune_short) |
328 | PruneShortSegments(Nodes,Segments,Ways,option_prune_short); |
329 | |
330 | FinishPruning(Nodes,Segments,Ways); |
331 | |
332 | /* Remove the pruned nodes and segments and update the indexes */ |
333 | |
334 | RemovePrunedNodes(Nodes,Segments); |
335 | RemovePrunedSegments(Segments,Ways); |
336 | CompactWayList(Ways,Segments); |
337 | RemovePrunedTurnRelations(Relations,Nodes); |
338 | IndexSegments(Segments,Nodes,Ways); |
339 | } |
340 | |
341 | /* Repeated iteration on Super-Nodes and Super-Segments */ |
342 | |
343 | do |
344 | { |
345 | int nsuper; |
346 | |
347 | printf("\nProcess Super-Data (iteration %d)\n================================%s\n\n",iteration,iteration>9?"=":""); |
348 | fflush(stdout); |
349 | |
350 | if(iteration==0) |
351 | { |
352 | /* Select the super-nodes */ |
353 | |
354 | ChooseSuperNodes(Nodes,Segments,Ways); |
355 | |
356 | /* Select the super-segments */ |
357 | |
358 | SuperSegments=CreateSuperSegments(Nodes,Segments,Ways); |
359 | |
360 | nsuper=Segments->number; |
361 | } |
362 | else |
363 | { |
364 | SegmentsX *SuperSegments2; |
365 | |
366 | /* Select the super-nodes */ |
367 | |
368 | ChooseSuperNodes(Nodes,SuperSegments,Ways); |
369 | |
370 | /* Select the super-segments */ |
371 | |
372 | SuperSegments2=CreateSuperSegments(Nodes,SuperSegments,Ways); |
373 | |
374 | nsuper=SuperSegments->number; |
375 | |
376 | FreeSegmentList(SuperSegments,0); |
377 | |
378 | SuperSegments=SuperSegments2; |
379 | } |
380 | |
381 | /* Sort the super-segments */ |
382 | |
383 | SortSegmentList(SuperSegments); |
384 | |
385 | /* Remove duplicated super-segments */ |
386 | |
387 | DeduplicateSegments(SuperSegments,Nodes,Ways); |
388 | |
389 | /* Index the segments */ |
390 | |
391 | IndexSegments(SuperSegments,Nodes,Ways); |
392 | |
393 | /* Check for end condition */ |
394 | |
395 | if(SuperSegments->number==nsuper) |
396 | quit=1; |
397 | |
398 | iteration++; |
399 | |
400 | if(iteration>max_iterations) |
401 | quit=1; |
402 | } |
403 | while(!quit); |
404 | |
405 | /* Combine the super-segments */ |
406 | |
407 | printf("\nCombine Segments and Super-Segments\n===================================\n\n"); |
408 | fflush(stdout); |
409 | |
410 | /* Merge the super-segments */ |
411 | |
412 | MergedSegments=MergeSuperSegments(Segments,SuperSegments); |
413 | |
414 | FreeSegmentList(Segments,0); |
415 | |
416 | FreeSegmentList(SuperSegments,0); |
417 | |
418 | Segments=MergedSegments; |
419 | |
420 | /* Sort and re-index the merged segments */ |
421 | |
422 | SortSegmentList(Segments); |
423 | |
424 | IndexSegments(Segments,Nodes,Ways); |
425 | |
426 | /* Cross reference the nodes and segments */ |
427 | |
428 | printf("\nCross-Reference Nodes and Segments\n==================================\n\n"); |
429 | fflush(stdout); |
430 | |
431 | /* Sort the nodes geographically and update the segment indexes accordingly */ |
432 | |
433 | SortNodeListGeographically(Nodes); |
434 | |
435 | UpdateSegments(Segments,Nodes,Ways); |
436 | |
437 | /* Sort the segments geographically and re-index them */ |
438 | |
439 | SortSegmentList(Segments); |
440 | |
441 | IndexSegments(Segments,Nodes,Ways); |
442 | |
443 | /* Update the nodes */ |
444 | |
445 | UpdateNodes(Nodes,Segments); |
446 | |
447 | /* Fix the turn relations after sorting nodes geographically */ |
448 | |
449 | UpdateTurnRelations(Relations,Nodes,Segments); |
450 | |
451 | SortTurnRelationList(Relations); |
452 | |
453 | /* Output the results */ |
454 | |
455 | printf("\nWrite Out Database Files\n========================\n\n"); |
456 | fflush(stdout); |
457 | |
458 | /* Write out the nodes */ |
459 | |
460 | SaveNodeList(Nodes,FileName(dirname,prefix,"nodes.mem")); |
461 | |
462 | FreeNodeList(Nodes,0); |
463 | |
464 | /* Write out the segments */ |
465 | |
466 | SaveSegmentList(Segments,FileName(dirname,prefix,"segments.mem")); |
467 | |
468 | FreeSegmentList(Segments,0); |
469 | |
470 | /* Write out the ways */ |
471 | |
472 | SaveWayList(Ways,FileName(dirname,prefix,"ways.mem")); |
473 | |
474 | FreeWayList(Ways,0); |
475 | |
476 | /* Write out the relations */ |
477 | |
478 | SaveRelationList(Relations,FileName(dirname,prefix,"relations.mem")); |
479 | |
480 | FreeRelationList(Relations,0); |
481 | |
482 | /* Close the error log file */ |
483 | |
484 | if(errorlog) |
485 | close_errorlog(); |
486 | |
487 | /* Print the total time */ |
488 | |
489 | if(option_logtime) |
490 | { |
491 | printf("\n"); |
492 | fprintf_elapsed_time(stdout,&start_time); |
493 | printf("Complete\n"); |
494 | fflush(stdout); |
495 | } |
496 | |
497 | return(0); |
498 | } |
499 | |
500 | |
501 | /*++++++++++++++++++++++++++++++++++++++ |
502 | Print out the usage information. |
503 | |
504 | int detail The level of detail to use - 0 = low, 1 = high. |
505 | |
506 | const char *argerr The argument that gave the error (if there is one). |
507 | |
508 | const char *err Other error message (if there is one). |
509 | ++++++++++++++++++++++++++++++++++++++*/ |
510 | |
511 | static void print_usage(int detail,const char *argerr,const char *err) |
512 | { |
513 | fprintf(stderr, |
514 | "Usage: planetsplitter [--help]\n" |
515 | " [--dir=<dirname>] [--prefix=<name>]\n" |
516 | #if defined(USE_PTHREADS) && USE_PTHREADS |
517 | " [--sort-ram-size=<size>] [--sort-threads=<number>]\n" |
518 | #else |
519 | " [--sort-ram-size=<size>]\n" |
520 | #endif |
521 | " [--tmpdir=<dirname>]\n" |
522 | " [--tagging=<filename>]\n" |
523 | " [--loggable] [--logtime]\n" |
524 | " [--errorlog[=<name>]]\n" |
525 | " [--parse-only | --process-only]\n" |
526 | " [--max-iterations=<number>]\n" |
527 | " [--prune-none]\n" |
528 | " [--prune-isolated=<len>]\n" |
529 | " [--prune-short=<len>]\n" |
530 | " [--prune-straight=<len>]\n" |
531 | " [<filename.osm> ...]\n"); |
532 | |
533 | if(argerr) |
534 | fprintf(stderr, |
535 | "\n" |
536 | "Error with command line parameter: %s\n",argerr); |
537 | |
538 | if(err) |
539 | fprintf(stderr, |
540 | "\n" |
541 | "Error: %s\n",err); |
542 | |
543 | if(detail) |
544 | fprintf(stderr, |
545 | "\n" |
546 | "--help Prints this information.\n" |
547 | "\n" |
548 | "--dir=<dirname> The directory containing the routing database.\n" |
549 | "--prefix=<name> The filename prefix for the routing database.\n" |
550 | "\n" |
551 | "--sort-ram-size=<size> The amount of RAM (in MB) to use for data sorting\n" |
552 | #if SLIM |
553 | " (defaults to 64MB otherwise.)\n" |
554 | #else |
555 | " (defaults to 256MB otherwise.)\n" |
556 | #endif |
557 | #if defined(USE_PTHREADS) && USE_PTHREADS |
558 | "--sort-threads=<number> The number of threads to use for data sorting.\n" |
559 | #endif |
560 | "\n" |
561 | "--tmpdir=<dirname> The directory name for temporary files.\n" |
562 | " (defaults to the '--dir' option directory.)\n" |
563 | "\n" |
564 | "--tagging=<filename> The name of the XML file containing the tagging rules\n" |
565 | " (defaults to 'tagging.xml' with '--dir' and\n" |
566 | " '--prefix' options or the file installed in\n" |
567 | " '" DATADIR "').\n" |
568 | "\n" |
569 | "--loggable Print progress messages suitable for logging to file.\n" |
570 | "--logtime Print the elapsed time for each processing step.\n" |
571 | "--errorlog[=<name>] Log parsing errors to 'error.log' or the given name\n" |
572 | " (the '--dir' and '--prefix' options are applied).\n" |
573 | "\n" |
574 | "--parse-only Parse the input OSM files and store the results.\n" |
575 | "--process-only Process the stored results from previous option.\n" |
576 | "\n" |
577 | "--max-iterations=<number> The number of iterations for finding super-nodes\n" |
578 | " (defaults to 5).\n" |
579 | "\n" |
580 | "--prune-none Disable the prune options below, they are re-enabled\n" |
581 | " by adding them to the command line after this option.\n" |
582 | "--prune-isolated=<len> Remove small disconnected groups of segments\n" |
583 | " (defaults to removing groups under 500m).\n" |
584 | "--prune-short=<len> Remove short segments (defaults to removing segments\n" |
585 | " up to a maximum length of 5m).\n" |
586 | "--prune-straight=<len> Remove nodes in almost straight highways (defaults to\n" |
587 | " removing nodes up to 3m offset from a straight line).\n" |
588 | "\n" |
589 | "<filename.osm> ... The name(s) of the file(s) to process (by default\n" |
590 | " data is read from standard input).\n" |
591 | "\n" |
592 | "<transport> defaults to all but can be set to:\n" |
593 | "%s" |
594 | "\n" |
595 | "<highway> can be selected from:\n" |
596 | "%s" |
597 | "\n" |
598 | "<property> can be selected from:\n" |
599 | "%s", |
600 | TransportList(),HighwayList(),PropertyList()); |
601 | |
602 | exit(!detail); |
603 | } |
Properties
Name | Value |
---|---|
cvs:description | Planet file splitter. |