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 1091 - (show annotations) (download) (as text)
Wed Oct 17 18:31:53 2012 UTC (12 years, 5 months ago) by amb
File MIME type: text/x-csrc
File size: 16929 byte(s)
Perform the Way compacting at the end (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,0);
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);
299
300 /* Convert the turn relations from ways into nodes */
301
302 ProcessTurnRelations2(Relations,Nodes,Segments,Ways);
303
304 /* Prune unwanted nodes/segments. */
305
306 if(option_prune_straight || option_prune_isolated || option_prune_short)
307 {
308 StartPruning(Nodes,Segments,Ways);
309
310 if(option_prune_straight)
311 PruneStraightHighwayNodes(Nodes,Segments,Ways,option_prune_straight);
312
313 if(option_prune_isolated)
314 PruneIsolatedRegions(Nodes,Segments,Ways,option_prune_isolated);
315
316 if(option_prune_short)
317 PruneShortSegments(Nodes,Segments,Ways,option_prune_short);
318
319 FinishPruning(Nodes,Segments,Ways);
320 }
321
322 /* Repeated iteration on Super-Nodes and Super-Segments */
323
324 do
325 {
326 int nsuper;
327
328 printf("\nProcess Super-Data (iteration %d)\n================================%s\n\n",iteration,iteration>9?"=":"");
329 fflush(stdout);
330
331 if(iteration==0)
332 {
333 /* Select the super-nodes */
334
335 ChooseSuperNodes(Nodes,Segments,Ways);
336
337 /* Select the super-segments */
338
339 SuperSegments=CreateSuperSegments(Nodes,Segments,Ways);
340
341 nsuper=Segments->number;
342 }
343 else
344 {
345 SegmentsX *SuperSegments2;
346
347 /* Select the super-nodes */
348
349 ChooseSuperNodes(Nodes,SuperSegments,Ways);
350
351 /* Select the super-segments */
352
353 SuperSegments2=CreateSuperSegments(Nodes,SuperSegments,Ways);
354
355 nsuper=SuperSegments->number;
356
357 FreeSegmentList(SuperSegments,0);
358
359 SuperSegments=SuperSegments2;
360 }
361
362 /* Sort the super-segments */
363
364 SortSegmentList(SuperSegments,0);
365
366 /* Remove duplicated super-segments */
367
368 DeduplicateSegments(SuperSegments,Nodes,Ways);
369
370 /* Index the segments */
371
372 IndexSegments(SuperSegments,Nodes);
373
374 /* Check for end condition */
375
376 if(SuperSegments->number==nsuper)
377 quit=1;
378
379 iteration++;
380
381 if(iteration>max_iterations)
382 quit=1;
383 }
384 while(!quit);
385
386 /* Combine the super-segments */
387
388 printf("\nCombine Segments and Super-Segments\n===================================\n\n");
389 fflush(stdout);
390
391 /* Merge the super-segments */
392
393 MergedSegments=MergeSuperSegments(Segments,SuperSegments);
394
395 FreeSegmentList(Segments,0);
396
397 FreeSegmentList(SuperSegments,0);
398
399 Segments=MergedSegments;
400
401 /* Sort and re-index the segments */
402
403 SortSegmentList(Segments,0);
404
405 IndexSegments(Segments,Nodes);
406
407 /* Cross reference the nodes and segments */
408
409 printf("\nCross-Reference Nodes and Segments\n==================================\n\n");
410 fflush(stdout);
411
412 /* Compact the ways (must be before updating the segments) */
413
414 CompactWayList(Ways);
415
416 /* Sort the nodes geographically and update the segment indexes accordingly */
417
418 SortNodeListGeographically(Nodes);
419
420 UpdateSegments(Segments,Nodes,Ways);
421
422 /* Sort the segments geographically and re-index them */
423
424 SortSegmentList(Segments,0);
425
426 IndexSegments(Segments,Nodes);
427
428 /* Update the nodes */
429
430 UpdateNodes(Nodes,Segments);
431
432 /* Fix the turn relations after sorting nodes geographically */
433
434 UpdateTurnRelations(Relations,Nodes,Segments);
435
436 SortTurnRelationList(Relations);
437
438 /* Output the results */
439
440 printf("\nWrite Out Database Files\n========================\n\n");
441 fflush(stdout);
442
443 /* Write out the nodes */
444
445 SaveNodeList(Nodes,FileName(dirname,prefix,"nodes.mem"));
446
447 FreeNodeList(Nodes,0);
448
449 /* Write out the segments */
450
451 SaveSegmentList(Segments,FileName(dirname,prefix,"segments.mem"));
452
453 FreeSegmentList(Segments,0);
454
455 /* Write out the ways */
456
457 SaveWayList(Ways,FileName(dirname,prefix,"ways.mem"));
458
459 FreeWayList(Ways,0);
460
461 /* Write out the relations */
462
463 SaveRelationList(Relations,FileName(dirname,prefix,"relations.mem"));
464
465 FreeRelationList(Relations,0);
466
467 /* Close the error log file */
468
469 if(errorlog)
470 close_errorlog();
471
472 /* Print the total time */
473
474 if(option_logtime)
475 {
476 printf("\n");
477 fprintf_elapsed_time(stdout,&start_time);
478 printf("Complete\n");
479 fflush(stdout);
480 }
481
482 return(0);
483 }
484
485
486 /*++++++++++++++++++++++++++++++++++++++
487 Print out the usage information.
488
489 int detail The level of detail to use - 0 = low, 1 = high.
490
491 const char *argerr The argument that gave the error (if there is one).
492
493 const char *err Other error message (if there is one).
494 ++++++++++++++++++++++++++++++++++++++*/
495
496 static void print_usage(int detail,const char *argerr,const char *err)
497 {
498 fprintf(stderr,
499 "Usage: planetsplitter [--help]\n"
500 " [--dir=<dirname>] [--prefix=<name>]\n"
501 #if defined(USE_PTHREADS) && USE_PTHREADS
502 " [--sort-ram-size=<size>] [--sort-threads=<number>]\n"
503 #else
504 " [--sort-ram-size=<size>]\n"
505 #endif
506 " [--tmpdir=<dirname>]\n"
507 " [--tagging=<filename>]\n"
508 " [--loggable] [--logtime]\n"
509 " [--errorlog[=<name>]]\n"
510 " [--parse-only | --process-only]\n"
511 " [--max-iterations=<number>]\n"
512 " [--prune-none]\n"
513 " [--prune-isolated=<len>]\n"
514 " [--prune-short=<len>]\n"
515 " [--prune-straight=<len>]\n"
516 " [<filename.osm> ...]\n");
517
518 if(argerr)
519 fprintf(stderr,
520 "\n"
521 "Error with command line parameter: %s\n",argerr);
522
523 if(err)
524 fprintf(stderr,
525 "\n"
526 "Error: %s\n",err);
527
528 if(detail)
529 fprintf(stderr,
530 "\n"
531 "--help Prints this information.\n"
532 "\n"
533 "--dir=<dirname> The directory containing the routing database.\n"
534 "--prefix=<name> The filename prefix for the routing database.\n"
535 "\n"
536 "--sort-ram-size=<size> The amount of RAM (in MB) to use for data sorting\n"
537 #if SLIM
538 " (defaults to 64MB otherwise.)\n"
539 #else
540 " (defaults to 256MB otherwise.)\n"
541 #endif
542 #if defined(USE_PTHREADS) && USE_PTHREADS
543 "--sort-threads=<number> The number of threads to use for data sorting.\n"
544 #endif
545 "\n"
546 "--tmpdir=<dirname> The directory name for temporary files.\n"
547 " (defaults to the '--dir' option directory.)\n"
548 "\n"
549 "--tagging=<filename> The name of the XML file containing the tagging rules\n"
550 " (defaults to 'tagging.xml' with '--dir' and\n"
551 " '--prefix' options or the file installed in\n"
552 " '" DATADIR "').\n"
553 "\n"
554 "--loggable Print progress messages suitable for logging to file.\n"
555 "--logtime Print the elapsed time for each processing step.\n"
556 "--errorlog[=<name>] Log parsing errors to 'error.log' or the given name\n"
557 " (the '--dir' and '--prefix' options are applied).\n"
558 "\n"
559 "--parse-only Parse the input OSM files and store the results.\n"
560 "--process-only Process the stored results from previous option.\n"
561 "\n"
562 "--max-iterations=<number> The number of iterations for finding super-nodes\n"
563 " (defaults to 5).\n"
564 "\n"
565 "--prune-none Disable the prune options below, they are re-enabled\n"
566 " by adding them to the command line after this option.\n"
567 "--prune-isolated=<len> Remove small disconnected groups of segments\n"
568 " (defaults to removing groups under 500m).\n"
569 "--prune-short=<len> Remove short segments (defaults to removing segments\n"
570 " up to a maximum length of 5m).\n"
571 "--prune-straight=<len> Remove nodes in almost straight highways (defaults to\n"
572 " removing nodes up to 3m offset from a straight line).\n"
573 "\n"
574 "<filename.osm> ... The name(s) of the file(s) to process (by default\n"
575 " data is read from standard input).\n"
576 "\n"
577 "<transport> defaults to all but can be set to:\n"
578 "%s"
579 "\n"
580 "<highway> can be selected from:\n"
581 "%s"
582 "\n"
583 "<property> can be selected from:\n"
584 "%s",
585 TransportList(),HighwayList(),PropertyList());
586
587 exit(!detail);
588 }

Properties

Name Value
cvs:description Planet file splitter.