1 /***************************************
2 $Header: /home/amb/routino/src/RCS/planetsplitter.c,v 1.81 2010/09/17 18:38:39 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"
41 #include "functions.h"
42 #include "functionsx.h"
46 /* Global variables */
48 /*+ The name of the temporary directory. +*/
49 char *option_tmpdirname=NULL;
51 /*+ The amount of RAM to use for filesorting. +*/
52 size_t option_filesort_ramsize=0;
57 static void print_usage(int detail,const char *argerr,const char *err);
60 /*++++++++++++++++++++++++++++++++++++++
61 The main program for the planetsplitter.
62 ++++++++++++++++++++++++++++++++++++++*/
64 int main(int argc,char** argv)
67 SegmentsX *Segments,*SuperSegments=NULL,*MergedSegments=NULL;
69 RelationsX *Relations;
70 int iteration=0,quit=0;
71 int max_iterations=10;
72 char *dirname=NULL,*prefix=NULL,*tagging=NULL;
73 int option_parse_only=0,option_process_only=0;
74 int option_filenames=0;
77 /* Parse the command line arguments */
79 for(arg=1;arg<argc;arg++)
81 if(!strcmp(argv[arg],"--help"))
82 print_usage(1,NULL,NULL);
83 else if(!strncmp(argv[arg],"--sort-ram-size=",16))
84 option_filesort_ramsize=atoi(&argv[arg][16]);
85 else if(!strncmp(argv[arg],"--dir=",6))
86 dirname=&argv[arg][6];
87 else if(!strncmp(argv[arg],"--tmpdir=",9))
88 option_tmpdirname=&argv[arg][9];
89 else if(!strncmp(argv[arg],"--prefix=",9))
91 else if(!strcmp(argv[arg],"--parse-only"))
93 else if(!strcmp(argv[arg],"--process-only"))
94 option_process_only=1;
95 else if(!strncmp(argv[arg],"--max-iterations=",17))
96 max_iterations=atoi(&argv[arg][17]);
97 else if(!strncmp(argv[arg],"--tagging=",10))
98 tagging=&argv[arg][10];
99 else if(argv[arg][0]=='-' && argv[arg][1]=='-')
100 print_usage(0,argv[arg],NULL);
105 /* Check the specified command line options */
107 if(option_parse_only && option_process_only)
108 print_usage(0,NULL,"Cannot use '--parse-only' and '--process-only' at the same time.");
110 if(option_filenames && option_process_only)
111 print_usage(0,NULL,"Cannot use '--process-only' and filenames at the same time.");
113 if(!option_filesort_ramsize)
116 option_filesort_ramsize=64*1024*1024;
118 option_filesort_ramsize=256*1024*1024;
122 option_filesort_ramsize*=1024*1024;
124 if(!option_tmpdirname)
127 option_tmpdirname=".";
129 option_tmpdirname=dirname;
134 if(!ExistsFile(tagging))
136 fprintf(stderr,"Error: The '--tagging' option specifies a file that does not exist.\n");
142 if(ExistsFile(FileName(dirname,prefix,"tagging.xml")))
143 tagging=FileName(dirname,prefix,"tagging.xml");
144 else if(ExistsFile(FileName(DATADIR,NULL,"tagging.xml")))
145 tagging=FileName(DATADIR,NULL,"tagging.xml");
148 fprintf(stderr,"Error: The '--tagging' option was not used and the default 'tagging.xml' does not exist.\n");
153 if(ParseXMLTaggingRules(tagging))
155 fprintf(stderr,"Error: Cannot read the tagging rules in the file '%s'.\n",tagging);
159 /* Create new node, segment, way and relation variables */
161 Nodes=NewNodeList(option_parse_only||option_process_only);
163 Segments=NewSegmentList(option_parse_only||option_process_only);
165 Ways=NewWayList(option_parse_only||option_process_only);
167 Relations=NewRelationList(option_parse_only||option_process_only);
173 for(arg=1;arg<argc;arg++)
177 if(argv[arg][0]=='-' && argv[arg][1]=='-')
180 file=fopen(argv[arg],"rb");
184 fprintf(stderr,"Cannot open file '%s' for reading [%s].\n",argv[arg],strerror(errno));
188 printf("\nParse OSM Data [%s]\n==============\n\n",argv[arg]);
191 if(ParseOSM(file,Nodes,Segments,Ways,Relations))
197 else if(!option_process_only)
199 printf("\nParse OSM Data\n==============\n\n");
202 if(ParseOSM(stdin,Nodes,Segments,Ways,Relations))
206 if(option_parse_only)
208 FreeNodeList(Nodes,1);
209 FreeSegmentList(Segments,1);
211 FreeRelationList(Relations,1);
216 /* Process the data */
218 printf("\nProcess OSM Data\n================\n\n");
221 /* Sort the nodes, segments, ways and relations */
225 SortSegmentList(Segments);
229 SortRelationList(Relations);
231 /* Process the route relations (must be before compacting the ways) */
233 ProcessRouteRelations(Relations,Ways);
235 FreeRelationList(Relations,0);
237 /* Compact the ways (must be before measuring the segments) */
239 CompactWayList(Ways);
241 /* Remove bad segments (must be after sorting the nodes and segments) */
243 RemoveBadSegments(Nodes,Segments);
245 /* Remove non-highway nodes (must be after removing the bad segments) */
247 RemoveNonHighwayNodes(Nodes,Segments);
249 /* Measure the segments and replace node/way id with index (must be after removing non-highway nodes) */
251 UpdateSegments(Segments,Nodes,Ways);
254 /* Repeated iteration on Super-Nodes and Super-Segments */
258 printf("\nProcess Super-Data (iteration %d)\n================================%s\n\n",iteration,iteration>9?"=":"");
263 /* Select the super-nodes */
265 ChooseSuperNodes(Nodes,Segments,Ways);
267 /* Select the super-segments */
269 SuperSegments=CreateSuperSegments(Nodes,Segments,Ways,iteration);
273 SegmentsX *SuperSegments2;
275 /* Select the super-nodes */
277 ChooseSuperNodes(Nodes,SuperSegments,Ways);
279 /* Select the super-segments */
281 SuperSegments2=CreateSuperSegments(Nodes,SuperSegments,Ways,iteration);
283 if(SuperSegments->xnumber==SuperSegments2->xnumber)
286 FreeSegmentList(SuperSegments,0);
288 SuperSegments=SuperSegments2;
291 /* Sort the super-segments */
293 SortSegmentList(SuperSegments);
295 /* Remove duplicated super-segments */
297 DeduplicateSegments(SuperSegments,Nodes,Ways);
301 if(iteration>max_iterations)
306 /* Combine the super-segments */
308 printf("\nCombine Segments and Super-Segments\n===================================\n\n");
311 /* Merge the super-segments */
313 MergedSegments=MergeSuperSegments(Segments,SuperSegments);
315 FreeSegmentList(Segments,0);
317 FreeSegmentList(SuperSegments,0);
319 Segments=MergedSegments;
321 /* Rotate segments so that node1<node2 */
323 RotateSegments(Segments);
325 /* Sort the segments */
327 SortSegmentList(Segments);
329 /* Remove duplicated segments */
331 DeduplicateSegments(Segments,Nodes,Ways);
333 /* Cross reference the nodes and segments */
335 printf("\nCross-Reference Nodes and Segments\n==================================\n\n");
338 /* Sort the node list geographically */
340 SortNodeListGeographically(Nodes);
342 /* Create the real segments and nodes */
344 CreateRealNodes(Nodes,iteration);
346 CreateRealSegments(Segments,Ways);
348 /* Fix the segment and node indexes */
350 IndexNodes(Nodes,Segments);
352 IndexSegments(Segments,Nodes);
354 /* Output the results */
356 printf("\nWrite Out Database Files\n========================\n\n");
359 /* Write out the nodes */
361 SaveNodeList(Nodes,FileName(dirname,prefix,"nodes.mem"));
363 FreeNodeList(Nodes,0);
365 /* Write out the segments */
367 SaveSegmentList(Segments,FileName(dirname,prefix,"segments.mem"));
369 FreeSegmentList(Segments,0);
371 /* Write out the ways */
373 SaveWayList(Ways,FileName(dirname,prefix,"ways.mem"));
381 /*++++++++++++++++++++++++++++++++++++++
382 Print out the usage information.
384 int detail The level of detail to use - 0 = low, 1 = high.
386 const char *argerr The argument that gave the error (if there is one).
388 const char *err Other error message (if there is one).
389 ++++++++++++++++++++++++++++++++++++++*/
391 static void print_usage(int detail,const char *argerr,const char *err)
394 "Usage: planetsplitter [--help]\n"
395 " [--dir=<dirname>] [--prefix=<name>]\n"
396 " [--sort-ram-size=<size>]\n"
397 " [--tmpdir=<dirname>]\n"
398 " [--parse-only | --process-only]\n"
399 " [--max-iterations=<number>]\n"
400 " [--tagging=<filename>]\n"
401 " [<filename.osm> ...]\n");
406 "Error with command line parameter: %s\n",argerr);
416 "--help Prints this information.\n"
418 "--dir=<dirname> The directory containing the routing database.\n"
419 "--prefix=<name> The filename prefix for the routing database.\n"
421 "--sort-ram-size=<size> The amount of RAM (in MB) to use for data sorting\n"
423 " (defaults to 64MB otherwise.)\n"
425 " (defaults to 256MB otherwise.)\n"
427 "--tmpdir=<dirname> The directory name for temporary files.\n"
428 " (defaults to the '--dir' option directory.)\n"
430 "--parse-only Parse the input OSM files and store the results.\n"
431 "--process-only Process the stored results from previous option.\n"
433 "--max-iterations=<number> The number of iterations for finding super-nodes.\n"
435 "--tagging=<filename> The name of the XML file containing the tagging rules\n"
436 " (defaults to 'tagging.xml' with '--dir' and\n"
437 " '--prefix' options or the file installed in\n"
440 "<filename.osm> ... The name(s) of the file(s) to process (by default\n"
441 " data is read from standard input).\n"
443 "<transport> defaults to all but can be set to:\n"
446 "<highway> can be selected from:\n"
449 "<property> can be selected from:\n"
451 TransportList(),HighwayList(),PropertyList());