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/planetsplitter.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1140 - (show annotations) (download) (as text)
Fri Nov 16 18:47:07 2012 UTC (12 years, 4 months ago) by amb
File MIME type: text/x-csrc
File size: 18976 byte(s)
Code to allow adding OSC change files (.osc files) to an existing set of parsed
(and preserved) data.

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_append=0,option_preserve=0,option_changes=0;
80 int option_filenames=0;
81 int option_prune_isolated=500,option_prune_short=5,option_prune_straight=3;
82 int arg;
83
84 gettimeofday(&start_time,NULL);
85
86 /* Parse the command line arguments */
87
88 for(arg=1;arg<argc;arg++)
89 {
90 if(!strcmp(argv[arg],"--help"))
91 print_usage(1,NULL,NULL);
92 else if(!strncmp(argv[arg],"--sort-ram-size=",16))
93 option_filesort_ramsize=atoi(&argv[arg][16]);
94 #if defined(USE_PTHREADS) && USE_PTHREADS
95 else if(!strncmp(argv[arg],"--sort-threads=",15))
96 option_filesort_threads=atoi(&argv[arg][15]);
97 #endif
98 else if(!strncmp(argv[arg],"--dir=",6))
99 dirname=&argv[arg][6];
100 else if(!strncmp(argv[arg],"--tmpdir=",9))
101 option_tmpdirname=&argv[arg][9];
102 else if(!strncmp(argv[arg],"--prefix=",9))
103 prefix=&argv[arg][9];
104 else if(!strcmp(argv[arg],"--parse-only"))
105 option_parse_only=1;
106 else if(!strcmp(argv[arg],"--process-only"))
107 option_process_only=1;
108 else if(!strcmp(argv[arg],"--append"))
109 option_append=1;
110 else if(!strcmp(argv[arg],"--preserve"))
111 option_preserve=1;
112 else if(!strcmp(argv[arg],"--changes"))
113 option_changes=1;
114 else if(!strcmp(argv[arg],"--loggable"))
115 option_loggable=1;
116 else if(!strcmp(argv[arg],"--logtime"))
117 option_logtime=1;
118 else if(!strcmp(argv[arg],"--errorlog"))
119 errorlog="error.log";
120 else if(!strncmp(argv[arg],"--errorlog=",11))
121 errorlog=&argv[arg][11];
122 else if(!strncmp(argv[arg],"--max-iterations=",17))
123 max_iterations=atoi(&argv[arg][17]);
124 else if(!strncmp(argv[arg],"--tagging=",10))
125 tagging=&argv[arg][10];
126 else if(!strncmp(argv[arg],"--prune",7))
127 {
128 if(!strcmp(&argv[arg][7],"-none"))
129 option_prune_isolated=option_prune_short=option_prune_straight=0;
130 else if(!strncmp(&argv[arg][7],"-isolated=",10))
131 option_prune_isolated=atoi(&argv[arg][17]);
132 else if(!strncmp(&argv[arg][7],"-short=",7))
133 option_prune_short=atoi(&argv[arg][14]);
134 else if(!strncmp(&argv[arg][7],"-straight=",10))
135 option_prune_straight=atoi(&argv[arg][17]);
136 else
137 print_usage(0,argv[arg],NULL);
138 }
139 else if(argv[arg][0]=='-' && argv[arg][1]=='-')
140 print_usage(0,argv[arg],NULL);
141 else
142 option_filenames++;
143 }
144
145 /* Check the specified command line options */
146
147 if(option_parse_only && option_process_only)
148 print_usage(0,NULL,"Cannot use '--parse-only' and '--process-only' at the same time.");
149
150 if(option_append && option_process_only)
151 print_usage(0,NULL,"Cannot use '--append' and '--process-only' at the same time.");
152
153 if(option_filenames && option_process_only)
154 print_usage(0,NULL,"Cannot use '--process-only' and filenames at the same time.");
155
156 if(!option_filesort_ramsize)
157 {
158 #if SLIM
159 option_filesort_ramsize=64*1024*1024;
160 #else
161 option_filesort_ramsize=256*1024*1024;
162 #endif
163 }
164 else
165 option_filesort_ramsize*=1024*1024;
166
167 if(!option_tmpdirname)
168 {
169 if(!dirname)
170 option_tmpdirname=".";
171 else
172 option_tmpdirname=dirname;
173 }
174
175 if(!option_process_only)
176 {
177 if(tagging)
178 {
179 if(!ExistsFile(tagging))
180 {
181 fprintf(stderr,"Error: The '--tagging' option specifies a file that does not exist.\n");
182 return(1);
183 }
184 }
185 else
186 {
187 if(ExistsFile(FileName(dirname,prefix,"tagging.xml")))
188 tagging=FileName(dirname,prefix,"tagging.xml");
189 else if(ExistsFile(FileName(DATADIR,NULL,"tagging.xml")))
190 tagging=FileName(DATADIR,NULL,"tagging.xml");
191 else
192 {
193 fprintf(stderr,"Error: The '--tagging' option was not used and the default 'tagging.xml' does not exist.\n");
194 return(1);
195 }
196 }
197
198 if(ParseXMLTaggingRules(tagging))
199 {
200 fprintf(stderr,"Error: Cannot read the tagging rules in the file '%s'.\n",tagging);
201 return(1);
202 }
203 }
204
205 /* Create new node, segment, way and relation variables */
206
207 Nodes=NewNodeList(option_append||option_changes,option_process_only);
208
209 Segments=NewSegmentList(option_append||option_changes,option_process_only);
210
211 Ways=NewWayList(option_append||option_changes,option_process_only);
212
213 Relations=NewRelationList(option_append||option_changes,option_process_only);
214
215 /* Create the error log file */
216
217 if(errorlog)
218 open_errorlog(FileName(dirname,prefix,errorlog),option_append||option_changes||option_process_only);
219
220 /* Parse the file */
221
222 if(!option_process_only)
223 {
224 if(option_filenames)
225 {
226 for(arg=1;arg<argc;arg++)
227 {
228 FILE *file;
229
230 if(argv[arg][0]=='-' && argv[arg][1]=='-')
231 continue;
232
233 file=fopen(argv[arg],"rb");
234
235 if(!file)
236 {
237 fprintf(stderr,"Cannot open file '%s' for reading [%s].\n",argv[arg],strerror(errno));
238 exit(EXIT_FAILURE);
239 }
240
241 if(option_changes)
242 {
243 printf("\nParse OSC Data [%s]\n==============\n\n",argv[arg]);
244 fflush(stdout);
245
246 if(ParseOSC(file,Nodes,Segments,Ways,Relations))
247 exit(EXIT_FAILURE);
248 }
249 else
250 {
251 printf("\nParse OSM Data [%s]\n==============\n\n",argv[arg]);
252 fflush(stdout);
253
254 if(ParseOSM(file,Nodes,Segments,Ways,Relations))
255 exit(EXIT_FAILURE);
256 }
257
258 fclose(file);
259 }
260 }
261 else
262 {
263 if(option_changes)
264 {
265 printf("\nParse OSC Data\n==============\n\n");
266 fflush(stdout);
267
268 if(ParseOSC(stdin,Nodes,Segments,Ways,Relations))
269 exit(EXIT_FAILURE);
270 }
271 else
272 {
273 printf("\nParse OSM Data\n==============\n\n");
274 fflush(stdout);
275
276 if(ParseOSM(stdin,Nodes,Segments,Ways,Relations))
277 exit(EXIT_FAILURE);
278 }
279 }
280
281 DeleteXMLTaggingRules();
282 }
283
284 FinishNodeList(Nodes,option_parse_only);
285 FinishSegmentList(Segments,option_parse_only);
286 FinishWayList(Ways,option_parse_only);
287 FinishRelationList(Relations,option_parse_only);
288
289 if(option_parse_only)
290 {
291 FreeNodeList(Nodes);
292 FreeSegmentList(Segments);
293 FreeWayList(Ways);
294 FreeRelationList(Relations);
295
296 return(0);
297 }
298
299
300 /* Process the data */
301
302 printf("\nProcess OSM Data\n================\n\n");
303 fflush(stdout);
304
305 /* Sort the nodes, segments, ways and relations */
306
307 SortNodeList(Nodes);
308
309 if(option_changes)
310 ApplySegmentChanges(Segments);
311
312 SortSegmentList(Segments);
313
314 SortWayList(Ways);
315
316 SortRelationList(Relations);
317
318 /* Extract the way names (must be before using the ways) */
319
320 ExtractWayNames(Ways,option_preserve||option_changes);
321
322 /* Remove bad segments (must be after sorting the nodes, segments and ways) */
323
324 RemoveBadSegments(Segments,Nodes,Ways,option_preserve||option_changes);
325
326 /* Remove non-highway nodes (must be after removing the bad segments) */
327
328 RemoveNonHighwayNodes(Nodes,Segments,option_preserve||option_changes);
329
330 /* Process the route relations and first part of turn relations (must be before compacting the ways) */
331
332 ProcessRouteRelations(Relations,Ways,option_preserve||option_changes);
333
334 ProcessTurnRelations1(Relations,Nodes,Ways,option_preserve||option_changes);
335
336 /* Measure the segments and replace node/way id with index (must be after removing non-highway nodes) */
337
338 MeasureSegments(Segments,Nodes,Ways);
339
340 /* Index the segments */
341
342 IndexSegments(Segments,Nodes,Ways);
343
344 /* Convert the turn relations from ways into nodes */
345
346 ProcessTurnRelations2(Relations,Nodes,Segments,Ways);
347
348 /* Compact the ways (must be after turn relations 2) */
349
350 CompactWayList(Ways,Segments);
351
352 /* Index the segments */
353
354 IndexSegments(Segments,Nodes,Ways);
355
356 /* Prune unwanted nodes/segments. */
357
358 if(option_prune_straight || option_prune_isolated || option_prune_short)
359 {
360 printf("\nPrune Unneeded Data\n===================\n\n");
361 fflush(stdout);
362
363 StartPruning(Nodes,Segments,Ways);
364
365 if(option_prune_straight)
366 PruneStraightHighwayNodes(Nodes,Segments,Ways,option_prune_straight);
367
368 if(option_prune_isolated)
369 PruneIsolatedRegions(Nodes,Segments,Ways,option_prune_isolated);
370
371 if(option_prune_short)
372 PruneShortSegments(Nodes,Segments,Ways,option_prune_short);
373
374 FinishPruning(Nodes,Segments,Ways);
375
376 /* Remove the pruned nodes and segments and update the indexes */
377
378 RemovePrunedNodes(Nodes,Segments);
379 RemovePrunedSegments(Segments,Ways);
380 CompactWayList(Ways,Segments);
381 RemovePrunedTurnRelations(Relations,Nodes);
382 IndexSegments(Segments,Nodes,Ways);
383 }
384
385 /* Repeated iteration on Super-Nodes and Super-Segments */
386
387 do
388 {
389 int nsuper;
390
391 printf("\nProcess Super-Data (iteration %d)\n================================%s\n\n",iteration,iteration>9?"=":"");
392 fflush(stdout);
393
394 if(iteration==0)
395 {
396 /* Select the super-nodes */
397
398 ChooseSuperNodes(Nodes,Segments,Ways);
399
400 /* Select the super-segments */
401
402 SuperSegments=CreateSuperSegments(Nodes,Segments,Ways);
403
404 nsuper=Segments->number;
405 }
406 else
407 {
408 SegmentsX *SuperSegments2;
409
410 /* Select the super-nodes */
411
412 ChooseSuperNodes(Nodes,SuperSegments,Ways);
413
414 /* Select the super-segments */
415
416 SuperSegments2=CreateSuperSegments(Nodes,SuperSegments,Ways);
417
418 nsuper=SuperSegments->number;
419
420 FreeSegmentList(SuperSegments);
421
422 SuperSegments=SuperSegments2;
423 }
424
425 /* Sort the super-segments and remove duplicates */
426
427 DeduplicateSuperSegments(SuperSegments,Ways);
428
429 /* Index the segments */
430
431 IndexSegments(SuperSegments,Nodes,Ways);
432
433 /* Check for end condition */
434
435 if(SuperSegments->number==nsuper)
436 quit=1;
437
438 iteration++;
439
440 if(iteration>max_iterations)
441 quit=1;
442 }
443 while(!quit);
444
445 /* Combine the super-segments */
446
447 printf("\nCombine Segments and Super-Segments\n===================================\n\n");
448 fflush(stdout);
449
450 /* Merge the super-segments */
451
452 MergedSegments=MergeSuperSegments(Segments,SuperSegments);
453
454 FreeSegmentList(Segments);
455
456 FreeSegmentList(SuperSegments);
457
458 Segments=MergedSegments;
459
460 /* Re-index the merged segments */
461
462 IndexSegments(Segments,Nodes,Ways);
463
464 /* Cross reference the nodes and segments */
465
466 printf("\nCross-Reference Nodes and Segments\n==================================\n\n");
467 fflush(stdout);
468
469 /* Sort the nodes and segments geographically */
470
471 SortNodeListGeographically(Nodes);
472
473 SortSegmentListGeographically(Segments,Nodes);
474
475 /* Re-index the segments */
476
477 IndexSegments(Segments,Nodes,Ways);
478
479 /* Sort the turn relations geographically */
480
481 SortTurnRelationListGeographically(Relations,Nodes,Segments);
482
483 /* Output the results */
484
485 printf("\nWrite Out Database Files\n========================\n\n");
486 fflush(stdout);
487
488 /* Write out the nodes */
489
490 SaveNodeList(Nodes,FileName(dirname,prefix,"nodes.mem"),Segments);
491
492 FreeNodeList(Nodes);
493
494 /* Write out the segments */
495
496 SaveSegmentList(Segments,FileName(dirname,prefix,"segments.mem"));
497
498 FreeSegmentList(Segments);
499
500 /* Write out the ways */
501
502 SaveWayList(Ways,FileName(dirname,prefix,"ways.mem"));
503
504 FreeWayList(Ways);
505
506 /* Write out the relations */
507
508 SaveRelationList(Relations,FileName(dirname,prefix,"relations.mem"));
509
510 FreeRelationList(Relations);
511
512 /* Close the error log file */
513
514 if(errorlog)
515 close_errorlog();
516
517 /* Print the total time */
518
519 if(option_logtime)
520 {
521 printf("\n");
522 fprintf_elapsed_time(stdout,&start_time);
523 printf("Complete\n");
524 fflush(stdout);
525 }
526
527 return(0);
528 }
529
530
531 /*++++++++++++++++++++++++++++++++++++++
532 Print out the usage information.
533
534 int detail The level of detail to use - 0 = low, 1 = high.
535
536 const char *argerr The argument that gave the error (if there is one).
537
538 const char *err Other error message (if there is one).
539 ++++++++++++++++++++++++++++++++++++++*/
540
541 static void print_usage(int detail,const char *argerr,const char *err)
542 {
543 fprintf(stderr,
544 "Usage: planetsplitter [--help]\n"
545 " [--dir=<dirname>] [--prefix=<name>]\n"
546 #if defined(USE_PTHREADS) && USE_PTHREADS
547 " [--sort-ram-size=<size>] [--sort-threads=<number>]\n"
548 #else
549 " [--sort-ram-size=<size>]\n"
550 #endif
551 " [--tmpdir=<dirname>]\n"
552 " [--tagging=<filename>]\n"
553 " [--loggable] [--logtime]\n"
554 " [--errorlog[=<name>]]\n"
555 " [--parse-only | --process-only]\n"
556 " [--append] [--preserve] [--changes]\n"
557 " [--max-iterations=<number>]\n"
558 " [--prune-none]\n"
559 " [--prune-isolated=<len>]\n"
560 " [--prune-short=<len>]\n"
561 " [--prune-straight=<len>]\n"
562 " [<filename.osm> ...]\n");
563
564 if(argerr)
565 fprintf(stderr,
566 "\n"
567 "Error with command line parameter: %s\n",argerr);
568
569 if(err)
570 fprintf(stderr,
571 "\n"
572 "Error: %s\n",err);
573
574 if(detail)
575 fprintf(stderr,
576 "\n"
577 "--help Prints this information.\n"
578 "\n"
579 "--dir=<dirname> The directory containing the routing database.\n"
580 "--prefix=<name> The filename prefix for the routing database.\n"
581 "\n"
582 "--sort-ram-size=<size> The amount of RAM (in MB) to use for data sorting\n"
583 #if SLIM
584 " (defaults to 64MB otherwise.)\n"
585 #else
586 " (defaults to 256MB otherwise.)\n"
587 #endif
588 #if defined(USE_PTHREADS) && USE_PTHREADS
589 "--sort-threads=<number> The number of threads to use for data sorting.\n"
590 #endif
591 "\n"
592 "--tmpdir=<dirname> The directory name for temporary files.\n"
593 " (defaults to the '--dir' option directory.)\n"
594 "\n"
595 "--tagging=<filename> The name of the XML file containing the tagging rules\n"
596 " (defaults to 'tagging.xml' with '--dir' and\n"
597 " '--prefix' options or the file installed in\n"
598 " '" DATADIR "').\n"
599 "\n"
600 "--loggable Print progress messages suitable for logging to file.\n"
601 "--logtime Print the elapsed time for each processing step.\n"
602 "--errorlog[=<name>] Log parsing errors to 'error.log' or the given name\n"
603 " (the '--dir' and '--prefix' options are applied).\n"
604 "\n"
605 "--parse-only Parse the OSM/OSC file(s) and store the results.\n"
606 "--process-only Process the stored results from previous option.\n"
607 "--append Parse the OSM file(s) and append to existing results.\n"
608 "--preserve Keep the intermediate files after parsing & sorting.\n"
609 "--changes Parse the data as an OSC file and apply the changes.\n"
610 "\n"
611 "--max-iterations=<number> The number of iterations for finding super-nodes\n"
612 " (defaults to 5).\n"
613 "\n"
614 "--prune-none Disable the prune options below, they are re-enabled\n"
615 " by adding them to the command line after this option.\n"
616 "--prune-isolated=<len> Remove access from small disconnected segment groups\n"
617 " (defaults to removing groups under 500m).\n"
618 "--prune-short=<len> Remove short segments (defaults to removing segments\n"
619 " up to a maximum length of 5m).\n"
620 "--prune-straight=<len> Remove nodes in almost straight highways (defaults to\n"
621 " removing nodes up to 3m offset from a straight line).\n"
622 "\n"
623 "<filename.osm> ... The name(s) of the file(s) to process (by default\n"
624 " data is read from standard input).\n"
625 "\n"
626 "<transport> defaults to all but can be set to:\n"
627 "%s"
628 "\n"
629 "<highway> can be selected from:\n"
630 "%s"
631 "\n"
632 "<property> can be selected from:\n"
633 "%s",
634 TransportList(),HighwayList(),PropertyList());
635
636 exit(!detail);
637 }

Properties

Name Value
cvs:description Planet file splitter.