Routino SVN Repository Browser

Check out the latest version of Routino: svn co http://routino.org/svn/trunk routino

ViewVC logotype

Annotation of /trunk/src/relationsx.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 806 - (hide annotations) (download) (as text)
Sun Jul 10 17:53:58 2011 UTC (13 years, 8 months ago) by amb
File MIME type: text/x-csrc
File size: 27997 byte(s)
Check turn relations more carefully and discard them if they are invalid.

1 amb 496 /***************************************
2     Extended Relation data type functions.
3    
4     Part of the Routino routing software.
5     ******************/ /******************
6 amb 597 This file Copyright 2010-2011 Andrew M. Bishop
7 amb 496
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 amb 542 #include "types.h"
30     #include "relations.h"
31    
32     #include "nodesx.h"
33     #include "segmentsx.h"
34 amb 496 #include "waysx.h"
35     #include "relationsx.h"
36    
37     #include "files.h"
38 amb 519 #include "logging.h"
39 amb 532 #include "sorting.h"
40 amb 496
41    
42 amb 680 /* Local functions */
43 amb 540
44 amb 559 static int sort_by_id(TurnRestrictRelX *a,TurnRestrictRelX *b);
45 amb 540 static int deduplicate_by_id(TurnRestrictRelX *relationx,index_t index);
46    
47 amb 559 static int sort_by_via(TurnRestrictRelX *a,TurnRestrictRelX *b);
48 amb 540
49 amb 559
50 amb 496 /* Variables */
51    
52     /*+ The command line '--tmpdir' option or its default value. +*/
53     extern char *option_tmpdirname;
54    
55 amb 540 static RelationsX* sortrelationsx;
56 amb 496
57 amb 540
58 amb 496 /*++++++++++++++++++++++++++++++++++++++
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 amb 540
75 amb 559 /* Route Relations */
76    
77 amb 496 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 amb 788 sprintf(relationsx->rfilename,"%s/relationsx.route.%p.tmp",option_tmpdirname,(void*)relationsx);
83 amb 496
84     if(append)
85     {
86     off_t size,position=0;
87    
88 amb 505 relationsx->rfd=OpenFileAppend(relationsx->rfilename);
89 amb 496
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 amb 650 relationsx->rnumber++;
100 amb 496 position+=relationsize+FILESORT_VARSIZE;
101     }
102    
103     SeekFile(relationsx->rfd,size);
104     }
105     else
106 amb 505 relationsx->rfd=OpenFileNew(relationsx->rfilename);
107 amb 496
108 amb 540
109 amb 559 /* Turn Restriction Relations */
110    
111 amb 540 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 amb 788 sprintf(relationsx->trfilename,"%s/relationsx.turn.%p.tmp",option_tmpdirname,(void*)relationsx);
117 amb 540
118     if(append)
119     {
120     off_t size;
121    
122     relationsx->trfd=OpenFileAppend(relationsx->trfilename);
123    
124     size=SizeFile(relationsx->trfilename);
125    
126 amb 650 relationsx->trnumber=size/sizeof(TurnRestrictRelX);
127 amb 540 }
128     else
129     relationsx->trfd=OpenFileNew(relationsx->trfilename);
130    
131 amb 496 return(relationsx);
132     }
133    
134    
135     /*++++++++++++++++++++++++++++++++++++++
136     Free a relation list.
137    
138 amb 681 RelationsX *relationsx The set of relations to be freed.
139 amb 496
140 amb 680 int keep Set to 1 if the file is to be kept (for appending later).
141 amb 496 ++++++++++++++++++++++++++++++++++++++*/
142    
143     void FreeRelationList(RelationsX *relationsx,int keep)
144     {
145 amb 540 /* Route relations */
146    
147 amb 496 if(!keep)
148     DeleteFile(relationsx->rfilename);
149    
150     free(relationsx->rfilename);
151    
152 amb 559
153 amb 540 /* Turn Restriction relations */
154    
155     if(!keep)
156     DeleteFile(relationsx->trfilename);
157    
158     free(relationsx->trfilename);
159    
160 amb 496 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 amb 529 transports_t routes The types of routes that this relation is for.
172 amb 496
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 amb 540 void AppendRouteRelation(RelationsX* relationsx,relation_t id,
183     transports_t routes,
184 amb 496 way_t *ways,int nways,
185     relation_t *relations,int nrelations)
186     {
187     RouteRelX relationx;
188 amb 505 FILESORT_VARINT size;
189 amb 805 way_t noway=NO_WAY;
190     relation_t norelation=NO_RELATION;
191 amb 496
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 amb 505 WriteFile(relationsx->rfd,&size,FILESORT_VARSIZE);
198 amb 496 WriteFile(relationsx->rfd,&relationx,sizeof(RouteRelX));
199    
200 amb 805 WriteFile(relationsx->rfd,ways ,nways*sizeof(way_t));
201     WriteFile(relationsx->rfd,&noway, sizeof(way_t));
202 amb 496
203 amb 805 WriteFile(relationsx->rfd,relations ,nrelations*sizeof(relation_t));
204     WriteFile(relationsx->rfd,&norelation, sizeof(relation_t));
205 amb 496
206 amb 650 relationsx->rnumber++;
207 amb 496
208 amb 650 assert(!(relationsx->rnumber==0)); /* Zero marks the high-water mark for relations. */
209 amb 496 }
210    
211    
212     /*++++++++++++++++++++++++++++++++++++++
213 amb 540 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 amb 786 relationx.restriction=restriction;
241 amb 540 relationx.except=except;
242    
243     WriteFile(relationsx->trfd,&relationx,sizeof(TurnRestrictRelX));
244    
245 amb 650 relationsx->trnumber++;
246 amb 540
247 amb 650 assert(!(relationsx->trnumber==0)); /* Zero marks the high-water mark for relations. */
248 amb 540 }
249    
250    
251     /*++++++++++++++++++++++++++++++++++++++
252 amb 496 Sort the list of relations.
253    
254     RelationsX* relationsx The set of relations to process.
255     ++++++++++++++++++++++++++++++++++++++*/
256    
257     void SortRelationList(RelationsX* relationsx)
258     {
259 amb 559 /* Close the files (finished appending) */
260 amb 540
261 amb 612 relationsx->rfd=CloseFile(relationsx->rfd);
262 amb 542
263 amb 612 relationsx->trfd=CloseFile(relationsx->trfd);
264 amb 540
265 amb 559
266     /* Route Relations */
267    
268    
269     /* Turn Restriction Relations. */
270    
271 amb 650 if(relationsx->trnumber)
272 amb 551 {
273 amb 650 index_t trxnumber;
274 amb 551 int trfd;
275 amb 540
276 amb 551 /* Print the start message */
277 amb 540
278 amb 551 printf_first("Sorting Turn Restriction Relations");
279 amb 540
280 amb 555 /* Re-open the file read-only and a new file writeable */
281    
282     relationsx->trfd=ReOpenFile(relationsx->trfilename);
283    
284 amb 551 DeleteFile(relationsx->trfilename);
285 amb 540
286 amb 551 trfd=OpenFileNew(relationsx->trfilename);
287 amb 540
288 amb 551 /* Sort the relations */
289 amb 540
290 amb 650 trxnumber=relationsx->trnumber;
291     relationsx->trnumber=0;
292    
293 amb 551 sortrelationsx=relationsx;
294 amb 540
295 amb 559 filesort_fixed(relationsx->trfd,trfd,sizeof(TurnRestrictRelX),(int (*)(const void*,const void*))sort_by_id,(int (*)(void*,index_t))deduplicate_by_id);
296 amb 540
297 amb 555 /* Close the files */
298 amb 540
299 amb 612 relationsx->trfd=CloseFile(relationsx->trfd);
300 amb 551 CloseFile(trfd);
301 amb 540
302 amb 551 /* Print the final message */
303 amb 540
304 amb 790 printf_last("Sorted Relations: Relations=%"Pindex_t" Duplicates=%"Pindex_t,trxnumber,trxnumber-relationsx->trnumber);
305 amb 551 }
306 amb 496 }
307    
308    
309     /*++++++++++++++++++++++++++++++++++++++
310 amb 559 Sort the turn restriction relations into id order.
311 amb 540
312 amb 559 int sort_by_id Returns the comparison of the id fields.
313 amb 540
314     TurnRestrictRelX *a The first extended relation.
315    
316     TurnRestrictRelX *b The second extended relation.
317     ++++++++++++++++++++++++++++++++++++++*/
318    
319 amb 559 static int sort_by_id(TurnRestrictRelX *a,TurnRestrictRelX *b)
320 amb 540 {
321 amb 559 relation_t a_id=a->id;
322     relation_t b_id=b->id;
323 amb 540
324     if(a_id<b_id)
325     return(-1);
326     else if(a_id>b_id)
327     return(1);
328     else
329 amb 559 return(0);
330 amb 540 }
331    
332    
333     /*++++++++++++++++++++++++++++++++++++++
334     Deduplicate the extended relations using the id after sorting.
335    
336 amb 680 int deduplicate_by_id Return 1 if the value is to be kept, otherwise 0.
337 amb 540
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 amb 559 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 amb 612 relationsx->trfd=CloseFile(relationsx->trfd);
392 amb 559 CloseFile(trfd);
393    
394     /* Print the final message */
395    
396 amb 790 printf_last("Sorted Relations: Relations=%"Pindex_t,relationsx->trnumber);
397 amb 559 }
398    
399    
400     /*++++++++++++++++++++++++++++++++++++++
401 amb 770 Sort the turn restriction relations into via index order (then by from and to segments).
402 amb 559
403 amb 680 int sort_by_via Returns the comparison of the via, from and to fields.
404 amb 559
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 amb 770 index_t a_id=a->via;
413     index_t b_id=b->via;
414 amb 559
415     if(a_id<b_id)
416     return(-1);
417     else if(a_id>b_id)
418     return(1);
419     else
420     {
421 amb 770 index_t a_id=a->from;
422     index_t b_id=b->from;
423 amb 559
424     if(a_id<b_id)
425     return(-1);
426     else if(a_id>b_id)
427     return(1);
428     else
429     {
430 amb 770 index_t a_id=a->to;
431     index_t b_id=b->to;
432 amb 559
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 amb 496 Process the route relations and apply the information to the ways.
446    
447 amb 681 RelationsX *relationsx The set of relations to use.
448 amb 496
449 amb 681 WaysX *waysx The set of ways to modify.
450 amb 496 ++++++++++++++++++++++++++++++++++++++*/
451    
452     void ProcessRouteRelations(RelationsX *relationsx,WaysX *waysx)
453     {
454 amb 505 RouteRelX *unmatched=NULL,*lastunmatched=NULL;
455 amb 761 int nunmatched=0,lastnunmatched=0,iteration=1;
456 amb 496
457 amb 510 if(waysx->number==0)
458 amb 508 return;
459    
460 amb 555 /* Map into memory / open the files */
461 amb 496
462 amb 505 #if !SLIM
463 amb 651 waysx->data=MapFileWriteable(waysx->filename);
464 amb 555 #else
465     waysx->fd=ReOpenFileWriteable(waysx->filename);
466 amb 505 #endif
467 amb 496
468 amb 555 /* Re-open the file read-only */
469 amb 548
470     relationsx->rfd=ReOpenFile(relationsx->rfilename);
471    
472 amb 542 /* Read through the file. */
473 amb 496
474 amb 505 do
475 amb 496 {
476 amb 522 int ways=0,relations=0;
477 amb 780 index_t i;
478 amb 522
479 amb 505 SeekFile(relationsx->rfd,0);
480 amb 496
481 amb 505 /* Print the start message */
482 amb 496
483 amb 761 printf_first("Processing Route Relations (%d): Relations=0 Modified Ways=0",iteration);
484 amb 496
485 amb 650 for(i=0;i<relationsx->rnumber;i++)
486 amb 496 {
487 amb 505 FILESORT_VARINT size;
488     RouteRelX relationx;
489     way_t wayid;
490     relation_t relationid;
491 amb 529 transports_t routes=Transports_None;
492 amb 496
493 amb 505 /* Read each route relation */
494 amb 496
495 amb 505 ReadFile(relationsx->rfd,&size,FILESORT_VARSIZE);
496     ReadFile(relationsx->rfd,&relationx,sizeof(RouteRelX));
497 amb 496
498 amb 505 /* Decide what type of route it is */
499 amb 496
500 amb 761 if(iteration==1)
501 amb 522 {
502     relations++;
503 amb 505 routes=relationx.routes;
504 amb 522 }
505 amb 505 else
506 amb 506 {
507 amb 522 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 amb 506 routes=lastunmatched[j].routes;
518 amb 806
519 amb 522 break;
520     }
521 amb 506 }
522 amb 496
523 amb 505 /* Loop through the ways */
524    
525     do
526     {
527     ReadFile(relationsx->rfd,&wayid,sizeof(way_t));
528    
529     /* Update the ways that are listed for the relation */
530    
531 amb 805 if(wayid==NO_WAY)
532 amb 806 continue;
533    
534     if(routes)
535 amb 505 {
536 amb 506 index_t way=IndexWayX(waysx,wayid);
537 amb 505
538     if(way!=NO_WAY)
539     {
540     WayX *wayx=LookupWayX(waysx,way,1);
541    
542 amb 529 if(routes&Transports_Foot)
543 amb 505 wayx->way.props|=Properties_FootRoute;
544    
545 amb 529 if(routes&Transports_Bicycle)
546 amb 505 wayx->way.props|=Properties_BicycleRoute;
547    
548     PutBackWayX(waysx,way,1);
549 amb 522
550     ways++;
551 amb 505 }
552     }
553     }
554 amb 805 while(wayid!=NO_WAY);
555 amb 505
556     /* Loop through the relations */
557    
558     do
559     {
560     ReadFile(relationsx->rfd,&relationid,sizeof(relation_t));
561    
562     /* Add the relations that are listed for this relation to the list for next time */
563    
564 amb 805 if(relationid==NO_RELATION)
565 amb 806 continue;
566    
567 amb 805 else if(routes && relationid!=relationx.id)
568 amb 505 {
569     if(nunmatched%256==0)
570     unmatched=(RouteRelX*)realloc((void*)unmatched,(nunmatched+256)*sizeof(RouteRelX));
571    
572     unmatched[nunmatched].id=relationid;
573     unmatched[nunmatched].routes=routes;
574    
575     nunmatched++;
576     }
577     }
578 amb 805 while(relationid!=NO_RELATION);
579 amb 505
580 amb 757 if(!((i+1)%1000))
581 amb 790 printf_middle("Processing Route Relations (%d): Relations=%"Pindex_t" Modified Ways=%"Pindex_t,iteration,relations,ways);
582 amb 496 }
583    
584 amb 505 if(lastunmatched)
585     free(lastunmatched);
586    
587     lastunmatched=unmatched;
588     lastnunmatched=nunmatched;
589    
590     unmatched=NULL;
591     nunmatched=0;
592    
593     /* Print the final message */
594    
595 amb 790 printf_last("Processed Route Relations (%d): Relations=%"Pindex_t" Modified Ways=%"Pindex_t,iteration,relations,ways);
596 amb 496 }
597 amb 761 while(lastnunmatched && iteration++<5);
598 amb 496
599 amb 505 if(lastunmatched)
600     free(lastunmatched);
601    
602 amb 555 /* Close the file */
603    
604 amb 612 relationsx->rfd=CloseFile(relationsx->rfd);
605 amb 496
606 amb 555 /* Unmap from memory / close the files */
607 amb 496
608 amb 505 #if !SLIM
609 amb 651 waysx->data=UnmapFile(waysx->filename);
610 amb 555 #else
611 amb 612 waysx->fd=CloseFile(waysx->fd);
612 amb 511 #endif
613 amb 496 }
614 amb 542
615    
616     /*++++++++++++++++++++++++++++++++++++++
617 amb 645 Process the turn relations (first part) to update them with the node/way information.
618 amb 542
619 amb 681 RelationsX *relationsx The set of relations to modify.
620 amb 542
621 amb 680 NodesX *nodesx The set of nodes to use.
622 amb 542
623 amb 680 WaysX *waysx The set of ways to use.
624 amb 645 ++++++++++++++++++++++++++++++++++++++*/
625    
626     void ProcessTurnRelations1(RelationsX *relationsx,NodesX *nodesx,WaysX *waysx)
627     {
628 amb 780 int trfd;
629     index_t i,deleted=0;
630 amb 645
631     /* Print the start message */
632    
633 amb 761 printf_first("Processing Turn Relations (1): Relations=0");
634 amb 645
635     /* Re-open the file read-only and a new file writeable */
636    
637     relationsx->trfd=ReOpenFile(relationsx->trfilename);
638    
639     DeleteFile(relationsx->trfilename);
640    
641     trfd=OpenFileNew(relationsx->trfilename);
642    
643     /* Process all of the relations */
644    
645     for(i=0;i<relationsx->trnumber;i++)
646     {
647     TurnRestrictRelX relationx;
648    
649     ReadFile(relationsx->trfd,&relationx,sizeof(TurnRestrictRelX));
650    
651     relationx.via =IndexNodeX(nodesx,relationx.via);
652     relationx.from=IndexWayX(waysx,relationx.from);
653     relationx.to =IndexWayX(waysx,relationx.to);
654    
655 amb 756 if(relationx.via==NO_NODE || relationx.from==NO_WAY || relationx.to==NO_WAY)
656     deleted++;
657     else
658     WriteFile(trfd,&relationx,sizeof(TurnRestrictRelX));
659 amb 645
660 amb 757 if(!((i+1)%1000))
661 amb 790 printf_middle("Processing Turn Relations (1): Relations=%"Pindex_t" Deleted=%"Pindex_t,i+1-deleted,deleted);
662 amb 645 }
663    
664     /* Close the files */
665    
666     relationsx->trfd=CloseFile(relationsx->trfd);
667     CloseFile(trfd);
668    
669     /* Print the final message */
670    
671 amb 790 printf_last("Processed Turn Relations (1): Relations=%"Pindex_t" Deleted=%"Pindex_t,relationsx->trnumber-deleted,deleted);
672 amb 756
673     relationsx->trnumber-=deleted;
674 amb 645 }
675    
676    
677     /*++++++++++++++++++++++++++++++++++++++
678     Process the turn relations (second part) to convert them to nodes.
679    
680 amb 681 RelationsX *relationsx The set of relations to modify.
681 amb 645
682 amb 680 NodesX *nodesx The set of nodes to use.
683 amb 645
684 amb 680 SegmentsX *segmentsx The set of segments to use.
685 amb 542
686 amb 680 WaysX *waysx The set of ways to use.
687 amb 542 ++++++++++++++++++++++++++++++++++++++*/
688    
689 amb 645 void ProcessTurnRelations2(RelationsX *relationsx,NodesX *nodesx,SegmentsX *segmentsx,WaysX *waysx)
690 amb 542 {
691 amb 548 TurnRestrictRelX relationx;
692 amb 542 int trfd;
693 amb 780 index_t total=0,deleted=0;
694 amb 542
695 amb 548 if(nodesx->number==0 || segmentsx->number==0)
696     return;
697    
698 amb 542 /* Print the start message */
699    
700 amb 761 printf_first("Processing Turn Relations (2): Relations=0");
701 amb 542
702 amb 555 /* Map into memory / open the files */
703 amb 548
704     #if !SLIM
705 amb 651 nodesx->data=MapFileWriteable(nodesx->filename);
706     segmentsx->data=MapFile(segmentsx->filename);
707 amb 806 waysx->data=MapFile(waysx->filename);
708 amb 555 #else
709 amb 560 nodesx->fd=ReOpenFileWriteable(nodesx->filename);
710 amb 555 segmentsx->fd=ReOpenFile(segmentsx->filename);
711 amb 806 waysx->fd=ReOpenFile(waysx->filename);
712 amb 548 #endif
713    
714 amb 555 /* Re-open the file read-only and a new file writeable */
715    
716 amb 548 relationsx->trfd=ReOpenFile(relationsx->trfilename);
717    
718 amb 542 DeleteFile(relationsx->trfilename);
719    
720     trfd=OpenFileNew(relationsx->trfilename);
721    
722     /* Process all of the relations */
723    
724 amb 548 while(!ReadFile(relationsx->trfd,&relationx,sizeof(TurnRestrictRelX)))
725 amb 542 {
726 amb 615 NodeX *nodex;
727 amb 645 SegmentX *segmentx;
728 amb 615
729 amb 786 if(relationx.restriction==TurnRestrict_no_right_turn ||
730     relationx.restriction==TurnRestrict_no_left_turn ||
731     relationx.restriction==TurnRestrict_no_u_turn ||
732     relationx.restriction==TurnRestrict_no_straight_on)
733 amb 548 {
734 amb 770 index_t node_from=NO_NODE,node_to=NO_NODE;
735 amb 806 int oneway_from=0,oneway_to=0,vehicles_from=1,vehicles_to=1;
736 amb 542
737 amb 548 /* Find the segments that join the node 'via' */
738 amb 542
739 amb 646 segmentx=FirstSegmentX(segmentsx,relationx.via,1);
740 amb 542
741 amb 742 while(segmentx)
742 amb 548 {
743 amb 645 if(segmentx->way==relationx.from)
744 amb 548 {
745 amb 806 WayX *wayx=LookupWayX(waysx,segmentx->way,1);
746    
747 amb 551 if(node_from!=NO_NODE) /* Only one segment can be on the 'from' way */
748 amb 742 {
749     deleted++;
750 amb 551 goto endloop;
751 amb 742 }
752 amb 548
753 amb 645 node_from=OtherNode(segmentx,relationx.via);
754 amb 806
755     if(IsOnewayFrom(segmentx,relationx.via))
756     oneway_from=1; /* not allowed */
757    
758     if(!(wayx->way.allow&(Transports_Moped|Transports_Motorbike|Transports_Motorcar|Transports_Goods|Transports_HGV|Transports_PSV)))
759     vehicles_from=0; /* not allowed */
760 amb 548 }
761    
762 amb 645 if(segmentx->way==relationx.to)
763 amb 548 {
764 amb 806 WayX *wayx=LookupWayX(waysx,segmentx->way,1);
765    
766 amb 551 if(node_to!=NO_NODE) /* Only one segment can be on the 'to' way */
767 amb 742 {
768     deleted++;
769 amb 551 goto endloop;
770 amb 742 }
771 amb 548
772 amb 806 node_to=OtherNode(segmentx,relationx.via);
773 amb 670
774 amb 806 if(IsOnewayTo(segmentx,relationx.via))
775     oneway_to=1; /* not allowed */
776    
777     if(!(wayx->way.allow&(Transports_Moped|Transports_Motorbike|Transports_Motorcar|Transports_Goods|Transports_HGV|Transports_PSV)))
778     vehicles_to=0; /* not allowed */
779 amb 548 }
780    
781 amb 646 segmentx=NextSegmentX(segmentsx,segmentx,relationx.via,1);
782 amb 548 }
783    
784 amb 806 if(oneway_from || oneway_to || !vehicles_from || !vehicles_to || node_from==NO_NODE || node_to==NO_NODE)
785 amb 742 {
786     deleted++;
787 amb 548 goto endloop;
788 amb 742 }
789 amb 548
790 amb 551 /* Write the results */
791 amb 548
792 amb 645 relationx.from=node_from;
793     relationx.to =node_to;
794 amb 548
795 amb 551 WriteFile(trfd,&relationx,sizeof(TurnRestrictRelX));
796 amb 548
797 amb 559 total++;
798 amb 548
799 amb 757 if(!(total%1000))
800 amb 790 printf_middle("Processing Turn Relations (2): Relations=%"Pindex_t" Deleted=%"Pindex_t" Added=%"Pindex_t,total,deleted,total-relationsx->trnumber+deleted);
801 amb 551 }
802     else
803     {
804 amb 806 index_t node_from=NO_NODE,node_to=NO_NODE,node_other[MAX_SEG_PER_NODE];
805     int nnodes_other=0,i;
806     int oneway_from=0,vehicles_from=1;
807 amb 548
808 amb 551 /* Find the segments that join the node 'via' */
809 amb 548
810 amb 646 segmentx=FirstSegmentX(segmentsx,relationx.via,1);
811 amb 548
812 amb 742 while(segmentx)
813 amb 551 {
814 amb 645 if(segmentx->way==relationx.from)
815 amb 551 {
816 amb 806 WayX *wayx=LookupWayX(waysx,segmentx->way,1);
817    
818 amb 551 if(node_from!=NO_NODE) /* Only one segment can be on the 'from' way */
819 amb 742 {
820     deleted++;
821 amb 548 goto endloop;
822 amb 742 }
823 amb 548
824 amb 645 node_from=OtherNode(segmentx,relationx.via);
825 amb 806
826     if(IsOnewayFrom(segmentx,relationx.via))
827     oneway_from=1; /* not allowed */
828    
829     if(!(wayx->way.allow&(Transports_Moped|Transports_Motorbike|Transports_Motorcar|Transports_Goods|Transports_HGV|Transports_PSV)))
830     vehicles_from=0; /* not allowed */
831 amb 548 }
832 amb 551
833 amb 806 if(segmentx->way==relationx.to)
834 amb 551 {
835 amb 806 if(node_to!=NO_NODE) /* Only one segment can be on the 'to' way */
836     {
837     deleted++;
838     goto endloop;
839     }
840 amb 551
841 amb 806 node_to=OtherNode(segmentx,relationx.via);
842     }
843 amb 670
844 amb 806 if(segmentx->way!=relationx.from && segmentx->way!=relationx.to)
845     {
846     WayX *wayx=LookupWayX(waysx,segmentx->way,1);
847    
848     assert(nnodes_other<MAX_SEG_PER_NODE); /* Only a limited amount of information stored. */
849    
850     if(IsOnewayTo(segmentx,relationx.via))
851     ; /* not allowed */
852     else if(!(wayx->way.allow&(Transports_Moped|Transports_Motorbike|Transports_Motorcar|Transports_Goods|Transports_HGV|Transports_PSV)))
853     ; /* not allowed */
854     else
855     node_other[nnodes_other++]=OtherNode(segmentx,relationx.via);
856 amb 551 }
857    
858 amb 646 segmentx=NextSegmentX(segmentsx,segmentx,relationx.via,1);
859 amb 548 }
860    
861 amb 806 if(oneway_from || !vehicles_from || node_from==NO_NODE || node_to==NO_NODE || nnodes_other==0)
862 amb 742 {
863     deleted++;
864 amb 548 goto endloop;
865 amb 742 }
866 amb 548
867     /* Write the results */
868    
869 amb 806 for(i=0;i<nnodes_other;i++)
870 amb 551 {
871 amb 645 relationx.from=node_from;
872 amb 806 relationx.to =node_other[i];
873 amb 548
874 amb 551 WriteFile(trfd,&relationx,sizeof(TurnRestrictRelX));
875 amb 548
876 amb 559 total++;
877 amb 548
878 amb 757 if(!(total%1000))
879 amb 790 printf_middle("Processing Turn Relations (2): Relations=%"Pindex_t" Deleted=%"Pindex_t" Added=%"Pindex_t,total,deleted,total-relationsx->trnumber+deleted);
880 amb 551 }
881 amb 615 }
882 amb 560
883 amb 615 /* Force super nodes on via node and adjacent nodes */
884    
885     nodex=LookupNodeX(nodesx,relationx.via,1);
886     nodex->flags|=NODE_TURNRSTRCT;
887     PutBackNodeX(nodesx,relationx.via,1);
888    
889 amb 646 segmentx=FirstSegmentX(segmentsx,relationx.via,1);
890 amb 615
891 amb 742 while(segmentx)
892 amb 615 {
893 amb 645 index_t othernode=OtherNode(segmentx,relationx.via);
894 amb 615
895     nodex=LookupNodeX(nodesx,othernode,1);
896 amb 597 nodex->flags|=NODE_TURNRSTRCT2;
897 amb 615 PutBackNodeX(nodesx,othernode,1);
898 amb 597
899 amb 646 segmentx=NextSegmentX(segmentsx,segmentx,relationx.via,1);
900 amb 548 }
901    
902     endloop: ;
903 amb 542 }
904    
905 amb 555 /* Close the files */
906 amb 542
907 amb 612 relationsx->trfd=CloseFile(relationsx->trfd);
908 amb 542 CloseFile(trfd);
909    
910 amb 555 /* Unmap from memory / close the files */
911 amb 542
912 amb 548 #if !SLIM
913 amb 651 nodesx->data=UnmapFile(nodesx->filename);
914     segmentsx->data=UnmapFile(segmentsx->filename);
915 amb 806 waysx->data=UnmapFile(waysx->filename);
916 amb 555 #else
917 amb 612 nodesx->fd=CloseFile(nodesx->fd);
918     segmentsx->fd=CloseFile(segmentsx->fd);
919 amb 806 waysx->fd=CloseFile(waysx->fd);
920 amb 548 #endif
921    
922 amb 542 /* Print the final message */
923    
924 amb 790 printf_last("Processed Turn Relations (2): Relations=%"Pindex_t" Deleted=%"Pindex_t" Added=%"Pindex_t,total,deleted,total-relationsx->trnumber+deleted);
925 amb 559
926     relationsx->trnumber=total;
927 amb 542 }
928    
929    
930     /*++++++++++++++++++++++++++++++++++++++
931 amb 665 Update the node indexes after geographical sorting.
932 amb 542
933 amb 681 RelationsX *relationsx The set of relations to modify.
934 amb 542
935 amb 665 NodesX *nodesx The set of nodes to use.
936    
937 amb 680 SegmentsX *segmentsx The set of segments to use.
938 amb 542 ++++++++++++++++++++++++++++++++++++++*/
939    
940 amb 665 void UpdateTurnRelations(RelationsX *relationsx,NodesX *nodesx,SegmentsX *segmentsx)
941 amb 542 {
942     int trfd;
943 amb 780 index_t i;
944 amb 542
945     /* Print the start message */
946    
947 amb 761 printf_first("Updating Turn Relations: Relations=0");
948 amb 542
949 amb 599 /* Map into memory / open the files */
950    
951     #if !SLIM
952 amb 651 segmentsx->data=MapFile(segmentsx->filename);
953 amb 599 #else
954     segmentsx->fd=ReOpenFile(segmentsx->filename);
955     #endif
956    
957 amb 555 /* Re-open the file read-only and a new file writeable */
958 amb 542
959 amb 555 relationsx->trfd=ReOpenFile(relationsx->trfilename);
960    
961 amb 542 DeleteFile(relationsx->trfilename);
962    
963     trfd=OpenFileNew(relationsx->trfilename);
964    
965     /* Process all of the relations */
966    
967 amb 559 for(i=0;i<relationsx->trnumber;i++)
968 amb 542 {
969 amb 559 TurnRestrictRelX relationx;
970 amb 645 SegmentX *segmentx;
971 amb 599 index_t from_node,via_node,to_node;
972 amb 542
973 amb 559 ReadFile(relationsx->trfd,&relationx,sizeof(TurnRestrictRelX));
974 amb 542
975 amb 665 from_node=nodesx->gdata[relationx.from];
976     via_node =nodesx->gdata[relationx.via];
977     to_node =nodesx->gdata[relationx.to];
978 amb 542
979 amb 665 segmentx=FirstSegmentX(segmentsx,via_node,1);
980 amb 542
981 amb 599 do
982     {
983 amb 665 if(OtherNode(segmentx,via_node)==from_node)
984 amb 645 relationx.from=IndexSegmentX(segmentsx,segmentx);
985 amb 599
986 amb 665 if(OtherNode(segmentx,via_node)==to_node)
987 amb 645 relationx.to=IndexSegmentX(segmentsx,segmentx);
988 amb 599
989 amb 665 segmentx=NextSegmentX(segmentsx,segmentx,via_node,1);
990 amb 599 }
991 amb 645 while(segmentx);
992 amb 599
993 amb 665 relationx.via=via_node;
994    
995 amb 542 WriteFile(trfd,&relationx,sizeof(TurnRestrictRelX));
996    
997 amb 757 if(!(relationsx->trnumber%1000))
998 amb 790 printf_middle("Updating Turn Relations: Relations=%"Pindex_t,relationsx->trnumber);
999 amb 542 }
1000    
1001 amb 555 /* Close the files */
1002 amb 542
1003 amb 612 relationsx->trfd=CloseFile(relationsx->trfd);
1004 amb 542 CloseFile(trfd);
1005    
1006 amb 599 /* Unmap from memory / close the files */
1007    
1008     #if !SLIM
1009 amb 651 segmentsx->data=UnmapFile(segmentsx->filename);
1010 amb 599 #else
1011 amb 612 segmentsx->fd=CloseFile(segmentsx->fd);
1012 amb 599 #endif
1013    
1014 amb 542 /* Print the final message */
1015    
1016 amb 790 printf_last("Updated Turn Relations: Relations=%"Pindex_t,relationsx->trnumber);
1017 amb 645 }
1018    
1019    
1020     /*++++++++++++++++++++++++++++++++++++++
1021 amb 542 Save the relation list to a file.
1022    
1023     RelationsX* relationsx The set of relations to save.
1024    
1025     const char *filename The name of the file to save.
1026     ++++++++++++++++++++++++++++++++++++++*/
1027    
1028     void SaveRelationList(RelationsX* relationsx,const char *filename)
1029     {
1030     index_t i;
1031     int fd;
1032     RelationsFile relationsfile={0};
1033    
1034     /* Print the start message */
1035    
1036     printf_first("Writing Relations: Turn Relations=0");
1037    
1038 amb 559 /* Re-open the file read-only */
1039 amb 555
1040     relationsx->trfd=ReOpenFile(relationsx->trfilename);
1041    
1042 amb 542 /* Write out the relations data */
1043    
1044     fd=OpenFileNew(filename);
1045    
1046     SeekFile(fd,sizeof(RelationsFile));
1047    
1048     for(i=0;i<relationsx->trnumber;i++)
1049     {
1050     TurnRestrictRelX relationx;
1051 amb 551 TurnRelation relation;
1052 amb 542
1053     ReadFile(relationsx->trfd,&relationx,sizeof(TurnRestrictRelX));
1054    
1055 amb 551 relation.from=relationx.from;
1056     relation.via=relationx.via;
1057     relation.to=relationx.to;
1058     relation.except=relationx.except;
1059 amb 542
1060 amb 551 WriteFile(fd,&relation,sizeof(TurnRelation));
1061    
1062 amb 757 if(!((i+1)%1000))
1063 amb 790 printf_middle("Writing Relations: Turn Relations=%"Pindex_t,i+1);
1064 amb 542 }
1065    
1066     /* Write out the header structure */
1067    
1068     relationsfile.trnumber=relationsx->trnumber;
1069    
1070     SeekFile(fd,0);
1071     WriteFile(fd,&relationsfile,sizeof(RelationsFile));
1072    
1073     CloseFile(fd);
1074    
1075     /* Close the file */
1076    
1077 amb 612 relationsx->trfd=CloseFile(relationsx->trfd);
1078 amb 542
1079     /* Print the final message */
1080    
1081 amb 790 printf_last("Wrote Relations: Turn Relations=%"Pindex_t,relationsx->trnumber);
1082 amb 542 }