double DeltaLat(double lon,distance_t distance) { double dlat; double c,d; if(distance==0) return 0; d = distance_to_km(distance); c = d / 6378.137; dlat = c; return dlat; }
double DeltaLon(double lat,distance_t distance) { double dlon; double a2,sa,c,d; if(distance==0) return 0; d = distance_to_km(distance); c = d / 6378.137; sa = sin(c/2); a2 = sa / cos(lat); dlon = 2*asin(a2); return dlon; }
void PruneStraightHighwayNodes(NodesX *nodesx,SegmentsX *segmentsx,WaysX *waysx,distance_t maximum) { index_t i; index_t npruned=0; index_t nalloc; BitMask *checked; index_t *nodes,*segments; double *lats,*lons; double maximumf; if(nodesx->number==0 || segmentsx->number==0 || waysx->number==0) return; /* Print the start message */ printf_first("Pruning Straight Highway Nodes: Nodes=0 Pruned=0"); /* Map into memory / open the files */ #if !SLIM nodesx->data=MapFile(nodesx->filename_tmp); segmentsx->data=MapFileWriteable(segmentsx->filename_tmp); waysx->data=MapFile(waysx->filename_tmp); #else nodesx->fd=SlimMapFile(nodesx->filename_tmp); segmentsx->fd=SlimMapFileWriteable(segmentsx->filename_tmp); waysx->fd=SlimMapFile(waysx->filename_tmp); InvalidateNodeXCache(nodesx->cache); InvalidateSegmentXCache(segmentsx->cache); InvalidateWayXCache(waysx->cache); #endif checked=AllocBitMask(nodesx->number); logassert(checked,"Failed to allocate memory (try using slim mode?)"); /* Check AllocBitMask() worked */ nodes =(index_t*)malloc((nalloc=1024)*sizeof(index_t)); segments=(index_t*)malloc( nalloc *sizeof(index_t)); logassert(nodes,"Failed to allocate memory (try using slim mode?)"); /* Check malloc() worked */ logassert(segments,"Failed to allocate memory (try using slim mode?)"); /* Check malloc() worked */ lats=(double*)malloc(nalloc*sizeof(double)); lons=(double*)malloc(nalloc*sizeof(double)); logassert(lats,"Failed to allocate memory (try using slim mode?)"); /* Check malloc() worked */ logassert(lons,"Failed to allocate memory (try using slim mode?)"); /* Check malloc() worked */ /* Loop through the nodes and find stretches of simple highway for possible modification */ maximumf=distance_to_km(maximum); for(i=0;i<nodesx->number;i++) { int lowerbounded=0,upperbounded=0; index_t lower=nalloc/2,current=nalloc/2,upper=nalloc/2; if(IsBitSet(checked,i)) goto endloop; if(segmentsx->firstnode[i]==NO_SEGMENT) goto endloop; /* Find all connected nodes */ nodes[current]=i; do { index_t node1=NO_NODE,node2=NO_NODE; index_t segment1=NO_SEGMENT,segment2=NO_SEGMENT; index_t way1=NO_WAY,way2=NO_WAY; int segcount=0; NodeX *nodex; /* Get the node data */ nodex=LookupNodeX(nodesx,nodes[current],1); lats[current]=latlong_to_radians(nodex->latitude); lons[current]=latlong_to_radians(nodex->longitude); /* Count the segments at the node if not forced to be an end node */ if(IsBitSet(checked,nodes[current])) ; else if(nodex->flags&NODE_MINIRNDBT) ; else if(nodex->flags&NODE_TURNRSTRCT2 || nodex->flags&NODE_TURNRSTRCT) ; else { SegmentX *segmentx; /* Count the segments connected to the node */ segmentx=FirstSegmentX(segmentsx,nodes[current],3); while(segmentx) { segcount++; if(node1==NO_NODE) { segment1=IndexSegmentX(segmentsx,segmentx); node1=OtherNode(segmentx,nodes[current]); way1=segmentx->way; } else if(node2==NO_NODE) { segment2=IndexSegmentX(segmentsx,segmentx); node2=OtherNode(segmentx,nodes[current]); way2=segmentx->way; } else break; segmentx=NextSegmentX(segmentsx,segmentx,nodes[current]); } } /* Check if allowed due to one-way properties */ if(segcount==2) { SegmentX *segmentx1,*segmentx2; segmentx1=LookupSegmentX(segmentsx,segment1,1); segmentx2=LookupSegmentX(segmentsx,segment2,2); if(!IsOneway(segmentx1) && !IsOneway(segmentx2)) ; else if(IsOneway(segmentx1) && IsOneway(segmentx2)) { if(IsOnewayTo(segmentx1,nodes[current]) && !IsOnewayFrom(segmentx2,nodes[current])) /* S1 is one-way but S2 doesn't continue */ segcount=0; if(IsOnewayFrom(segmentx1,nodes[current]) && !IsOnewayTo(segmentx2,nodes[current])) /* S1 is one-way but S2 doesn't continue */ segcount=0; } else segcount=0; } /* Check if allowed due to highway properties and node restrictions */ if(segcount==2) { WayX *wayx1,*wayx2; wayx1=LookupWayX(waysx,way1,1); wayx2=LookupWayX(waysx,way2,2); if(WaysCompare(&wayx1->way,&wayx2->way)) segcount=0; if(wayx1->way.name!=wayx2->way.name) segcount=0; if((nodex->allow&wayx1->way.allow)!=wayx1->way.allow) segcount=0; if((nodex->allow&wayx2->way.allow)!=wayx2->way.allow) segcount=0; } /* Update the lists */ if(segcount==2) { /* Make space in the lists */ if(upper==(nalloc-1)) { nodes =(index_t*)realloc(nodes ,(nalloc+=1024)*sizeof(index_t)); segments=(index_t*)realloc(segments, nalloc *sizeof(index_t)); lats=(double*)realloc(lats,nalloc*sizeof(double)); lons=(double*)realloc(lons,nalloc*sizeof(double)); } if(lower==0) /* move everything up by one */ { memmove(nodes+1 ,nodes ,(1+upper-lower)*sizeof(index_t)); memmove(segments+1,segments,(1+upper-lower)*sizeof(index_t)); memmove(lats+1,lats,(1+upper-lower)*sizeof(double)); memmove(lons+1,lons,(1+upper-lower)*sizeof(double)); current++; lower++; upper++; } if(lower==upper) /* first */ { lower--; nodes[lower]=node1; segments[lower]=segment1; upper++; nodes[upper]=node2; segments[upper-1]=segment2; segments[upper]=NO_SEGMENT; current--; } else if(current==lower) { lower--; if(nodes[current+1]==node2) { nodes[lower]=node1; segments[lower]=segment1; } else /* if(nodes[current+1]==node1) */ { nodes[lower]=node2; segments[lower]=segment2; } current--; } else /* if(current==upper) */ { upper++; if(nodes[current-1]==node2) { nodes[upper]=node1; segments[upper-1]=segment1; } else /* if(nodes[current-1]==node1) */ { nodes[upper]=node2; segments[upper-1]=segment2; } segments[upper]=NO_SEGMENT; current++; } if(nodes[upper]==nodes[lower]) { if(!lowerbounded && !upperbounded) { nodex=LookupNodeX(nodesx,nodes[lower],1); lats[lower]=latlong_to_radians(nodex->latitude); lons[lower]=latlong_to_radians(nodex->longitude); } lats[upper]=lats[lower]; lons[upper]=lons[lower]; lowerbounded=1; upperbounded=1; } } else /* if(segment!=2) */ { if(current==upper) upperbounded=1; if(current==lower) { lowerbounded=1; current=upper; } } } while(!(lowerbounded && upperbounded)); /* Mark the nodes */ for(current=lower;current<=upper;current++) SetBit(checked,nodes[current]); /* Check for straight highway */ for(;lower<(upper-1);lower++) { for(current=upper;current>(lower+1);current--) { SegmentX *segmentx; distance_t dist=0; double dist1,dist2,dist3,distp; index_t c; dist3=distance(lats[lower],lons[lower],lats[current],lons[current]); for(c=lower+1;c<current;c++) { dist1=distance(lats[lower] ,lons[lower] ,lats[c],lons[c]); dist2=distance(lats[current],lons[current],lats[c],lons[c]); /* Use law of cosines (assume flat Earth) */ if(dist3==0) distp=dist1; /* == dist2 */ else if((dist1+dist2)<dist3) distp=0; else { double dist3a=(dist1*dist1-dist2*dist2+dist3*dist3)/(2.0*dist3); double dist3b=dist3-dist3a; if(dist3a>=0 && dist3b>=0) distp=sqrt(dist1*dist1-dist3a*dist3a); else if(dist3a>0) distp=dist2; else /* if(dist3b>0) */ distp=dist1; } if(distp>maximumf) /* gone too far */ break; } if(c<current) /* not finished */ continue; /* Delete some segments and shift along */ for(c=lower+1;c<current;c++) { segmentx=LookupSegmentX(segmentsx,segments[c],1); dist+=DISTANCE(segmentx->distance); prune_segment(segmentsx,segmentx); npruned++; } segmentx=LookupSegmentX(segmentsx,segments[lower],1); if(nodes[lower]==nodes[current]) /* loop; all within maximum distance */ { prune_segment(segmentsx,segmentx); npruned++; } else { segmentx->distance+=dist; if(segmentx->node1==nodes[lower]) modify_segment(segmentsx,segmentx,nodes[lower],nodes[current]); else /* if(segmentx->node2==nodes[lower]) */ modify_segment(segmentsx,segmentx,nodes[current],nodes[lower]); } lower=current-1; break; } } endloop: if(!((i+1)%10000)) printf_middle("Pruning Straight Highway Nodes: Nodes=%"Pindex_t" Pruned=%"Pindex_t,i+1,npruned); } /* Unmap from memory / close the files */ free(checked); free(nodes); free(segments); free(lats); free(lons); #if !SLIM nodesx->data=UnmapFile(nodesx->data); segmentsx->data=UnmapFile(segmentsx->data); waysx->data=UnmapFile(waysx->data); #else nodesx->fd=SlimUnmapFile(nodesx->fd); segmentsx->fd=SlimUnmapFile(segmentsx->fd); waysx->fd=SlimUnmapFile(waysx->fd); #endif /* Print the final message */ printf_last("Pruned Straight Highway Nodes: Nodes=%"Pindex_t" Pruned=%"Pindex_t,nodesx->number,npruned); }
int main(int argc,char** argv) { Nodes *OSMNodes; Segments *OSMSegments; Ways *OSMWays; Relations *OSMRelations; Results *results[NWAYPOINTS+1]={NULL}; int point_used[NWAYPOINTS+1]={0}; double point_lon[NWAYPOINTS+1],point_lat[NWAYPOINTS+1]; index_t point_node[NWAYPOINTS+1]={NO_NODE}; double heading=-999; int help_profile=0,help_profile_xml=0,help_profile_json=0,help_profile_pl=0; char *dirname=NULL,*prefix=NULL; char *profiles=NULL,*profilename=NULL; char *translations=NULL,*language=NULL; int exactnodes=0,reverse=0,loop=0; Transport transport=Transport_None; Profile *profile=NULL; Translation *translation=NULL; index_t start_node,finish_node=NO_NODE; index_t join_segment=NO_SEGMENT; int arg,nresults=0; waypoint_t start_waypoint,finish_waypoint=NO_WAYPOINT; waypoint_t first_waypoint=NWAYPOINTS,last_waypoint=1,waypoint; int inc_dec_waypoint=1; printf_program_start(); /* Parse the command line arguments */ if(argc<2) print_usage(0,NULL,NULL); /* Get the non-routing, general program options */ for(arg=1;arg<argc;arg++) { if(!strcmp(argv[arg],"--version")) print_usage(-1,NULL,NULL); else if(!strcmp(argv[arg],"--help")) print_usage(1,NULL,NULL); else if(!strcmp(argv[arg],"--help-profile")) help_profile=1; else if(!strcmp(argv[arg],"--help-profile-xml")) help_profile_xml=1; else if(!strcmp(argv[arg],"--help-profile-json")) help_profile_json=1; else if(!strcmp(argv[arg],"--help-profile-perl")) help_profile_pl=1; else if(!strncmp(argv[arg],"--dir=",6)) dirname=&argv[arg][6]; else if(!strncmp(argv[arg],"--prefix=",9)) prefix=&argv[arg][9]; else if(!strncmp(argv[arg],"--profiles=",11)) profiles=&argv[arg][11]; else if(!strncmp(argv[arg],"--translations=",15)) translations=&argv[arg][15]; else if(!strcmp(argv[arg],"--exact-nodes-only")) exactnodes=1; else if(!strncmp(argv[arg],"--reverse",9)) { if(argv[arg][9]=='=') reverse=atoi(&argv[arg][10]); else reverse=1; } else if(!strncmp(argv[arg],"--loop",6)) { if(argv[arg][6]=='=') loop=atoi(&argv[arg][7]); else loop=1; } else if(!strcmp(argv[arg],"--quiet")) option_quiet=1; else if(!strcmp(argv[arg],"--loggable")) option_loggable=1; else if(!strcmp(argv[arg],"--logtime")) option_logtime=2; else if(!strcmp(argv[arg],"--logmemory")) option_logmemory=1; else if(!strcmp(argv[arg],"--output-html")) option_file_html=1; else if(!strcmp(argv[arg],"--output-gpx-track")) option_file_gpx_track=1; else if(!strcmp(argv[arg],"--output-gpx-route")) option_file_gpx_route=1; else if(!strcmp(argv[arg],"--output-text")) option_file_text=1; else if(!strcmp(argv[arg],"--output-text-all")) option_file_text_all=1; else if(!strcmp(argv[arg],"--output-none")) option_file_none=1; else if(!strcmp(argv[arg],"--output-stdout")) { option_file_stdout=1; option_quiet=1; } else if(!strncmp(argv[arg],"--profile=",10)) profilename=&argv[arg][10]; else if(!strncmp(argv[arg],"--language=",11)) language=&argv[arg][11]; else if(!strcmp(argv[arg],"--shortest")) option_quickest=0; else if(!strcmp(argv[arg],"--quickest")) option_quickest=1; else if(!strncmp(argv[arg],"--lon",5) && isdigit(argv[arg][5])) { int point; char *p=&argv[arg][6]; while(isdigit(*p)) p++; if(*p++!='=') print_usage(0,argv[arg],NULL); point=atoi(&argv[arg][5]); if(point>NWAYPOINTS || point_used[point]&1) print_usage(0,argv[arg],NULL); point_lon[point]=degrees_to_radians(atof(p)); point_used[point]+=1; if(point<first_waypoint) first_waypoint=point; if(point>last_waypoint) last_waypoint=point; } else if(!strncmp(argv[arg],"--lat",5) && isdigit(argv[arg][5])) { int point; char *p=&argv[arg][6]; while(isdigit(*p)) p++; if(*p++!='=') print_usage(0,argv[arg],NULL); point=atoi(&argv[arg][5]); if(point>NWAYPOINTS || point_used[point]&2) print_usage(0,argv[arg],NULL); point_lat[point]=degrees_to_radians(atof(p)); point_used[point]+=2; if(point<first_waypoint) first_waypoint=point; if(point>last_waypoint) last_waypoint=point; } else if(!strncmp(argv[arg],"--heading=",10)) { double h=atof(&argv[arg][10]); if(h>=-360 && h<=360) { heading=h; if(heading<0) heading+=360; } } else if(!strncmp(argv[arg],"--transport=",12)) { transport=TransportType(&argv[arg][12]); if(transport==Transport_None) print_usage(0,argv[arg],NULL); } else continue; argv[arg]=NULL; } /* Check the specified command line options */ if(option_file_stdout && (option_file_html+option_file_gpx_track+option_file_gpx_route+option_file_text+option_file_text_all)!=1) { fprintf(stderr,"Error: The '--output-stdout' option requires exactly one other output option (but not '--output-none').\n"); exit(EXIT_FAILURE); } if(option_file_html==0 && option_file_gpx_track==0 && option_file_gpx_route==0 && option_file_text==0 && option_file_text_all==0 && option_file_none==0) option_file_html=option_file_gpx_track=option_file_gpx_route=option_file_text=option_file_text_all=1; /* Load in the selected profiles */ if(transport==Transport_None) transport=Transport_Motorcar; if(profiles) { if(!ExistsFile(profiles)) { fprintf(stderr,"Error: The '--profiles' option specifies a file '%s' that does not exist.\n",profiles); exit(EXIT_FAILURE); } } else { profiles=FileName(dirname,prefix,"profiles.xml"); if(!ExistsFile(profiles)) { char *defaultprofiles=FileName(ROUTINO_DATADIR,NULL,"profiles.xml"); if(!ExistsFile(defaultprofiles)) { fprintf(stderr,"Error: The '--profiles' option was not used and the files '%s' and '%s' do not exist.\n",profiles,defaultprofiles); exit(EXIT_FAILURE); } free(profiles); profiles=defaultprofiles; } } if(!profilename) profilename=(char*)TransportName(transport); if(ParseXMLProfiles(profiles,profilename,(help_profile_xml|help_profile_json|help_profile_pl))) { fprintf(stderr,"Error: Cannot read the profiles in the file '%s'.\n",profiles); exit(EXIT_FAILURE); } profile=GetProfile(profilename); if(!profile) { fprintf(stderr,"Error: Cannot find a profile called '%s' in the file '%s'.\n",profilename,profiles); profile=(Profile*)calloc(1,sizeof(Profile)); profile->transport=transport; } /* Parse the other command line arguments that modify the profile */ for(arg=1;arg<argc;arg++) { if(!argv[arg]) continue; else if(!strncmp(argv[arg],"--highway-",10)) { Highway highway; char *equal=strchr(argv[arg],'='); char *string; double p; if(!equal) print_usage(0,argv[arg],NULL); string=strcpy((char*)malloc(strlen(argv[arg])),argv[arg]+10); string[equal-argv[arg]-10]=0; highway=HighwayType(string); if(highway==Highway_None) print_usage(0,argv[arg],NULL); p=atof(equal+1); if(p<0 || p>100) print_usage(0,argv[arg],NULL); profile->highway[highway]=(score_t)(p/100); free(string); } else if(!strncmp(argv[arg],"--speed-",8)) { Highway highway; char *equal=strchr(argv[arg],'='); char *string; double s; if(!equal) print_usage(0,argv[arg],NULL); string=strcpy((char*)malloc(strlen(argv[arg])),argv[arg]+8); string[equal-argv[arg]-8]=0; highway=HighwayType(string); if(highway==Highway_None) print_usage(0,argv[arg],NULL); s=atof(equal+1); if(s<0) print_usage(0,argv[arg],NULL); profile->speed[highway]=kph_to_speed(s); free(string); } else if(!strncmp(argv[arg],"--property-",11)) { Property property; char *equal=strchr(argv[arg],'='); char *string; double p; if(!equal) print_usage(0,argv[arg],NULL); string=strcpy((char*)malloc(strlen(argv[arg])),argv[arg]+11); string[equal-argv[arg]-11]=0; property=PropertyType(string); if(property==Property_None) print_usage(0,argv[arg],NULL); p=atof(equal+1); if(p<0 || p>100) print_usage(0,argv[arg],NULL); profile->props[property]=(score_t)(p/100); free(string); } else if(!strncmp(argv[arg],"--oneway=",9)) profile->oneway=!!atoi(&argv[arg][9]); else if(!strncmp(argv[arg],"--turns=",8)) profile->turns=!!atoi(&argv[arg][8]); else if(!strncmp(argv[arg],"--weight=",9)) profile->weight=tonnes_to_weight(atof(&argv[arg][9])); else if(!strncmp(argv[arg],"--height=",9)) profile->height=metres_to_height(atof(&argv[arg][9])); else if(!strncmp(argv[arg],"--width=",8)) profile->width=metres_to_width(atof(&argv[arg][8])); else if(!strncmp(argv[arg],"--length=",9)) profile->length=metres_to_length(atof(&argv[arg][9])); else print_usage(0,argv[arg],NULL); } /* Print one of the profiles if requested */ if(help_profile) { PrintProfile(profile); exit(EXIT_SUCCESS); } else if(help_profile_xml) { PrintProfilesXML(); exit(EXIT_SUCCESS); } else if(help_profile_json) { PrintProfilesJSON(); exit(EXIT_SUCCESS); } else if(help_profile_pl) { PrintProfilesPerl(); exit(EXIT_SUCCESS); } /* Load in the selected translation */ if(option_file_html || option_file_gpx_route || option_file_gpx_track || option_file_text || option_file_text_all) { if(translations) { if(!ExistsFile(translations)) { fprintf(stderr,"Error: The '--translations' option specifies a file '%s' that does not exist.\n",translations); exit(EXIT_FAILURE); } } else { translations=FileName(dirname,prefix,"translations.xml"); if(!ExistsFile(translations)) { char *defaulttranslations=FileName(ROUTINO_DATADIR,NULL,"translations.xml"); if(!ExistsFile(defaulttranslations)) { fprintf(stderr,"Error: The '--translations' option was not used and the files '%s' and '%s' do not exist.\n",translations,defaulttranslations); exit(EXIT_FAILURE); } free(translations); translations=defaulttranslations; } } if(ParseXMLTranslations(translations,language,0)) { fprintf(stderr,"Error: Cannot read the translations in the file '%s'.\n",translations); exit(EXIT_FAILURE); } if(language) { translation=GetTranslation(language); if(!translation) { fprintf(stderr,"Warning: Cannot find a translation called '%s' in the file '%s'.\n",language,translations); exit(EXIT_FAILURE); } } else { translation=GetTranslation(""); if(!translation) { fprintf(stderr,"Warning: No translations in '%s'.\n",translations); exit(EXIT_FAILURE); } } } /* Check the waypoints are valid */ for(waypoint=1;waypoint<=NWAYPOINTS;waypoint++) if(point_used[waypoint]==1 || point_used[waypoint]==2) print_usage(0,NULL,"All waypoints must have latitude and longitude."); if(first_waypoint>=last_waypoint) print_usage(0,NULL,"At least two waypoints must be specified."); /* Load in the data - Note: No error checking because Load*List() will call exit() in case of an error. */ if(!option_quiet) printf_first("Loading Files:"); OSMNodes=LoadNodeList(FileName(dirname,prefix,"nodes.mem")); OSMSegments=LoadSegmentList(FileName(dirname,prefix,"segments.mem")); OSMWays=LoadWayList(FileName(dirname,prefix,"ways.mem")); OSMRelations=LoadRelationList(FileName(dirname,prefix,"relations.mem")); if(!option_quiet) printf_last("Loaded Files: nodes, segments, ways & relations"); /* Check the profile is valid for use with this database */ if(UpdateProfile(profile,OSMWays)) { fprintf(stderr,"Error: Profile is invalid or not compatible with database.\n"); exit(EXIT_FAILURE); } /* Find all waypoints */ for(waypoint=first_waypoint;waypoint<=last_waypoint;waypoint++) { distance_t distmax=km_to_distance(MAXSEARCH); distance_t distmin; index_t segment=NO_SEGMENT; index_t node1,node2,node=NO_NODE; if(point_used[waypoint]!=3) continue; /* Find the closest point */ if(!option_quiet) printf_first("Finding Closest Point: Waypoint %d",waypoint); if(exactnodes) node=FindClosestNode(OSMNodes,OSMSegments,OSMWays,point_lat[waypoint],point_lon[waypoint],distmax,profile,&distmin); else { distance_t dist1,dist2; segment=FindClosestSegment(OSMNodes,OSMSegments,OSMWays,point_lat[waypoint],point_lon[waypoint],distmax,profile,&distmin,&node1,&node2,&dist1,&dist2); if(segment!=NO_SEGMENT) node=CreateFakes(OSMNodes,OSMSegments,waypoint,LookupSegment(OSMSegments,segment,1),node1,node2,dist1,dist2); } if(!option_quiet) printf_last("Found Closest Point: Waypoint %d",waypoint); if(node==NO_NODE) { fprintf(stderr,"Error: Cannot find node close to specified point %d.\n",waypoint); exit(EXIT_FAILURE); } if(!option_quiet) { double lat,lon; if(IsFakeNode(node)) GetFakeLatLong(node,&lat,&lon); else GetLatLong(OSMNodes,node,NULL,&lat,&lon); if(IsFakeNode(node)) printf("Waypoint %d is segment %"Pindex_t" (node %"Pindex_t" -> %"Pindex_t"): %3.6f %4.6f = %2.3f km\n",waypoint,segment,node1,node2, radians_to_degrees(lon),radians_to_degrees(lat),distance_to_km(distmin)); else printf("Waypoint %d is node %"Pindex_t": %3.6f %4.6f = %2.3f km\n",waypoint,node, radians_to_degrees(lon),radians_to_degrees(lat),distance_to_km(distmin)); } point_node[waypoint]=node; } /* Check for reverse direction */ if(reverse) { waypoint_t temp; temp=first_waypoint; first_waypoint=last_waypoint; last_waypoint=temp; inc_dec_waypoint=-1; } /* Loop through all pairs of waypoints */ if(loop && reverse) { finish_node=point_node[last_waypoint]; finish_waypoint=last_waypoint; } for(waypoint=first_waypoint;waypoint!=(last_waypoint+inc_dec_waypoint);waypoint+=inc_dec_waypoint) { if(point_used[waypoint]!=3) continue; start_node=finish_node; finish_node=point_node[waypoint]; start_waypoint=finish_waypoint; finish_waypoint=waypoint; if(start_node==NO_NODE) continue; if(heading!=-999 && join_segment==NO_SEGMENT) join_segment=FindClosestSegmentHeading(OSMNodes,OSMSegments,OSMWays,start_node,heading,profile); /* Calculate the route */ if(!option_quiet) printf("Routing from waypoint %d to waypoint %d\n",start_waypoint,finish_waypoint); results[nresults]=CalculateRoute(OSMNodes,OSMSegments,OSMWays,OSMRelations,profile,start_node,join_segment,finish_node,start_waypoint,finish_waypoint); if(!results[nresults]) exit(EXIT_FAILURE); join_segment=results[nresults]->last_segment; nresults++; } if(loop && !reverse) { start_node=finish_node; finish_node=point_node[first_waypoint]; start_waypoint=finish_waypoint; finish_waypoint=first_waypoint; /* Calculate the route */ if(!option_quiet) printf("Routing from waypoint %d to waypoint %d\n",start_waypoint,finish_waypoint); results[nresults]=CalculateRoute(OSMNodes,OSMSegments,OSMWays,OSMRelations,profile,start_node,join_segment,finish_node,start_waypoint,finish_waypoint); if(!results[nresults]) exit(EXIT_FAILURE); nresults++; } if(!option_quiet) { printf("Routed OK\n"); fflush(stdout); } /* Print out the combined route */ if(!option_quiet) printf_first("Generating Result Outputs"); if(!option_file_none) PrintRoute(results,nresults,OSMNodes,OSMSegments,OSMWays,OSMRelations,profile,translation); if(!option_quiet) printf_last("Generated Result Outputs"); /* Destroy the remaining results lists and data structures */ #ifdef DEBUG_MEMORY_LEAK for(waypoint=0;waypoint<nresults;waypoint++) FreeResultsList(results[waypoint]); DestroyNodeList(OSMNodes); DestroySegmentList(OSMSegments); DestroyWayList(OSMWays); DestroyRelationList(OSMRelations); FreeXMLProfiles(); FreeXMLTranslations(); #endif if(!option_quiet) printf_program_end(); exit(EXIT_SUCCESS); }
void PrintRoute(Results **results,int nresults,Nodes *nodes,Segments *segments,Ways *ways,Profile *profile) { FILE *htmlfile=NULL,*gpxtrackfile=NULL,*gpxroutefile=NULL,*textfile=NULL,*textallfile=NULL; char *prev_bearing=NULL,*prev_wayname=NULL; distance_t cum_distance=0; duration_t cum_duration=0; int point=1; int segment_count=0,route_count=0; int point_count=0; int roundabout=0; /* Open the files */ if(option_quickest==0) { /* Print the result for the shortest route */ if(option_html) htmlfile =fopen("shortest.html","w"); if(option_gpx_track) gpxtrackfile=fopen("shortest-track.gpx","w"); if(option_gpx_route) gpxroutefile=fopen("shortest-route.gpx","w"); if(option_text) textfile =fopen("shortest.txt","w"); if(option_text_all) textallfile =fopen("shortest-all.txt","w"); if(option_html && !htmlfile) fprintf(stderr,"Warning: Cannot open file 'shortest.html' for writing [%s].\n",strerror(errno)); if(option_gpx_track && !gpxtrackfile) fprintf(stderr,"Warning: Cannot open file 'shortest-track.gpx' for writing [%s].\n",strerror(errno)); if(option_gpx_route && !gpxroutefile) fprintf(stderr,"Warning: Cannot open file 'shortest-route.gpx' for writing [%s].\n",strerror(errno)); if(option_text && !textfile) fprintf(stderr,"Warning: Cannot open file 'shortest.txt' for writing [%s].\n",strerror(errno)); if(option_text_all && !textallfile) fprintf(stderr,"Warning: Cannot open file 'shortest-all.txt' for writing [%s].\n",strerror(errno)); } else { /* Print the result for the quickest route */ if(option_html) htmlfile =fopen("quickest.html","w"); if(option_gpx_track) gpxtrackfile=fopen("quickest-track.gpx","w"); if(option_gpx_route) gpxroutefile=fopen("quickest-route.gpx","w"); if(option_text) textfile =fopen("quickest.txt","w"); if(option_text_all) textallfile =fopen("quickest-all.txt","w"); if(option_html && !htmlfile) fprintf(stderr,"Warning: Cannot open file 'quickest.html' for writing [%s].\n",strerror(errno)); if(option_gpx_track && !gpxtrackfile) fprintf(stderr,"Warning: Cannot open file 'quickest-track.gpx' for writing [%s].\n",strerror(errno)); if(option_gpx_route && !gpxroutefile) fprintf(stderr,"Warning: Cannot open file 'quickest-route.gpx' for writing [%s].\n",strerror(errno)); if(option_text && !textfile) fprintf(stderr,"Warning: Cannot open file 'quickest.txt' for writing [%s].\n",strerror(errno)); if(option_text_all && !textallfile) fprintf(stderr,"Warning: Cannot open file 'quickest-all.txt' for writing [%s].\n",strerror(errno)); } /* Print the head of the files */ if(htmlfile) { fprintf(htmlfile,"<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">\n"); fprintf(htmlfile,"<HTML>\n"); if(translate_xml_copyright_creator[0] && translate_xml_copyright_creator[1]) fprintf(htmlfile,"<!-- %s : %s -->\n",translate_xml_copyright_creator[0],translate_xml_copyright_creator[1]); if(translate_xml_copyright_source[0] && translate_xml_copyright_source[1]) fprintf(htmlfile,"<!-- %s : %s -->\n",translate_xml_copyright_source[0],translate_xml_copyright_source[1]); if(translate_xml_copyright_license[0] && translate_xml_copyright_license[1]) fprintf(htmlfile,"<!-- %s : %s -->\n",translate_xml_copyright_license[0],translate_xml_copyright_license[1]); fprintf(htmlfile,"<HEAD>\n"); fprintf(htmlfile,"<TITLE>"); fprintf(htmlfile,translate_html_title,option_quickest?translate_xml_route_quickest:translate_xml_route_shortest); fprintf(htmlfile,"</TITLE>\n"); fprintf(htmlfile,"<META http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">\n"); fprintf(htmlfile,"<STYLE type=\"text/css\">\n"); fprintf(htmlfile,"<!--\n"); fprintf(htmlfile," table {table-layout: fixed; border: none; border-collapse: collapse;}\n"); fprintf(htmlfile," table.c {color: grey; font-size: x-small;} /* copyright */\n"); fprintf(htmlfile," tr {border: 0px;}\n"); fprintf(htmlfile," tr.c {display: none;} /* coords */\n"); fprintf(htmlfile," tr.n {} /* node */\n"); fprintf(htmlfile," tr.s {} /* segment */\n"); fprintf(htmlfile," tr.t {font-weight: bold;} /* total */\n"); fprintf(htmlfile," td.l {font-weight: bold;}\n"); fprintf(htmlfile," td.r {}\n"); fprintf(htmlfile," span.w {font-weight: bold;} /* waypoint */\n"); fprintf(htmlfile," span.h {text-decoration: underline;} /* highway */\n"); fprintf(htmlfile," span.d {} /* segment distance */\n"); fprintf(htmlfile," span.j {font-style: italic;} /* total journey distance */\n"); fprintf(htmlfile," span.t {font-variant: small-caps;} /* turn */\n"); fprintf(htmlfile," span.b {font-variant: small-caps;} /* bearing */\n"); fprintf(htmlfile,"-->\n"); fprintf(htmlfile,"</STYLE>\n"); fprintf(htmlfile,"</HEAD>\n"); fprintf(htmlfile,"<BODY>\n"); fprintf(htmlfile,"<H1>"); fprintf(htmlfile,translate_html_title,option_quickest?translate_xml_route_quickest:translate_xml_route_shortest); fprintf(htmlfile,"</H1>\n"); fprintf(htmlfile,"<table>\n"); } if(gpxtrackfile) { fprintf(gpxtrackfile,"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"); fprintf(gpxtrackfile,"<gpx version=\"1.1\" creator=\"Routino\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns=\"http://www.topografix.com/GPX/1/1\" xsi:schemaLocation=\"http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd\">\n"); fprintf(gpxtrackfile,"<metadata>\n"); fprintf(gpxtrackfile,"<desc>%s : %s</desc>\n",translate_xml_copyright_creator[0],translate_xml_copyright_creator[1]); if(translate_xml_copyright_source[1]) { fprintf(gpxtrackfile,"<copyright author=\"%s\">\n",translate_xml_copyright_source[1]); if(translate_xml_copyright_license[1]) fprintf(gpxtrackfile,"<license>%s</license>\n",translate_xml_copyright_license[1]); fprintf(gpxtrackfile,"</copyright>\n"); } fprintf(gpxtrackfile,"</metadata>\n"); fprintf(gpxtrackfile,"<trk>\n"); fprintf(gpxtrackfile,"<name>"); fprintf(gpxtrackfile,translate_gpx_name,option_quickest?translate_xml_route_quickest:translate_xml_route_shortest); fprintf(gpxtrackfile,"</name>\n"); fprintf(gpxtrackfile,"<desc>"); fprintf(gpxtrackfile,translate_gpx_desc,option_quickest?translate_xml_route_quickest:translate_xml_route_shortest); fprintf(gpxtrackfile,"</desc>\n"); } if(gpxroutefile) { fprintf(gpxroutefile,"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"); fprintf(gpxroutefile,"<gpx version=\"1.1\" creator=\"Routino\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns=\"http://www.topografix.com/GPX/1/1\" xsi:schemaLocation=\"http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd\">\n"); fprintf(gpxroutefile,"<metadata>\n"); fprintf(gpxroutefile,"<desc>%s : %s</desc>\n",translate_xml_copyright_creator[0],translate_xml_copyright_creator[1]); if(translate_xml_copyright_source[1]) { fprintf(gpxroutefile,"<copyright author=\"%s\">\n",translate_xml_copyright_source[1]); if(translate_xml_copyright_license[1]) fprintf(gpxroutefile,"<license>%s</license>\n",translate_xml_copyright_license[1]); fprintf(gpxroutefile,"</copyright>\n"); } fprintf(gpxroutefile,"</metadata>\n"); fprintf(gpxroutefile,"<rte>\n"); fprintf(gpxroutefile,"<name>"); fprintf(gpxroutefile,translate_gpx_name,option_quickest?translate_xml_route_quickest:translate_xml_route_shortest); fprintf(gpxroutefile,"</name>\n"); fprintf(gpxroutefile,"<desc>"); fprintf(gpxroutefile,translate_gpx_desc,option_quickest?translate_xml_route_quickest:translate_xml_route_shortest); fprintf(gpxroutefile,"</desc>\n"); } if(textfile) { if(translate_raw_copyright_creator[0] && translate_raw_copyright_creator[1]) fprintf(textfile,"# %s : %s\n",translate_raw_copyright_creator[0],translate_raw_copyright_creator[1]); if(translate_raw_copyright_source[0] && translate_raw_copyright_source[1]) fprintf(textfile,"# %s : %s\n",translate_raw_copyright_source[0],translate_raw_copyright_source[1]); if(translate_raw_copyright_license[0] && translate_raw_copyright_license[1]) fprintf(textfile,"# %s : %s\n",translate_raw_copyright_license[0],translate_raw_copyright_license[1]); if((translate_raw_copyright_creator[0] && translate_raw_copyright_creator[1]) || (translate_raw_copyright_source[0] && translate_raw_copyright_source[1]) || (translate_raw_copyright_license[0] && translate_raw_copyright_license[1])) fprintf(textfile,"#\n"); fprintf(textfile,"#Latitude\tLongitude\tSection \tSection \tTotal \tTotal \tPoint\tTurn\tBearing\tHighway\n"); fprintf(textfile,"# \t \tDistance\tDuration\tDistance\tDuration\tType \t \t \t \n"); /* "%10.6f\t%11.6f\t%6.3f km\t%4.1f min\t%5.1f km\t%4.0f min\t%s\t %+d\t %+d\t%s\n" */ } if(textallfile) { if(translate_raw_copyright_creator[0] && translate_raw_copyright_creator[1]) fprintf(textallfile,"# %s : %s\n",translate_raw_copyright_creator[0],translate_raw_copyright_creator[1]); if(translate_raw_copyright_source[0] && translate_raw_copyright_source[1]) fprintf(textallfile,"# %s : %s\n",translate_raw_copyright_source[0],translate_raw_copyright_source[1]); if(translate_raw_copyright_license[0] && translate_raw_copyright_license[1]) fprintf(textallfile,"# %s : %s\n",translate_raw_copyright_license[0],translate_raw_copyright_license[1]); if((translate_raw_copyright_creator[0] && translate_raw_copyright_creator[1]) || (translate_raw_copyright_source[0] && translate_raw_copyright_source[1]) || (translate_raw_copyright_license[0] && translate_raw_copyright_license[1])) fprintf(textallfile,"#\n"); fprintf(textallfile,"#Latitude\tLongitude\t Node\tType\tSegment\tSegment\tTotal\tTotal \tSpeed\tBearing\tHighway\n"); fprintf(textallfile,"# \t \t \t \tDist \tDurat'n\tDist \tDurat'n\t \t \t \n"); /* "%10.6f\t%11.6f\t%8d%c\t%s\t%5.3f\t%5.2f\t%5.2f\t%5.1f\t%3d\t%4d\t%s\n" */ } /* Loop through all the sections of the route and print them */ while(!results[point]) point++; while(point<=nresults) { int next_point=point; distance_t junc_distance=0; duration_t junc_duration=0; Result *result; result=FindResult(results[point],results[point]->start_node,results[point]->prev_segment); /* Print the start of the segment */ if(gpxtrackfile) fprintf(gpxtrackfile,"<trkseg>\n"); /* Loop through all the points within a section of the route and print them */ do { double latitude,longitude; Node *resultnode=NULL; index_t realsegment=NO_SEGMENT,next_realsegment=NO_SEGMENT; Segment *resultsegment=NULL,*next_resultsegment=NULL; Way *resultway=NULL,*next_resultway=NULL; Result *next_result; int important=IMP_UNIMPORTANT; distance_t seg_distance=0; duration_t seg_duration=0; speed_t seg_speed=0; char *waynameraw=NULL,*wayname=NULL,*next_waynameraw=NULL,*next_wayname=NULL; int bearing_int=0,turn_int=0,next_bearing_int=0; char *turn=NULL,*next_bearing=NULL; /* Calculate the information about this point */ if(IsFakeNode(result->node)) GetFakeLatLong(result->node,&latitude,&longitude); else GetLatLong(nodes,result->node,&latitude,&longitude); if(!IsFakeNode(result->node)) resultnode=LookupNode(nodes,result->node,6); /* Calculate the next result */ next_result=result->next; if(!next_result) for(next_point=point+1; next_point<=nresults; next_point++) if(results[next_point]) { next_result=FindResult(results[next_point],results[next_point]->start_node,results[next_point]->prev_segment); next_result=next_result->next; break; } /* Calculate the information about this segment */ if(result->node!=results[point]->start_node) /* not first point of a section of the route */ { if(IsFakeSegment(result->segment)) { resultsegment=LookupFakeSegment(result->segment); realsegment=IndexRealSegment(result->segment); } else { resultsegment=LookupSegment(segments,result->segment,2); realsegment=result->segment; } resultway=LookupWay(ways,resultsegment->way,1); seg_distance+=DISTANCE(resultsegment->distance); seg_duration+=Duration(resultsegment,resultway,profile); /* Calculate the cumulative distance/duration */ junc_distance+=seg_distance; junc_duration+=seg_duration; cum_distance+=seg_distance; cum_duration+=seg_duration; } /* Calculate the information about the next segment */ if(next_result) { if(IsFakeSegment(next_result->segment)) { next_resultsegment=LookupFakeSegment(next_result->segment); next_realsegment=IndexRealSegment(next_result->segment); } else { next_resultsegment=LookupSegment(segments,next_result->segment,1); next_realsegment=next_result->segment; } } /* Decide if this is a roundabout */ if(next_result) { next_resultway=LookupWay(ways,next_resultsegment->way,2); if(next_resultway->type&Way_Roundabout) { if(roundabout==0) { roundabout++; important=IMP_RB_ENTRY; } else { Segment *segment=FirstSegment(segments,resultnode,3); do { index_t othernode=OtherNode(segment,result->node); if(othernode!=result->prev->node && IndexSegment(segments,segment)!=realsegment) if(IsNormalSegment(segment) && (!profile->oneway || !IsOnewayTo(segment,result->node))) { Way *way=LookupWay(ways,segment->way,3); if(!(way->type&Way_Roundabout)) if(othernode!=next_result->node) { roundabout++; important=IMP_RB_NOT_EXIT; } } segment=NextSegment(segments,segment,result->node); } while(segment); } } else if(roundabout) { roundabout++; important=IMP_RB_EXIT; } } /* Decide if this is an important junction */ if(roundabout) /* roundabout */ ; else if(point_count==0) /* first point overall = Waypoint */ important=IMP_WAYPOINT; else if(result->node==results[point]->finish_node) /* Waypoint */ important=IMP_WAYPOINT; else if(result->node==results[point]->start_node) /* first point of a section of the route */ important=IMP_IGNORE; else if(realsegment==next_realsegment) /* U-turn */ important=IMP_UTURN; else if(resultnode && (resultnode->flags&NODE_MINIRNDBT)) important=IMP_MINI_RB; /* mini-roundabout */ else { Segment *segment=FirstSegment(segments,resultnode,3); do { index_t othernode=OtherNode(segment,result->node); if(othernode!=result->prev->node && IndexSegment(segments,segment)!=realsegment) if(IsNormalSegment(segment) && (!profile->oneway || !IsOnewayTo(segment,result->node))) { Way *way=LookupWay(ways,segment->way,3); if(othernode==next_result->node) /* the next segment that we follow */ { if(HIGHWAY(way->type)!=HIGHWAY(resultway->type)) if(important<IMP_CHANGE) important=IMP_CHANGE; } else if(IsFakeNode(next_result->node)) ; else /* a segment that we don't follow */ { if(junction_other_way[HIGHWAY(resultway->type)-1][HIGHWAY(way->type)-1]) if(important<IMP_JUNCT_IMPORT) important=IMP_JUNCT_IMPORT; if(important<IMP_JUNCT_CONT) important=IMP_JUNCT_CONT; } } segment=NextSegment(segments,segment,result->node); } while(segment); } /* Calculate the strings to be used */ if(resultway && textallfile) { waynameraw=WayName(ways,resultway); if(!*waynameraw) waynameraw=translate_raw_highway[HIGHWAY(resultway->type)]; bearing_int=(int)BearingAngle(nodes,resultsegment,result->node); seg_speed=profile->speed[HIGHWAY(resultway->type)]; } if(next_result && important>IMP_JUNCT_CONT) { if(resultsegment && (htmlfile || textfile)) { turn_int=(int)TurnAngle(nodes,resultsegment,next_resultsegment,result->node); turn=translate_xml_turn[((202+turn_int)/45)%8]; } if(gpxroutefile || htmlfile) { next_waynameraw=WayName(ways,next_resultway); if(!*next_waynameraw) next_waynameraw=translate_raw_highway[HIGHWAY(next_resultway->type)]; next_wayname=ParseXML_Encode_Safe_XML(next_waynameraw); } if(htmlfile || gpxroutefile || textfile) { next_bearing_int=(int)BearingAngle(nodes,next_resultsegment,next_result->node); next_bearing=translate_xml_heading[(4+(22+next_bearing_int)/45)%8]; } } /* Print out the important points (junctions / waypoints) */ if(important>IMP_JUNCT_CONT) { if(htmlfile) { char *type; if(important==IMP_WAYPOINT) type=translate_html_waypoint; else if(important==IMP_MINI_RB) type=translate_html_roundabout; else type=translate_html_junction; if(point_count>0) /* not the first point */ { /* <tr class='s'><td class='l'>Follow:<td class='r'><span class='h'>*highway name*</span> for <span class='d'>*distance* km, *time* min</span> [<span class='j'>*distance* km, *time* minutes</span>] */ fprintf(htmlfile,"<tr class='s'><td class='l'>%s:<td class='r'>",translate_html_segment[0]); fprintf(htmlfile,translate_html_segment[1], (roundabout>1?translate_html_roundabout:prev_wayname), distance_to_km(junc_distance),duration_to_minutes(junc_duration)); fprintf(htmlfile," [<span class='j'>"); fprintf(htmlfile,translate_html_total[1], distance_to_km(cum_distance),duration_to_minutes(cum_duration)); fprintf(htmlfile,"</span>]\n"); } /* <tr class='c'><td class='l'>*N*:<td class='r'>*latitude* *longitude* */ fprintf(htmlfile,"<tr class='c'><td class='l'>%d:<td class='r'>%.6f %.6f\n", point_count+1, radians_to_degrees(latitude),radians_to_degrees(longitude)); if(point_count==0) /* first point */ { /* <tr class='n'><td class='l'>Start:<td class='r'>At <span class='w'>Waypoint</span>, head <span class='b'>*heading*</span> */ fprintf(htmlfile,"<tr class='n'><td class='l'>%s:<td class='r'>",translate_html_start[0]); fprintf(htmlfile,translate_html_start[1], translate_html_waypoint, next_bearing); fprintf(htmlfile,"\n"); } else if(next_result) /* middle point */ { if(roundabout>1) { /* <tr class='n'><td class='l'>At:<td class='r'>Roundabout, take <span class='t'>the *Nth* exit</span> heading <span class='b'>*heading*</span> */ fprintf(htmlfile,"<tr class='n'><td class='l'>%s:<td class='r'>",translate_html_rbnode[0]); fprintf(htmlfile,translate_html_rbnode[1], translate_html_roundabout, translate_xml_ordinal[roundabout-2], next_bearing); fprintf(htmlfile,"\n"); } else { /* <tr class='n'><td class='l'>At:<td class='r'>Junction, go <span class='t'>*direction*</span> heading <span class='b'>*heading*</span> */ fprintf(htmlfile,"<tr class='n'><td class='l'>%s:<td class='r'>",translate_html_node[0]); fprintf(htmlfile,translate_html_node[1], type, turn, next_bearing); fprintf(htmlfile,"\n"); } } else /* end point */ { /* <tr class='n'><td class='l'>Stop:<td class='r'>At <span class='w'>Waypoint</span> */ fprintf(htmlfile,"<tr class='n'><td class='l'>%s:<td class='r'>",translate_html_stop[0]); fprintf(htmlfile,translate_html_stop[1], translate_html_waypoint); fprintf(htmlfile,"\n"); /* <tr class='t'><td class='l'>Total:<td class='r'><span class='j'>*distance* km, *time* minutes</span> */ fprintf(htmlfile,"<tr class='t'><td class='l'>%s:<td class='r'><span class='j'>",translate_html_total[0]); fprintf(htmlfile,translate_html_total[1], distance_to_km(cum_distance),duration_to_minutes(cum_duration)); fprintf(htmlfile,"</span>\n"); } } if(gpxroutefile) { if(point_count>0) /* not first point */ { fprintf(gpxroutefile,"<desc>"); fprintf(gpxroutefile,translate_gpx_step, prev_bearing, prev_wayname, distance_to_km(junc_distance),duration_to_minutes(junc_duration)); fprintf(gpxroutefile,"</desc></rtept>\n"); } if(point_count==0) /* first point */ { fprintf(gpxroutefile,"<rtept lat=\"%.6f\" lon=\"%.6f\"><name>%s</name>\n", radians_to_degrees(latitude),radians_to_degrees(longitude), translate_gpx_start); } else if(!next_result) /* end point */ { fprintf(gpxroutefile,"<rtept lat=\"%.6f\" lon=\"%.6f\"><name>%s</name>\n", radians_to_degrees(latitude),radians_to_degrees(longitude), translate_gpx_finish); fprintf(gpxroutefile,"<desc>"); fprintf(gpxroutefile,translate_gpx_final, distance_to_km(cum_distance),duration_to_minutes(cum_duration)); fprintf(gpxroutefile,"</desc></rtept>\n"); } else /* middle point */ { if(important==IMP_WAYPOINT) fprintf(gpxroutefile,"<rtept lat=\"%.6f\" lon=\"%.6f\"><name>%s%d</name>\n", radians_to_degrees(latitude),radians_to_degrees(longitude), translate_gpx_inter,++segment_count); else fprintf(gpxroutefile,"<rtept lat=\"%.6f\" lon=\"%.6f\"><name>%s%03d</name>\n", radians_to_degrees(latitude),radians_to_degrees(longitude), translate_gpx_trip,++route_count); } } if(textfile) { char *type; if(important==IMP_WAYPOINT) type="Waypt"; else type="Junct"; if(point_count==0) /* first point */ { fprintf(textfile,"%10.6f\t%11.6f\t%6.3f km\t%4.1f min\t%5.1f km\t%4.0f min\t%s\t\t %+d\t%s\n", radians_to_degrees(latitude),radians_to_degrees(longitude), 0.0,0.0,0.0,0.0, type, ((22+next_bearing_int)/45+4)%8-4, next_waynameraw); } else if(!next_result) /* end point */ { fprintf(textfile,"%10.6f\t%11.6f\t%6.3f km\t%4.1f min\t%5.1f km\t%4.0f min\t%s\t\t\t\n", radians_to_degrees(latitude),radians_to_degrees(longitude), distance_to_km(junc_distance),duration_to_minutes(junc_duration), distance_to_km(cum_distance),duration_to_minutes(cum_duration), type); } else /* middle point */ { fprintf(textfile,"%10.6f\t%11.6f\t%6.3f km\t%4.1f min\t%5.1f km\t%4.0f min\t%s\t %+d\t %+d\t%s\n", radians_to_degrees(latitude),radians_to_degrees(longitude), distance_to_km(junc_distance),duration_to_minutes(junc_duration), distance_to_km(cum_distance),duration_to_minutes(cum_duration), type, (22+turn_int)/45, ((22+next_bearing_int)/45+4)%8-4, next_waynameraw); } } junc_distance=0; junc_duration=0; if(htmlfile || gpxroutefile) { if(prev_wayname) free(prev_wayname); if(next_wayname) prev_wayname=strcpy((char*)malloc(strlen(next_wayname)+1),next_wayname); else prev_wayname=NULL; if(next_wayname && next_wayname!=next_waynameraw) free(next_wayname); } if(gpxroutefile) prev_bearing=next_bearing; if(roundabout>1) roundabout=0; } /* Print out all of the results */ if(gpxtrackfile) fprintf(gpxtrackfile,"<trkpt lat=\"%.6f\" lon=\"%.6f\"/>\n", radians_to_degrees(latitude),radians_to_degrees(longitude)); if(important>IMP_IGNORE) { if(textallfile) { char *type; if(important==IMP_WAYPOINT) type="Waypt"; else if(important==IMP_UTURN) type="U-turn"; else if(important==IMP_MINI_RB) type="Mini-RB"; else if(important==IMP_CHANGE) type="Change"; else if(important==IMP_UNIMPORTANT) type="Inter"; else type="Junct"; if(point_count==0) /* first point */ { fprintf(textallfile,"%10.6f\t%11.6f\t%8d%c\t%s\t%5.3f\t%5.2f\t%5.2f\t%5.1f\t\t\t\n", radians_to_degrees(latitude),radians_to_degrees(longitude), IsFakeNode(result->node)?(NODE_FAKE-result->node):result->node, (resultnode && IsSuperNode(resultnode))?'*':' ',type, 0.0,0.0,0.0,0.0); } else /* not the first point */ { fprintf(textallfile,"%10.6f\t%11.6f\t%8d%c\t%s\t%5.3f\t%5.2f\t%5.2f\t%5.1f\t%3d\t%4d\t%s\n", radians_to_degrees(latitude),radians_to_degrees(longitude), IsFakeNode(result->node)?(NODE_FAKE-result->node):result->node, (resultnode && IsSuperNode(resultnode))?'*':' ',type, distance_to_km(seg_distance),duration_to_minutes(seg_duration), distance_to_km(cum_distance),duration_to_minutes(cum_duration), speed_to_kph(seg_speed), bearing_int, waynameraw); } } } if(wayname && wayname!=waynameraw) free(wayname); result=next_result; if(important>IMP_JUNCT_CONT) point_count++; } while(point==next_point); /* Print the end of the segment */ if(gpxtrackfile) fprintf(gpxtrackfile,"</trkseg>\n"); point=next_point; } /* Print the tail of the files */ if(htmlfile) { fprintf(htmlfile,"</table>\n"); if((translate_xml_copyright_creator[0] && translate_xml_copyright_creator[1]) || (translate_xml_copyright_source[0] && translate_xml_copyright_source[1]) || (translate_xml_copyright_license[0] && translate_xml_copyright_license[1])) { fprintf(htmlfile,"<p>\n"); fprintf(htmlfile,"<table class='c'>\n"); if(translate_xml_copyright_creator[0] && translate_xml_copyright_creator[1]) fprintf(htmlfile,"<tr><td class='l'>%s:<td class='r'>%s\n",translate_xml_copyright_creator[0],translate_xml_copyright_creator[1]); if(translate_xml_copyright_source[0] && translate_xml_copyright_source[1]) fprintf(htmlfile,"<tr><td class='l'>%s:<td class='r'>%s\n",translate_xml_copyright_source[0],translate_xml_copyright_source[1]); if(translate_xml_copyright_license[0] && translate_xml_copyright_license[1]) fprintf(htmlfile,"<tr><td class='l'>%s:<td class='r'>%s\n",translate_xml_copyright_license[0],translate_xml_copyright_license[1]); fprintf(htmlfile,"</table>\n"); } fprintf(htmlfile,"</BODY>\n"); fprintf(htmlfile,"</HTML>\n"); } if(gpxtrackfile) { fprintf(gpxtrackfile,"</trk>\n"); fprintf(gpxtrackfile,"</gpx>\n"); } if(gpxroutefile) { fprintf(gpxroutefile,"</rte>\n"); fprintf(gpxroutefile,"</gpx>\n"); } /* Close the files */ if(htmlfile) fclose(htmlfile); if(gpxtrackfile) fclose(gpxtrackfile); if(gpxroutefile) fclose(gpxroutefile); if(textfile) fclose(textfile); if(textallfile) fclose(textallfile); }