1 /***************************************
2 $Header: /home/amb/routino/src/RCS/filedumper.c,v 1.54 2010/09/15 18:19:36 amb Exp $
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 ***************************************/
38 #include "visualiser.h"
44 static void print_node(Nodes* nodes,index_t item);
45 static void print_segment(Segments *segments,index_t item);
46 static void print_way(Ways *ways,index_t item);
48 static void print_head_osm(void);
49 static void print_node_osm(Nodes* nodes,index_t item);
50 static void print_segment_osm(Segments *segments,index_t item,Ways *ways);
51 static void print_tail_osm(void);
53 static char *RFC822Date(time_t t);
55 static void print_usage(int detail,const char *argerr,const char *err);
58 /*++++++++++++++++++++++++++++++++++++++
59 The main program for the file dumper.
60 ++++++++++++++++++++++++++++++++++++++*/
62 int main(int argc,char** argv)
65 Segments *OSMSegments;
68 char *dirname=NULL,*prefix=NULL;
69 char *nodes_filename,*segments_filename,*ways_filename;
70 int option_statistics=0;
71 int option_visualiser=0,coordcount=0;
72 double latmin=0,latmax=0,lonmin=0,lonmax=0;
73 char *option_data=NULL;
75 int option_dump_osm=0,option_no_super=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],"--dir=",6))
84 dirname=&argv[arg][6];
85 else if(!strncmp(argv[arg],"--prefix=",9))
87 else if(!strcmp(argv[arg],"--statistics"))
89 else if(!strcmp(argv[arg],"--visualiser"))
91 else if(!strcmp(argv[arg],"--dump"))
93 else if(!strcmp(argv[arg],"--dump-osm"))
95 else if(!strncmp(argv[arg],"--latmin",8) && argv[arg][8]=='=')
96 {latmin=degrees_to_radians(atof(&argv[arg][9]));coordcount++;}
97 else if(!strncmp(argv[arg],"--latmax",8) && argv[arg][8]=='=')
98 {latmax=degrees_to_radians(atof(&argv[arg][9]));coordcount++;}
99 else if(!strncmp(argv[arg],"--lonmin",8) && argv[arg][8]=='=')
100 {lonmin=degrees_to_radians(atof(&argv[arg][9]));coordcount++;}
101 else if(!strncmp(argv[arg],"--lonmax",8) && argv[arg][8]=='=')
102 {lonmax=degrees_to_radians(atof(&argv[arg][9]));coordcount++;}
103 else if(!strncmp(argv[arg],"--data",6) && argv[arg][6]=='=')
104 option_data=&argv[arg][7];
105 else if(!strcmp(argv[arg],"--no-super"))
107 else if(!strncmp(argv[arg],"--node=",7))
109 else if(!strncmp(argv[arg],"--segment=",10))
111 else if(!strncmp(argv[arg],"--way=",6))
114 print_usage(0,argv[arg],NULL);
117 if((option_statistics + option_visualiser + option_dump + option_dump_osm)!=1)
118 print_usage(0,NULL,"Must choose --visualiser, --statistics, --dump or --dump-osm.");
120 /* Load in the data - Note: No error checking because Load*List() will call exit() in case of an error. */
122 OSMNodes=LoadNodeList(nodes_filename=FileName(dirname,prefix,"nodes.mem"));
124 OSMSegments=LoadSegmentList(segments_filename=FileName(dirname,prefix,"segments.mem"));
126 OSMWays=LoadWayList(ways_filename=FileName(dirname,prefix,"ways.mem"));
128 /* Write out the visualiser data */
130 if(option_visualiser)
133 print_usage(0,NULL,"The --visualiser option must have --latmin, --latmax, --lonmin, --lonmax.\n");
136 print_usage(0,NULL,"The --visualiser option must have --data.\n");
138 if(!strcmp(option_data,"junctions"))
139 OutputJunctions(OSMNodes,OSMSegments,OSMWays,latmin,latmax,lonmin,lonmax);
140 else if(!strcmp(option_data,"super"))
141 OutputSuper(OSMNodes,OSMSegments,OSMWays,latmin,latmax,lonmin,lonmax);
142 else if(!strcmp(option_data,"oneway"))
143 OutputOneway(OSMNodes,OSMSegments,OSMWays,latmin,latmax,lonmin,lonmax);
144 else if(!strcmp(option_data,"speed"))
145 OutputSpeedLimits(OSMNodes,OSMSegments,OSMWays,latmin,latmax,lonmin,lonmax);
146 else if(!strcmp(option_data,"weight"))
147 OutputWeightLimits(OSMNodes,OSMSegments,OSMWays,latmin,latmax,lonmin,lonmax);
148 else if(!strcmp(option_data,"height"))
149 OutputHeightLimits(OSMNodes,OSMSegments,OSMWays,latmin,latmax,lonmin,lonmax);
150 else if(!strcmp(option_data,"width"))
151 OutputWidthLimits(OSMNodes,OSMSegments,OSMWays,latmin,latmax,lonmin,lonmax);
152 else if(!strcmp(option_data,"length"))
153 OutputLengthLimits(OSMNodes,OSMSegments,OSMWays,latmin,latmax,lonmin,lonmax);
155 print_usage(0,option_data,NULL);
158 /* Print out statistics */
160 if(option_statistics)
164 /* Examine the files */
170 stat(nodes_filename,&buf);
172 printf("'%s%snodes.mem' - %9lld Bytes\n",prefix?prefix:"",prefix?"-":"",(long long)buf.st_size);
173 printf("%s\n",RFC822Date(buf.st_mtime));
176 stat(segments_filename,&buf);
178 printf("'%s%ssegments.mem' - %9lld Bytes\n",prefix?prefix:"",prefix?"-":"",(long long)buf.st_size);
179 printf("%s\n",RFC822Date(buf.st_mtime));
182 stat(ways_filename,&buf);
184 printf("'%s%sways.mem' - %9lld Bytes\n",prefix?prefix:"",prefix?"-":"",(long long)buf.st_size);
185 printf("%s\n",RFC822Date(buf.st_mtime));
188 /* Examine the nodes */
194 printf("sizeof(Node) =%9d Bytes\n",sizeof(Node));
195 printf("Number =%9d\n",OSMNodes->file.number);
196 printf("Number(super)=%9d\n",OSMNodes->file.snumber);
199 printf("Lat bins= %4d\n",OSMNodes->file.latbins);
200 printf("Lon bins= %4d\n",OSMNodes->file.lonbins);
203 printf("Lat zero=%5d (%8.4f deg)\n",OSMNodes->file.latzero,radians_to_degrees(latlong_to_radians(bin_to_latlong(OSMNodes->file.latzero))));
204 printf("Lon zero=%5d (%8.4f deg)\n",OSMNodes->file.lonzero,radians_to_degrees(latlong_to_radians(bin_to_latlong(OSMNodes->file.lonzero))));
206 /* Examine the segments */
209 printf("Segments\n");
210 printf("--------\n");
213 printf("sizeof(Segment)=%9d Bytes\n",sizeof(Segment));
214 printf("Number(total) =%9d\n",OSMSegments->file.number);
215 printf("Number(super) =%9d\n",OSMSegments->file.snumber);
216 printf("Number(normal) =%9d\n",OSMSegments->file.nnumber);
218 /* Examine the ways */
225 printf("sizeof(Way) =%9d Bytes\n",sizeof(Way));
226 printf("Number(compacted)=%9d\n",OSMWays->file.number);
227 printf("Number(original) =%9d\n",OSMWays->file.onumber);
230 printf("Total names =%9ld Bytes\n",(long)buf.st_size-sizeof(Ways)-OSMWays->file.number*sizeof(Way));
233 printf("Included transports: %s\n",AllowedNameList(OSMWays->file.allow));
234 printf("Included properties: %s\n",PropertiesNameList(OSMWays->file.props));
237 /* Print out internal data */
243 for(arg=1;arg<argc;arg++)
244 if(!strcmp(argv[arg],"--node=all"))
246 for(item=0;item<OSMNodes->file.number;item++)
247 print_node(OSMNodes,item);
249 else if(!strncmp(argv[arg],"--node=",7))
251 item=atoi(&argv[arg][7]);
253 if(item>=0 && item<OSMNodes->file.number)
254 print_node(OSMNodes,item);
256 printf("Invalid node number; minimum=0, maximum=%d.\n",OSMNodes->file.number-1);
258 else if(!strcmp(argv[arg],"--segment=all"))
260 for(item=0;item<OSMSegments->file.number;item++)
261 print_segment(OSMSegments,item);
263 else if(!strncmp(argv[arg],"--segment=",10))
265 item=atoi(&argv[arg][10]);
267 if(item>=0 && item<OSMSegments->file.number)
268 print_segment(OSMSegments,item);
270 printf("Invalid segment number; minimum=0, maximum=%d.\n",OSMSegments->file.number-1);
272 else if(!strcmp(argv[arg],"--way=all"))
274 for(item=0;item<OSMWays->file.number;item++)
275 print_way(OSMWays,item);
277 else if(!strncmp(argv[arg],"--way=",6))
279 item=atoi(&argv[arg][6]);
281 if(item>=0 && item<OSMWays->file.number)
282 print_way(OSMWays,item);
284 printf("Invalid way number; minimum=0, maximum=%d.\n",OSMWays->file.number-1);
288 /* Print out internal data in XML format */
292 if(coordcount>0 && coordcount!=4)
293 print_usage(0,NULL,"The --dump-osm option must have all of --latmin, --latmax, --lonmin, --lonmax or none.\n");
299 int32_t latminbin=latlong_to_bin(radians_to_latlong(latmin))-OSMNodes->file.latzero;
300 int32_t latmaxbin=latlong_to_bin(radians_to_latlong(latmax))-OSMNodes->file.latzero;
301 int32_t lonminbin=latlong_to_bin(radians_to_latlong(lonmin))-OSMNodes->file.lonzero;
302 int32_t lonmaxbin=latlong_to_bin(radians_to_latlong(lonmax))-OSMNodes->file.lonzero;
304 index_t item,index1,index2;
306 /* Loop through all of the nodes. */
308 for(latb=latminbin;latb<=latmaxbin;latb++)
309 for(lonb=lonminbin;lonb<=lonmaxbin;lonb++)
311 llbin=lonb*OSMNodes->file.latbins+latb;
313 if(llbin<0 || llbin>(OSMNodes->file.latbins*OSMNodes->file.lonbins))
316 index1=LookupNodeOffset(OSMNodes,llbin);
317 index2=LookupNodeOffset(OSMNodes,llbin+1);
319 for(item=index1;item<index2;item++)
321 Node *node=LookupNode(OSMNodes,item,1);
322 double lat=latlong_to_radians(bin_to_latlong(OSMNodes->file.latzero+latb)+off_to_latlong(node->latoffset));
323 double lon=latlong_to_radians(bin_to_latlong(OSMNodes->file.lonzero+lonb)+off_to_latlong(node->lonoffset));
325 if(lat>latmin && lat<latmax && lon>lonmin && lon<lonmax)
329 print_node_osm(OSMNodes,item);
331 segment=FirstSegment(OSMSegments,OSMNodes,item);
335 if(item>OtherNode(segment,item))
336 if(!option_no_super || IsNormalSegment(segment))
337 print_segment_osm(OSMSegments,IndexSegment(OSMSegments,segment),OSMWays);
339 segment=NextSegment(OSMSegments,segment,item);
349 for(item=0;item<OSMNodes->file.number;item++)
350 print_node_osm(OSMNodes,item);
352 for(item=0;item<OSMSegments->file.number;item++)
353 if(!option_no_super || IsNormalSegment(LookupSegment(OSMSegments,item,1)))
354 print_segment_osm(OSMSegments,item,OSMWays);
364 /*++++++++++++++++++++++++++++++++++++++
365 Print out the contents of a node from the routing database.
367 Nodes *nodes The set of nodes to use.
369 index_t item The node index to print.
370 ++++++++++++++++++++++++++++++++++++++*/
372 static void print_node(Nodes* nodes,index_t item)
374 Node *node=LookupNode(nodes,item,1);
375 double latitude,longitude;
377 GetLatLong(nodes,item,&latitude,&longitude);
379 printf("Node %d\n",item);
380 printf(" firstseg=%d\n",node->firstseg);
381 printf(" latoffset=%d lonoffset=%d (latitude=%.6f longitude=%.6f)\n",node->latoffset,node->lonoffset,radians_to_degrees(latitude),radians_to_degrees(longitude));
382 printf(" allow=%02x (%s)\n",node->allow,AllowedNameList(node->allow));
383 if(IsSuperNode(nodes,item))
384 printf(" Super-Node\n");
388 /*++++++++++++++++++++++++++++++++++++++
389 Print out the contents of a segment from the routing database.
391 Segments *segments The set of segments to use.
393 index_t item The segment index to print.
394 ++++++++++++++++++++++++++++++++++++++*/
396 static void print_segment(Segments *segments,index_t item)
398 Segment *segment=LookupSegment(segments,item,1);
400 printf("Segment %d\n",item);
401 printf(" node1=%d node2=%d\n",segment->node1,segment->node2);
402 printf(" next2=%d\n",segment->next2);
403 printf(" way=%d\n",segment->way);
404 printf(" distance=%d (%.3f km)\n",DISTANCE(segment->distance),distance_to_km(DISTANCE(segment->distance)));
405 if(IsSuperSegment(segment) && IsNormalSegment(segment))
406 printf(" Super-Segment AND normal Segment\n");
407 else if(IsSuperSegment(segment) && !IsNormalSegment(segment))
408 printf(" Super-Segment\n");
409 if(IsOnewayTo(segment,segment->node1))
410 printf(" One-Way from node2 to node1\n");
411 if(IsOnewayTo(segment,segment->node2))
412 printf(" One-Way from node1 to node2\n");
416 /*++++++++++++++++++++++++++++++++++++++
417 Print out the contents of a way from the routing database.
419 Ways *ways The set of ways to use.
421 index_t item The way index to print.
422 ++++++++++++++++++++++++++++++++++++++*/
424 static void print_way(Ways *ways,index_t item)
426 Way *way=LookupWay(ways,item,1);
428 printf("Way %d\n",item);
429 if(*WayName(ways,way))
430 printf(" name=%s\n",WayName(ways,way));
431 printf(" type=%02x (%s%s%s)\n",way->type,HighwayName(HIGHWAY(way->type)),way->type&Way_OneWay?",One-Way":"",way->type&Way_Roundabout?",Roundabout":"");
432 printf(" allow=%02x (%s)\n",way->allow,AllowedNameList(way->allow));
434 printf(" props=%02x (%s)\n",way->props,PropertiesNameList(way->props));
436 printf(" speed=%d (%d km/hr)\n",way->speed,speed_to_kph(way->speed));
438 printf(" weight=%d (%.1f tonnes)\n",way->weight,weight_to_tonnes(way->weight));
440 printf(" height=%d (%.1f m)\n",way->height,height_to_metres(way->height));
442 printf(" width=%d (%.1f m)\n",way->width,width_to_metres(way->width));
444 printf(" length=%d (%.1f m)\n",way->length,length_to_metres(way->length));
448 /*++++++++++++++++++++++++++++++++++++++
449 Print out a header in OSM XML format.
450 ++++++++++++++++++++++++++++++++++++++*/
452 static void print_head_osm(void)
454 printf("<?xml version='1.0' encoding='UTF-8'?>\n");
455 printf("<osm version='0.6' generator='JOSM'>\n");
459 /*++++++++++++++++++++++++++++++++++++++
460 Print out the contents of a node from the routing database in OSM XML format.
462 Nodes *nodes The set of nodes to use.
464 index_t item The node index to print.
465 ++++++++++++++++++++++++++++++++++++++*/
467 static void print_node_osm(Nodes* nodes,index_t item)
469 Node *node=LookupNode(nodes,item,1);
470 double latitude,longitude;
472 GetLatLong(nodes,item,&latitude,&longitude);
474 if(IsSuperNode(nodes,item))
478 printf(" <node id='%lu' lat='%.7f' lon='%.7f' version='1'>\n",(unsigned long)item+1,radians_to_degrees(latitude),radians_to_degrees(longitude));
479 printf(" <tag k='routino:super' v='yes' />\n");
481 for(i=1;i<Transport_Count;i++)
482 if(!(node->allow & ALLOWED(i)))
483 printf(" <tag k='%s' v='no' />\n",TransportName(i));
485 printf(" </node>\n");
488 printf(" <node id='%lu' lat='%.7f' lon='%.7f' version='1' />\n",(unsigned long)item+1,radians_to_degrees(latitude),radians_to_degrees(longitude));
492 /*++++++++++++++++++++++++++++++++++++++
493 Print out the contents of a segment from the routing database as a way in OSM XML format.
495 Segments *segments The set of segments to use.
497 index_t item The segment index to print.
499 Ways *ways The set of ways to use.
500 ++++++++++++++++++++++++++++++++++++++*/
502 static void print_segment_osm(Segments *segments,index_t item,Ways *ways)
504 Segment *segment=LookupSegment(segments,item,1);
505 Way *way=LookupWay(ways,segment->way,1);
508 printf(" <way id='%lu' version='1'>\n",(unsigned long)item+1);
510 if(IsOnewayTo(segment,segment->node1))
512 printf(" <nd ref='%lu' />\n",(unsigned long)segment->node2+1);
513 printf(" <nd ref='%lu' />\n",(unsigned long)segment->node1+1);
517 printf(" <nd ref='%lu' />\n",(unsigned long)segment->node1+1);
518 printf(" <nd ref='%lu' />\n",(unsigned long)segment->node2+1);
521 if(IsSuperSegment(segment))
522 printf(" <tag k='routino:super' v='yes' />\n");
523 if(IsNormalSegment(segment))
524 printf(" <tag k='routino:normal' v='yes' />\n");
526 if(way->type & Way_OneWay)
527 printf(" <tag k='oneway' v='yes' />\n");
528 if(way->type & Way_Roundabout)
529 printf(" <tag k='junction' v='roundabout' />\n");
531 printf(" <tag k='highway' v='%s' />\n",HighwayName(HIGHWAY(way->type)));
533 if(IsNormalSegment(segment) && *WayName(ways,way))
534 printf(" <tag k='name' v='%s' />\n",ParseXML_Encode_Safe_XML(WayName(ways,way)));
536 for(i=1;i<Transport_Count;i++)
537 if(way->allow & ALLOWED(i))
538 printf(" <tag k='%s' v='yes' />\n",TransportName(i));
540 for(i=1;i<Property_Count;i++)
541 if(way->props & PROPERTIES(i))
542 printf(" <tag k='%s' v='yes' />\n",PropertyName(i));
545 printf(" <tag k='maxspeed' v='%d' />\n",speed_to_kph(way->speed));
548 printf(" <tag k='maxweight' v='%.1f' />\n",weight_to_tonnes(way->weight));
550 printf(" <tag k='maxheight' v='%.1f' />\n",height_to_metres(way->height));
552 printf(" <tag k='maxwidth' v='%.1f' />\n",width_to_metres(way->width));
554 printf(" <tag k='maxlength' v='%.1f' />\n",length_to_metres(way->length));
560 /*++++++++++++++++++++++++++++++++++++++
561 Print out a tail in OSM XML format.
562 ++++++++++++++++++++++++++++++++++++++*/
564 static void print_tail_osm(void)
570 /*+ Conversion from time_t to date string (day of week). +*/
571 static const char* const weekdays[7]={"Sun","Mon","Tue","Wed","Thu","Fri","Sat"};
573 /*+ Conversion from time_t to date string (month of year). +*/
574 static const char* const months[12]={"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"};
577 /*++++++++++++++++++++++++++++++++++++++
578 Convert the time into an RFC 822 compliant date.
580 char *RFC822Date Returns a pointer to a fixed string containing the date.
583 ++++++++++++++++++++++++++++++++++++++*/
585 static char *RFC822Date(time_t t)
587 static char value[32];
594 strcpy(weekday,weekdays[tim->tm_wday]);
595 strcpy(month,months[tim->tm_mon]);
597 /* Sun, 06 Nov 1994 08:49:37 GMT ; RFC 822, updated by RFC 1123 */
599 sprintf(value,"%3s, %02d %3s %4d %02d:%02d:%02d %s",
614 /*++++++++++++++++++++++++++++++++++++++
615 Print out the usage information.
617 int detail The level of detail to use - 0 = low, 1 = high.
619 const char *argerr The argument that gave the error (if there is one).
621 const char *err Other error message (if there is one).
622 ++++++++++++++++++++++++++++++++++++++*/
624 static void print_usage(int detail,const char *argerr,const char *err)
627 "Usage: filedumper [--help]\n"
628 " [--dir=<dirname>] [--prefix=<name>]\n"
630 " [--visualiser --latmin=<latmin> --latmax=<latmax>\n"
631 " --lonmin=<lonmin> --lonmax=<lonmax>\n"
632 " --data=<data-type>]\n"
633 " [--dump [--node=<node> ...]\n"
634 " [--segment=<segment> ...]\n"
635 " [--way=<way> ...]]\n"
636 " [--dump-osm [--no-super]\n"
637 " [--latmin=<latmin> --latmax=<latmax>\n"
638 " --lonmin=<lonmin> --lonmax=<lonmax>]]\n");
643 "Error with command line parameter: %s\n",argerr);
653 "--help Prints this information.\n"
655 "--dir=<dirname> The directory containing the routing database.\n"
656 "--prefix=<name> The filename prefix for the routing database.\n"
658 "--statistics Print statistics about the routing database.\n"
660 "--visualiser Extract selected data from the routing database:\n"
661 " --latmin=<latmin> * the minimum latitude (degrees N).\n"
662 " --latmax=<latmax> * the maximum latitude (degrees N).\n"
663 " --lonmin=<lonmin> * the minimum longitude (degrees E).\n"
664 " --lonmax=<lonmax> * the maximum longitude (degrees E).\n"
665 " --data=<data-type> * the type of data to select.\n"
667 " <data-type> can be selected from:\n"
668 " junctions = segment count at each junction.\n"
669 " super = super-node and super-segments.\n"
670 " oneway = oneway segments.\n"
671 " speed = speed limits.\n"
672 " weight = weight limits.\n"
673 " height = height limits.\n"
674 " width = width limits.\n"
675 " length = length limits.\n"
677 "--dump Dump selected contents of the database.\n"
678 " --node=<node> * the node with the selected number.\n"
679 " --segment=<segment> * the segment with the selected number.\n"
680 " --way=<way> * the way with the selected number.\n"
681 " Use 'all' instead of a number to get all of them.\n"
683 "--dump-osm Dump all or part of the database as an XML file.\n"
684 " --no-super * exclude the super-segments.\n"
685 " --latmin=<latmin> * the minimum latitude (degrees N).\n"
686 " --latmax=<latmax> * the maximum latitude (degrees N).\n"
687 " --lonmin=<lonmin> * the minimum longitude (degrees E).\n"
688 " --lonmax=<lonmax> * the maximum longitude (degrees E).\n");