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 786 - (show annotations) (download) (as text)
Sat Jun 18 10:37:45 2011 UTC (13 years, 9 months ago) by amb
File MIME type: text/x-csrc
File size: 25956 byte(s)
Rename structure element "restrict" to "restriction" to avoid C99 error
(reserved word).

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 /* Local 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->rnumber++;
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->trnumber=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 set of relations to be freed.
139
140 int keep Set to 1 if the file is to be kept (for appending later).
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->rnumber++;
207
208 assert(!(relationsx->rnumber==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.restriction=restriction;
241 relationx.except=except;
242
243 WriteFile(relationsx->trfd,&relationx,sizeof(TurnRestrictRelX));
244
245 relationsx->trnumber++;
246
247 assert(!(relationsx->trnumber==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 relationsx->rfd=CloseFile(relationsx->rfd);
262
263 relationsx->trfd=CloseFile(relationsx->trfd);
264
265
266 /* Route Relations */
267
268
269 /* Turn Restriction Relations. */
270
271 if(relationsx->trnumber)
272 {
273 index_t trxnumber;
274 int trfd;
275
276 /* Print the start message */
277
278 printf_first("Sorting Turn Restriction Relations");
279
280 /* Re-open the file read-only and a new file writeable */
281
282 relationsx->trfd=ReOpenFile(relationsx->trfilename);
283
284 DeleteFile(relationsx->trfilename);
285
286 trfd=OpenFileNew(relationsx->trfilename);
287
288 /* Sort the relations */
289
290 trxnumber=relationsx->trnumber;
291 relationsx->trnumber=0;
292
293 sortrelationsx=relationsx;
294
295 filesort_fixed(relationsx->trfd,trfd,sizeof(TurnRestrictRelX),(int (*)(const void*,const void*))sort_by_id,(int (*)(void*,index_t))deduplicate_by_id);
296
297 /* Close the files */
298
299 relationsx->trfd=CloseFile(relationsx->trfd);
300 CloseFile(trfd);
301
302 /* Print the final message */
303
304 printf_last("Sorted Relations: Relations=%d Duplicates=%d",trxnumber,trxnumber-relationsx->trnumber);
305 }
306 }
307
308
309 /*++++++++++++++++++++++++++++++++++++++
310 Sort the turn restriction relations into id order.
311
312 int sort_by_id Returns the comparison of the id fields.
313
314 TurnRestrictRelX *a The first extended relation.
315
316 TurnRestrictRelX *b The second extended relation.
317 ++++++++++++++++++++++++++++++++++++++*/
318
319 static int sort_by_id(TurnRestrictRelX *a,TurnRestrictRelX *b)
320 {
321 relation_t a_id=a->id;
322 relation_t b_id=b->id;
323
324 if(a_id<b_id)
325 return(-1);
326 else if(a_id>b_id)
327 return(1);
328 else
329 return(0);
330 }
331
332
333 /*++++++++++++++++++++++++++++++++++++++
334 Deduplicate the extended relations using the id after sorting.
335
336 int deduplicate_by_id Return 1 if the value is to be kept, otherwise 0.
337
338 TurnRestrictRelX *relationx The extended relation.
339
340 index_t index The index of this relation in the total.
341 ++++++++++++++++++++++++++++++++++++++*/
342
343 static int deduplicate_by_id(TurnRestrictRelX *relationx,index_t index)
344 {
345 static relation_t previd;
346
347 if(index==0 || relationx->id!=previd)
348 {
349 previd=relationx->id;
350
351 sortrelationsx->trnumber++;
352
353 return(1);
354 }
355
356 return(0);
357 }
358
359
360 /*++++++++++++++++++++++++++++++++++++++
361 Sort the list of turn relations.
362
363 RelationsX* relationsx The set of relations to process.
364 ++++++++++++++++++++++++++++++++++++++*/
365
366 void SortTurnRelationList(RelationsX* relationsx)
367 {
368 int trfd;
369
370 if(relationsx->trnumber==0)
371 return;
372
373 /* Print the start message */
374
375 printf_first("Sorting Turn Restriction Relations");
376
377 /* Re-open the file read-only and a new file writeable */
378
379 relationsx->trfd=ReOpenFile(relationsx->trfilename);
380
381 DeleteFile(relationsx->trfilename);
382
383 trfd=OpenFileNew(relationsx->trfilename);
384
385 /* Sort the relations */
386
387 filesort_fixed(relationsx->trfd,trfd,sizeof(TurnRestrictRelX),(int (*)(const void*,const void*))sort_by_via,NULL);
388
389 /* Close the files */
390
391 relationsx->trfd=CloseFile(relationsx->trfd);
392 CloseFile(trfd);
393
394 /* Print the final message */
395
396 printf_last("Sorted Relations: Relations=%d",relationsx->trnumber);
397 }
398
399
400 /*++++++++++++++++++++++++++++++++++++++
401 Sort the turn restriction relations into via index order (then by from and to segments).
402
403 int sort_by_via Returns the comparison of the via, from and to fields.
404
405 TurnRestrictRelX *a The first extended relation.
406
407 TurnRestrictRelX *b The second extended relation.
408 ++++++++++++++++++++++++++++++++++++++*/
409
410 static int sort_by_via(TurnRestrictRelX *a,TurnRestrictRelX *b)
411 {
412 index_t a_id=a->via;
413 index_t b_id=b->via;
414
415 if(a_id<b_id)
416 return(-1);
417 else if(a_id>b_id)
418 return(1);
419 else
420 {
421 index_t a_id=a->from;
422 index_t b_id=b->from;
423
424 if(a_id<b_id)
425 return(-1);
426 else if(a_id>b_id)
427 return(1);
428 else
429 {
430 index_t a_id=a->to;
431 index_t b_id=b->to;
432
433 if(a_id<b_id)
434 return(-1);
435 else if(a_id>b_id)
436 return(1);
437 else
438 return(0);
439 }
440 }
441 }
442
443
444 /*++++++++++++++++++++++++++++++++++++++
445 Process the route relations and apply the information to the ways.
446
447 RelationsX *relationsx The set of relations to use.
448
449 WaysX *waysx The set of ways to modify.
450 ++++++++++++++++++++++++++++++++++++++*/
451
452 void ProcessRouteRelations(RelationsX *relationsx,WaysX *waysx)
453 {
454 RouteRelX *unmatched=NULL,*lastunmatched=NULL;
455 int nunmatched=0,lastnunmatched=0,iteration=1;
456
457 if(waysx->number==0)
458 return;
459
460 /* Map into memory / open the files */
461
462 #if !SLIM
463 waysx->data=MapFileWriteable(waysx->filename);
464 #else
465 waysx->fd=ReOpenFileWriteable(waysx->filename);
466 #endif
467
468 /* Re-open the file read-only */
469
470 relationsx->rfd=ReOpenFile(relationsx->rfilename);
471
472 /* Read through the file. */
473
474 do
475 {
476 int ways=0,relations=0;
477 index_t i;
478
479 SeekFile(relationsx->rfd,0);
480
481 /* Print the start message */
482
483 printf_first("Processing Route Relations (%d): Relations=0 Modified Ways=0",iteration);
484
485 for(i=0;i<relationsx->rnumber;i++)
486 {
487 FILESORT_VARINT size;
488 RouteRelX relationx;
489 way_t wayid;
490 relation_t relationid;
491 transports_t routes=Transports_None;
492
493 /* Read each route relation */
494
495 ReadFile(relationsx->rfd,&size,FILESORT_VARSIZE);
496 ReadFile(relationsx->rfd,&relationx,sizeof(RouteRelX));
497
498 /* Decide what type of route it is */
499
500 if(iteration==1)
501 {
502 relations++;
503 routes=relationx.routes;
504 }
505 else
506 {
507 int j;
508
509 for(j=0;j<lastnunmatched;j++)
510 if(lastunmatched[j].id==relationx.id)
511 {
512 relations++;
513
514 if((lastunmatched[j].routes|relationx.routes)==relationx.routes)
515 routes=0; /* Nothing new to add */
516 else
517 routes=lastunmatched[j].routes;
518 break;
519 }
520 }
521
522 /* Loop through the ways */
523
524 do
525 {
526 ReadFile(relationsx->rfd,&wayid,sizeof(way_t));
527
528 /* Update the ways that are listed for the relation */
529
530 if(wayid && routes)
531 {
532 index_t way=IndexWayX(waysx,wayid);
533
534 if(way!=NO_WAY)
535 {
536 WayX *wayx=LookupWayX(waysx,way,1);
537
538 if(routes&Transports_Foot)
539 wayx->way.props|=Properties_FootRoute;
540
541 if(routes&Transports_Bicycle)
542 wayx->way.props|=Properties_BicycleRoute;
543
544 PutBackWayX(waysx,way,1);
545
546 ways++;
547 }
548 }
549 }
550 while(wayid);
551
552 /* Loop through the relations */
553
554 do
555 {
556 ReadFile(relationsx->rfd,&relationid,sizeof(relation_t));
557
558 /* Add the relations that are listed for this relation to the list for next time */
559
560 if(relationid && routes && relationid!=relationx.id)
561 {
562 if(nunmatched%256==0)
563 unmatched=(RouteRelX*)realloc((void*)unmatched,(nunmatched+256)*sizeof(RouteRelX));
564
565 unmatched[nunmatched].id=relationid;
566 unmatched[nunmatched].routes=routes;
567
568 nunmatched++;
569 }
570 }
571 while(relationid);
572
573 if(!((i+1)%1000))
574 printf_middle("Processing Route Relations (%d): Relations=%d Modified Ways=%d",iteration,relations,ways);
575 }
576
577 if(lastunmatched)
578 free(lastunmatched);
579
580 lastunmatched=unmatched;
581 lastnunmatched=nunmatched;
582
583 unmatched=NULL;
584 nunmatched=0;
585
586 /* Print the final message */
587
588 printf_last("Processed Route Relations (%d): Relations=%d Modified Ways=%d",iteration,relations,ways);
589 }
590 while(lastnunmatched && iteration++<5);
591
592 if(lastunmatched)
593 free(lastunmatched);
594
595 /* Close the file */
596
597 relationsx->rfd=CloseFile(relationsx->rfd);
598
599 /* Unmap from memory / close the files */
600
601 #if !SLIM
602 waysx->data=UnmapFile(waysx->filename);
603 #else
604 waysx->fd=CloseFile(waysx->fd);
605 #endif
606 }
607
608
609 /*++++++++++++++++++++++++++++++++++++++
610 Process the turn relations (first part) to update them with the node/way information.
611
612 RelationsX *relationsx The set of relations to modify.
613
614 NodesX *nodesx The set of nodes to use.
615
616 WaysX *waysx The set of ways to use.
617 ++++++++++++++++++++++++++++++++++++++*/
618
619 void ProcessTurnRelations1(RelationsX *relationsx,NodesX *nodesx,WaysX *waysx)
620 {
621 int trfd;
622 index_t i,deleted=0;
623
624 /* Print the start message */
625
626 printf_first("Processing Turn Relations (1): Relations=0");
627
628 /* Re-open the file read-only and a new file writeable */
629
630 relationsx->trfd=ReOpenFile(relationsx->trfilename);
631
632 DeleteFile(relationsx->trfilename);
633
634 trfd=OpenFileNew(relationsx->trfilename);
635
636 /* Process all of the relations */
637
638 for(i=0;i<relationsx->trnumber;i++)
639 {
640 TurnRestrictRelX relationx;
641
642 ReadFile(relationsx->trfd,&relationx,sizeof(TurnRestrictRelX));
643
644 relationx.via =IndexNodeX(nodesx,relationx.via);
645 relationx.from=IndexWayX(waysx,relationx.from);
646 relationx.to =IndexWayX(waysx,relationx.to);
647
648 if(relationx.via==NO_NODE || relationx.from==NO_WAY || relationx.to==NO_WAY)
649 deleted++;
650 else
651 WriteFile(trfd,&relationx,sizeof(TurnRestrictRelX));
652
653 if(!((i+1)%1000))
654 printf_middle("Processing Turn Relations (1): Relations=%d Deleted=%d",i+1-deleted,deleted);
655 }
656
657 /* Close the files */
658
659 relationsx->trfd=CloseFile(relationsx->trfd);
660 CloseFile(trfd);
661
662 /* Print the final message */
663
664 printf_last("Processed Turn Relations (1): Relations=%d Deleted=%d",relationsx->trnumber-deleted,deleted);
665
666 relationsx->trnumber-=deleted;
667 }
668
669
670 /*++++++++++++++++++++++++++++++++++++++
671 Process the turn relations (second part) to convert them to nodes.
672
673 RelationsX *relationsx The set of relations to modify.
674
675 NodesX *nodesx The set of nodes to use.
676
677 SegmentsX *segmentsx The set of segments to use.
678
679 WaysX *waysx The set of ways to use.
680 ++++++++++++++++++++++++++++++++++++++*/
681
682 void ProcessTurnRelations2(RelationsX *relationsx,NodesX *nodesx,SegmentsX *segmentsx,WaysX *waysx)
683 {
684 TurnRestrictRelX relationx;
685 int trfd;
686 index_t total=0,deleted=0;
687
688 if(nodesx->number==0 || segmentsx->number==0)
689 return;
690
691 /* Print the start message */
692
693 printf_first("Processing Turn Relations (2): Relations=0");
694
695 /* Map into memory / open the files */
696
697 #if !SLIM
698 nodesx->data=MapFileWriteable(nodesx->filename);
699 segmentsx->data=MapFile(segmentsx->filename);
700 #else
701 nodesx->fd=ReOpenFileWriteable(nodesx->filename);
702 segmentsx->fd=ReOpenFile(segmentsx->filename);
703 #endif
704
705 /* Re-open the file read-only and a new file writeable */
706
707 relationsx->trfd=ReOpenFile(relationsx->trfilename);
708
709 DeleteFile(relationsx->trfilename);
710
711 trfd=OpenFileNew(relationsx->trfilename);
712
713 /* Process all of the relations */
714
715 while(!ReadFile(relationsx->trfd,&relationx,sizeof(TurnRestrictRelX)))
716 {
717 NodeX *nodex;
718 SegmentX *segmentx;
719
720 if(relationx.restriction==TurnRestrict_no_right_turn ||
721 relationx.restriction==TurnRestrict_no_left_turn ||
722 relationx.restriction==TurnRestrict_no_u_turn ||
723 relationx.restriction==TurnRestrict_no_straight_on)
724 {
725 index_t node_from=NO_NODE,node_to=NO_NODE;
726
727 /* Find the segments that join the node 'via' */
728
729 segmentx=FirstSegmentX(segmentsx,relationx.via,1);
730
731 while(segmentx)
732 {
733 if(segmentx->way==relationx.from)
734 {
735 if(node_from!=NO_NODE) /* Only one segment can be on the 'from' way */
736 {
737 deleted++;
738 goto endloop;
739 }
740
741 node_from=OtherNode(segmentx,relationx.via);
742 }
743
744 if(segmentx->way==relationx.to)
745 {
746 if(node_to!=NO_NODE) /* Only one segment can be on the 'to' way */
747 {
748 deleted++;
749 goto endloop;
750 }
751
752 /* Don't bother with restrictions banning going the wrong way down a one-way road */
753
754 if(!IsOnewayTo(segmentx,relationx.via))
755 node_to=OtherNode(segmentx,relationx.via);
756 }
757
758 segmentx=NextSegmentX(segmentsx,segmentx,relationx.via,1);
759 }
760
761 if(node_to==NO_NODE || node_from==NO_NODE) /* Not enough segments for the selected ways */
762 {
763 deleted++;
764 goto endloop;
765 }
766
767 /* Write the results */
768
769 relationx.from=node_from;
770 relationx.to =node_to;
771
772 WriteFile(trfd,&relationx,sizeof(TurnRestrictRelX));
773
774 total++;
775
776 if(!(total%1000))
777 printf_middle("Processing Turn Relations (2): Relations=%d Deleted=%d Added=%d",total,deleted,total-relationsx->trnumber+deleted);
778 }
779 else
780 {
781 index_t node_from=NO_NODE,node_to[MAX_SEG_PER_NODE];
782 int nnodes_to=0,i;
783
784 /* Find the segments that join the node 'via' */
785
786 segmentx=FirstSegmentX(segmentsx,relationx.via,1);
787
788 while(segmentx)
789 {
790 if(segmentx->way==relationx.from)
791 {
792 if(node_from!=NO_NODE) /* Only one segment can be on the 'from' way */
793 {
794 deleted++;
795 goto endloop;
796 }
797
798 node_from=OtherNode(segmentx,relationx.via);
799 }
800
801 if(segmentx->way!=relationx.to)
802 {
803 assert(nnodes_to<MAX_SEG_PER_NODE); /* Only a limited amount of information stored. */
804
805 /* Don't bother with restrictions banning going the wrong way down a one-way road */
806
807 if(!IsOnewayTo(segmentx,relationx.via))
808 node_to[nnodes_to++]=OtherNode(segmentx,relationx.via);
809 }
810
811 segmentx=NextSegmentX(segmentsx,segmentx,relationx.via,1);
812 }
813
814 if(nnodes_to==0 || node_from==NO_NODE) /* Not enough segments for the selected ways */
815 {
816 deleted++;
817 goto endloop;
818 }
819
820 /* Write the results */
821
822 for(i=0;i<nnodes_to;i++)
823 {
824 if(node_to[i]==node_from)
825 continue;
826
827 relationx.from=node_from;
828 relationx.to =node_to[i];
829
830 WriteFile(trfd,&relationx,sizeof(TurnRestrictRelX));
831
832 total++;
833
834 if(!(total%1000))
835 printf_middle("Processing Turn Relations (2): Relations=%d Deleted=%d Added=%d",total,deleted,total-relationsx->trnumber+deleted);
836 }
837 }
838
839 /* Force super nodes on via node and adjacent nodes */
840
841 nodex=LookupNodeX(nodesx,relationx.via,1);
842 nodex->flags|=NODE_TURNRSTRCT;
843 PutBackNodeX(nodesx,relationx.via,1);
844
845 segmentx=FirstSegmentX(segmentsx,relationx.via,1);
846
847 while(segmentx)
848 {
849 index_t othernode=OtherNode(segmentx,relationx.via);
850
851 nodex=LookupNodeX(nodesx,othernode,1);
852 nodex->flags|=NODE_TURNRSTRCT2;
853 PutBackNodeX(nodesx,othernode,1);
854
855 segmentx=NextSegmentX(segmentsx,segmentx,relationx.via,1);
856 }
857
858 endloop: ;
859 }
860
861 /* Close the files */
862
863 relationsx->trfd=CloseFile(relationsx->trfd);
864 CloseFile(trfd);
865
866 /* Unmap from memory / close the files */
867
868 #if !SLIM
869 nodesx->data=UnmapFile(nodesx->filename);
870 segmentsx->data=UnmapFile(segmentsx->filename);
871 #else
872 nodesx->fd=CloseFile(nodesx->fd);
873 segmentsx->fd=CloseFile(segmentsx->fd);
874 #endif
875
876 /* Print the final message */
877
878 printf_last("Processed Turn Relations (2): Relations=%d Deleted=%d Added=%d",total,deleted,total-relationsx->trnumber+deleted);
879
880 relationsx->trnumber=total;
881 }
882
883
884 /*++++++++++++++++++++++++++++++++++++++
885 Update the node indexes after geographical sorting.
886
887 RelationsX *relationsx The set of relations to modify.
888
889 NodesX *nodesx The set of nodes to use.
890
891 SegmentsX *segmentsx The set of segments to use.
892 ++++++++++++++++++++++++++++++++++++++*/
893
894 void UpdateTurnRelations(RelationsX *relationsx,NodesX *nodesx,SegmentsX *segmentsx)
895 {
896 int trfd;
897 index_t i;
898
899 /* Print the start message */
900
901 printf_first("Updating Turn Relations: Relations=0");
902
903 /* Map into memory / open the files */
904
905 #if !SLIM
906 segmentsx->data=MapFile(segmentsx->filename);
907 #else
908 segmentsx->fd=ReOpenFile(segmentsx->filename);
909 #endif
910
911 /* Re-open the file read-only and a new file writeable */
912
913 relationsx->trfd=ReOpenFile(relationsx->trfilename);
914
915 DeleteFile(relationsx->trfilename);
916
917 trfd=OpenFileNew(relationsx->trfilename);
918
919 /* Process all of the relations */
920
921 for(i=0;i<relationsx->trnumber;i++)
922 {
923 TurnRestrictRelX relationx;
924 SegmentX *segmentx;
925 index_t from_node,via_node,to_node;
926
927 ReadFile(relationsx->trfd,&relationx,sizeof(TurnRestrictRelX));
928
929 from_node=nodesx->gdata[relationx.from];
930 via_node =nodesx->gdata[relationx.via];
931 to_node =nodesx->gdata[relationx.to];
932
933 segmentx=FirstSegmentX(segmentsx,via_node,1);
934
935 do
936 {
937 if(OtherNode(segmentx,via_node)==from_node)
938 relationx.from=IndexSegmentX(segmentsx,segmentx);
939
940 if(OtherNode(segmentx,via_node)==to_node)
941 relationx.to=IndexSegmentX(segmentsx,segmentx);
942
943 segmentx=NextSegmentX(segmentsx,segmentx,via_node,1);
944 }
945 while(segmentx);
946
947 relationx.via=via_node;
948
949 WriteFile(trfd,&relationx,sizeof(TurnRestrictRelX));
950
951 if(!(relationsx->trnumber%1000))
952 printf_middle("Updating Turn Relations: Relations=%d",relationsx->trnumber);
953 }
954
955 /* Close the files */
956
957 relationsx->trfd=CloseFile(relationsx->trfd);
958 CloseFile(trfd);
959
960 /* Unmap from memory / close the files */
961
962 #if !SLIM
963 segmentsx->data=UnmapFile(segmentsx->filename);
964 #else
965 segmentsx->fd=CloseFile(segmentsx->fd);
966 #endif
967
968 /* Print the final message */
969
970 printf_last("Updated Turn Relations: Relations=%d",relationsx->trnumber);
971 }
972
973
974 /*++++++++++++++++++++++++++++++++++++++
975 Save the relation list to a file.
976
977 RelationsX* relationsx The set of relations to save.
978
979 const char *filename The name of the file to save.
980 ++++++++++++++++++++++++++++++++++++++*/
981
982 void SaveRelationList(RelationsX* relationsx,const char *filename)
983 {
984 index_t i;
985 int fd;
986 RelationsFile relationsfile={0};
987
988 /* Print the start message */
989
990 printf_first("Writing Relations: Turn Relations=0");
991
992 /* Re-open the file read-only */
993
994 relationsx->trfd=ReOpenFile(relationsx->trfilename);
995
996 /* Write out the relations data */
997
998 fd=OpenFileNew(filename);
999
1000 SeekFile(fd,sizeof(RelationsFile));
1001
1002 for(i=0;i<relationsx->trnumber;i++)
1003 {
1004 TurnRestrictRelX relationx;
1005 TurnRelation relation;
1006
1007 ReadFile(relationsx->trfd,&relationx,sizeof(TurnRestrictRelX));
1008
1009 relation.from=relationx.from;
1010 relation.via=relationx.via;
1011 relation.to=relationx.to;
1012 relation.except=relationx.except;
1013
1014 WriteFile(fd,&relation,sizeof(TurnRelation));
1015
1016 if(!((i+1)%1000))
1017 printf_middle("Writing Relations: Turn Relations=%d",i+1);
1018 }
1019
1020 /* Write out the header structure */
1021
1022 relationsfile.trnumber=relationsx->trnumber;
1023
1024 SeekFile(fd,0);
1025 WriteFile(fd,&relationsfile,sizeof(RelationsFile));
1026
1027 CloseFile(fd);
1028
1029 /* Close the file */
1030
1031 relationsx->trfd=CloseFile(relationsx->trfd);
1032
1033 /* Print the final message */
1034
1035 printf_last("Wrote Relations: Turn Relations=%d",relationsx->trnumber);
1036 }