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 1098 - (hide annotations) (download) (as text)
Sat Oct 20 12:52:01 2012 UTC (12 years, 4 months ago) by amb
File MIME type: text/x-csrc
File size: 33696 byte(s)
Delete the pruned nodes before searching for super-nodes etc.

1 amb 496 /***************************************
2     Extended Relation data type functions.
3    
4     Part of the Routino routing software.
5     ******************/ /******************
6 amb 948 This file Copyright 2010-2012 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 <string.h>
26    
27 amb 542 #include "types.h"
28 amb 955 #include "segments.h"
29 amb 542 #include "relations.h"
30    
31     #include "nodesx.h"
32     #include "segmentsx.h"
33 amb 496 #include "waysx.h"
34     #include "relationsx.h"
35    
36     #include "files.h"
37 amb 519 #include "logging.h"
38 amb 532 #include "sorting.h"
39 amb 496
40    
41 amb 680 /* Local functions */
42 amb 540
43 amb 559 static int sort_by_id(TurnRestrictRelX *a,TurnRestrictRelX *b);
44 amb 540 static int deduplicate_by_id(TurnRestrictRelX *relationx,index_t index);
45    
46 amb 559 static int sort_by_via(TurnRestrictRelX *a,TurnRestrictRelX *b);
47 amb 540
48 amb 559
49 amb 496 /* Variables */
50    
51     /*+ The command line '--tmpdir' option or its default value. +*/
52     extern char *option_tmpdirname;
53    
54    
55     /*++++++++++++++++++++++++++++++++++++++
56     Allocate a new relation list (create a new file or open an existing one).
57    
58     RelationsX *NewRelationList Returns the relation list.
59    
60     int append Set to 1 if the file is to be opened for appending (now or later).
61     ++++++++++++++++++++++++++++++++++++++*/
62    
63     RelationsX *NewRelationList(int append)
64     {
65     RelationsX *relationsx;
66    
67     relationsx=(RelationsX*)calloc(1,sizeof(RelationsX));
68    
69     assert(relationsx); /* Check calloc() worked */
70    
71 amb 540
72 amb 559 /* Route Relations */
73    
74 amb 496 relationsx->rfilename=(char*)malloc(strlen(option_tmpdirname)+32);
75    
76     if(append)
77     sprintf(relationsx->rfilename,"%s/relationsx.route.input.tmp",option_tmpdirname);
78     else
79 amb 788 sprintf(relationsx->rfilename,"%s/relationsx.route.%p.tmp",option_tmpdirname,(void*)relationsx);
80 amb 496
81     if(append)
82     {
83     off_t size,position=0;
84    
85 amb 505 relationsx->rfd=OpenFileAppend(relationsx->rfilename);
86 amb 496
87     size=SizeFile(relationsx->rfilename);
88    
89     while(position<size)
90     {
91     FILESORT_VARINT relationsize;
92    
93 amb 887 SeekReadFile(relationsx->rfd,&relationsize,FILESORT_VARSIZE,position);
94 amb 496
95 amb 650 relationsx->rnumber++;
96 amb 496 position+=relationsize+FILESORT_VARSIZE;
97     }
98    
99     SeekFile(relationsx->rfd,size);
100     }
101     else
102 amb 505 relationsx->rfd=OpenFileNew(relationsx->rfilename);
103 amb 496
104 amb 540
105 amb 559 /* Turn Restriction Relations */
106    
107 amb 540 relationsx->trfilename=(char*)malloc(strlen(option_tmpdirname)+32);
108    
109     if(append)
110     sprintf(relationsx->trfilename,"%s/relationsx.turn.input.tmp",option_tmpdirname);
111     else
112 amb 788 sprintf(relationsx->trfilename,"%s/relationsx.turn.%p.tmp",option_tmpdirname,(void*)relationsx);
113 amb 540
114     if(append)
115     {
116     off_t size;
117    
118     relationsx->trfd=OpenFileAppend(relationsx->trfilename);
119    
120     size=SizeFile(relationsx->trfilename);
121    
122 amb 650 relationsx->trnumber=size/sizeof(TurnRestrictRelX);
123 amb 540 }
124     else
125     relationsx->trfd=OpenFileNew(relationsx->trfilename);
126    
127 amb 496 return(relationsx);
128     }
129    
130    
131     /*++++++++++++++++++++++++++++++++++++++
132     Free a relation list.
133    
134 amb 681 RelationsX *relationsx The set of relations to be freed.
135 amb 496
136 amb 680 int keep Set to 1 if the file is to be kept (for appending later).
137 amb 496 ++++++++++++++++++++++++++++++++++++++*/
138    
139     void FreeRelationList(RelationsX *relationsx,int keep)
140     {
141 amb 540 /* Route relations */
142    
143 amb 496 if(!keep)
144     DeleteFile(relationsx->rfilename);
145    
146     free(relationsx->rfilename);
147    
148 amb 559
149 amb 540 /* Turn Restriction relations */
150    
151     if(!keep)
152     DeleteFile(relationsx->trfilename);
153    
154     free(relationsx->trfilename);
155    
156 amb 496 free(relationsx);
157     }
158    
159    
160     /*++++++++++++++++++++++++++++++++++++++
161     Append a single relation to an unsorted route relation list.
162    
163     RelationsX* relationsx The set of relations to process.
164    
165     relation_t id The ID of the relation.
166    
167 amb 529 transports_t routes The types of routes that this relation is for.
168 amb 496
169     way_t *ways The array of ways that are members of the relation.
170    
171     int nways The number of ways that are members of the relation.
172    
173     relation_t *relations The array of relations that are members of the relation.
174    
175     int nrelations The number of relations that are members of the relation.
176     ++++++++++++++++++++++++++++++++++++++*/
177    
178 amb 540 void AppendRouteRelation(RelationsX* relationsx,relation_t id,
179     transports_t routes,
180 amb 496 way_t *ways,int nways,
181     relation_t *relations,int nrelations)
182     {
183     RouteRelX relationx;
184 amb 505 FILESORT_VARINT size;
185 amb 805 way_t noway=NO_WAY;
186     relation_t norelation=NO_RELATION;
187 amb 496
188     relationx.id=id;
189     relationx.routes=routes;
190    
191     size=sizeof(RouteRelX)+(nways+1)*sizeof(way_t)+(nrelations+1)*sizeof(relation_t);
192    
193 amb 505 WriteFile(relationsx->rfd,&size,FILESORT_VARSIZE);
194 amb 496 WriteFile(relationsx->rfd,&relationx,sizeof(RouteRelX));
195    
196 amb 805 WriteFile(relationsx->rfd,ways ,nways*sizeof(way_t));
197     WriteFile(relationsx->rfd,&noway, sizeof(way_t));
198 amb 496
199 amb 805 WriteFile(relationsx->rfd,relations ,nrelations*sizeof(relation_t));
200     WriteFile(relationsx->rfd,&norelation, sizeof(relation_t));
201 amb 496
202 amb 650 relationsx->rnumber++;
203 amb 496
204 amb 1065 assert(relationsx->rnumber!=0); /* Zero marks the high-water mark for relations. */
205 amb 496 }
206    
207    
208     /*++++++++++++++++++++++++++++++++++++++
209 amb 540 Append a single relation to an unsorted turn restriction relation list.
210    
211     RelationsX* relationsx The set of relations to process.
212    
213     relation_t id The ID of the relation.
214    
215     way_t from The way that the turn restriction starts from.
216    
217     way_t to The way that the restriction finished on.
218    
219     node_t via The node that the turn restriction passes through.
220    
221     TurnRestriction restriction The type of restriction.
222    
223     transports_t except The set of transports allowed to bypass the restriction.
224     ++++++++++++++++++++++++++++++++++++++*/
225    
226     void AppendTurnRestrictRelation(RelationsX* relationsx,relation_t id,
227     way_t from,way_t to,node_t via,
228     TurnRestriction restriction,transports_t except)
229     {
230 amb 951 TurnRestrictRelX relationx={0};
231 amb 540
232     relationx.id=id;
233     relationx.from=from;
234     relationx.to=to;
235     relationx.via=via;
236 amb 786 relationx.restriction=restriction;
237 amb 540 relationx.except=except;
238    
239     WriteFile(relationsx->trfd,&relationx,sizeof(TurnRestrictRelX));
240    
241 amb 650 relationsx->trnumber++;
242 amb 540
243 amb 1065 assert(relationsx->trnumber!=0); /* Zero marks the high-water mark for relations. */
244 amb 540 }
245    
246    
247     /*++++++++++++++++++++++++++++++++++++++
248 amb 496 Sort the list of relations.
249    
250     RelationsX* relationsx The set of relations to process.
251     ++++++++++++++++++++++++++++++++++++++*/
252    
253     void SortRelationList(RelationsX* relationsx)
254     {
255 amb 559 /* Close the files (finished appending) */
256 amb 540
257 amb 612 relationsx->rfd=CloseFile(relationsx->rfd);
258 amb 542
259 amb 612 relationsx->trfd=CloseFile(relationsx->trfd);
260 amb 540
261 amb 559
262     /* Route Relations */
263    
264    
265     /* Turn Restriction Relations. */
266    
267 amb 650 if(relationsx->trnumber)
268 amb 551 {
269 amb 650 index_t trxnumber;
270 amb 551 int trfd;
271 amb 540
272 amb 551 /* Print the start message */
273 amb 540
274 amb 895 printf_first("Sorting Turn Relations");
275 amb 540
276 amb 555 /* Re-open the file read-only and a new file writeable */
277    
278     relationsx->trfd=ReOpenFile(relationsx->trfilename);
279    
280 amb 551 DeleteFile(relationsx->trfilename);
281 amb 540
282 amb 551 trfd=OpenFileNew(relationsx->trfilename);
283 amb 540
284 amb 551 /* Sort the relations */
285 amb 540
286 amb 650 trxnumber=relationsx->trnumber;
287     relationsx->trnumber=0;
288    
289 amb 948 relationsx->trnumber=filesort_fixed(relationsx->trfd,trfd,sizeof(TurnRestrictRelX),(int (*)(const void*,const void*))sort_by_id,(int (*)(void*,index_t))deduplicate_by_id);
290 amb 540
291 amb 555 /* Close the files */
292 amb 540
293 amb 612 relationsx->trfd=CloseFile(relationsx->trfd);
294 amb 551 CloseFile(trfd);
295 amb 540
296 amb 551 /* Print the final message */
297 amb 540
298 amb 895 printf_last("Sorted Turn Relations: Relations=%"Pindex_t" Duplicates=%"Pindex_t,trxnumber,trxnumber-relationsx->trnumber);
299 amb 551 }
300 amb 496 }
301    
302    
303     /*++++++++++++++++++++++++++++++++++++++
304 amb 559 Sort the turn restriction relations into id order.
305 amb 540
306 amb 559 int sort_by_id Returns the comparison of the id fields.
307 amb 540
308     TurnRestrictRelX *a The first extended relation.
309    
310     TurnRestrictRelX *b The second extended relation.
311     ++++++++++++++++++++++++++++++++++++++*/
312    
313 amb 559 static int sort_by_id(TurnRestrictRelX *a,TurnRestrictRelX *b)
314 amb 540 {
315 amb 559 relation_t a_id=a->id;
316     relation_t b_id=b->id;
317 amb 540
318     if(a_id<b_id)
319     return(-1);
320     else if(a_id>b_id)
321     return(1);
322     else
323 amb 559 return(0);
324 amb 540 }
325    
326    
327     /*++++++++++++++++++++++++++++++++++++++
328     Deduplicate the extended relations using the id after sorting.
329    
330 amb 680 int deduplicate_by_id Return 1 if the value is to be kept, otherwise 0.
331 amb 540
332     TurnRestrictRelX *relationx The extended relation.
333    
334     index_t index The index of this relation in the total.
335     ++++++++++++++++++++++++++++++++++++++*/
336    
337     static int deduplicate_by_id(TurnRestrictRelX *relationx,index_t index)
338     {
339     static relation_t previd;
340    
341     if(index==0 || relationx->id!=previd)
342     {
343     previd=relationx->id;
344    
345     return(1);
346     }
347 amb 812 else
348     {
349     logerror("Relation %"Prelation_t" is duplicated.\n",relationx->id);
350 amb 540
351 amb 812 return(0);
352     }
353 amb 540 }
354    
355    
356     /*++++++++++++++++++++++++++++++++++++++
357 amb 559 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 amb 895 printf_first("Sorting Turn Relations");
372 amb 559
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 amb 612 relationsx->trfd=CloseFile(relationsx->trfd);
388 amb 559 CloseFile(trfd);
389    
390     /* Print the final message */
391    
392 amb 895 printf_last("Sorted Turn Relations: Relations=%"Pindex_t,relationsx->trnumber);
393 amb 559 }
394    
395    
396     /*++++++++++++++++++++++++++++++++++++++
397 amb 770 Sort the turn restriction relations into via index order (then by from and to segments).
398 amb 559
399 amb 680 int sort_by_via Returns the comparison of the via, from and to fields.
400 amb 559
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 amb 770 index_t a_id=a->via;
409     index_t b_id=b->via;
410 amb 559
411     if(a_id<b_id)
412     return(-1);
413     else if(a_id>b_id)
414     return(1);
415     else
416     {
417 amb 770 index_t a_id=a->from;
418     index_t b_id=b->from;
419 amb 559
420     if(a_id<b_id)
421     return(-1);
422     else if(a_id>b_id)
423     return(1);
424     else
425     {
426 amb 770 index_t a_id=a->to;
427     index_t b_id=b->to;
428 amb 559
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 amb 496 Process the route relations and apply the information to the ways.
442    
443 amb 681 RelationsX *relationsx The set of relations to use.
444 amb 496
445 amb 681 WaysX *waysx The set of ways to modify.
446 amb 496 ++++++++++++++++++++++++++++++++++++++*/
447    
448     void ProcessRouteRelations(RelationsX *relationsx,WaysX *waysx)
449     {
450 amb 505 RouteRelX *unmatched=NULL,*lastunmatched=NULL;
451 amb 761 int nunmatched=0,lastnunmatched=0,iteration=1;
452 amb 496
453 amb 510 if(waysx->number==0)
454 amb 508 return;
455    
456 amb 555 /* Map into memory / open the files */
457 amb 496
458 amb 505 #if !SLIM
459 amb 651 waysx->data=MapFileWriteable(waysx->filename);
460 amb 555 #else
461     waysx->fd=ReOpenFileWriteable(waysx->filename);
462 amb 505 #endif
463 amb 496
464 amb 555 /* Re-open the file read-only */
465 amb 548
466     relationsx->rfd=ReOpenFile(relationsx->rfilename);
467    
468 amb 542 /* Read through the file. */
469 amb 496
470 amb 505 do
471 amb 496 {
472 amb 522 int ways=0,relations=0;
473 amb 780 index_t i;
474 amb 522
475 amb 505 SeekFile(relationsx->rfd,0);
476 amb 496
477 amb 505 /* Print the start message */
478 amb 496
479 amb 761 printf_first("Processing Route Relations (%d): Relations=0 Modified Ways=0",iteration);
480 amb 496
481 amb 650 for(i=0;i<relationsx->rnumber;i++)
482 amb 496 {
483 amb 505 FILESORT_VARINT size;
484     RouteRelX relationx;
485     way_t wayid;
486     relation_t relationid;
487 amb 529 transports_t routes=Transports_None;
488 amb 496
489 amb 505 /* Read each route relation */
490 amb 496
491 amb 505 ReadFile(relationsx->rfd,&size,FILESORT_VARSIZE);
492     ReadFile(relationsx->rfd,&relationx,sizeof(RouteRelX));
493 amb 496
494 amb 505 /* Decide what type of route it is */
495 amb 496
496 amb 761 if(iteration==1)
497 amb 522 {
498     relations++;
499 amb 505 routes=relationx.routes;
500 amb 522 }
501 amb 505 else
502 amb 506 {
503 amb 522 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 amb 506 routes=lastunmatched[j].routes;
514 amb 806
515 amb 522 break;
516     }
517 amb 506 }
518 amb 496
519 amb 505 /* Loop through the ways */
520    
521     do
522     {
523     ReadFile(relationsx->rfd,&wayid,sizeof(way_t));
524    
525     /* Update the ways that are listed for the relation */
526    
527 amb 805 if(wayid==NO_WAY)
528 amb 806 continue;
529    
530     if(routes)
531 amb 505 {
532 amb 506 index_t way=IndexWayX(waysx,wayid);
533 amb 505
534     if(way!=NO_WAY)
535     {
536     WayX *wayx=LookupWayX(waysx,way,1);
537    
538 amb 529 if(routes&Transports_Foot)
539 amb 995 {
540 amb 1064 if(!(wayx->way.allow&Transports_Foot))
541     {
542     logerror("Route Relation %"Prelation_t" for Foot contains Way %"Pway_t" that does not allow Foot transport; overriding.\n",relationx.id,wayid);
543     wayx->way.allow|=Transports_Foot;
544     }
545 amb 505 wayx->way.props|=Properties_FootRoute;
546 amb 995 }
547 amb 505
548 amb 529 if(routes&Transports_Bicycle)
549 amb 995 {
550 amb 1064 if(!(wayx->way.allow&Transports_Bicycle))
551     {
552     logerror("Route Relation %"Prelation_t" for Bicycle contains Way %"Pway_t" that does not allow Bicycle transport; overriding.\n",relationx.id,wayid);
553     wayx->way.allow|=Transports_Bicycle;
554     }
555 amb 505 wayx->way.props|=Properties_BicycleRoute;
556 amb 995 }
557 amb 505
558 amb 942 PutBackWayX(waysx,wayx);
559 amb 522
560     ways++;
561 amb 505 }
562 amb 812 else
563 amb 832 logerror("Route Relation %"Prelation_t" contains Way %"Pway_t" but it does not exist in the Routino database.\n",relationx.id,wayid);
564 amb 505 }
565     }
566 amb 805 while(wayid!=NO_WAY);
567 amb 505
568     /* Loop through the relations */
569    
570     do
571     {
572     ReadFile(relationsx->rfd,&relationid,sizeof(relation_t));
573    
574     /* Add the relations that are listed for this relation to the list for next time */
575    
576 amb 805 if(relationid==NO_RELATION)
577 amb 806 continue;
578    
579 amb 812 if(relationid==relationx.id)
580     logerror("Relation %"Prelation_t" contains itself.\n",relationx.id);
581     else if(routes)
582 amb 505 {
583     if(nunmatched%256==0)
584     unmatched=(RouteRelX*)realloc((void*)unmatched,(nunmatched+256)*sizeof(RouteRelX));
585    
586     unmatched[nunmatched].id=relationid;
587     unmatched[nunmatched].routes=routes;
588    
589     nunmatched++;
590     }
591     }
592 amb 805 while(relationid!=NO_RELATION);
593 amb 505
594 amb 757 if(!((i+1)%1000))
595 amb 790 printf_middle("Processing Route Relations (%d): Relations=%"Pindex_t" Modified Ways=%"Pindex_t,iteration,relations,ways);
596 amb 496 }
597    
598 amb 505 if(lastunmatched)
599     free(lastunmatched);
600    
601     lastunmatched=unmatched;
602     lastnunmatched=nunmatched;
603    
604     unmatched=NULL;
605     nunmatched=0;
606    
607     /* Print the final message */
608    
609 amb 790 printf_last("Processed Route Relations (%d): Relations=%"Pindex_t" Modified Ways=%"Pindex_t,iteration,relations,ways);
610 amb 496 }
611 amb 812 while(lastnunmatched && iteration++<8);
612 amb 496
613 amb 505 if(lastunmatched)
614     free(lastunmatched);
615    
616 amb 555 /* Close the file */
617    
618 amb 612 relationsx->rfd=CloseFile(relationsx->rfd);
619 amb 496
620 amb 555 /* Unmap from memory / close the files */
621 amb 496
622 amb 505 #if !SLIM
623 amb 651 waysx->data=UnmapFile(waysx->filename);
624 amb 555 #else
625 amb 612 waysx->fd=CloseFile(waysx->fd);
626 amb 511 #endif
627 amb 496 }
628 amb 542
629    
630     /*++++++++++++++++++++++++++++++++++++++
631 amb 645 Process the turn relations (first part) to update them with the node/way information.
632 amb 542
633 amb 681 RelationsX *relationsx The set of relations to modify.
634 amb 542
635 amb 680 NodesX *nodesx The set of nodes to use.
636 amb 542
637 amb 680 WaysX *waysx The set of ways to use.
638 amb 645 ++++++++++++++++++++++++++++++++++++++*/
639    
640     void ProcessTurnRelations1(RelationsX *relationsx,NodesX *nodesx,WaysX *waysx)
641     {
642 amb 780 int trfd;
643     index_t i,deleted=0;
644 amb 645
645     /* Print the start message */
646    
647 amb 761 printf_first("Processing Turn Relations (1): Relations=0");
648 amb 645
649     /* Re-open the file read-only and a new file writeable */
650    
651     relationsx->trfd=ReOpenFile(relationsx->trfilename);
652    
653     DeleteFile(relationsx->trfilename);
654    
655     trfd=OpenFileNew(relationsx->trfilename);
656    
657     /* Process all of the relations */
658    
659     for(i=0;i<relationsx->trnumber;i++)
660     {
661     TurnRestrictRelX relationx;
662 amb 812 node_t via;
663     way_t from,to;
664 amb 645
665     ReadFile(relationsx->trfd,&relationx,sizeof(TurnRestrictRelX));
666    
667 amb 812 via =IndexNodeX(nodesx,relationx.via);
668     from=IndexWayX(waysx,relationx.from);
669     to =IndexWayX(waysx,relationx.to);
670 amb 645
671 amb 812 if(via==NO_NODE)
672 amb 832 logerror("Turn Relation %"Prelation_t" contains Node %"Pnode_t" but it does not exist in the Routino database.\n",relationx.id,relationx.via);
673 amb 812
674     if(from==NO_WAY)
675 amb 832 logerror("Turn Relation %"Prelation_t" contains Way %"Pway_t" but it does not exist in the Routino database.\n",relationx.id,relationx.from);
676 amb 812
677     if(to==NO_WAY)
678 amb 832 logerror("Turn Relation %"Prelation_t" contains Way %"Pway_t" but it does not exist in the Routino database.\n",relationx.id,relationx.to);
679 amb 812
680     relationx.via =via;
681     relationx.from=from;
682     relationx.to =to;
683    
684 amb 756 if(relationx.via==NO_NODE || relationx.from==NO_WAY || relationx.to==NO_WAY)
685     deleted++;
686     else
687     WriteFile(trfd,&relationx,sizeof(TurnRestrictRelX));
688 amb 645
689 amb 757 if(!((i+1)%1000))
690 amb 790 printf_middle("Processing Turn Relations (1): Relations=%"Pindex_t" Deleted=%"Pindex_t,i+1-deleted,deleted);
691 amb 645 }
692    
693     /* Close the files */
694    
695     relationsx->trfd=CloseFile(relationsx->trfd);
696     CloseFile(trfd);
697    
698     /* Print the final message */
699    
700 amb 790 printf_last("Processed Turn Relations (1): Relations=%"Pindex_t" Deleted=%"Pindex_t,relationsx->trnumber-deleted,deleted);
701 amb 756
702     relationsx->trnumber-=deleted;
703 amb 645 }
704    
705    
706     /*++++++++++++++++++++++++++++++++++++++
707     Process the turn relations (second part) to convert them to nodes.
708    
709 amb 681 RelationsX *relationsx The set of relations to modify.
710 amb 645
711 amb 680 NodesX *nodesx The set of nodes to use.
712 amb 645
713 amb 680 SegmentsX *segmentsx The set of segments to use.
714 amb 542
715 amb 680 WaysX *waysx The set of ways to use.
716 amb 542 ++++++++++++++++++++++++++++++++++++++*/
717    
718 amb 645 void ProcessTurnRelations2(RelationsX *relationsx,NodesX *nodesx,SegmentsX *segmentsx,WaysX *waysx)
719 amb 542 {
720 amb 548 TurnRestrictRelX relationx;
721 amb 542 int trfd;
722 amb 780 index_t total=0,deleted=0;
723 amb 542
724 amb 548 if(nodesx->number==0 || segmentsx->number==0)
725     return;
726    
727 amb 542 /* Print the start message */
728    
729 amb 761 printf_first("Processing Turn Relations (2): Relations=0");
730 amb 542
731 amb 555 /* Map into memory / open the files */
732 amb 548
733     #if !SLIM
734 amb 651 nodesx->data=MapFileWriteable(nodesx->filename);
735     segmentsx->data=MapFile(segmentsx->filename);
736 amb 806 waysx->data=MapFile(waysx->filename);
737 amb 555 #else
738 amb 560 nodesx->fd=ReOpenFileWriteable(nodesx->filename);
739 amb 555 segmentsx->fd=ReOpenFile(segmentsx->filename);
740 amb 806 waysx->fd=ReOpenFile(waysx->filename);
741 amb 548 #endif
742    
743 amb 555 /* Re-open the file read-only and a new file writeable */
744    
745 amb 548 relationsx->trfd=ReOpenFile(relationsx->trfilename);
746    
747 amb 542 DeleteFile(relationsx->trfilename);
748    
749     trfd=OpenFileNew(relationsx->trfilename);
750    
751     /* Process all of the relations */
752    
753 amb 548 while(!ReadFile(relationsx->trfd,&relationx,sizeof(TurnRestrictRelX)))
754 amb 542 {
755 amb 615 NodeX *nodex;
756 amb 645 SegmentX *segmentx;
757 amb 615
758 amb 786 if(relationx.restriction==TurnRestrict_no_right_turn ||
759     relationx.restriction==TurnRestrict_no_left_turn ||
760     relationx.restriction==TurnRestrict_no_u_turn ||
761     relationx.restriction==TurnRestrict_no_straight_on)
762 amb 548 {
763 amb 770 index_t node_from=NO_NODE,node_to=NO_NODE;
764 amb 806 int oneway_from=0,oneway_to=0,vehicles_from=1,vehicles_to=1;
765 amb 542
766 amb 548 /* Find the segments that join the node 'via' */
767 amb 542
768 amb 646 segmentx=FirstSegmentX(segmentsx,relationx.via,1);
769 amb 542
770 amb 742 while(segmentx)
771 amb 548 {
772 amb 645 if(segmentx->way==relationx.from)
773 amb 548 {
774 amb 806 WayX *wayx=LookupWayX(waysx,segmentx->way,1);
775    
776 amb 551 if(node_from!=NO_NODE) /* Only one segment can be on the 'from' way */
777 amb 742 {
778 amb 832 logerror("Turn Relation %"Prelation_t" is not stored because the 'via' node is not at the end of the 'from' way.\n",relationx.id);
779 amb 742 deleted++;
780 amb 551 goto endloop;
781 amb 742 }
782 amb 548
783 amb 645 node_from=OtherNode(segmentx,relationx.via);
784 amb 806
785     if(IsOnewayFrom(segmentx,relationx.via))
786     oneway_from=1; /* not allowed */
787    
788 amb 837 if(!(wayx->way.allow&(Transports_Bicycle|Transports_Moped|Transports_Motorbike|Transports_Motorcar|Transports_Goods|Transports_HGV|Transports_PSV)))
789 amb 806 vehicles_from=0; /* not allowed */
790 amb 548 }
791    
792 amb 645 if(segmentx->way==relationx.to)
793 amb 548 {
794 amb 806 WayX *wayx=LookupWayX(waysx,segmentx->way,1);
795    
796 amb 551 if(node_to!=NO_NODE) /* Only one segment can be on the 'to' way */
797 amb 742 {
798 amb 832 logerror("Turn Relation %"Prelation_t" is not stored because the 'via' node is not at the end of the 'to' way.\n",relationx.id);
799 amb 742 deleted++;
800 amb 551 goto endloop;
801 amb 742 }
802 amb 548
803 amb 806 node_to=OtherNode(segmentx,relationx.via);
804 amb 670
805 amb 806 if(IsOnewayTo(segmentx,relationx.via))
806     oneway_to=1; /* not allowed */
807    
808 amb 837 if(!(wayx->way.allow&(Transports_Bicycle|Transports_Moped|Transports_Motorbike|Transports_Motorcar|Transports_Goods|Transports_HGV|Transports_PSV)))
809 amb 806 vehicles_to=0; /* not allowed */
810 amb 548 }
811    
812 amb 943 segmentx=NextSegmentX(segmentsx,segmentx,relationx.via);
813 amb 548 }
814    
815 amb 812 if(node_from==NO_NODE)
816 amb 832 logerror("Turn Relation %"Prelation_t" is not stored because the 'via' node is not part of the 'from' way.\n",relationx.id);
817 amb 812
818     if(node_to==NO_NODE)
819 amb 832 logerror("Turn Relation %"Prelation_t" is not stored because the 'via' node is not part of the 'to' way.\n",relationx.id);
820 amb 812
821     if(oneway_from)
822     logerror("Turn Relation %"Prelation_t" is not stored because the 'from' way is oneway away from the 'via' node.\n",relationx.id);
823    
824     if(oneway_to)
825     logerror("Turn Relation %"Prelation_t" is not needed because the 'to' way is oneway towards the 'via' node.\n",relationx.id);
826    
827     if(!vehicles_from)
828 amb 837 logerror("Turn Relation %"Prelation_t" is not stored because the 'from' way does not allow vehicles.\n",relationx.id);
829 amb 812
830     if(!vehicles_to)
831 amb 837 logerror("Turn Relation %"Prelation_t" is not needed because the 'to' way does not allow vehicles.\n",relationx.id);
832 amb 812
833 amb 806 if(oneway_from || oneway_to || !vehicles_from || !vehicles_to || node_from==NO_NODE || node_to==NO_NODE)
834 amb 742 {
835     deleted++;
836 amb 548 goto endloop;
837 amb 742 }
838 amb 548
839 amb 551 /* Write the results */
840 amb 548
841 amb 645 relationx.from=node_from;
842     relationx.to =node_to;
843 amb 548
844 amb 551 WriteFile(trfd,&relationx,sizeof(TurnRestrictRelX));
845 amb 548
846 amb 559 total++;
847 amb 548
848 amb 757 if(!(total%1000))
849 amb 790 printf_middle("Processing Turn Relations (2): Relations=%"Pindex_t" Deleted=%"Pindex_t" Added=%"Pindex_t,total,deleted,total-relationsx->trnumber+deleted);
850 amb 551 }
851     else
852     {
853 amb 806 index_t node_from=NO_NODE,node_to=NO_NODE,node_other[MAX_SEG_PER_NODE];
854     int nnodes_other=0,i;
855     int oneway_from=0,vehicles_from=1;
856 amb 548
857 amb 551 /* Find the segments that join the node 'via' */
858 amb 548
859 amb 646 segmentx=FirstSegmentX(segmentsx,relationx.via,1);
860 amb 548
861 amb 742 while(segmentx)
862 amb 551 {
863 amb 645 if(segmentx->way==relationx.from)
864 amb 551 {
865 amb 806 WayX *wayx=LookupWayX(waysx,segmentx->way,1);
866    
867 amb 551 if(node_from!=NO_NODE) /* Only one segment can be on the 'from' way */
868 amb 742 {
869 amb 832 logerror("Turn Relation %"Prelation_t" is not stored because the 'via' node is not at the end of the 'from' way.\n",relationx.id);
870 amb 742 deleted++;
871 amb 548 goto endloop;
872 amb 742 }
873 amb 548
874 amb 645 node_from=OtherNode(segmentx,relationx.via);
875 amb 806
876     if(IsOnewayFrom(segmentx,relationx.via))
877     oneway_from=1; /* not allowed */
878    
879 amb 837 if(!(wayx->way.allow&(Transports_Bicycle|Transports_Moped|Transports_Motorbike|Transports_Motorcar|Transports_Goods|Transports_HGV|Transports_PSV)))
880 amb 806 vehicles_from=0; /* not allowed */
881 amb 548 }
882 amb 551
883 amb 806 if(segmentx->way==relationx.to)
884 amb 551 {
885 amb 806 if(node_to!=NO_NODE) /* Only one segment can be on the 'to' way */
886     {
887 amb 832 logerror("Turn Relation %"Prelation_t" is not stored because the 'via' node is not at the end of the 'to' way.\n",relationx.id);
888 amb 806 deleted++;
889     goto endloop;
890     }
891 amb 551
892 amb 806 node_to=OtherNode(segmentx,relationx.via);
893     }
894 amb 670
895 amb 806 if(segmentx->way!=relationx.from && segmentx->way!=relationx.to)
896     {
897     WayX *wayx=LookupWayX(waysx,segmentx->way,1);
898    
899     if(IsOnewayTo(segmentx,relationx.via))
900     ; /* not allowed */
901 amb 837 else if(!(wayx->way.allow&(Transports_Bicycle|Transports_Moped|Transports_Motorbike|Transports_Motorcar|Transports_Goods|Transports_HGV|Transports_PSV)))
902 amb 806 ; /* not allowed */
903     else
904 amb 812 {
905     assert(nnodes_other<MAX_SEG_PER_NODE); /* Only a limited amount of information stored. */
906    
907 amb 806 node_other[nnodes_other++]=OtherNode(segmentx,relationx.via);
908 amb 812 }
909 amb 551 }
910    
911 amb 943 segmentx=NextSegmentX(segmentsx,segmentx,relationx.via);
912 amb 548 }
913    
914 amb 812 if(node_from==NO_NODE)
915 amb 832 logerror("Turn Relation %"Prelation_t" is not stored because the 'via' node is not part of the 'from' way.\n",relationx.id);
916 amb 812
917     if(node_to==NO_NODE)
918 amb 832 logerror("Turn Relation %"Prelation_t" is not stored because the 'via' node is not part of the 'to' way.\n",relationx.id);
919 amb 812
920     if(nnodes_other==0)
921     logerror("Turn Relation %"Prelation_t" is not needed because the only allowed exit from the 'via' node is the 'to' way.\n",relationx.id);
922    
923     if(oneway_from)
924 amb 832 logerror("Turn Relation %"Prelation_t" is not needed because the 'from' way is oneway away from the 'via' node.\n",relationx.id);
925 amb 812
926     if(!vehicles_from)
927 amb 837 logerror("Turn Relation %"Prelation_t" is not stored because the 'from' way does not allow vehicles.\n",relationx.id);
928 amb 812
929 amb 806 if(oneway_from || !vehicles_from || node_from==NO_NODE || node_to==NO_NODE || nnodes_other==0)
930 amb 742 {
931     deleted++;
932 amb 548 goto endloop;
933 amb 742 }
934 amb 548
935     /* Write the results */
936    
937 amb 806 for(i=0;i<nnodes_other;i++)
938 amb 551 {
939 amb 645 relationx.from=node_from;
940 amb 806 relationx.to =node_other[i];
941 amb 548
942 amb 551 WriteFile(trfd,&relationx,sizeof(TurnRestrictRelX));
943 amb 548
944 amb 559 total++;
945 amb 548
946 amb 757 if(!(total%1000))
947 amb 790 printf_middle("Processing Turn Relations (2): Relations=%"Pindex_t" Deleted=%"Pindex_t" Added=%"Pindex_t,total,deleted,total-relationsx->trnumber+deleted);
948 amb 551 }
949 amb 615 }
950 amb 560
951 amb 615 /* Force super nodes on via node and adjacent nodes */
952    
953     nodex=LookupNodeX(nodesx,relationx.via,1);
954     nodex->flags|=NODE_TURNRSTRCT;
955 amb 942 PutBackNodeX(nodesx,nodex);
956 amb 615
957 amb 646 segmentx=FirstSegmentX(segmentsx,relationx.via,1);
958 amb 615
959 amb 742 while(segmentx)
960 amb 615 {
961 amb 645 index_t othernode=OtherNode(segmentx,relationx.via);
962 amb 615
963     nodex=LookupNodeX(nodesx,othernode,1);
964 amb 597 nodex->flags|=NODE_TURNRSTRCT2;
965 amb 942 PutBackNodeX(nodesx,nodex);
966 amb 597
967 amb 943 segmentx=NextSegmentX(segmentsx,segmentx,relationx.via);
968 amb 548 }
969    
970     endloop: ;
971 amb 542 }
972    
973 amb 555 /* Close the files */
974 amb 542
975 amb 612 relationsx->trfd=CloseFile(relationsx->trfd);
976 amb 542 CloseFile(trfd);
977    
978 amb 555 /* Unmap from memory / close the files */
979 amb 542
980 amb 548 #if !SLIM
981 amb 651 nodesx->data=UnmapFile(nodesx->filename);
982     segmentsx->data=UnmapFile(segmentsx->filename);
983 amb 806 waysx->data=UnmapFile(waysx->filename);
984 amb 555 #else
985 amb 612 nodesx->fd=CloseFile(nodesx->fd);
986     segmentsx->fd=CloseFile(segmentsx->fd);
987 amb 806 waysx->fd=CloseFile(waysx->fd);
988 amb 548 #endif
989    
990 amb 542 /* Print the final message */
991    
992 amb 790 printf_last("Processed Turn Relations (2): Relations=%"Pindex_t" Deleted=%"Pindex_t" Added=%"Pindex_t,total,deleted,total-relationsx->trnumber+deleted);
993 amb 559
994     relationsx->trnumber=total;
995 amb 542 }
996    
997    
998     /*++++++++++++++++++++++++++++++++++++++
999 amb 1098 Remove pruned turn relations and update the node indexes after pruning nodes.
1000 amb 542
1001 amb 681 RelationsX *relationsx The set of relations to modify.
1002 amb 542
1003 amb 665 NodesX *nodesx The set of nodes to use.
1004 amb 1098 ++++++++++++++++++++++++++++++++++++++*/
1005 amb 665
1006 amb 1098 void RemovePrunedTurnRelations(RelationsX *relationsx,NodesX *nodesx)
1007     {
1008     TurnRestrictRelX relationx;
1009     index_t total=0,pruned=0,notpruned=0;
1010     int trfd;
1011    
1012     /* Print the start message */
1013    
1014     printf_first("Deleting Pruned Turn Relations: Relations=0 Pruned=0");
1015    
1016     /* Re-open the file read-only and a new file writeable */
1017    
1018     relationsx->trfd=ReOpenFile(relationsx->trfilename);
1019    
1020     DeleteFile(relationsx->trfilename);
1021    
1022     trfd=OpenFileNew(relationsx->trfilename);
1023    
1024     /* Process all of the relations */
1025    
1026     while(!ReadFile(relationsx->trfd,&relationx,sizeof(TurnRestrictRelX)))
1027     {
1028     relationx.from=nodesx->pdata[relationx.from];
1029     relationx.via =nodesx->pdata[relationx.via];
1030     relationx.to =nodesx->pdata[relationx.to];
1031    
1032     if(relationx.from==NO_NODE || relationx.via==NO_NODE || relationx.to==NO_NODE)
1033     pruned++;
1034     else
1035     {
1036     WriteFile(trfd,&relationx,sizeof(TurnRestrictRelX));
1037    
1038     notpruned++;
1039     }
1040    
1041     total++;
1042    
1043     if(!(total%1000))
1044     printf_middle("Deleting Pruned Turn Relations: Relations=%"Pindex_t" Pruned=%"Pindex_t,total,pruned);
1045     }
1046    
1047     relationsx->trnumber=notpruned;
1048    
1049     /* Close the files */
1050    
1051     relationsx->trfd=CloseFile(relationsx->trfd);
1052     CloseFile(trfd);
1053    
1054     /* Free the memory */
1055    
1056     if(nodesx->pdata)
1057     {
1058     free(nodesx->pdata);
1059     nodesx->pdata=NULL;
1060     }
1061    
1062     /* Print the final message */
1063    
1064     printf_last("Deleted Pruned Turn Relations: Relations=%"Pindex_t" Pruned=%"Pindex_t,total,pruned);
1065     }
1066    
1067    
1068     /*++++++++++++++++++++++++++++++++++++++
1069     Update the node indexes and replace nodes with segments after geographical sorting.
1070    
1071     RelationsX *relationsx The set of relations to modify.
1072    
1073     NodesX *nodesx The set of nodes to use.
1074    
1075 amb 680 SegmentsX *segmentsx The set of segments to use.
1076 amb 542 ++++++++++++++++++++++++++++++++++++++*/
1077    
1078 amb 665 void UpdateTurnRelations(RelationsX *relationsx,NodesX *nodesx,SegmentsX *segmentsx)
1079 amb 542 {
1080     int trfd;
1081 amb 1098 index_t i;
1082 amb 542
1083     /* Print the start message */
1084    
1085 amb 1098 printf_first("Updating Turn Relations: Relations=0");
1086 amb 542
1087 amb 599 /* Map into memory / open the files */
1088    
1089     #if !SLIM
1090 amb 651 segmentsx->data=MapFile(segmentsx->filename);
1091 amb 599 #else
1092     segmentsx->fd=ReOpenFile(segmentsx->filename);
1093     #endif
1094    
1095 amb 555 /* Re-open the file read-only and a new file writeable */
1096 amb 542
1097 amb 555 relationsx->trfd=ReOpenFile(relationsx->trfilename);
1098    
1099 amb 542 DeleteFile(relationsx->trfilename);
1100    
1101     trfd=OpenFileNew(relationsx->trfilename);
1102    
1103     /* Process all of the relations */
1104    
1105 amb 559 for(i=0;i<relationsx->trnumber;i++)
1106 amb 542 {
1107 amb 559 TurnRestrictRelX relationx;
1108 amb 1098 SegmentX *segmentx;
1109 amb 599 index_t from_node,via_node,to_node;
1110 amb 542
1111 amb 559 ReadFile(relationsx->trfd,&relationx,sizeof(TurnRestrictRelX));
1112 amb 542
1113 amb 665 from_node=nodesx->gdata[relationx.from];
1114     via_node =nodesx->gdata[relationx.via];
1115     to_node =nodesx->gdata[relationx.to];
1116 amb 542
1117 amb 1098 segmentx=FirstSegmentX(segmentsx,via_node,1);
1118    
1119     do
1120 amb 599 {
1121 amb 1098 if(OtherNode(segmentx,via_node)==from_node)
1122     relationx.from=IndexSegmentX(segmentsx,segmentx);
1123 amb 599
1124 amb 1098 if(OtherNode(segmentx,via_node)==to_node)
1125     relationx.to=IndexSegmentX(segmentsx,segmentx);
1126 amb 599
1127 amb 1098 segmentx=NextSegmentX(segmentsx,segmentx,via_node);
1128     }
1129     while(segmentx);
1130 amb 599
1131 amb 1098 relationx.via=via_node;
1132 amb 665
1133 amb 1098 WriteFile(trfd,&relationx,sizeof(TurnRestrictRelX));
1134 amb 542
1135 amb 1098 if(!((i+1)%1000))
1136     printf_middle("Updating Turn Relations: Relations=%"Pindex_t,i+1);
1137 amb 542 }
1138    
1139 amb 555 /* Close the files */
1140 amb 542
1141 amb 612 relationsx->trfd=CloseFile(relationsx->trfd);
1142 amb 542 CloseFile(trfd);
1143    
1144 amb 599 /* Unmap from memory / close the files */
1145    
1146     #if !SLIM
1147 amb 651 segmentsx->data=UnmapFile(segmentsx->filename);
1148 amb 599 #else
1149 amb 612 segmentsx->fd=CloseFile(segmentsx->fd);
1150 amb 599 #endif
1151    
1152 amb 542 /* Print the final message */
1153    
1154 amb 1098 printf_last("Updated Turn Relations: Relations=%"Pindex_t,relationsx->trnumber);
1155 amb 645 }
1156    
1157    
1158     /*++++++++++++++++++++++++++++++++++++++
1159 amb 542 Save the relation list to a file.
1160    
1161     RelationsX* relationsx The set of relations to save.
1162    
1163     const char *filename The name of the file to save.
1164     ++++++++++++++++++++++++++++++++++++++*/
1165    
1166     void SaveRelationList(RelationsX* relationsx,const char *filename)
1167     {
1168     index_t i;
1169     int fd;
1170     RelationsFile relationsfile={0};
1171    
1172     /* Print the start message */
1173    
1174     printf_first("Writing Relations: Turn Relations=0");
1175    
1176 amb 559 /* Re-open the file read-only */
1177 amb 555
1178     relationsx->trfd=ReOpenFile(relationsx->trfilename);
1179    
1180 amb 542 /* Write out the relations data */
1181    
1182     fd=OpenFileNew(filename);
1183    
1184     SeekFile(fd,sizeof(RelationsFile));
1185    
1186     for(i=0;i<relationsx->trnumber;i++)
1187     {
1188     TurnRestrictRelX relationx;
1189 amb 944 TurnRelation relation={0};
1190 amb 542
1191     ReadFile(relationsx->trfd,&relationx,sizeof(TurnRestrictRelX));
1192    
1193 amb 551 relation.from=relationx.from;
1194     relation.via=relationx.via;
1195     relation.to=relationx.to;
1196     relation.except=relationx.except;
1197 amb 542
1198 amb 551 WriteFile(fd,&relation,sizeof(TurnRelation));
1199    
1200 amb 757 if(!((i+1)%1000))
1201 amb 790 printf_middle("Writing Relations: Turn Relations=%"Pindex_t,i+1);
1202 amb 542 }
1203    
1204     /* Write out the header structure */
1205    
1206     relationsfile.trnumber=relationsx->trnumber;
1207    
1208     SeekFile(fd,0);
1209     WriteFile(fd,&relationsfile,sizeof(RelationsFile));
1210    
1211     CloseFile(fd);
1212    
1213     /* Close the file */
1214    
1215 amb 612 relationsx->trfd=CloseFile(relationsx->trfd);
1216 amb 542
1217     /* Print the final message */
1218    
1219 amb 790 printf_last("Wrote Relations: Turn Relations=%"Pindex_t,relationsx->trnumber);
1220 amb 542 }