Check out the latest version of Routino: svn co http://routino.org/svn/trunk routino
Annotation of /trunk/src/nodesx.c
Parent Directory
|
Revision Log
Revision 1101 -
(hide annotations)
(download)
(as text)
Sun Oct 21 13:51:27 2012 UTC (12 years, 4 months ago) by amb
File MIME type: text/x-csrc
File size: 18166 byte(s)
Sun Oct 21 13:51:27 2012 UTC (12 years, 4 months ago) by amb
File MIME type: text/x-csrc
File size: 18166 byte(s)
Remove some unused parts of the SortNodeListGeographically() function.
1 | amb | 110 | /*************************************** |
2 | Extented Node data type functions. | ||
3 | amb | 151 | |
4 | Part of the Routino routing software. | ||
5 | amb | 110 | ******************/ /****************** |
6 | amb | 947 | This file Copyright 2008-2012 Andrew M. Bishop |
7 | amb | 110 | |
8 | amb | 151 | 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 | amb | 110 | ***************************************/ |
21 | |||
22 | |||
23 | #include <assert.h> | ||
24 | #include <stdlib.h> | ||
25 | amb | 249 | #include <string.h> |
26 | amb | 110 | |
27 | #include "types.h" | ||
28 | amb | 449 | #include "nodes.h" |
29 | |||
30 | amb | 955 | #include "typesx.h" |
31 | amb | 110 | #include "nodesx.h" |
32 | #include "segmentsx.h" | ||
33 | amb | 204 | #include "waysx.h" |
34 | amb | 110 | |
35 | amb | 449 | #include "files.h" |
36 | amb | 519 | #include "logging.h" |
37 | amb | 532 | #include "sorting.h" |
38 | amb | 110 | |
39 | amb | 449 | |
40 | amb | 252 | /* Variables */ |
41 | amb | 110 | |
42 | amb | 289 | /*+ The command line '--tmpdir' option or its default value. +*/ |
43 | amb | 284 | extern char *option_tmpdirname; |
44 | amb | 110 | |
45 | amb | 284 | /*+ A temporary file-local variable for use by the sort functions. +*/ |
46 | static NodesX *sortnodesx; | ||
47 | |||
48 | amb | 110 | /* Functions */ |
49 | |||
50 | amb | 271 | static int sort_by_id(NodeX *a,NodeX *b); |
51 | amb | 680 | static int deduplicate_and_index_by_id(NodeX *nodex,index_t index); |
52 | amb | 271 | |
53 | amb | 281 | static int sort_by_lat_long(NodeX *a,NodeX *b); |
54 | amb | 1098 | static int index_by_lat_long(NodeX *nodex,index_t index); |
55 | amb | 110 | |
56 | |||
57 | /*++++++++++++++++++++++++++++++++++++++ | ||
58 | amb | 326 | Allocate a new node list (create a new file or open an existing one). |
59 | amb | 110 | |
60 | amb | 680 | NodesX *NewNodeList Returns a pointer to the node list. |
61 | amb | 326 | |
62 | int append Set to 1 if the file is to be opened for appending (now or later). | ||
63 | amb | 110 | ++++++++++++++++++++++++++++++++++++++*/ |
64 | |||
65 | amb | 326 | NodesX *NewNodeList(int append) |
66 | amb | 110 | { |
67 | NodesX *nodesx; | ||
68 | |||
69 | amb | 213 | nodesx=(NodesX*)calloc(1,sizeof(NodesX)); |
70 | amb | 110 | |
71 | amb | 243 | assert(nodesx); /* Check calloc() worked */ |
72 | |||
73 | amb | 284 | nodesx->filename=(char*)malloc(strlen(option_tmpdirname)+32); |
74 | amb | 216 | |
75 | amb | 326 | if(append) |
76 | amb | 447 | sprintf(nodesx->filename,"%s/nodesx.input.tmp",option_tmpdirname); |
77 | amb | 326 | else |
78 | amb | 788 | sprintf(nodesx->filename,"%s/nodesx.%p.tmp",option_tmpdirname,(void*)nodesx); |
79 | amb | 249 | |
80 | amb | 326 | if(append) |
81 | { | ||
82 | amb | 331 | off_t size; |
83 | amb | 326 | |
84 | amb | 502 | nodesx->fd=OpenFileAppend(nodesx->filename); |
85 | amb | 326 | |
86 | amb | 331 | size=SizeFile(nodesx->filename); |
87 | amb | 326 | |
88 | amb | 650 | nodesx->number=size/sizeof(NodeX); |
89 | amb | 326 | } |
90 | else | ||
91 | amb | 502 | nodesx->fd=OpenFileNew(nodesx->filename); |
92 | amb | 326 | |
93 | amb | 110 | return(nodesx); |
94 | } | ||
95 | |||
96 | |||
97 | /*++++++++++++++++++++++++++++++++++++++ | ||
98 | amb | 226 | Free a node list. |
99 | |||
100 | amb | 681 | NodesX *nodesx The set of nodes to be freed. |
101 | amb | 326 | |
102 | amb | 680 | int keep Set to 1 if the file is to be kept (for appending later). |
103 | amb | 226 | ++++++++++++++++++++++++++++++++++++++*/ |
104 | |||
105 | amb | 326 | void FreeNodeList(NodesX *nodesx,int keep) |
106 | amb | 226 | { |
107 | amb | 326 | if(!keep) |
108 | DeleteFile(nodesx->filename); | ||
109 | |||
110 | amb | 283 | free(nodesx->filename); |
111 | amb | 226 | |
112 | if(nodesx->idata) | ||
113 | free(nodesx->idata); | ||
114 | |||
115 | amb | 552 | if(nodesx->gdata) |
116 | free(nodesx->gdata); | ||
117 | amb | 226 | |
118 | amb | 1101 | if(nodesx->pdata) |
119 | free(nodesx->pdata); | ||
120 | |||
121 | amb | 283 | if(nodesx->super) |
122 | free(nodesx->super); | ||
123 | |||
124 | amb | 226 | free(nodesx); |
125 | } | ||
126 | |||
127 | |||
128 | /*++++++++++++++++++++++++++++++++++++++ | ||
129 | amb | 493 | Append a single node to an unsorted node list. |
130 | amb | 243 | |
131 | amb | 681 | NodesX *nodesx The set of nodes to modify. |
132 | amb | 243 | |
133 | amb | 680 | node_t id The node identifier from the original OSM data. |
134 | amb | 110 | |
135 | amb | 252 | double latitude The latitude of the node. |
136 | amb | 110 | |
137 | amb | 252 | double longitude The longitude of the node. |
138 | amb | 469 | |
139 | amb | 529 | transports_t allow The allowed traffic types through the node. |
140 | amb | 537 | |
141 | amb | 538 | uint16_t flags The flags to set for this node. |
142 | amb | 252 | ++++++++++++++++++++++++++++++++++++++*/ |
143 | amb | 110 | |
144 | amb | 681 | void AppendNode(NodesX *nodesx,node_t id,double latitude,double longitude,transports_t allow,uint16_t flags) |
145 | amb | 252 | { |
146 | NodeX nodex; | ||
147 | amb | 249 | |
148 | amb | 252 | nodex.id=id; |
149 | nodex.latitude =radians_to_latlong(latitude); | ||
150 | nodex.longitude=radians_to_latlong(longitude); | ||
151 | amb | 469 | nodex.allow=allow; |
152 | amb | 538 | nodex.flags=flags; |
153 | amb | 252 | |
154 | WriteFile(nodesx->fd,&nodex,sizeof(NodeX)); | ||
155 | |||
156 | amb | 650 | nodesx->number++; |
157 | amb | 466 | |
158 | amb | 650 | assert(nodesx->number<NODE_FAKE); /* NODE_FAKE marks the high-water mark for real nodes. */ |
159 | amb | 110 | } |
160 | |||
161 | |||
162 | /*++++++++++++++++++++++++++++++++++++++ | ||
163 | amb | 680 | Sort the node list. |
164 | amb | 110 | |
165 | amb | 681 | NodesX *nodesx The set of nodes to modify. |
166 | amb | 110 | ++++++++++++++++++++++++++++++++++++++*/ |
167 | |||
168 | amb | 681 | void SortNodeList(NodesX *nodesx) |
169 | amb | 110 | { |
170 | amb | 263 | int fd; |
171 | amb | 650 | index_t xnumber; |
172 | amb | 110 | |
173 | amb | 263 | /* Print the start message */ |
174 | |||
175 | amb | 519 | printf_first("Sorting Nodes"); |
176 | amb | 132 | |
177 | amb | 555 | /* Close the file (finished appending) */ |
178 | amb | 260 | |
179 | amb | 612 | nodesx->fd=CloseFile(nodesx->fd); |
180 | amb | 555 | |
181 | /* Re-open the file read-only and a new file writeable */ | ||
182 | |||
183 | amb | 260 | nodesx->fd=ReOpenFile(nodesx->filename); |
184 | |||
185 | amb | 271 | DeleteFile(nodesx->filename); |
186 | |||
187 | amb | 502 | fd=OpenFileNew(nodesx->filename); |
188 | amb | 271 | |
189 | amb | 252 | /* Allocate the array of indexes */ |
190 | amb | 249 | |
191 | amb | 650 | nodesx->idata=(node_t*)malloc(nodesx->number*sizeof(node_t)); |
192 | amb | 249 | |
193 | amb | 252 | assert(nodesx->idata); /* Check malloc() worked */ |
194 | amb | 249 | |
195 | amb | 1097 | /* Sort the nodes by ID and index them */ |
196 | amb | 249 | |
197 | amb | 650 | xnumber=nodesx->number; |
198 | nodesx->number=0; | ||
199 | |||
200 | amb | 271 | sortnodesx=nodesx; |
201 | amb | 110 | |
202 | amb | 948 | nodesx->number=filesort_fixed(nodesx->fd,fd,sizeof(NodeX),(int (*)(const void*,const void*))sort_by_id,(int (*)(void*,index_t))deduplicate_and_index_by_id); |
203 | amb | 252 | |
204 | amb | 555 | /* Close the files */ |
205 | amb | 260 | |
206 | amb | 612 | nodesx->fd=CloseFile(nodesx->fd); |
207 | amb | 257 | CloseFile(fd); |
208 | amb | 252 | |
209 | amb | 263 | /* Print the final message */ |
210 | |||
211 | amb | 790 | printf_last("Sorted Nodes: Nodes=%"Pindex_t" Duplicates=%"Pindex_t,xnumber,xnumber-nodesx->number); |
212 | amb | 110 | } |
213 | |||
214 | |||
215 | /*++++++++++++++++++++++++++++++++++++++ | ||
216 | Sort the nodes into id order. | ||
217 | |||
218 | int sort_by_id Returns the comparison of the id fields. | ||
219 | |||
220 | amb | 271 | NodeX *a The first extended node. |
221 | amb | 110 | |
222 | amb | 271 | NodeX *b The second extended node. |
223 | amb | 110 | ++++++++++++++++++++++++++++++++++++++*/ |
224 | |||
225 | amb | 271 | static int sort_by_id(NodeX *a,NodeX *b) |
226 | amb | 110 | { |
227 | amb | 271 | node_t a_id=a->id; |
228 | node_t b_id=b->id; | ||
229 | amb | 252 | |
230 | if(a_id<b_id) | ||
231 | return(-1); | ||
232 | else if(a_id>b_id) | ||
233 | amb | 249 | return(1); |
234 | amb | 110 | else |
235 | amb | 252 | return(0); |
236 | amb | 110 | } |
237 | |||
238 | |||
239 | amb | 271 | /*++++++++++++++++++++++++++++++++++++++ |
240 | amb | 680 | Create the index of identifiers and discard duplicate nodes. |
241 | amb | 252 | |
242 | amb | 680 | int deduplicate_and_index_by_id Return 1 if the value is to be kept, otherwise 0. |
243 | amb | 273 | |
244 | amb | 271 | NodeX *nodex The extended node. |
245 | amb | 252 | |
246 | amb | 1101 | index_t index The index of this node in the total that have been kept. |
247 | amb | 271 | ++++++++++++++++++++++++++++++++++++++*/ |
248 | |||
249 | amb | 680 | static int deduplicate_and_index_by_id(NodeX *nodex,index_t index) |
250 | amb | 271 | { |
251 | amb | 273 | if(index==0 || sortnodesx->idata[index-1]!=nodex->id) |
252 | { | ||
253 | sortnodesx->idata[index]=nodex->id; | ||
254 | amb | 271 | |
255 | amb | 273 | return(1); |
256 | } | ||
257 | amb | 812 | else |
258 | { | ||
259 | logerror("Node %"Pnode_t" is duplicated\n",nodex->id); | ||
260 | amb | 273 | |
261 | amb | 812 | return(0); |
262 | } | ||
263 | amb | 271 | } |
264 | |||
265 | |||
266 | amb | 110 | /*++++++++++++++++++++++++++++++++++++++ |
267 | amb | 212 | Sort the node list geographically. |
268 | |||
269 | amb | 681 | NodesX *nodesx The set of nodes to modify. |
270 | amb | 212 | ++++++++++++++++++++++++++++++++++++++*/ |
271 | |||
272 | amb | 681 | void SortNodeListGeographically(NodesX *nodesx) |
273 | amb | 212 | { |
274 | amb | 281 | int fd; |
275 | amb | 212 | |
276 | amb | 263 | /* Print the start message */ |
277 | |||
278 | amb | 1101 | printf_first("Sorting Nodes Geographically"); |
279 | amb | 212 | |
280 | amb | 552 | /* Allocate the memory for the geographical index array */ |
281 | |||
282 | nodesx->gdata=(index_t*)malloc(nodesx->number*sizeof(index_t)); | ||
283 | |||
284 | assert(nodesx->gdata); /* Check malloc() worked */ | ||
285 | |||
286 | amb | 555 | /* Re-open the file read-only and a new file writeable */ |
287 | amb | 212 | |
288 | amb | 281 | nodesx->fd=ReOpenFile(nodesx->filename); |
289 | amb | 243 | |
290 | amb | 281 | DeleteFile(nodesx->filename); |
291 | amb | 212 | |
292 | amb | 502 | fd=OpenFileNew(nodesx->filename); |
293 | amb | 212 | |
294 | amb | 1097 | /* Sort nodes geographically and index them */ |
295 | amb | 252 | |
296 | amb | 281 | sortnodesx=nodesx; |
297 | amb | 257 | |
298 | amb | 1101 | filesort_fixed(nodesx->fd,fd,sizeof(NodeX),(int (*)(const void*,const void*))sort_by_lat_long,(int (*)(void*,index_t))index_by_lat_long); |
299 | amb | 257 | |
300 | amb | 555 | /* Close the files */ |
301 | amb | 257 | |
302 | amb | 612 | nodesx->fd=CloseFile(nodesx->fd); |
303 | amb | 281 | CloseFile(fd); |
304 | amb | 257 | |
305 | amb | 263 | /* Print the final message */ |
306 | amb | 257 | |
307 | amb | 1101 | printf_last("Sorted Nodes Geographically: Nodes=%"Pindex_t,nodesx->number); |
308 | amb | 212 | } |
309 | |||
310 | |||
311 | /*++++++++++++++++++++++++++++++++++++++ | ||
312 | amb | 680 | Sort the nodes into latitude and longitude order (first by longitude bin |
313 | number, then by latitude bin number and then by exact longitude and then by | ||
314 | exact latitude). | ||
315 | amb | 110 | |
316 | int sort_by_lat_long Returns the comparison of the latitude and longitude fields. | ||
317 | |||
318 | amb | 281 | NodeX *a The first extended node. |
319 | amb | 110 | |
320 | amb | 281 | NodeX *b The second extended node. |
321 | amb | 110 | ++++++++++++++++++++++++++++++++++++++*/ |
322 | |||
323 | amb | 281 | static int sort_by_lat_long(NodeX *a,NodeX *b) |
324 | amb | 110 | { |
325 | amb | 1098 | ll_bin_t a_lon=latlong_to_bin(a->longitude); |
326 | ll_bin_t b_lon=latlong_to_bin(b->longitude); | ||
327 | amb | 110 | |
328 | amb | 1098 | if(a_lon<b_lon) |
329 | return(-1); | ||
330 | else if(a_lon>b_lon) | ||
331 | amb | 949 | return(1); |
332 | amb | 110 | else |
333 | { | ||
334 | amb | 1098 | ll_bin_t a_lat=latlong_to_bin(a->latitude); |
335 | ll_bin_t b_lat=latlong_to_bin(b->latitude); | ||
336 | amb | 110 | |
337 | amb | 1098 | if(a_lat<b_lat) |
338 | amb | 110 | return(-1); |
339 | amb | 1098 | else if(a_lat>b_lat) |
340 | amb | 110 | return(1); |
341 | else | ||
342 | amb | 281 | { |
343 | amb | 1098 | if(a->longitude<b->longitude) |
344 | amb | 281 | return(-1); |
345 | amb | 1098 | else if(a->longitude>b->longitude) |
346 | amb | 281 | return(1); |
347 | else | ||
348 | amb | 664 | { |
349 | amb | 1098 | if(a->latitude<b->latitude) |
350 | amb | 664 | return(-1); |
351 | amb | 1098 | else if(a->latitude>b->latitude) |
352 | amb | 664 | return(1); |
353 | amb | 1098 | } |
354 | amb | 949 | |
355 | amb | 1098 | return(0); |
356 | amb | 281 | } |
357 | amb | 110 | } |
358 | } | ||
359 | |||
360 | |||
361 | /*++++++++++++++++++++++++++++++++++++++ | ||
362 | amb | 1098 | Create the index between the sorted and unsorted nodes. |
363 | amb | 281 | |
364 | amb | 1098 | int index_by_lat_long Return 1 if the value is to be kept, otherwise 0. |
365 | amb | 281 | |
366 | NodeX *nodex The extended node. | ||
367 | |||
368 | amb | 1101 | index_t index The index of this node in the total that have been kept. |
369 | amb | 281 | ++++++++++++++++++++++++++++++++++++++*/ |
370 | |||
371 | amb | 1098 | static int index_by_lat_long(NodeX *nodex,index_t index) |
372 | amb | 281 | { |
373 | amb | 552 | sortnodesx->gdata[nodex->id]=index; |
374 | |||
375 | amb | 281 | return(1); |
376 | } | ||
377 | |||
378 | |||
379 | /*++++++++++++++++++++++++++++++++++++++ | ||
380 | amb | 285 | Find a particular node index. |
381 | |||
382 | index_t IndexNodeX Returns the index of the extended node with the specified id. | ||
383 | |||
384 | amb | 681 | NodesX *nodesx The set of nodes to use. |
385 | amb | 285 | |
386 | node_t id The node id to look for. | ||
387 | ++++++++++++++++++++++++++++++++++++++*/ | ||
388 | |||
389 | amb | 681 | index_t IndexNodeX(NodesX *nodesx,node_t id) |
390 | amb | 285 | { |
391 | amb | 780 | index_t start=0; |
392 | index_t end=nodesx->number-1; | ||
393 | index_t mid; | ||
394 | amb | 285 | |
395 | /* Binary search - search key exact match only is required. | ||
396 | * | ||
397 | * # <- start | Check mid and move start or end if it doesn't match | ||
398 | * # | | ||
399 | * # | Since an exact match is wanted we can set end=mid-1 | ||
400 | * # <- mid | or start=mid+1 because we know that mid doesn't match. | ||
401 | * # | | ||
402 | * # | Eventually either end=start or end=start+1 and one of | ||
403 | * # <- end | start or end is the wanted one. | ||
404 | */ | ||
405 | |||
406 | if(end<start) /* There are no nodes */ | ||
407 | return(NO_NODE); | ||
408 | else if(id<nodesx->idata[start]) /* Check key is not before start */ | ||
409 | return(NO_NODE); | ||
410 | else if(id>nodesx->idata[end]) /* Check key is not after end */ | ||
411 | return(NO_NODE); | ||
412 | else | ||
413 | { | ||
414 | do | ||
415 | { | ||
416 | mid=(start+end)/2; /* Choose mid point */ | ||
417 | |||
418 | if(nodesx->idata[mid]<id) /* Mid point is too low */ | ||
419 | start=mid+1; | ||
420 | else if(nodesx->idata[mid]>id) /* Mid point is too high */ | ||
421 | amb | 843 | end=mid?(mid-1):mid; |
422 | amb | 285 | else /* Mid point is correct */ |
423 | return(mid); | ||
424 | } | ||
425 | while((end-start)>1); | ||
426 | |||
427 | if(nodesx->idata[start]==id) /* Start is correct */ | ||
428 | return(start); | ||
429 | |||
430 | if(nodesx->idata[end]==id) /* End is correct */ | ||
431 | return(end); | ||
432 | } | ||
433 | |||
434 | return(NO_NODE); | ||
435 | } | ||
436 | |||
437 | |||
438 | /*++++++++++++++++++++++++++++++++++++++ | ||
439 | amb | 110 | Remove any nodes that are not part of a highway. |
440 | |||
441 | amb | 681 | NodesX *nodesx The set of nodes to modify. |
442 | amb | 110 | |
443 | amb | 680 | SegmentsX *segmentsx The set of segments to use. |
444 | amb | 110 | ++++++++++++++++++++++++++++++++++++++*/ |
445 | |||
446 | void RemoveNonHighwayNodes(NodesX *nodesx,SegmentsX *segmentsx) | ||
447 | { | ||
448 | amb | 263 | NodeX nodex; |
449 | amb | 780 | index_t total=0,highway=0,nothighway=0; |
450 | amb | 263 | int fd; |
451 | amb | 110 | |
452 | amb | 263 | /* Print the start message */ |
453 | |||
454 | amb | 627 | printf_first("Checking Nodes: Nodes=0"); |
455 | amb | 227 | |
456 | amb | 555 | /* Re-open the file read-only and a new file writeable */ |
457 | amb | 263 | |
458 | amb | 555 | nodesx->fd=ReOpenFile(nodesx->filename); |
459 | |||
460 | amb | 263 | DeleteFile(nodesx->filename); |
461 | |||
462 | amb | 502 | fd=OpenFileNew(nodesx->filename); |
463 | amb | 263 | |
464 | amb | 555 | /* Modify the on-disk image */ |
465 | |||
466 | amb | 263 | while(!ReadFile(nodesx->fd,&nodex,sizeof(NodeX))) |
467 | amb | 110 | { |
468 | amb | 655 | if(!IsBitSet(segmentsx->usednode,total)) |
469 | amb | 271 | nothighway++; |
470 | else | ||
471 | amb | 263 | { |
472 | amb | 280 | nodex.id=highway; |
473 | amb | 1098 | nodesx->idata[highway]=nodesx->idata[total]; |
474 | amb | 280 | |
475 | amb | 263 | WriteFile(fd,&nodex,sizeof(NodeX)); |
476 | |||
477 | amb | 110 | highway++; |
478 | amb | 263 | } |
479 | amb | 110 | |
480 | amb | 263 | total++; |
481 | |||
482 | if(!(total%10000)) | ||
483 | amb | 790 | printf_middle("Checking Nodes: Nodes=%"Pindex_t" Highway=%"Pindex_t" not-Highway=%"Pindex_t,total,highway,nothighway); |
484 | amb | 110 | } |
485 | |||
486 | amb | 555 | nodesx->number=highway; |
487 | amb | 263 | |
488 | amb | 555 | /* Close the files */ |
489 | |||
490 | amb | 612 | nodesx->fd=CloseFile(nodesx->fd); |
491 | amb | 263 | CloseFile(fd); |
492 | |||
493 | amb | 643 | /* Free the now-unneeded index */ |
494 | |||
495 | free(segmentsx->usednode); | ||
496 | segmentsx->usednode=NULL; | ||
497 | |||
498 | amb | 263 | /* Print the final message */ |
499 | |||
500 | amb | 790 | printf_last("Checked Nodes: Nodes=%"Pindex_t" Highway=%"Pindex_t" not-Highway=%"Pindex_t,total,highway,nothighway); |
501 | amb | 110 | } |
502 | |||
503 | |||
504 | /*++++++++++++++++++++++++++++++++++++++ | ||
505 | amb | 1098 | Remove any nodes that have been pruned. |
506 | |||
507 | NodesX *nodesx The set of nodes to prune. | ||
508 | |||
509 | SegmentsX *segmentsx The set of segments to use. | ||
510 | ++++++++++++++++++++++++++++++++++++++*/ | ||
511 | |||
512 | void RemovePrunedNodes(NodesX *nodesx,SegmentsX *segmentsx) | ||
513 | { | ||
514 | NodeX nodex; | ||
515 | index_t total=0,pruned=0,notpruned=0; | ||
516 | int fd; | ||
517 | |||
518 | /* Print the start message */ | ||
519 | |||
520 | printf_first("Deleting Pruned Nodes: Nodes=0 Pruned=0"); | ||
521 | |||
522 | /* Allocate the array of indexes */ | ||
523 | |||
524 | nodesx->pdata=(index_t*)malloc(nodesx->number*sizeof(index_t)); | ||
525 | |||
526 | assert(nodesx->pdata); /* Check malloc() worked */ | ||
527 | |||
528 | /* Re-open the file read-only and a new file writeable */ | ||
529 | |||
530 | nodesx->fd=ReOpenFile(nodesx->filename); | ||
531 | |||
532 | DeleteFile(nodesx->filename); | ||
533 | |||
534 | fd=OpenFileNew(nodesx->filename); | ||
535 | |||
536 | /* Modify the on-disk image */ | ||
537 | |||
538 | while(!ReadFile(nodesx->fd,&nodex,sizeof(NodeX))) | ||
539 | { | ||
540 | if(segmentsx->firstnode[total]==NO_SEGMENT) | ||
541 | amb | 1099 | { |
542 | amb | 1098 | pruned++; |
543 | amb | 1099 | |
544 | nodesx->pdata[total]=NO_NODE; | ||
545 | } | ||
546 | amb | 1098 | else |
547 | { | ||
548 | nodex.id=notpruned; | ||
549 | nodesx->pdata[total]=notpruned; | ||
550 | |||
551 | WriteFile(fd,&nodex,sizeof(NodeX)); | ||
552 | |||
553 | notpruned++; | ||
554 | } | ||
555 | |||
556 | total++; | ||
557 | |||
558 | if(!(total%10000)) | ||
559 | printf_middle("Deleting Pruned Nodes: Nodes=%"Pindex_t" Pruned=%"Pindex_t,total,pruned); | ||
560 | } | ||
561 | |||
562 | nodesx->number=notpruned; | ||
563 | |||
564 | /* Close the files */ | ||
565 | |||
566 | nodesx->fd=CloseFile(nodesx->fd); | ||
567 | CloseFile(fd); | ||
568 | |||
569 | /* Print the final message */ | ||
570 | |||
571 | printf_last("Deleted Pruned Nodes: Nodes=%"Pindex_t" Pruned=%"Pindex_t,total,pruned); | ||
572 | } | ||
573 | |||
574 | |||
575 | /*++++++++++++++++++++++++++++++++++++++ | ||
576 | amb | 643 | Insert the super-node flag and the first segment indexes after geographical sorting. |
577 | amb | 110 | |
578 | amb | 681 | NodesX *nodesx The set of nodes to modify. |
579 | amb | 110 | |
580 | amb | 643 | SegmentsX *segmentsx The set of segments to use. |
581 | amb | 110 | ++++++++++++++++++++++++++++++++++++++*/ |
582 | |||
583 | amb | 653 | void UpdateNodes(NodesX *nodesx,SegmentsX *segmentsx) |
584 | amb | 110 | { |
585 | amb | 214 | index_t i; |
586 | amb | 643 | int fd; |
587 | amb | 1097 | ll_bin_t lat_min_bin,lat_max_bin,lon_min_bin,lon_max_bin; |
588 | latlong_t lat_min,lat_max,lon_min,lon_max; | ||
589 | amb | 110 | |
590 | amb | 275 | /* Print the start message */ |
591 | |||
592 | amb | 1097 | printf_first("Updating Nodes: Nodes=0"); |
593 | amb | 227 | |
594 | amb | 1097 | /* While we are here we can work out the range of data */ |
595 | |||
596 | lat_min=radians_to_latlong( 2); | ||
597 | lat_max=radians_to_latlong(-2); | ||
598 | lon_min=radians_to_latlong( 4); | ||
599 | lon_max=radians_to_latlong(-4); | ||
600 | |||
601 | amb | 643 | /* Re-open the file read-only and a new file writeable */ |
602 | amb | 275 | |
603 | amb | 643 | nodesx->fd=ReOpenFile(nodesx->filename); |
604 | amb | 554 | |
605 | amb | 643 | DeleteFile(nodesx->filename); |
606 | amb | 212 | |
607 | amb | 643 | fd=OpenFileNew(nodesx->filename); |
608 | |||
609 | /* Modify the on-disk image */ | ||
610 | |||
611 | amb | 110 | for(i=0;i<nodesx->number;i++) |
612 | { | ||
613 | amb | 643 | NodeX nodex; |
614 | amb | 208 | |
615 | amb | 643 | ReadFile(nodesx->fd,&nodex,sizeof(NodeX)); |
616 | amb | 212 | |
617 | amb | 654 | if(IsBitSet(nodesx->super,nodex.id)) |
618 | amb | 643 | nodex.flags|=NODE_SUPER; |
619 | amb | 212 | |
620 | amb | 674 | nodex.id=segmentsx->firstnode[nodesx->gdata[nodex.id]]; |
621 | amb | 448 | |
622 | amb | 643 | WriteFile(fd,&nodex,sizeof(NodeX)); |
623 | |||
624 | amb | 1097 | if(nodex.latitude<lat_min) |
625 | lat_min=nodex.latitude; | ||
626 | if(nodex.latitude>lat_max) | ||
627 | lat_max=nodex.latitude; | ||
628 | if(nodex.longitude<lon_min) | ||
629 | lon_min=nodex.longitude; | ||
630 | if(nodex.longitude>lon_max) | ||
631 | lon_max=nodex.longitude; | ||
632 | |||
633 | amb | 110 | if(!((i+1)%10000)) |
634 | amb | 1097 | printf_middle("Updating Nodes: Nodes=%"Pindex_t,i+1); |
635 | amb | 110 | } |
636 | |||
637 | amb | 643 | /* Close the files */ |
638 | |||
639 | nodesx->fd=CloseFile(nodesx->fd); | ||
640 | CloseFile(fd); | ||
641 | |||
642 | amb | 947 | /* Free the memory */ |
643 | |||
644 | free(nodesx->super); | ||
645 | nodesx->super=NULL; | ||
646 | |||
647 | amb | 1097 | /* Work out the number of bins */ |
648 | |||
649 | lat_min_bin=latlong_to_bin(lat_min); | ||
650 | lon_min_bin=latlong_to_bin(lon_min); | ||
651 | lat_max_bin=latlong_to_bin(lat_max); | ||
652 | lon_max_bin=latlong_to_bin(lon_max); | ||
653 | |||
654 | nodesx->latzero=lat_min_bin; | ||
655 | nodesx->lonzero=lon_min_bin; | ||
656 | |||
657 | nodesx->latbins=(lat_max_bin-lat_min_bin)+1; | ||
658 | nodesx->lonbins=(lon_max_bin-lon_min_bin)+1; | ||
659 | |||
660 | amb | 275 | /* Print the final message */ |
661 | |||
662 | amb | 1097 | printf_last("Updated Nodes: Nodes=%"Pindex_t,nodesx->number); |
663 | amb | 110 | } |
664 | |||
665 | |||
666 | /*++++++++++++++++++++++++++++++++++++++ | ||
667 | amb | 680 | Save the final node list database to a file. |
668 | amb | 285 | |
669 | amb | 681 | NodesX *nodesx The set of nodes to save. |
670 | amb | 285 | |
671 | const char *filename The name of the file to save. | ||
672 | ++++++++++++++++++++++++++++++++++++++*/ | ||
673 | |||
674 | amb | 681 | void SaveNodeList(NodesX *nodesx,const char *filename) |
675 | amb | 285 | { |
676 | index_t i; | ||
677 | int fd; | ||
678 | amb | 500 | NodesFile nodesfile={0}; |
679 | amb | 780 | index_t super_number=0; |
680 | amb | 788 | ll_bin2_t latlonbin=0,maxlatlonbins; |
681 | amb | 780 | index_t *offsets; |
682 | amb | 285 | |
683 | /* Print the start message */ | ||
684 | |||
685 | amb | 519 | printf_first("Writing Nodes: Nodes=0"); |
686 | amb | 285 | |
687 | amb | 553 | /* Allocate the memory for the geographical offsets array */ |
688 | |||
689 | offsets=(index_t*)malloc((nodesx->latbins*nodesx->lonbins+1)*sizeof(index_t)); | ||
690 | |||
691 | assert(offsets); /* Check malloc() worked */ | ||
692 | |||
693 | latlonbin=0; | ||
694 | |||
695 | amb | 556 | /* Re-open the file */ |
696 | amb | 285 | |
697 | amb | 555 | nodesx->fd=ReOpenFile(nodesx->filename); |
698 | amb | 285 | |
699 | amb | 461 | /* Write out the nodes data */ |
700 | amb | 285 | |
701 | amb | 502 | fd=OpenFileNew(filename); |
702 | amb | 285 | |
703 | amb | 553 | SeekFile(fd,sizeof(NodesFile)+(nodesx->latbins*nodesx->lonbins+1)*sizeof(index_t)); |
704 | amb | 285 | |
705 | amb | 461 | for(i=0;i<nodesx->number;i++) |
706 | amb | 285 | { |
707 | amb | 556 | NodeX nodex; |
708 | amb | 944 | Node node={0}; |
709 | amb | 553 | ll_bin_t latbin,lonbin; |
710 | amb | 780 | ll_bin2_t llbin; |
711 | amb | 285 | |
712 | amb | 556 | ReadFile(nodesx->fd,&nodex,sizeof(NodeX)); |
713 | |||
714 | amb | 553 | /* Create the Node */ |
715 | |||
716 | amb | 556 | node.latoffset=latlong_to_off(nodex.latitude); |
717 | node.lonoffset=latlong_to_off(nodex.longitude); | ||
718 | node.firstseg=nodex.id; | ||
719 | node.allow=nodex.allow; | ||
720 | node.flags=nodex.flags; | ||
721 | amb | 552 | |
722 | if(node.flags&NODE_SUPER) | ||
723 | amb | 461 | super_number++; |
724 | |||
725 | amb | 553 | /* Work out the offsets */ |
726 | |||
727 | amb | 556 | latbin=latlong_to_bin(nodex.latitude )-nodesx->latzero; |
728 | lonbin=latlong_to_bin(nodex.longitude)-nodesx->lonzero; | ||
729 | amb | 553 | llbin=lonbin*nodesx->latbins+latbin; |
730 | |||
731 | for(;latlonbin<=llbin;latlonbin++) | ||
732 | offsets[latlonbin]=i; | ||
733 | |||
734 | /* Write the data */ | ||
735 | |||
736 | amb | 552 | WriteFile(fd,&node,sizeof(Node)); |
737 | amb | 285 | |
738 | if(!((i+1)%10000)) | ||
739 | amb | 790 | printf_middle("Writing Nodes: Nodes=%"Pindex_t,i+1); |
740 | amb | 285 | } |
741 | |||
742 | amb | 556 | /* Close the file */ |
743 | |||
744 | amb | 612 | nodesx->fd=CloseFile(nodesx->fd); |
745 | amb | 556 | |
746 | amb | 553 | /* Finish off the offset indexing and write them out */ |
747 | |||
748 | amb | 788 | maxlatlonbins=nodesx->latbins*nodesx->lonbins; |
749 | |||
750 | for(;latlonbin<=maxlatlonbins;latlonbin++) | ||
751 | amb | 553 | offsets[latlonbin]=nodesx->number; |
752 | |||
753 | SeekFile(fd,sizeof(NodesFile)); | ||
754 | WriteFile(fd,offsets,(nodesx->latbins*nodesx->lonbins+1)*sizeof(index_t)); | ||
755 | |||
756 | amb | 795 | free(offsets); |
757 | |||
758 | amb | 461 | /* Write out the header structure */ |
759 | |||
760 | nodesfile.number=nodesx->number; | ||
761 | nodesfile.snumber=super_number; | ||
762 | |||
763 | nodesfile.latbins=nodesx->latbins; | ||
764 | nodesfile.lonbins=nodesx->lonbins; | ||
765 | |||
766 | nodesfile.latzero=nodesx->latzero; | ||
767 | nodesfile.lonzero=nodesx->lonzero; | ||
768 | |||
769 | SeekFile(fd,0); | ||
770 | WriteFile(fd,&nodesfile,sizeof(NodesFile)); | ||
771 | |||
772 | amb | 285 | CloseFile(fd); |
773 | |||
774 | /* Print the final message */ | ||
775 | |||
776 | amb | 790 | printf_last("Wrote Nodes: Nodes=%"Pindex_t,nodesx->number); |
777 | amb | 285 | } |
Properties
Name | Value |
---|---|
cvs:description | Extended nodes functions. |