1 /***************************************
2 $Header: /home/amb/routino/src/RCS/planetsplitter.c,v 1.82 2010/11/13 14:22:28 amb Exp $
4 OSM planet file splitter.
6 Part of the Routino routing software.
7 ******************/ /******************
8 This file Copyright 2008-2010 Andrew M. Bishop
10 This program is free software: you can redistribute it and/or modify
11 it under the terms of the GNU Affero General Public License as published by
12 the Free Software Foundation, either version 3 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU Affero General Public License for more details.
20 You should have received a copy of the GNU Affero General Public License
21 along with this program. If not, see <http://www.gnu.org/licenses/>.
22 ***************************************/
35 #include "segmentsx.h"
37 #include "relationsx.h"
42 #include "functions.h"
43 #include "functionsx.h"
47 /* Global variables */
49 /*+ The name of the temporary directory. +*/
50 char *option_tmpdirname=NULL;
52 /*+ The amount of RAM to use for filesorting. +*/
53 size_t option_filesort_ramsize=0;
58 static void print_usage(int detail,const char *argerr,const char *err);
61 /*++++++++++++++++++++++++++++++++++++++
62 The main program for the planetsplitter.
63 ++++++++++++++++++++++++++++++++++++++*/
65 int main(int argc,char** argv)
68 SegmentsX *Segments,*SuperSegments=NULL,*MergedSegments=NULL;
70 RelationsX *Relations;
71 int iteration=0,quit=0;
72 int max_iterations=10;
73 char *dirname=NULL,*prefix=NULL,*tagging=NULL;
74 int option_parse_only=0,option_process_only=0;
75 int option_filenames=0;
78 /* Parse the command line arguments */
80 for(arg=1;arg<argc;arg++)
82 if(!strcmp(argv[arg],"--help"))
83 print_usage(1,NULL,NULL);
84 else if(!strncmp(argv[arg],"--sort-ram-size=",16))
85 option_filesort_ramsize=atoi(&argv[arg][16]);
86 else if(!strncmp(argv[arg],"--dir=",6))
87 dirname=&argv[arg][6];
88 else if(!strncmp(argv[arg],"--tmpdir=",9))
89 option_tmpdirname=&argv[arg][9];
90 else if(!strncmp(argv[arg],"--prefix=",9))
92 else if(!strcmp(argv[arg],"--parse-only"))
94 else if(!strcmp(argv[arg],"--process-only"))
95 option_process_only=1;
96 else if(!strcmp(argv[arg],"--loggable"))
98 else if(!strncmp(argv[arg],"--max-iterations=",17))
99 max_iterations=atoi(&argv[arg][17]);
100 else if(!strncmp(argv[arg],"--tagging=",10))
101 tagging=&argv[arg][10];
102 else if(argv[arg][0]=='-' && argv[arg][1]=='-')
103 print_usage(0,argv[arg],NULL);
108 /* Check the specified command line options */
110 if(option_parse_only && option_process_only)
111 print_usage(0,NULL,"Cannot use '--parse-only' and '--process-only' at the same time.");
113 if(option_filenames && option_process_only)
114 print_usage(0,NULL,"Cannot use '--process-only' and filenames at the same time.");
116 if(!option_filesort_ramsize)
119 option_filesort_ramsize=64*1024*1024;
121 option_filesort_ramsize=256*1024*1024;
125 option_filesort_ramsize*=1024*1024;
127 if(!option_tmpdirname)
130 option_tmpdirname=".";
132 option_tmpdirname=dirname;
137 if(!ExistsFile(tagging))
139 fprintf(stderr,"Error: The '--tagging' option specifies a file that does not exist.\n");
145 if(ExistsFile(FileName(dirname,prefix,"tagging.xml")))
146 tagging=FileName(dirname,prefix,"tagging.xml");
147 else if(ExistsFile(FileName(DATADIR,NULL,"tagging.xml")))
148 tagging=FileName(DATADIR,NULL,"tagging.xml");
151 fprintf(stderr,"Error: The '--tagging' option was not used and the default 'tagging.xml' does not exist.\n");
156 if(ParseXMLTaggingRules(tagging))
158 fprintf(stderr,"Error: Cannot read the tagging rules in the file '%s'.\n",tagging);
162 /* Create new node, segment, way and relation variables */
164 Nodes=NewNodeList(option_parse_only||option_process_only);
166 Segments=NewSegmentList(option_parse_only||option_process_only);
168 Ways=NewWayList(option_parse_only||option_process_only);
170 Relations=NewRelationList(option_parse_only||option_process_only);
176 for(arg=1;arg<argc;arg++)
180 if(argv[arg][0]=='-' && argv[arg][1]=='-')
183 file=fopen(argv[arg],"rb");
187 fprintf(stderr,"Cannot open file '%s' for reading [%s].\n",argv[arg],strerror(errno));
191 printf("\nParse OSM Data [%s]\n==============\n\n",argv[arg]);
194 if(ParseOSM(file,Nodes,Segments,Ways,Relations))
200 else if(!option_process_only)
202 printf("\nParse OSM Data\n==============\n\n");
205 if(ParseOSM(stdin,Nodes,Segments,Ways,Relations))
209 if(option_parse_only)
211 FreeNodeList(Nodes,1);
212 FreeSegmentList(Segments,1);
214 FreeRelationList(Relations,1);
219 /* Process the data */
221 printf("\nProcess OSM Data\n================\n\n");
224 /* Sort the nodes, segments, ways and relations */
228 SortSegmentList(Segments);
232 SortRelationList(Relations);
234 /* Process the route relations (must be before compacting the ways) */
236 ProcessRouteRelations(Relations,Ways);
238 FreeRelationList(Relations,0);
240 /* Compact the ways (must be before measuring the segments) */
242 CompactWayList(Ways);
244 /* Remove bad segments (must be after sorting the nodes and segments) */
246 RemoveBadSegments(Nodes,Segments);
248 /* Remove non-highway nodes (must be after removing the bad segments) */
250 RemoveNonHighwayNodes(Nodes,Segments);
252 /* Measure the segments and replace node/way id with index (must be after removing non-highway nodes) */
254 UpdateSegments(Segments,Nodes,Ways);
257 /* Repeated iteration on Super-Nodes and Super-Segments */
261 printf("\nProcess Super-Data (iteration %d)\n================================%s\n\n",iteration,iteration>9?"=":"");
266 /* Select the super-nodes */
268 ChooseSuperNodes(Nodes,Segments,Ways);
270 /* Select the super-segments */
272 SuperSegments=CreateSuperSegments(Nodes,Segments,Ways,iteration);
276 SegmentsX *SuperSegments2;
278 /* Select the super-nodes */
280 ChooseSuperNodes(Nodes,SuperSegments,Ways);
282 /* Select the super-segments */
284 SuperSegments2=CreateSuperSegments(Nodes,SuperSegments,Ways,iteration);
286 if(SuperSegments->xnumber==SuperSegments2->xnumber)
289 FreeSegmentList(SuperSegments,0);
291 SuperSegments=SuperSegments2;
294 /* Sort the super-segments */
296 SortSegmentList(SuperSegments);
298 /* Remove duplicated super-segments */
300 DeduplicateSegments(SuperSegments,Nodes,Ways);
304 if(iteration>max_iterations)
309 /* Combine the super-segments */
311 printf("\nCombine Segments and Super-Segments\n===================================\n\n");
314 /* Merge the super-segments */
316 MergedSegments=MergeSuperSegments(Segments,SuperSegments);
318 FreeSegmentList(Segments,0);
320 FreeSegmentList(SuperSegments,0);
322 Segments=MergedSegments;
324 /* Rotate segments so that node1<node2 */
326 RotateSegments(Segments);
328 /* Sort the segments */
330 SortSegmentList(Segments);
332 /* Remove duplicated segments */
334 DeduplicateSegments(Segments,Nodes,Ways);
336 /* Cross reference the nodes and segments */
338 printf("\nCross-Reference Nodes and Segments\n==================================\n\n");
341 /* Sort the node list geographically */
343 SortNodeListGeographically(Nodes);
345 /* Create the real segments and nodes */
347 CreateRealNodes(Nodes,iteration);
349 CreateRealSegments(Segments,Ways);
351 /* Fix the segment and node indexes */
353 IndexNodes(Nodes,Segments);
355 IndexSegments(Segments,Nodes);
357 /* Output the results */
359 printf("\nWrite Out Database Files\n========================\n\n");
362 /* Write out the nodes */
364 SaveNodeList(Nodes,FileName(dirname,prefix,"nodes.mem"));
366 FreeNodeList(Nodes,0);
368 /* Write out the segments */
370 SaveSegmentList(Segments,FileName(dirname,prefix,"segments.mem"));
372 FreeSegmentList(Segments,0);
374 /* Write out the ways */
376 SaveWayList(Ways,FileName(dirname,prefix,"ways.mem"));
384 /*++++++++++++++++++++++++++++++++++++++
385 Print out the usage information.
387 int detail The level of detail to use - 0 = low, 1 = high.
389 const char *argerr The argument that gave the error (if there is one).
391 const char *err Other error message (if there is one).
392 ++++++++++++++++++++++++++++++++++++++*/
394 static void print_usage(int detail,const char *argerr,const char *err)
397 "Usage: planetsplitter [--help]\n"
398 " [--dir=<dirname>] [--prefix=<name>]\n"
399 " [--sort-ram-size=<size>]\n"
400 " [--tmpdir=<dirname>]\n"
401 " [--parse-only | --process-only]\n"
403 " [--max-iterations=<number>]\n"
404 " [--tagging=<filename>]\n"
405 " [<filename.osm> ...]\n");
410 "Error with command line parameter: %s\n",argerr);
420 "--help Prints this information.\n"
422 "--dir=<dirname> The directory containing the routing database.\n"
423 "--prefix=<name> The filename prefix for the routing database.\n"
425 "--sort-ram-size=<size> The amount of RAM (in MB) to use for data sorting\n"
427 " (defaults to 64MB otherwise.)\n"
429 " (defaults to 256MB otherwise.)\n"
431 "--tmpdir=<dirname> The directory name for temporary files.\n"
432 " (defaults to the '--dir' option directory.)\n"
434 "--parse-only Parse the input OSM files and store the results.\n"
435 "--process-only Process the stored results from previous option.\n"
437 "--loggable Print progress messages suitable for logging to file.\n"
439 "--max-iterations=<number> The number of iterations for finding super-nodes.\n"
441 "--tagging=<filename> The name of the XML file containing the tagging rules\n"
442 " (defaults to 'tagging.xml' with '--dir' and\n"
443 " '--prefix' options or the file installed in\n"
446 "<filename.osm> ... The name(s) of the file(s) to process (by default\n"
447 " data is read from standard input).\n"
449 "<transport> defaults to all but can be set to:\n"
452 "<highway> can be selected from:\n"
455 "<property> can be selected from:\n"
457 TransportList(),HighwayList(),PropertyList());