1 /***************************************
2 $Header: /home/amb/routino/src/RCS/relationsx.c,v 1.10 2010/11/13 14:57:30 amb Exp $
4 Extended Relation data type functions.
6 Part of the Routino routing software.
7 ******************/ /******************
8 This file Copyright 2010 Andrew M. Bishop
10 This program is free software: you can redistribute it and/or modify
11 it under the terms of the GNU Affero General Public License as published by
12 the Free Software Foundation, either version 3 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU Affero General Public License for more details.
20 You should have received a copy of the GNU Affero General Public License
21 along with this program. If not, see <http://www.gnu.org/licenses/>.
22 ***************************************/
32 #include "relationsx.h"
36 #include "functions.h"
41 /*+ The command line '--tmpdir' option or its default value. +*/
42 extern char *option_tmpdirname;
45 /*++++++++++++++++++++++++++++++++++++++
46 Allocate a new relation list (create a new file or open an existing one).
48 RelationsX *NewRelationList Returns the relation list.
50 int append Set to 1 if the file is to be opened for appending (now or later).
51 ++++++++++++++++++++++++++++++++++++++*/
53 RelationsX *NewRelationList(int append)
55 RelationsX *relationsx;
57 relationsx=(RelationsX*)calloc(1,sizeof(RelationsX));
59 assert(relationsx); /* Check calloc() worked */
61 relationsx->rfilename=(char*)malloc(strlen(option_tmpdirname)+32);
64 sprintf(relationsx->rfilename,"%s/relationsx.route.input.tmp",option_tmpdirname);
66 sprintf(relationsx->rfilename,"%s/relationsx.route.%p.tmp",option_tmpdirname,relationsx);
70 off_t size,position=0;
72 relationsx->rfd=OpenFileAppend(relationsx->rfilename);
74 size=SizeFile(relationsx->rfilename);
78 FILESORT_VARINT relationsize;
80 SeekFile(relationsx->rfd,position);
81 ReadFile(relationsx->rfd,&relationsize,FILESORT_VARSIZE);
83 relationsx->rxnumber++;
84 position+=relationsize+FILESORT_VARSIZE;
87 SeekFile(relationsx->rfd,size);
90 relationsx->rfd=OpenFileNew(relationsx->rfilename);
96 /*++++++++++++++++++++++++++++++++++++++
99 RelationsX *relationsx The list to be freed.
101 int keep Set to 1 if the file is to be kept.
102 ++++++++++++++++++++++++++++++++++++++*/
104 void FreeRelationList(RelationsX *relationsx,int keep)
107 DeleteFile(relationsx->rfilename);
109 free(relationsx->rfilename);
115 /*++++++++++++++++++++++++++++++++++++++
116 Append a single relation to an unsorted route relation list.
118 RelationsX* relationsx The set of relations to process.
120 relation_t id The ID of the relation.
122 allow_t routes The types of routes that this relation is for.
124 way_t *ways The array of ways that are members of the relation.
126 int nways The number of ways that are members of the relation.
128 relation_t *relations The array of relations that are members of the relation.
130 int nrelations The number of relations that are members of the relation.
131 ++++++++++++++++++++++++++++++++++++++*/
133 void AppendRouteRelation(RelationsX* relationsx,relation_t id,allow_t routes,
134 way_t *ways,int nways,
135 relation_t *relations,int nrelations)
138 FILESORT_VARINT size;
140 relation_t zerorelation=0;
143 relationx.routes=routes;
145 size=sizeof(RouteRelX)+(nways+1)*sizeof(way_t)+(nrelations+1)*sizeof(relation_t);
147 WriteFile(relationsx->rfd,&size,FILESORT_VARSIZE);
148 WriteFile(relationsx->rfd,&relationx,sizeof(RouteRelX));
150 WriteFile(relationsx->rfd,ways ,nways*sizeof(way_t));
151 WriteFile(relationsx->rfd,&zeroway, sizeof(way_t));
153 WriteFile(relationsx->rfd,relations ,nrelations*sizeof(relation_t));
154 WriteFile(relationsx->rfd,&zerorelation, sizeof(relation_t));
156 relationsx->rxnumber++;
158 assert(!(relationsx->rxnumber==0)); /* Zero marks the high-water mark for relations. */
162 /*++++++++++++++++++++++++++++++++++++++
163 Sort the list of relations.
165 RelationsX* relationsx The set of relations to process.
166 ++++++++++++++++++++++++++++++++++++++*/
168 void SortRelationList(RelationsX* relationsx)
170 /* Don't need to sort route relations */
174 /*++++++++++++++++++++++++++++++++++++++
175 Process the route relations and apply the information to the ways.
177 RelationsX *relationsx The set of relations to process.
179 WaysX *waysx The set of ways to update.
180 ++++++++++++++++++++++++++++++++++++++*/
182 void ProcessRouteRelations(RelationsX *relationsx,WaysX *waysx)
184 RouteRelX *unmatched=NULL,*lastunmatched=NULL;
185 int nunmatched=0,lastnunmatched=0,iteration=0;
190 /* Map into memory */
193 waysx->xdata=MapFileWriteable(waysx->filename);
196 /* Re-open the ways file read/write */
199 CloseFile(waysx->fd);
200 waysx->fd=ReOpenFileWriteable(waysx->filename);
203 /* Open the file and read through it */
205 relationsx->rfd=ReOpenFile(relationsx->rfilename);
209 int ways=0,relations=0;
212 SeekFile(relationsx->rfd,0);
214 /* Print the start message */
216 printf_first("Processing Route Relations: Iteration=%d Relations=0 Modified Ways=0",iteration);
218 for(i=0;i<relationsx->rxnumber;i++)
220 FILESORT_VARINT size;
223 relation_t relationid;
224 allow_t routes=Allow_None;
226 /* Read each route relation */
228 ReadFile(relationsx->rfd,&size,FILESORT_VARSIZE);
229 ReadFile(relationsx->rfd,&relationx,sizeof(RouteRelX));
231 /* Decide what type of route it is */
236 routes=relationx.routes;
242 for(j=0;j<lastnunmatched;j++)
243 if(lastunmatched[j].id==relationx.id)
247 if((lastunmatched[j].routes|relationx.routes)==relationx.routes)
248 routes=0; /* Nothing new to add */
250 routes=lastunmatched[j].routes;
255 /* Loop through the ways */
259 ReadFile(relationsx->rfd,&wayid,sizeof(way_t));
261 /* Update the ways that are listed for the relation */
265 index_t way=IndexWayX(waysx,wayid);
269 WayX *wayx=LookupWayX(waysx,way,1);
271 if(routes&Allow_Foot)
272 wayx->way.props|=Properties_FootRoute;
274 if(routes&Allow_Bicycle)
275 wayx->way.props|=Properties_BicycleRoute;
278 PutBackWayX(waysx,way,1);
287 /* Loop through the relations */
291 ReadFile(relationsx->rfd,&relationid,sizeof(relation_t));
293 /* Add the relations that are listed for this relation to the list for next time */
295 if(relationid && routes && relationid!=relationx.id)
297 if(nunmatched%256==0)
298 unmatched=(RouteRelX*)realloc((void*)unmatched,(nunmatched+256)*sizeof(RouteRelX));
300 unmatched[nunmatched].id=relationid;
301 unmatched[nunmatched].routes=routes;
309 printf_middle("Processing Route Relations: Iteration=%d Relations=%d Modified Ways=%d",iteration,relations,ways);
315 lastunmatched=unmatched;
316 lastnunmatched=nunmatched;
321 /* Print the final message */
323 printf_last("Processed Route Relations: Iteration=%d Relations=%d Modified Ways=%d",iteration,relations,ways);
325 while(lastnunmatched && ++iteration<5);
330 CloseFile(relationsx->rfd);
332 /* Unmap from memory */
335 waysx->xdata=UnmapFile(waysx->filename);
338 /* Re-open the ways file read only */
341 CloseFile(waysx->fd);
342 waysx->fd=ReOpenFile(waysx->filename);