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/relationsx.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 597 - (show annotations) (download) (as text)
Sun Jan 9 16:55:05 2011 UTC (14 years, 2 months ago) by amb
File MIME type: text/x-csrc
File size: 23263 byte(s)
Make the 'from' and 'to' nodes of turn restrictions super-nodes.

1 /***************************************
2 Extended Relation data type functions.
3
4 Part of the Routino routing software.
5 ******************/ /******************
6 This file Copyright 2010-2011 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 <assert.h>
24 #include <stdlib.h>
25 #include <stdio.h>
26 #include <string.h>
27 #include <sys/stat.h>
28
29 #include "types.h"
30 #include "relations.h"
31
32 #include "nodesx.h"
33 #include "segmentsx.h"
34 #include "waysx.h"
35 #include "relationsx.h"
36
37 #include "files.h"
38 #include "logging.h"
39 #include "sorting.h"
40
41
42 /* Functions */
43
44 static int sort_by_id(TurnRestrictRelX *a,TurnRestrictRelX *b);
45 static int deduplicate_by_id(TurnRestrictRelX *relationx,index_t index);
46
47 static int sort_by_via(TurnRestrictRelX *a,TurnRestrictRelX *b);
48
49
50 /* Variables */
51
52 /*+ The command line '--tmpdir' option or its default value. +*/
53 extern char *option_tmpdirname;
54
55 static RelationsX* sortrelationsx;
56
57
58 /*++++++++++++++++++++++++++++++++++++++
59 Allocate a new relation list (create a new file or open an existing one).
60
61 RelationsX *NewRelationList Returns the relation list.
62
63 int append Set to 1 if the file is to be opened for appending (now or later).
64 ++++++++++++++++++++++++++++++++++++++*/
65
66 RelationsX *NewRelationList(int append)
67 {
68 RelationsX *relationsx;
69
70 relationsx=(RelationsX*)calloc(1,sizeof(RelationsX));
71
72 assert(relationsx); /* Check calloc() worked */
73
74
75 /* Route Relations */
76
77 relationsx->rfilename=(char*)malloc(strlen(option_tmpdirname)+32);
78
79 if(append)
80 sprintf(relationsx->rfilename,"%s/relationsx.route.input.tmp",option_tmpdirname);
81 else
82 sprintf(relationsx->rfilename,"%s/relationsx.route.%p.tmp",option_tmpdirname,relationsx);
83
84 if(append)
85 {
86 off_t size,position=0;
87
88 relationsx->rfd=OpenFileAppend(relationsx->rfilename);
89
90 size=SizeFile(relationsx->rfilename);
91
92 while(position<size)
93 {
94 FILESORT_VARINT relationsize;
95
96 SeekFile(relationsx->rfd,position);
97 ReadFile(relationsx->rfd,&relationsize,FILESORT_VARSIZE);
98
99 relationsx->rxnumber++;
100 position+=relationsize+FILESORT_VARSIZE;
101 }
102
103 SeekFile(relationsx->rfd,size);
104 }
105 else
106 relationsx->rfd=OpenFileNew(relationsx->rfilename);
107
108
109 /* Turn Restriction Relations */
110
111 relationsx->trfilename=(char*)malloc(strlen(option_tmpdirname)+32);
112
113 if(append)
114 sprintf(relationsx->trfilename,"%s/relationsx.turn.input.tmp",option_tmpdirname);
115 else
116 sprintf(relationsx->trfilename,"%s/relationsx.turn.%p.tmp",option_tmpdirname,relationsx);
117
118 if(append)
119 {
120 off_t size;
121
122 relationsx->trfd=OpenFileAppend(relationsx->trfilename);
123
124 size=SizeFile(relationsx->trfilename);
125
126 relationsx->trxnumber=size/sizeof(TurnRestrictRelX);
127 }
128 else
129 relationsx->trfd=OpenFileNew(relationsx->trfilename);
130
131 return(relationsx);
132 }
133
134
135 /*++++++++++++++++++++++++++++++++++++++
136 Free a relation list.
137
138 RelationsX *relationsx The list to be freed.
139
140 int keep Set to 1 if the file is to be kept.
141 ++++++++++++++++++++++++++++++++++++++*/
142
143 void FreeRelationList(RelationsX *relationsx,int keep)
144 {
145 /* Route relations */
146
147 if(!keep)
148 DeleteFile(relationsx->rfilename);
149
150 free(relationsx->rfilename);
151
152
153 /* Turn Restriction relations */
154
155 if(!keep)
156 DeleteFile(relationsx->trfilename);
157
158 free(relationsx->trfilename);
159
160 free(relationsx);
161 }
162
163
164 /*++++++++++++++++++++++++++++++++++++++
165 Append a single relation to an unsorted route relation list.
166
167 RelationsX* relationsx The set of relations to process.
168
169 relation_t id The ID of the relation.
170
171 transports_t routes The types of routes that this relation is for.
172
173 way_t *ways The array of ways that are members of the relation.
174
175 int nways The number of ways that are members of the relation.
176
177 relation_t *relations The array of relations that are members of the relation.
178
179 int nrelations The number of relations that are members of the relation.
180 ++++++++++++++++++++++++++++++++++++++*/
181
182 void AppendRouteRelation(RelationsX* relationsx,relation_t id,
183 transports_t routes,
184 way_t *ways,int nways,
185 relation_t *relations,int nrelations)
186 {
187 RouteRelX relationx;
188 FILESORT_VARINT size;
189 way_t zeroway=0;
190 relation_t zerorelation=0;
191
192 relationx.id=id;
193 relationx.routes=routes;
194
195 size=sizeof(RouteRelX)+(nways+1)*sizeof(way_t)+(nrelations+1)*sizeof(relation_t);
196
197 WriteFile(relationsx->rfd,&size,FILESORT_VARSIZE);
198 WriteFile(relationsx->rfd,&relationx,sizeof(RouteRelX));
199
200 WriteFile(relationsx->rfd,ways ,nways*sizeof(way_t));
201 WriteFile(relationsx->rfd,&zeroway, sizeof(way_t));
202
203 WriteFile(relationsx->rfd,relations ,nrelations*sizeof(relation_t));
204 WriteFile(relationsx->rfd,&zerorelation, sizeof(relation_t));
205
206 relationsx->rxnumber++;
207
208 assert(!(relationsx->rxnumber==0)); /* Zero marks the high-water mark for relations. */
209 }
210
211
212 /*++++++++++++++++++++++++++++++++++++++
213 Append a single relation to an unsorted turn restriction relation list.
214
215 RelationsX* relationsx The set of relations to process.
216
217 relation_t id The ID of the relation.
218
219 way_t from The way that the turn restriction starts from.
220
221 way_t to The way that the restriction finished on.
222
223 node_t via The node that the turn restriction passes through.
224
225 TurnRestriction restriction The type of restriction.
226
227 transports_t except The set of transports allowed to bypass the restriction.
228 ++++++++++++++++++++++++++++++++++++++*/
229
230 void AppendTurnRestrictRelation(RelationsX* relationsx,relation_t id,
231 way_t from,way_t to,node_t via,
232 TurnRestriction restriction,transports_t except)
233 {
234 TurnRestrictRelX relationx;
235
236 relationx.id=id;
237 relationx.from=from;
238 relationx.to=to;
239 relationx.via=via;
240 relationx.restrict=restriction;
241 relationx.except=except;
242
243 WriteFile(relationsx->trfd,&relationx,sizeof(TurnRestrictRelX));
244
245 relationsx->trxnumber++;
246
247 assert(!(relationsx->trxnumber==0)); /* Zero marks the high-water mark for relations. */
248 }
249
250
251 /*++++++++++++++++++++++++++++++++++++++
252 Sort the list of relations.
253
254 RelationsX* relationsx The set of relations to process.
255 ++++++++++++++++++++++++++++++++++++++*/
256
257 void SortRelationList(RelationsX* relationsx)
258 {
259 /* Close the files (finished appending) */
260
261 CloseFile(relationsx->rfd);
262
263 CloseFile(relationsx->trfd);
264
265
266 /* Route Relations */
267
268
269 /* Turn Restriction Relations. */
270
271 if(relationsx->trxnumber)
272 {
273 int trfd;
274
275 /* Print the start message */
276
277 printf_first("Sorting Turn Restriction Relations");
278
279 /* Re-open the file read-only and a new file writeable */
280
281 relationsx->trfd=ReOpenFile(relationsx->trfilename);
282
283 DeleteFile(relationsx->trfilename);
284
285 trfd=OpenFileNew(relationsx->trfilename);
286
287 /* Sort the relations */
288
289 sortrelationsx=relationsx;
290
291 filesort_fixed(relationsx->trfd,trfd,sizeof(TurnRestrictRelX),(int (*)(const void*,const void*))sort_by_id,(int (*)(void*,index_t))deduplicate_by_id);
292
293 /* Close the files */
294
295 CloseFile(relationsx->trfd);
296 CloseFile(trfd);
297
298 /* Print the final message */
299
300 printf_last("Sorted Relations: Relations=%d Duplicates=%d",relationsx->trxnumber,relationsx->trxnumber-relationsx->trnumber);
301 }
302 }
303
304
305 /*++++++++++++++++++++++++++++++++++++++
306 Sort the turn restriction relations into id order.
307
308 int sort_by_id Returns the comparison of the id fields.
309
310 TurnRestrictRelX *a The first extended relation.
311
312 TurnRestrictRelX *b The second extended relation.
313 ++++++++++++++++++++++++++++++++++++++*/
314
315 static int sort_by_id(TurnRestrictRelX *a,TurnRestrictRelX *b)
316 {
317 relation_t a_id=a->id;
318 relation_t b_id=b->id;
319
320 if(a_id<b_id)
321 return(-1);
322 else if(a_id>b_id)
323 return(1);
324 else
325 return(0);
326 }
327
328
329 /*++++++++++++++++++++++++++++++++++++++
330 Deduplicate the extended relations using the id after sorting.
331
332 int deduplicate_by_id Return 1 if the value is to be kept, otherwise zero.
333
334 TurnRestrictRelX *relationx The extended relation.
335
336 index_t index The index of this relation in the total.
337 ++++++++++++++++++++++++++++++++++++++*/
338
339 static int deduplicate_by_id(TurnRestrictRelX *relationx,index_t index)
340 {
341 static relation_t previd;
342
343 if(index==0 || relationx->id!=previd)
344 {
345 previd=relationx->id;
346
347 sortrelationsx->trnumber++;
348
349 return(1);
350 }
351
352 return(0);
353 }
354
355
356 /*++++++++++++++++++++++++++++++++++++++
357 Sort the list of turn relations.
358
359 RelationsX* relationsx The set of relations to process.
360 ++++++++++++++++++++++++++++++++++++++*/
361
362 void SortTurnRelationList(RelationsX* relationsx)
363 {
364 int trfd;
365
366 if(relationsx->trnumber==0)
367 return;
368
369 /* Print the start message */
370
371 printf_first("Sorting Turn Restriction Relations");
372
373 /* Re-open the file read-only and a new file writeable */
374
375 relationsx->trfd=ReOpenFile(relationsx->trfilename);
376
377 DeleteFile(relationsx->trfilename);
378
379 trfd=OpenFileNew(relationsx->trfilename);
380
381 /* Sort the relations */
382
383 filesort_fixed(relationsx->trfd,trfd,sizeof(TurnRestrictRelX),(int (*)(const void*,const void*))sort_by_via,NULL);
384
385 /* Close the files */
386
387 CloseFile(relationsx->trfd);
388 CloseFile(trfd);
389
390 /* Print the final message */
391
392 printf_last("Sorted Relations: Relations=%d",relationsx->trnumber);
393 }
394
395
396 /*++++++++++++++++++++++++++++++++++++++
397 Sort the turn restriction relations into via node order.
398
399 int sort_by_via Returns the comparison of the via fields.
400
401 TurnRestrictRelX *a The first extended relation.
402
403 TurnRestrictRelX *b The second extended relation.
404 ++++++++++++++++++++++++++++++++++++++*/
405
406 static int sort_by_via(TurnRestrictRelX *a,TurnRestrictRelX *b)
407 {
408 node_t a_id=a->via;
409 node_t b_id=b->via;
410
411 if(a_id<b_id)
412 return(-1);
413 else if(a_id>b_id)
414 return(1);
415 else
416 {
417 node_t a_id=a->from;
418 node_t b_id=b->from;
419
420 if(a_id<b_id)
421 return(-1);
422 else if(a_id>b_id)
423 return(1);
424 else
425 {
426 node_t a_id=a->to;
427 node_t b_id=b->to;
428
429 if(a_id<b_id)
430 return(-1);
431 else if(a_id>b_id)
432 return(1);
433 else
434 return(0);
435 }
436 }
437 }
438
439
440 /*++++++++++++++++++++++++++++++++++++++
441 Process the route relations and apply the information to the ways.
442
443 RelationsX *relationsx The set of relations to process.
444
445 WaysX *waysx The set of ways to update.
446 ++++++++++++++++++++++++++++++++++++++*/
447
448 void ProcessRouteRelations(RelationsX *relationsx,WaysX *waysx)
449 {
450 RouteRelX *unmatched=NULL,*lastunmatched=NULL;
451 int nunmatched=0,lastnunmatched=0,iteration=0;
452
453 if(waysx->number==0)
454 return;
455
456 /* Map into memory / open the files */
457
458 #if !SLIM
459 waysx->xdata=MapFileWriteable(waysx->filename);
460 #else
461 waysx->fd=ReOpenFileWriteable(waysx->filename);
462 #endif
463
464 /* Re-open the file read-only */
465
466 relationsx->rfd=ReOpenFile(relationsx->rfilename);
467
468 /* Read through the file. */
469
470 do
471 {
472 int ways=0,relations=0;
473 int i;
474
475 SeekFile(relationsx->rfd,0);
476
477 /* Print the start message */
478
479 printf_first("Processing Route Relations: Iteration=%d Relations=0 Modified Ways=0",iteration);
480
481 for(i=0;i<relationsx->rxnumber;i++)
482 {
483 FILESORT_VARINT size;
484 RouteRelX relationx;
485 way_t wayid;
486 relation_t relationid;
487 transports_t routes=Transports_None;
488
489 /* Read each route relation */
490
491 ReadFile(relationsx->rfd,&size,FILESORT_VARSIZE);
492 ReadFile(relationsx->rfd,&relationx,sizeof(RouteRelX));
493
494 /* Decide what type of route it is */
495
496 if(iteration==0)
497 {
498 relations++;
499 routes=relationx.routes;
500 }
501 else
502 {
503 int j;
504
505 for(j=0;j<lastnunmatched;j++)
506 if(lastunmatched[j].id==relationx.id)
507 {
508 relations++;
509
510 if((lastunmatched[j].routes|relationx.routes)==relationx.routes)
511 routes=0; /* Nothing new to add */
512 else
513 routes=lastunmatched[j].routes;
514 break;
515 }
516 }
517
518 /* Loop through the ways */
519
520 do
521 {
522 ReadFile(relationsx->rfd,&wayid,sizeof(way_t));
523
524 /* Update the ways that are listed for the relation */
525
526 if(wayid && routes)
527 {
528 index_t way=IndexWayX(waysx,wayid);
529
530 if(way!=NO_WAY)
531 {
532 WayX *wayx=LookupWayX(waysx,way,1);
533
534 if(routes&Transports_Foot)
535 wayx->way.props|=Properties_FootRoute;
536
537 if(routes&Transports_Bicycle)
538 wayx->way.props|=Properties_BicycleRoute;
539
540 PutBackWayX(waysx,way,1);
541
542 ways++;
543 }
544 }
545 }
546 while(wayid);
547
548 /* Loop through the relations */
549
550 do
551 {
552 ReadFile(relationsx->rfd,&relationid,sizeof(relation_t));
553
554 /* Add the relations that are listed for this relation to the list for next time */
555
556 if(relationid && routes && relationid!=relationx.id)
557 {
558 if(nunmatched%256==0)
559 unmatched=(RouteRelX*)realloc((void*)unmatched,(nunmatched+256)*sizeof(RouteRelX));
560
561 unmatched[nunmatched].id=relationid;
562 unmatched[nunmatched].routes=routes;
563
564 nunmatched++;
565 }
566 }
567 while(relationid);
568
569 if(!((i+1)%10000))
570 printf_middle("Processing Route Relations: Iteration=%d Relations=%d Modified Ways=%d",iteration,relations,ways);
571 }
572
573 if(lastunmatched)
574 free(lastunmatched);
575
576 lastunmatched=unmatched;
577 lastnunmatched=nunmatched;
578
579 unmatched=NULL;
580 nunmatched=0;
581
582 /* Print the final message */
583
584 printf_last("Processed Route Relations: Iteration=%d Relations=%d Modified Ways=%d",iteration,relations,ways);
585 }
586 while(lastnunmatched && ++iteration<5);
587
588 if(lastunmatched)
589 free(lastunmatched);
590
591 /* Close the file */
592
593 CloseFile(relationsx->rfd);
594
595 /* Unmap from memory / close the files */
596
597 #if !SLIM
598 waysx->xdata=UnmapFile(waysx->filename);
599 #else
600 CloseFile(waysx->fd);
601 #endif
602 }
603
604
605 /*++++++++++++++++++++++++++++++++++++++
606 Process the turn relations (first part) to update them with the node information.
607
608 RelationsX *relationsx The set of relations to process.
609
610 NodesX *nodesx The set of nodes to process.
611
612 SegmentsX *segmentsx The set of segments to process.
613
614 WaysX *waysx The set of ways to process.
615 ++++++++++++++++++++++++++++++++++++++*/
616
617 void ProcessTurnRelations1(RelationsX *relationsx,NodesX *nodesx,SegmentsX *segmentsx,WaysX *waysx)
618 {
619 TurnRestrictRelX relationx;
620 int trfd;
621 int total=0;
622
623 if(nodesx->number==0 || segmentsx->number==0)
624 return;
625
626 /* Print the start message */
627
628 printf_first("Processing Turn Restriction Relations (1): Turn Relations=0");
629
630 /* Map into memory / open the files */
631
632 #if !SLIM
633 nodesx->xdata=MapFileWriteable(nodesx->filename);
634 segmentsx->xdata=MapFile(segmentsx->filename);
635 #else
636 nodesx->fd=ReOpenFileWriteable(nodesx->filename);
637 segmentsx->fd=ReOpenFile(segmentsx->filename);
638 #endif
639
640 /* Re-open the file read-only and a new file writeable */
641
642 relationsx->trfd=ReOpenFile(relationsx->trfilename);
643
644 DeleteFile(relationsx->trfilename);
645
646 trfd=OpenFileNew(relationsx->trfilename);
647
648 /* Process all of the relations */
649
650 while(!ReadFile(relationsx->trfd,&relationx,sizeof(TurnRestrictRelX)))
651 {
652 if(relationx.restrict==TurnRestrict_no_right_turn ||
653 relationx.restrict==TurnRestrict_no_left_turn ||
654 relationx.restrict==TurnRestrict_no_u_turn ||
655 relationx.restrict==TurnRestrict_no_straight_on)
656 {
657 NodeX *nodex;
658 index_t seg;
659 node_t node_from=NO_NODE,node_to=NO_NODE;
660
661 /* Find the segments that join the node 'via' */
662
663 seg=IndexFirstSegmentX1(segmentsx,relationx.via);
664
665 do
666 {
667 SegmentX *segx=LookupSegmentX(segmentsx,seg,1);
668
669 if(segx->way==relationx.from)
670 {
671 if(node_from!=NO_NODE) /* Only one segment can be on the 'from' way */
672 goto endloop;
673
674 node_from=segx->node2;
675 }
676
677 if(segx->way==relationx.to)
678 {
679 if(node_to!=NO_NODE) /* Only one segment can be on the 'to' way */
680 goto endloop;
681
682 node_to=segx->node2;
683 }
684
685 seg=IndexNextSegmentX1(segmentsx,seg,relationx.via);
686 }
687 while(seg!=NO_SEGMENT);
688
689 if(node_to==NO_NODE || node_from==NO_NODE) /* Not enough segments for the selected ways */
690 goto endloop;
691
692 /* Write the results */
693
694 relationx.from=IndexNodeX(nodesx,node_from);
695 relationx.to =IndexNodeX(nodesx,node_to);
696 relationx.via =IndexNodeX(nodesx,relationx.via);
697
698 WriteFile(trfd,&relationx,sizeof(TurnRestrictRelX));
699
700 nodex=LookupNodeX(nodesx,relationx.from,1);
701 nodex->flags|=NODE_TURNRSTRCT2;
702 PutBackNodeX(nodesx,relationx.from,1);
703
704 nodex=LookupNodeX(nodesx,relationx.via,1);
705 nodex->flags|=NODE_TURNRSTRCT;
706 PutBackNodeX(nodesx,relationx.via,1);
707
708 nodex=LookupNodeX(nodesx,relationx.to,1);
709 nodex->flags|=NODE_TURNRSTRCT2;
710 PutBackNodeX(nodesx,relationx.to,1);
711
712 total++;
713
714 if(!(total%10000))
715 printf_middle("Processing Turn Restriction Relations (1): Turn Relations=%d New=%d",total,total-relationsx->trnumber);
716 }
717 else
718 {
719 NodeX *nodex;
720 index_t seg;
721 node_t node_from=NO_NODE,node_to[8];
722 int nnodes_to=0,i;
723
724 /* Find the segments that join the node 'via' */
725
726 seg=IndexFirstSegmentX1(segmentsx,relationx.via);
727
728 do
729 {
730 SegmentX *segx=LookupSegmentX(segmentsx,seg,1);
731
732 if(segx->way==relationx.from)
733 {
734 if(node_from!=NO_NODE) /* Only one segment can be on the 'from' way */
735 goto endloop;
736
737 node_from=segx->node2;
738 }
739
740 if(segx->way!=relationx.to)
741 {
742 if(nnodes_to==8) /* Too many segments (arbitrary choice) */
743 goto endloop;
744
745 node_to[nnodes_to++]=segx->node2;
746 }
747
748 seg=IndexNextSegmentX1(segmentsx,seg,relationx.via);
749 }
750 while(seg!=NO_SEGMENT);
751
752 if(nnodes_to==0 || node_from==NO_NODE) /* Not enough segments for the selected ways */
753 goto endloop;
754
755 /* Write the results */
756
757 relationx.via=IndexNodeX(nodesx,relationx.via);
758
759 for(i=0;i<nnodes_to;i++)
760 {
761 if(node_to[i]==node_from)
762 continue;
763
764 relationx.from=IndexNodeX(nodesx,node_from);
765 relationx.to =IndexNodeX(nodesx,node_to[i]);
766
767 WriteFile(trfd,&relationx,sizeof(TurnRestrictRelX));
768
769 nodex=LookupNodeX(nodesx,relationx.to,1);
770 nodex->flags|=NODE_TURNRSTRCT2;
771 PutBackNodeX(nodesx,relationx.to,1);
772
773 total++;
774
775 if(!(total%10000))
776 printf_middle("Processing Turn Restriction Relations (1): Turn Relations=%d New=%d",total,total-relationsx->trnumber);
777 }
778
779 nodex=LookupNodeX(nodesx,relationx.from,1);
780 nodex->flags|=NODE_TURNRSTRCT2;
781 PutBackNodeX(nodesx,relationx.from,1);
782
783 nodex=LookupNodeX(nodesx,relationx.via,1);
784 nodex->flags|=NODE_TURNRSTRCT;
785 PutBackNodeX(nodesx,relationx.via,1);
786 }
787
788 endloop: ;
789 }
790
791 /* Close the files */
792
793 CloseFile(relationsx->trfd);
794 CloseFile(trfd);
795
796 /* Unmap from memory / close the files */
797
798 #if !SLIM
799 nodesx->xdata=UnmapFile(nodesx->filename);
800 segmentsx->xdata=UnmapFile(segmentsx->filename);
801 #else
802 CloseFile(nodesx->fd);
803 CloseFile(segmentsx->fd);
804 #endif
805
806 /* Print the final message */
807
808 printf_last("Processing Turn Restriction Relations (1): Turn Relations=%d New=%d",total,total-relationsx->trnumber);
809
810 relationsx->trnumber=total;
811 }
812
813
814 /*++++++++++++++++++++++++++++++++++++++
815 Process the turn relations (second part) to update them with the re-ordered node information.
816
817 RelationsX *relationsx The set of relations to process.
818
819 NodesX *nodesx The set of nodes to process.
820 ++++++++++++++++++++++++++++++++++++++*/
821
822 void ProcessTurnRelations2(RelationsX *relationsx,NodesX *nodesx)
823 {
824 int trfd;
825 int i;
826
827 /* Print the start message */
828
829 printf_first("Processing Turn Restriction Relations (2): Turn Relations=0");
830
831 /* Re-open the file read-only and a new file writeable */
832
833 relationsx->trfd=ReOpenFile(relationsx->trfilename);
834
835 DeleteFile(relationsx->trfilename);
836
837 trfd=OpenFileNew(relationsx->trfilename);
838
839 /* Process all of the relations */
840
841 for(i=0;i<relationsx->trnumber;i++)
842 {
843 TurnRestrictRelX relationx;
844
845 ReadFile(relationsx->trfd,&relationx,sizeof(TurnRestrictRelX));
846
847 relationx.from=nodesx->gdata[relationx.from];
848
849 relationx.via=nodesx->gdata[relationx.via];
850
851 relationx.to=nodesx->gdata[relationx.to];
852
853 WriteFile(trfd,&relationx,sizeof(TurnRestrictRelX));
854
855 if(!(relationsx->trnumber%10000))
856 printf_middle("Processing Turn Restriction Relations (2): Turn Relations=%d",relationsx->trnumber);
857 }
858
859 /* Close the files */
860
861 CloseFile(relationsx->trfd);
862 CloseFile(trfd);
863
864 /* Print the final message */
865
866 printf_last("Processing Turn Restriction Relations (2): Turn Relations=%d",relationsx->trnumber);
867 }
868
869
870 /*++++++++++++++++++++++++++++++++++++++
871 Save the relation list to a file.
872
873 RelationsX* relationsx The set of relations to save.
874
875 const char *filename The name of the file to save.
876 ++++++++++++++++++++++++++++++++++++++*/
877
878 void SaveRelationList(RelationsX* relationsx,const char *filename)
879 {
880 index_t i;
881 int fd;
882 RelationsFile relationsfile={0};
883
884 /* Print the start message */
885
886 printf_first("Writing Relations: Turn Relations=0");
887
888 /* Re-open the file read-only */
889
890 relationsx->trfd=ReOpenFile(relationsx->trfilename);
891
892 /* Write out the relations data */
893
894 fd=OpenFileNew(filename);
895
896 SeekFile(fd,sizeof(RelationsFile));
897
898 for(i=0;i<relationsx->trnumber;i++)
899 {
900 TurnRestrictRelX relationx;
901 TurnRelation relation;
902
903 ReadFile(relationsx->trfd,&relationx,sizeof(TurnRestrictRelX));
904
905 relation.from=relationx.from;
906 relation.via=relationx.via;
907 relation.to=relationx.to;
908 relation.except=relationx.except;
909
910 WriteFile(fd,&relation,sizeof(TurnRelation));
911
912 if(!((i+1)%10000))
913 printf_middle("Writing Relations: Turn Relations=%d",i+1);
914 }
915
916 /* Write out the header structure */
917
918 relationsfile.trnumber=relationsx->trnumber;
919
920 SeekFile(fd,0);
921 WriteFile(fd,&relationsfile,sizeof(RelationsFile));
922
923 CloseFile(fd);
924
925 /* Close the file */
926
927 CloseFile(relationsx->trfd);
928
929 /* Print the final message */
930
931 printf_last("Wrote Relations: Turn Relations=%d",relationsx->trnumber);
932 }