ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
shptree.c
Go to the documentation of this file.
1 /******************************************************************************
2  * $Id$
3  *
4  * Project: Shapelib
5  * Purpose: Implementation of quadtree building and searching functions.
6  * Author: Frank Warmerdam, warmerdam@pobox.com
7  *
8  ******************************************************************************
9  * Copyright (c) 1999, Frank Warmerdam
10  * Copyright (c) 2012, Even Rouault <even dot rouault at mines-paris dot org>
11  *
12  * This software is available under the following "MIT Style" license,
13  * or at the option of the licensee under the LGPL (see COPYING). This
14  * option is discussed in more detail in shapelib.html.
15  *
16  * --
17  *
18  * Permission is hereby granted, free of charge, to any person obtaining a
19  * copy of this software and associated documentation files (the "Software"),
20  * to deal in the Software without restriction, including without limitation
21  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
22  * and/or sell copies of the Software, and to permit persons to whom the
23  * Software is furnished to do so, subject to the following conditions:
24  *
25  * The above copyright notice and this permission notice shall be included
26  * in all copies or substantial portions of the Software.
27  *
28  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
29  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
30  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
31  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
32  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
33  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
34  * DEALINGS IN THE SOFTWARE.
35  ******************************************************************************
36  *
37  * $Log$
38  * Revision 1.20 2018-08-16 15:39:07 erouault
39  * * shpopen.c, dbfopen.c, shptree.c, sbnsearch.c: resyc with GDAL
40  * internal shapelib. Mostly to allow building those files as C++
41  * without warning. Also add FTDate entry in DBFFieldType
42  * (see https://github.com/OSGeo/gdal/pull/308). And some other
43  * code cleanups
44  *
45  * Revision 1.19 2016-12-05 12:44:06 erouault
46  * * Major overhaul of Makefile build system to use autoconf/automake.
47  *
48  * * Warning fixes in contrib/
49  *
50  * Revision 1.18 2016-12-04 15:30:15 erouault
51  * * shpopen.c, dbfopen.c, shptree.c, shapefil.h: resync with
52  * GDAL Shapefile driver. Mostly cleanups. SHPObject and DBFInfo
53  * structures extended with new members. New functions:
54  * DBFSetLastModifiedDate, SHPOpenLLEx, SHPRestoreSHX,
55  * SHPSetFastModeReadObject
56  *
57  * * sbnsearch.c: new file to implement original ESRI .sbn spatial
58  * index reading. (no write support). New functions:
59  * SBNOpenDiskTree, SBNCloseDiskTree, SBNSearchDiskTree,
60  * SBNSearchDiskTreeInteger, SBNSearchFreeIds
61  *
62  * * Makefile, makefile.vc, CMakeLists.txt, shapelib.def: updates
63  * with new file and symbols.
64  *
65  * * commit: helper script to cvs commit
66  *
67  * Revision 1.17 2012-01-27 21:09:26 fwarmerdam
68  * optimize .qix output (gdal #4472)
69  *
70  * Revision 1.16 2011-12-11 22:26:46 fwarmerdam
71  * upgrade .qix access code to use SAHooks (gdal #3365)
72  *
73  * Revision 1.15 2011-07-24 05:59:25 fwarmerdam
74  * minimize use of CPLError in favor of SAHooks.Error()
75  *
76  * Revision 1.14 2010-08-27 23:43:27 fwarmerdam
77  * add SHPAPI_CALL attribute in code
78  *
79  * Revision 1.13 2010-06-29 05:50:15 fwarmerdam
80  * fix sign of Z/M comparisons in SHPCheckObjectContained (#2223)
81  *
82  * Revision 1.12 2008-11-12 15:39:50 fwarmerdam
83  * improve safety in face of buggy .shp file.
84  *
85  * Revision 1.11 2007/10/27 03:31:14 fwarmerdam
86  * limit default depth of tree to 12 levels (gdal ticket #1594)
87  *
88  * Revision 1.10 2005/01/03 22:30:13 fwarmerdam
89  * added support for saved quadtrees
90  *
91  * Revision 1.9 2003/01/28 15:53:41 warmerda
92  * Avoid build warnings.
93  *
94  * Revision 1.8 2002/05/07 13:07:45 warmerda
95  * use qsort() - patch from Bernhard Herzog
96  *
97  * Revision 1.7 2002/01/15 14:36:07 warmerda
98  * updated email address
99  *
100  * Revision 1.6 2001/05/23 13:36:52 warmerda
101  * added use of SHPAPI_CALL
102  *
103  * Revision 1.5 1999/11/05 14:12:05 warmerda
104  * updated license terms
105  *
106  * Revision 1.4 1999/06/02 18:24:21 warmerda
107  * added trimming code
108  *
109  * Revision 1.3 1999/06/02 17:56:12 warmerda
110  * added quad'' subnode support for trees
111  *
112  * Revision 1.2 1999/05/18 19:11:11 warmerda
113  * Added example searching capability
114  *
115  * Revision 1.1 1999/05/18 17:49:20 warmerda
116  * New
117  *
118  */
119 
120 #include "shapefil.h"
121 
122 #include <math.h>
123 #include <assert.h>
124 #include <stdlib.h>
125 #include <string.h>
126 #include <limits.h>
127 
128 #ifdef USE_CPL
129 #include "cpl_error.h"
130 #endif
131 
132 SHP_CVSID("$Id$")
133 
134 #ifndef TRUE
135 # define TRUE 1
136 # define FALSE 0
137 #endif
138 
139 static int bBigEndian = 0;
140 
141 
142 /* -------------------------------------------------------------------- */
143 /* If the following is 0.5, nodes will be split in half. If it */
144 /* is 0.6 then each subnode will contain 60% of the parent */
145 /* node, with 20% representing overlap. This can be help to */
146 /* prevent small objects on a boundary from shifting too high */
147 /* up the tree. */
148 /* -------------------------------------------------------------------- */
149 
150 #define SHP_SPLIT_RATIO 0.55
151 
152 #ifdef __cplusplus
153 #define STATIC_CAST(type,x) static_cast<type>(x)
154 #define REINTERPRET_CAST(type,x) reinterpret_cast<type>(x)
155 #define CONST_CAST(type,x) const_cast<type>(x)
156 #define SHPLIB_NULLPTR nullptr
157 #else
158 #define STATIC_CAST(type,x) ((type)(x))
159 #define REINTERPRET_CAST(type,x) ((type)(x))
160 #define CONST_CAST(type,x) ((type)(x))
161 #define SHPLIB_NULLPTR NULL
162 #endif
163 
164 /************************************************************************/
165 /* SfRealloc() */
166 /* */
167 /* A realloc cover function that will access a NULL pointer as */
168 /* a valid input. */
169 /************************************************************************/
170 
171 static void * SfRealloc( void * pMem, int nNewSize )
172 
173 {
174  if( pMem == SHPLIB_NULLPTR )
175  return malloc(nNewSize);
176  else
177  return realloc(pMem,nNewSize);
178 }
179 
180 /************************************************************************/
181 /* SHPTreeNodeInit() */
182 /* */
183 /* Initialize a tree node. */
184 /************************************************************************/
185 
186 static SHPTreeNode *SHPTreeNodeCreate( double * padfBoundsMin,
187  double * padfBoundsMax )
188 
189 {
190  SHPTreeNode *psTreeNode;
191 
192  psTreeNode = STATIC_CAST(SHPTreeNode *, malloc(sizeof(SHPTreeNode)));
193  if( SHPLIB_NULLPTR == psTreeNode )
194  return SHPLIB_NULLPTR;
195 
196  psTreeNode->nShapeCount = 0;
197  psTreeNode->panShapeIds = SHPLIB_NULLPTR;
198  psTreeNode->papsShapeObj = SHPLIB_NULLPTR;
199 
200  psTreeNode->nSubNodes = 0;
201 
202  if( padfBoundsMin != SHPLIB_NULLPTR )
203  memcpy( psTreeNode->adfBoundsMin, padfBoundsMin, sizeof(double) * 4 );
204 
205  if( padfBoundsMax != SHPLIB_NULLPTR )
206  memcpy( psTreeNode->adfBoundsMax, padfBoundsMax, sizeof(double) * 4 );
207 
208  return psTreeNode;
209 }
210 
211 
212 /************************************************************************/
213 /* SHPCreateTree() */
214 /************************************************************************/
215 
217  SHPCreateTree( SHPHandle hSHP, int nDimension, int nMaxDepth,
218  double *padfBoundsMin, double *padfBoundsMax )
219 
220 {
221  SHPTree *psTree;
222 
223  if( padfBoundsMin == SHPLIB_NULLPTR && hSHP == SHPLIB_NULLPTR )
224  return SHPLIB_NULLPTR;
225 
226 /* -------------------------------------------------------------------- */
227 /* Allocate the tree object */
228 /* -------------------------------------------------------------------- */
229  psTree = STATIC_CAST(SHPTree *, malloc(sizeof(SHPTree)));
230  if( SHPLIB_NULLPTR == psTree )
231  {
232  return SHPLIB_NULLPTR;
233  }
234 
235  psTree->hSHP = hSHP;
236  psTree->nMaxDepth = nMaxDepth;
237  psTree->nDimension = nDimension;
238  psTree->nTotalCount = 0;
239 
240 /* -------------------------------------------------------------------- */
241 /* If no max depth was defined, try to select a reasonable one */
242 /* that implies approximately 8 shapes per node. */
243 /* -------------------------------------------------------------------- */
244  if( psTree->nMaxDepth == 0 && hSHP != SHPLIB_NULLPTR )
245  {
246  int nMaxNodeCount = 1;
247  int nShapeCount;
248 
249  SHPGetInfo( hSHP, &nShapeCount, SHPLIB_NULLPTR, SHPLIB_NULLPTR, SHPLIB_NULLPTR );
250  while( nMaxNodeCount*4 < nShapeCount )
251  {
252  psTree->nMaxDepth += 1;
253  nMaxNodeCount = nMaxNodeCount * 2;
254  }
255 
256 #ifdef USE_CPL
257  CPLDebug( "Shape",
258  "Estimated spatial index tree depth: %d",
259  psTree->nMaxDepth );
260 #endif
261 
262  /* NOTE: Due to problems with memory allocation for deep trees,
263  * automatically estimated depth is limited up to 12 levels.
264  * See Ticket #1594 for detailed discussion.
265  */
266  if( psTree->nMaxDepth > MAX_DEFAULT_TREE_DEPTH )
267  {
269 
270 #ifdef USE_CPL
271  CPLDebug( "Shape",
272  "Falling back to max number of allowed index tree levels (%d).",
274 #endif
275  }
276  }
277 
278 /* -------------------------------------------------------------------- */
279 /* Allocate the root node. */
280 /* -------------------------------------------------------------------- */
281  psTree->psRoot = SHPTreeNodeCreate( padfBoundsMin, padfBoundsMax );
282  if( SHPLIB_NULLPTR == psTree->psRoot )
283  {
284  free( psTree );
285  return SHPLIB_NULLPTR;
286  }
287 
288 /* -------------------------------------------------------------------- */
289 /* Assign the bounds to the root node. If none are passed in, */
290 /* use the bounds of the provided file otherwise the create */
291 /* function will have already set the bounds. */
292 /* -------------------------------------------------------------------- */
293  if( padfBoundsMin == SHPLIB_NULLPTR )
294  {
296  psTree->psRoot->adfBoundsMin,
297  psTree->psRoot->adfBoundsMax );
298  }
299 
300 /* -------------------------------------------------------------------- */
301 /* If we have a file, insert all its shapes into the tree. */
302 /* -------------------------------------------------------------------- */
303  if( hSHP != SHPLIB_NULLPTR )
304  {
305  int iShape, nShapeCount;
306 
307  SHPGetInfo( hSHP, &nShapeCount, SHPLIB_NULLPTR, SHPLIB_NULLPTR, SHPLIB_NULLPTR );
308 
309  for( iShape = 0; iShape < nShapeCount; iShape++ )
310  {
311  SHPObject *psShape;
312 
313  psShape = SHPReadObject( hSHP, iShape );
314  if( psShape != SHPLIB_NULLPTR )
315  {
316  SHPTreeAddShapeId( psTree, psShape );
317  SHPDestroyObject( psShape );
318  }
319  }
320  }
321 
322  return psTree;
323 }
324 
325 /************************************************************************/
326 /* SHPDestroyTreeNode() */
327 /************************************************************************/
328 
329 static void SHPDestroyTreeNode( SHPTreeNode * psTreeNode )
330 
331 {
332  int i;
333 
334  assert( SHPLIB_NULLPTR != psTreeNode );
335 
336  for( i = 0; i < psTreeNode->nSubNodes; i++ )
337  {
338  if( psTreeNode->apsSubNode[i] != SHPLIB_NULLPTR )
339  SHPDestroyTreeNode( psTreeNode->apsSubNode[i] );
340  }
341 
342  if( psTreeNode->panShapeIds != SHPLIB_NULLPTR )
343  free( psTreeNode->panShapeIds );
344 
345  if( psTreeNode->papsShapeObj != SHPLIB_NULLPTR )
346  {
347  for( i = 0; i < psTreeNode->nShapeCount; i++ )
348  {
349  if( psTreeNode->papsShapeObj[i] != SHPLIB_NULLPTR )
350  SHPDestroyObject( psTreeNode->papsShapeObj[i] );
351  }
352 
353  free( psTreeNode->papsShapeObj );
354  }
355 
356  free( psTreeNode );
357 }
358 
359 /************************************************************************/
360 /* SHPDestroyTree() */
361 /************************************************************************/
362 
363 void SHPAPI_CALL
365 
366 {
367  SHPDestroyTreeNode( psTree->psRoot );
368  free( psTree );
369 }
370 
371 /************************************************************************/
372 /* SHPCheckBoundsOverlap() */
373 /* */
374 /* Do the given boxes overlap at all? */
375 /************************************************************************/
376 
377 int SHPAPI_CALL
378 SHPCheckBoundsOverlap( double * padfBox1Min, double * padfBox1Max,
379  double * padfBox2Min, double * padfBox2Max,
380  int nDimension )
381 
382 {
383  int iDim;
384 
385  for( iDim = 0; iDim < nDimension; iDim++ )
386  {
387  if( padfBox2Max[iDim] < padfBox1Min[iDim] )
388  return FALSE;
389 
390  if( padfBox1Max[iDim] < padfBox2Min[iDim] )
391  return FALSE;
392  }
393 
394  return TRUE;
395 }
396 
397 /************************************************************************/
398 /* SHPCheckObjectContained() */
399 /* */
400 /* Does the given shape fit within the indicated extents? */
401 /************************************************************************/
402 
403 static int SHPCheckObjectContained( SHPObject * psObject, int nDimension,
404  double * padfBoundsMin, double * padfBoundsMax )
405 
406 {
407  if( psObject->dfXMin < padfBoundsMin[0]
408  || psObject->dfXMax > padfBoundsMax[0] )
409  return FALSE;
410 
411  if( psObject->dfYMin < padfBoundsMin[1]
412  || psObject->dfYMax > padfBoundsMax[1] )
413  return FALSE;
414 
415  if( nDimension == 2 )
416  return TRUE;
417 
418  if( psObject->dfZMin < padfBoundsMin[2]
419  || psObject->dfZMax > padfBoundsMax[2] )
420  return FALSE;
421 
422  if( nDimension == 3 )
423  return TRUE;
424 
425  if( psObject->dfMMin < padfBoundsMin[3]
426  || psObject->dfMMax > padfBoundsMax[3] )
427  return FALSE;
428 
429  return TRUE;
430 }
431 
432 /************************************************************************/
433 /* SHPTreeSplitBounds() */
434 /* */
435 /* Split a region into two subregion evenly, cutting along the */
436 /* longest dimension. */
437 /************************************************************************/
438 
439 static void
440 SHPTreeSplitBounds( double *padfBoundsMinIn, double *padfBoundsMaxIn,
441  double *padfBoundsMin1, double * padfBoundsMax1,
442  double *padfBoundsMin2, double * padfBoundsMax2 )
443 
444 {
445 /* -------------------------------------------------------------------- */
446 /* The output bounds will be very similar to the input bounds, */
447 /* so just copy over to start. */
448 /* -------------------------------------------------------------------- */
449  memcpy( padfBoundsMin1, padfBoundsMinIn, sizeof(double) * 4 );
450  memcpy( padfBoundsMax1, padfBoundsMaxIn, sizeof(double) * 4 );
451  memcpy( padfBoundsMin2, padfBoundsMinIn, sizeof(double) * 4 );
452  memcpy( padfBoundsMax2, padfBoundsMaxIn, sizeof(double) * 4 );
453 
454 /* -------------------------------------------------------------------- */
455 /* Split in X direction. */
456 /* -------------------------------------------------------------------- */
457  if( (padfBoundsMaxIn[0] - padfBoundsMinIn[0])
458  > (padfBoundsMaxIn[1] - padfBoundsMinIn[1]) )
459  {
460  double dfRange = padfBoundsMaxIn[0] - padfBoundsMinIn[0];
461 
462  padfBoundsMax1[0] = padfBoundsMinIn[0] + dfRange * SHP_SPLIT_RATIO;
463  padfBoundsMin2[0] = padfBoundsMaxIn[0] - dfRange * SHP_SPLIT_RATIO;
464  }
465 
466 /* -------------------------------------------------------------------- */
467 /* Otherwise split in Y direction. */
468 /* -------------------------------------------------------------------- */
469  else
470  {
471  double dfRange = padfBoundsMaxIn[1] - padfBoundsMinIn[1];
472 
473  padfBoundsMax1[1] = padfBoundsMinIn[1] + dfRange * SHP_SPLIT_RATIO;
474  padfBoundsMin2[1] = padfBoundsMaxIn[1] - dfRange * SHP_SPLIT_RATIO;
475  }
476 }
477 
478 /************************************************************************/
479 /* SHPTreeNodeAddShapeId() */
480 /************************************************************************/
481 
482 static int
483 SHPTreeNodeAddShapeId( SHPTreeNode * psTreeNode, SHPObject * psObject,
484  int nMaxDepth, int nDimension )
485 
486 {
487  int i;
488 
489 /* -------------------------------------------------------------------- */
490 /* If there are subnodes, then consider whether this object */
491 /* will fit in them. */
492 /* -------------------------------------------------------------------- */
493  if( nMaxDepth > 1 && psTreeNode->nSubNodes > 0 )
494  {
495  for( i = 0; i < psTreeNode->nSubNodes; i++ )
496  {
497  if( SHPCheckObjectContained(psObject, nDimension,
498  psTreeNode->apsSubNode[i]->adfBoundsMin,
499  psTreeNode->apsSubNode[i]->adfBoundsMax))
500  {
501  return SHPTreeNodeAddShapeId( psTreeNode->apsSubNode[i],
502  psObject, nMaxDepth-1,
503  nDimension );
504  }
505  }
506  }
507 
508 /* -------------------------------------------------------------------- */
509 /* Otherwise, consider creating four subnodes if could fit into */
510 /* them, and adding to the appropriate subnode. */
511 /* -------------------------------------------------------------------- */
512 #if MAX_SUBNODE == 4
513  else if( nMaxDepth > 1 && psTreeNode->nSubNodes == 0 )
514  {
515  double adfBoundsMinH1[4], adfBoundsMaxH1[4];
516  double adfBoundsMinH2[4], adfBoundsMaxH2[4];
517  double adfBoundsMin1[4], adfBoundsMax1[4];
518  double adfBoundsMin2[4], adfBoundsMax2[4];
519  double adfBoundsMin3[4], adfBoundsMax3[4];
520  double adfBoundsMin4[4], adfBoundsMax4[4];
521 
522  SHPTreeSplitBounds( psTreeNode->adfBoundsMin,
523  psTreeNode->adfBoundsMax,
524  adfBoundsMinH1, adfBoundsMaxH1,
525  adfBoundsMinH2, adfBoundsMaxH2 );
526 
527  SHPTreeSplitBounds( adfBoundsMinH1, adfBoundsMaxH1,
528  adfBoundsMin1, adfBoundsMax1,
529  adfBoundsMin2, adfBoundsMax2 );
530 
531  SHPTreeSplitBounds( adfBoundsMinH2, adfBoundsMaxH2,
532  adfBoundsMin3, adfBoundsMax3,
533  adfBoundsMin4, adfBoundsMax4 );
534 
535  if( SHPCheckObjectContained(psObject, nDimension,
536  adfBoundsMin1, adfBoundsMax1)
537  || SHPCheckObjectContained(psObject, nDimension,
538  adfBoundsMin2, adfBoundsMax2)
539  || SHPCheckObjectContained(psObject, nDimension,
540  adfBoundsMin3, adfBoundsMax3)
541  || SHPCheckObjectContained(psObject, nDimension,
542  adfBoundsMin4, adfBoundsMax4) )
543  {
544  psTreeNode->nSubNodes = 4;
545  psTreeNode->apsSubNode[0] = SHPTreeNodeCreate( adfBoundsMin1,
546  adfBoundsMax1 );
547  psTreeNode->apsSubNode[1] = SHPTreeNodeCreate( adfBoundsMin2,
548  adfBoundsMax2 );
549  psTreeNode->apsSubNode[2] = SHPTreeNodeCreate( adfBoundsMin3,
550  adfBoundsMax3 );
551  psTreeNode->apsSubNode[3] = SHPTreeNodeCreate( adfBoundsMin4,
552  adfBoundsMax4 );
553 
554  /* recurse back on this node now that it has subnodes */
555  return( SHPTreeNodeAddShapeId( psTreeNode, psObject,
556  nMaxDepth, nDimension ) );
557  }
558  }
559 #endif /* MAX_SUBNODE == 4 */
560 
561 /* -------------------------------------------------------------------- */
562 /* Otherwise, consider creating two subnodes if could fit into */
563 /* them, and adding to the appropriate subnode. */
564 /* -------------------------------------------------------------------- */
565 #if MAX_SUBNODE == 2
566  else if( nMaxDepth > 1 && psTreeNode->nSubNodes == 0 )
567  {
568  double adfBoundsMin1[4], adfBoundsMax1[4];
569  double adfBoundsMin2[4], adfBoundsMax2[4];
570 
571  SHPTreeSplitBounds( psTreeNode->adfBoundsMin, psTreeNode->adfBoundsMax,
572  adfBoundsMin1, adfBoundsMax1,
573  adfBoundsMin2, adfBoundsMax2 );
574 
575  if( SHPCheckObjectContained(psObject, nDimension,
576  adfBoundsMin1, adfBoundsMax1))
577  {
578  psTreeNode->nSubNodes = 2;
579  psTreeNode->apsSubNode[0] = SHPTreeNodeCreate( adfBoundsMin1,
580  adfBoundsMax1 );
581  psTreeNode->apsSubNode[1] = SHPTreeNodeCreate( adfBoundsMin2,
582  adfBoundsMax2 );
583 
584  return( SHPTreeNodeAddShapeId( psTreeNode->apsSubNode[0], psObject,
585  nMaxDepth - 1, nDimension ) );
586  }
587  else if( SHPCheckObjectContained(psObject, nDimension,
588  adfBoundsMin2, adfBoundsMax2) )
589  {
590  psTreeNode->nSubNodes = 2;
591  psTreeNode->apsSubNode[0] = SHPTreeNodeCreate( adfBoundsMin1,
592  adfBoundsMax1 );
593  psTreeNode->apsSubNode[1] = SHPTreeNodeCreate( adfBoundsMin2,
594  adfBoundsMax2 );
595 
596  return( SHPTreeNodeAddShapeId( psTreeNode->apsSubNode[1], psObject,
597  nMaxDepth - 1, nDimension ) );
598  }
599  }
600 #endif /* MAX_SUBNODE == 2 */
601 
602 /* -------------------------------------------------------------------- */
603 /* If none of that worked, just add it to this nodes list. */
604 /* -------------------------------------------------------------------- */
605  psTreeNode->nShapeCount++;
606 
607  psTreeNode->panShapeIds = STATIC_CAST(int *,
608  SfRealloc( psTreeNode->panShapeIds,
609  sizeof(int) * psTreeNode->nShapeCount ));
610  psTreeNode->panShapeIds[psTreeNode->nShapeCount-1] = psObject->nShapeId;
611 
612  if( psTreeNode->papsShapeObj != SHPLIB_NULLPTR )
613  {
614  psTreeNode->papsShapeObj = STATIC_CAST(SHPObject **,
615  SfRealloc( psTreeNode->papsShapeObj,
616  sizeof(void *) * psTreeNode->nShapeCount ));
617  psTreeNode->papsShapeObj[psTreeNode->nShapeCount-1] = SHPLIB_NULLPTR;
618  }
619 
620  return TRUE;
621 }
622 
623 /************************************************************************/
624 /* SHPTreeAddShapeId() */
625 /* */
626 /* Add a shape to the tree, but don't keep a pointer to the */
627 /* object data, just keep the shapeid. */
628 /************************************************************************/
629 
630 int SHPAPI_CALL
631 SHPTreeAddShapeId( SHPTree * psTree, SHPObject * psObject )
632 
633 {
634  psTree->nTotalCount++;
635 
636  return( SHPTreeNodeAddShapeId( psTree->psRoot, psObject,
637  psTree->nMaxDepth, psTree->nDimension ) );
638 }
639 
640 /************************************************************************/
641 /* SHPTreeCollectShapesIds() */
642 /* */
643 /* Work function implementing SHPTreeFindLikelyShapes() on a */
644 /* tree node by tree node basis. */
645 /************************************************************************/
646 
647 static void
649  double * padfBoundsMin, double * padfBoundsMax,
650  int * pnShapeCount, int * pnMaxShapes,
651  int ** ppanShapeList )
652 
653 {
654  int i;
655 
656 /* -------------------------------------------------------------------- */
657 /* Does this node overlap the area of interest at all? If not, */
658 /* return without adding to the list at all. */
659 /* -------------------------------------------------------------------- */
660  if( !SHPCheckBoundsOverlap( psTreeNode->adfBoundsMin,
661  psTreeNode->adfBoundsMax,
662  padfBoundsMin,
663  padfBoundsMax,
664  hTree->nDimension ) )
665  return;
666 
667 /* -------------------------------------------------------------------- */
668 /* Grow the list to hold the shapes on this node. */
669 /* -------------------------------------------------------------------- */
670  if( *pnShapeCount + psTreeNode->nShapeCount > *pnMaxShapes )
671  {
672  *pnMaxShapes = (*pnShapeCount + psTreeNode->nShapeCount) * 2 + 20;
673  *ppanShapeList = STATIC_CAST(int *,
674  SfRealloc(*ppanShapeList,sizeof(int) * *pnMaxShapes));
675  }
676 
677 /* -------------------------------------------------------------------- */
678 /* Add the local nodes shapeids to the list. */
679 /* -------------------------------------------------------------------- */
680  for( i = 0; i < psTreeNode->nShapeCount; i++ )
681  {
682  (*ppanShapeList)[(*pnShapeCount)++] = psTreeNode->panShapeIds[i];
683  }
684 
685 /* -------------------------------------------------------------------- */
686 /* Recurse to subnodes if they exist. */
687 /* -------------------------------------------------------------------- */
688  for( i = 0; i < psTreeNode->nSubNodes; i++ )
689  {
690  if( psTreeNode->apsSubNode[i] != SHPLIB_NULLPTR )
691  SHPTreeCollectShapeIds( hTree, psTreeNode->apsSubNode[i],
692  padfBoundsMin, padfBoundsMax,
693  pnShapeCount, pnMaxShapes,
694  ppanShapeList );
695  }
696 }
697 
698 /************************************************************************/
699 /* SHPTreeFindLikelyShapes() */
700 /* */
701 /* Find all shapes within tree nodes for which the tree node */
702 /* bounding box overlaps the search box. The return value is */
703 /* an array of shapeids terminated by a -1. The shapeids will */
704 /* be in order, as hopefully this will result in faster (more */
705 /* sequential) reading from the file. */
706 /************************************************************************/
707 
708 /* helper for qsort */
709 static int
710 compare_ints( const void * a, const void * b)
711 {
712  return *REINTERPRET_CAST(const int*, a) - *REINTERPRET_CAST(const int*, b);
713 }
714 
715 int SHPAPI_CALL1(*)
717  double * padfBoundsMin, double * padfBoundsMax,
718  int * pnShapeCount )
719 
720 {
721  int *panShapeList=SHPLIB_NULLPTR, nMaxShapes = 0;
722 
723 /* -------------------------------------------------------------------- */
724 /* Perform the search by recursive descent. */
725 /* -------------------------------------------------------------------- */
726  *pnShapeCount = 0;
727 
728  SHPTreeCollectShapeIds( hTree, hTree->psRoot,
729  padfBoundsMin, padfBoundsMax,
730  pnShapeCount, &nMaxShapes,
731  &panShapeList );
732 
733 /* -------------------------------------------------------------------- */
734 /* Sort the id array */
735 /* -------------------------------------------------------------------- */
736 
737  if( panShapeList != SHPLIB_NULLPTR )
738  qsort(panShapeList, *pnShapeCount, sizeof(int), compare_ints);
739 
740  return panShapeList;
741 }
742 
743 /************************************************************************/
744 /* SHPTreeNodeTrim() */
745 /* */
746 /* This is the recursive version of SHPTreeTrimExtraNodes() that */
747 /* walks the tree cleaning it up. */
748 /************************************************************************/
749 
750 static int SHPTreeNodeTrim( SHPTreeNode * psTreeNode )
751 
752 {
753  int i;
754 
755 /* -------------------------------------------------------------------- */
756 /* Trim subtrees, and free subnodes that come back empty. */
757 /* -------------------------------------------------------------------- */
758  for( i = 0; i < psTreeNode->nSubNodes; i++ )
759  {
760  if( SHPTreeNodeTrim( psTreeNode->apsSubNode[i] ) )
761  {
762  SHPDestroyTreeNode( psTreeNode->apsSubNode[i] );
763 
764  psTreeNode->apsSubNode[i] =
765  psTreeNode->apsSubNode[psTreeNode->nSubNodes-1];
766 
767  psTreeNode->nSubNodes--;
768 
769  i--; /* process the new occupant of this subnode entry */
770  }
771  }
772 
773 /* -------------------------------------------------------------------- */
774 /* If the current node has 1 subnode and no shapes, promote that */
775 /* subnode to the current node position. */
776 /* -------------------------------------------------------------------- */
777  if( psTreeNode->nSubNodes == 1 && psTreeNode->nShapeCount == 0)
778  {
779  SHPTreeNode* psSubNode = psTreeNode->apsSubNode[0];
780 
781  memcpy(psTreeNode->adfBoundsMin, psSubNode->adfBoundsMin,
782  sizeof(psSubNode->adfBoundsMin));
783  memcpy(psTreeNode->adfBoundsMax, psSubNode->adfBoundsMax,
784  sizeof(psSubNode->adfBoundsMax));
785  psTreeNode->nShapeCount = psSubNode->nShapeCount;
786  assert(psTreeNode->panShapeIds == SHPLIB_NULLPTR);
787  psTreeNode->panShapeIds = psSubNode->panShapeIds;
788  assert(psTreeNode->papsShapeObj == SHPLIB_NULLPTR);
789  psTreeNode->papsShapeObj = psSubNode->papsShapeObj;
790  psTreeNode->nSubNodes = psSubNode->nSubNodes;
791  for( i = 0; i < psSubNode->nSubNodes; i++ )
792  psTreeNode->apsSubNode[i] = psSubNode->apsSubNode[i];
793  free(psSubNode);
794  }
795 
796 /* -------------------------------------------------------------------- */
797 /* We should be trimmed if we have no subnodes, and no shapes. */
798 /* -------------------------------------------------------------------- */
799  return( psTreeNode->nSubNodes == 0 && psTreeNode->nShapeCount == 0 );
800 }
801 
802 /************************************************************************/
803 /* SHPTreeTrimExtraNodes() */
804 /* */
805 /* Trim empty nodes from the tree. Note that we never trim an */
806 /* empty root node. */
807 /************************************************************************/
808 
809 void SHPAPI_CALL
811 
812 {
813  SHPTreeNodeTrim( hTree->psRoot );
814 }
815 
816 /************************************************************************/
817 /* SwapWord() */
818 /* */
819 /* Swap a 2, 4 or 8 byte word. */
820 /************************************************************************/
821 
822 static void SwapWord( int length, void * wordP )
823 
824 {
825  int i;
826  unsigned char temp;
827 
828  for( i=0; i < length/2; i++ )
829  {
830  temp = STATIC_CAST(unsigned char*, wordP)[i];
831  STATIC_CAST(unsigned char*, wordP)[i] = STATIC_CAST(unsigned char*, wordP)[length-i-1];
832  STATIC_CAST(unsigned char*, wordP)[length-i-1] = temp;
833  }
834 }
835 
836 
838 {
841 };
842 
843 /************************************************************************/
844 /* SHPOpenDiskTree() */
845 /************************************************************************/
846 
847 SHPTreeDiskHandle SHPOpenDiskTree( const char* pszQIXFilename,
848  SAHooks *psHooks )
849 {
850  SHPTreeDiskHandle hDiskTree;
851 
852  hDiskTree = STATIC_CAST(SHPTreeDiskHandle, calloc(sizeof(struct SHPDiskTreeInfo),1));
853 
854  if (psHooks == SHPLIB_NULLPTR)
855  SASetupDefaultHooks( &(hDiskTree->sHooks) );
856  else
857  memcpy( &(hDiskTree->sHooks), psHooks, sizeof(SAHooks) );
858 
859  hDiskTree->fpQIX = hDiskTree->sHooks.FOpen(pszQIXFilename, "rb");
860  if (hDiskTree->fpQIX == SHPLIB_NULLPTR)
861  {
862  free(hDiskTree);
863  return SHPLIB_NULLPTR;
864  }
865 
866  return hDiskTree;
867 }
868 
869 /***********************************************************************/
870 /* SHPCloseDiskTree() */
871 /************************************************************************/
872 
874 {
875  if (hDiskTree == SHPLIB_NULLPTR)
876  return;
877 
878  hDiskTree->sHooks.FClose(hDiskTree->fpQIX);
879  free(hDiskTree);
880 }
881 
882 /************************************************************************/
883 /* SHPSearchDiskTreeNode() */
884 /************************************************************************/
885 
886 static int
887 SHPSearchDiskTreeNode( SHPTreeDiskHandle hDiskTree, double *padfBoundsMin, double *padfBoundsMax,
888  int **ppanResultBuffer, int *pnBufferMax,
889  int *pnResultCount, int bNeedSwap, int nRecLevel )
890 
891 {
892  unsigned int i;
893  unsigned int offset;
894  unsigned int numshapes, numsubnodes;
895  double adfNodeBoundsMin[2], adfNodeBoundsMax[2];
896  int nFReadAcc;
897 
898 /* -------------------------------------------------------------------- */
899 /* Read and unswap first part of node info. */
900 /* -------------------------------------------------------------------- */
901  nFReadAcc = STATIC_CAST(int, hDiskTree->sHooks.FRead( &offset, 4, 1, hDiskTree->fpQIX ));
902  if ( bNeedSwap ) SwapWord ( 4, &offset );
903 
904  nFReadAcc += STATIC_CAST(int, hDiskTree->sHooks.FRead( adfNodeBoundsMin, sizeof(double), 2, hDiskTree->fpQIX ));
905  nFReadAcc += STATIC_CAST(int, hDiskTree->sHooks.FRead( adfNodeBoundsMax, sizeof(double), 2, hDiskTree->fpQIX ));
906  if ( bNeedSwap )
907  {
908  SwapWord( 8, adfNodeBoundsMin + 0 );
909  SwapWord( 8, adfNodeBoundsMin + 1 );
910  SwapWord( 8, adfNodeBoundsMax + 0 );
911  SwapWord( 8, adfNodeBoundsMax + 1 );
912  }
913 
914  nFReadAcc += STATIC_CAST(int, hDiskTree->sHooks.FRead( &numshapes, 4, 1, hDiskTree->fpQIX ));
915  if ( bNeedSwap ) SwapWord ( 4, &numshapes );
916 
917  /* Check that we could read all previous values */
918  if( nFReadAcc != 1 + 2 + 2 + 1 )
919  {
920  hDiskTree->sHooks.Error("I/O error");
921  return FALSE;
922  }
923 
924  /* Sanity checks to avoid int overflows in later computation */
925  if( offset > INT_MAX - sizeof(int) )
926  {
927  hDiskTree->sHooks.Error("Invalid value for offset");
928  return FALSE;
929  }
930 
931  if( numshapes > (INT_MAX - offset - sizeof(int)) / sizeof(int) ||
932  numshapes > INT_MAX / sizeof(int) - *pnResultCount )
933  {
934  hDiskTree->sHooks.Error("Invalid value for numshapes");
935  return FALSE;
936  }
937 
938 /* -------------------------------------------------------------------- */
939 /* If we don't overlap this node at all, we can just fseek() */
940 /* pass this node info and all subnodes. */
941 /* -------------------------------------------------------------------- */
942  if( !SHPCheckBoundsOverlap( adfNodeBoundsMin, adfNodeBoundsMax,
943  padfBoundsMin, padfBoundsMax, 2 ) )
944  {
945  offset += numshapes*sizeof(int) + sizeof(int);
946  hDiskTree->sHooks.FSeek(hDiskTree->fpQIX, offset, SEEK_CUR);
947  return TRUE;
948  }
949 
950 /* -------------------------------------------------------------------- */
951 /* Add all the shapeids at this node to our list. */
952 /* -------------------------------------------------------------------- */
953  if(numshapes > 0)
954  {
955  if( *pnResultCount + numshapes > STATIC_CAST(unsigned int, *pnBufferMax) )
956  {
957  int* pNewBuffer;
958 
959  *pnBufferMax = (*pnResultCount + numshapes + 100) * 5 / 4;
960 
961  if( STATIC_CAST(size_t, *pnBufferMax) > INT_MAX / sizeof(int) )
962  *pnBufferMax = *pnResultCount + numshapes;
963 
964  pNewBuffer = STATIC_CAST(int *,
965  SfRealloc( *ppanResultBuffer, *pnBufferMax * sizeof(int) ));
966 
967  if( pNewBuffer == SHPLIB_NULLPTR )
968  {
969  hDiskTree->sHooks.Error("Out of memory error");
970  return FALSE;
971  }
972 
973  *ppanResultBuffer = pNewBuffer;
974  }
975 
976  if( hDiskTree->sHooks.FRead( *ppanResultBuffer + *pnResultCount,
977  sizeof(int), numshapes, hDiskTree->fpQIX ) != numshapes )
978  {
979  hDiskTree->sHooks.Error("I/O error");
980  return FALSE;
981  }
982 
983  if (bNeedSwap )
984  {
985  for( i=0; i<numshapes; i++ )
986  SwapWord( 4, *ppanResultBuffer + *pnResultCount + i );
987  }
988 
989  *pnResultCount += numshapes;
990  }
991 
992 /* -------------------------------------------------------------------- */
993 /* Process the subnodes. */
994 /* -------------------------------------------------------------------- */
995  if( hDiskTree->sHooks.FRead( &numsubnodes, 4, 1, hDiskTree->fpQIX ) != 1 )
996  {
997  hDiskTree->sHooks.Error("I/O error");
998  return FALSE;
999  }
1000  if ( bNeedSwap ) SwapWord ( 4, &numsubnodes );
1001  if( numsubnodes > 0 && nRecLevel == 32 )
1002  {
1003  hDiskTree->sHooks.Error("Shape tree is too deep");
1004  return FALSE;
1005  }
1006 
1007  for(i=0; i<numsubnodes; i++)
1008  {
1009  if( !SHPSearchDiskTreeNode( hDiskTree, padfBoundsMin, padfBoundsMax,
1010  ppanResultBuffer, pnBufferMax,
1011  pnResultCount, bNeedSwap, nRecLevel + 1 ) )
1012  return FALSE;
1013  }
1014 
1015  return TRUE;
1016 }
1017 
1018 /************************************************************************/
1019 /* SHPTreeReadLibc() */
1020 /************************************************************************/
1021 
1022 static
1024 
1025 {
1026  return STATIC_CAST(SAOffset, fread( p, STATIC_CAST(size_t, size),
1027  STATIC_CAST(size_t, nmemb),
1028  REINTERPRET_CAST(FILE*, file) ));
1029 }
1030 
1031 /************************************************************************/
1032 /* SHPTreeSeekLibc() */
1033 /************************************************************************/
1034 
1035 static
1037 
1038 {
1039  return STATIC_CAST(SAOffset, fseek( REINTERPRET_CAST(FILE*, file),
1040  STATIC_CAST(long, offset), whence ));
1041 }
1042 
1043 /************************************************************************/
1044 /* SHPSearchDiskTree() */
1045 /************************************************************************/
1046 
1047 int SHPAPI_CALL1(*)
1049  double *padfBoundsMin, double *padfBoundsMax,
1050  int *pnShapeCount )
1051 {
1052  struct SHPDiskTreeInfo sDiskTree;
1053  memset(&sDiskTree.sHooks, 0, sizeof(sDiskTree.sHooks));
1054 
1055  /* We do not use SASetupDefaultHooks() because the FILE* */
1056  /* is a libc FILE* */
1057  sDiskTree.sHooks.FSeek = SHPTreeSeekLibc;
1058  sDiskTree.sHooks.FRead = SHPTreeReadLibc;
1059 
1060  sDiskTree.fpQIX = REINTERPRET_CAST(SAFile, fp);
1061 
1062  return SHPSearchDiskTreeEx( &sDiskTree, padfBoundsMin, padfBoundsMax,
1063  pnShapeCount );
1064 }
1065 
1066 /***********************************************************************/
1067 /* SHPSearchDiskTreeEx() */
1068 /************************************************************************/
1069 
1071  double *padfBoundsMin, double *padfBoundsMax,
1072  int *pnShapeCount )
1073 
1074 {
1075  int i, bNeedSwap, nBufferMax = 0;
1076  unsigned char abyBuf[16];
1077  int *panResultBuffer = SHPLIB_NULLPTR;
1078 
1079  *pnShapeCount = 0;
1080 
1081 /* -------------------------------------------------------------------- */
1082 /* Establish the byte order on this machine. */
1083 /* -------------------------------------------------------------------- */
1084  i = 1;
1085  if( *REINTERPRET_CAST(unsigned char *, &i) == 1 )
1086  bBigEndian = FALSE;
1087  else
1088  bBigEndian = TRUE;
1089 
1090 /* -------------------------------------------------------------------- */
1091 /* Read the header. */
1092 /* -------------------------------------------------------------------- */
1093  hDiskTree->sHooks.FSeek( hDiskTree->fpQIX, 0, SEEK_SET );
1094  hDiskTree->sHooks.FRead( abyBuf, 16, 1, hDiskTree->fpQIX );
1095 
1096  if( memcmp( abyBuf, "SQT", 3 ) != 0 )
1097  return SHPLIB_NULLPTR;
1098 
1099  if( (abyBuf[3] == 2 && bBigEndian)
1100  || (abyBuf[3] == 1 && !bBigEndian) )
1101  bNeedSwap = FALSE;
1102  else
1103  bNeedSwap = TRUE;
1104 
1105 /* -------------------------------------------------------------------- */
1106 /* Search through root node and its descendants. */
1107 /* -------------------------------------------------------------------- */
1108  if( !SHPSearchDiskTreeNode( hDiskTree, padfBoundsMin, padfBoundsMax,
1109  &panResultBuffer, &nBufferMax,
1110  pnShapeCount, bNeedSwap, 0 ) )
1111  {
1112  if( panResultBuffer != SHPLIB_NULLPTR )
1113  free( panResultBuffer );
1114  *pnShapeCount = 0;
1115  return SHPLIB_NULLPTR;
1116  }
1117 /* -------------------------------------------------------------------- */
1118 /* Sort the id array */
1119 /* -------------------------------------------------------------------- */
1120 
1121  /* To distinguish between empty intersection from error case */
1122  if( panResultBuffer == SHPLIB_NULLPTR )
1123  panResultBuffer = STATIC_CAST(int*, calloc(1, sizeof(int)));
1124  else
1125  qsort(panResultBuffer, *pnShapeCount, sizeof(int), compare_ints);
1126 
1127 
1128  return panResultBuffer;
1129 }
1130 
1131 /************************************************************************/
1132 /* SHPGetSubNodeOffset() */
1133 /* */
1134 /* Determine how big all the subnodes of this node (and their */
1135 /* children) will be. This will allow disk based searchers to */
1136 /* seek past them all efficiently. */
1137 /************************************************************************/
1138 
1140 {
1141  int i;
1142  int offset=0;
1143 
1144  for(i=0; i<node->nSubNodes; i++ )
1145  {
1146  if(node->apsSubNode[i])
1147  {
1148  offset += 4*sizeof(double)
1149  + (node->apsSubNode[i]->nShapeCount+3)*sizeof(int);
1150  offset += SHPGetSubNodeOffset(node->apsSubNode[i]);
1151  }
1152  }
1153 
1154  return(offset);
1155 }
1156 
1157 /************************************************************************/
1158 /* SHPWriteTreeNode() */
1159 /************************************************************************/
1160 
1161 static void SHPWriteTreeNode( SAFile fp, SHPTreeNode *node, SAHooks* psHooks)
1162 {
1163  int i,j;
1164  int offset;
1165  unsigned char *pabyRec;
1166  assert( SHPLIB_NULLPTR != node );
1167 
1168  offset = SHPGetSubNodeOffset(node);
1169 
1170  pabyRec = STATIC_CAST(unsigned char *,
1171  malloc(sizeof(double) * 4
1172  + (3 * sizeof(int)) + (node->nShapeCount * sizeof(int)) ));
1173  if( SHPLIB_NULLPTR == pabyRec )
1174  {
1175 #ifdef USE_CPL
1176  CPLError( CE_Fatal, CPLE_OutOfMemory, "Memory allocation failure");
1177 #endif
1178  assert( 0 );
1179  return;
1180  }
1181 
1182  memcpy( pabyRec, &offset, 4);
1183 
1184  /* minx, miny, maxx, maxy */
1185  memcpy( pabyRec+ 4, node->adfBoundsMin+0, sizeof(double) );
1186  memcpy( pabyRec+12, node->adfBoundsMin+1, sizeof(double) );
1187  memcpy( pabyRec+20, node->adfBoundsMax+0, sizeof(double) );
1188  memcpy( pabyRec+28, node->adfBoundsMax+1, sizeof(double) );
1189 
1190  memcpy( pabyRec+36, &node->nShapeCount, 4);
1191  j = node->nShapeCount * sizeof(int);
1192  if( j )
1193  memcpy( pabyRec+40, node->panShapeIds, j);
1194  memcpy( pabyRec+j+40, &node->nSubNodes, 4);
1195 
1196  psHooks->FWrite( pabyRec, 44+j, 1, fp );
1197  free (pabyRec);
1198 
1199  for(i=0; i<node->nSubNodes; i++ )
1200  {
1201  if(node->apsSubNode[i])
1202  SHPWriteTreeNode( fp, node->apsSubNode[i], psHooks);
1203  }
1204 }
1205 
1206 /************************************************************************/
1207 /* SHPWriteTree() */
1208 /************************************************************************/
1209 
1210 int SHPAPI_CALL SHPWriteTree(SHPTree *tree, const char *filename )
1211 {
1212  SAHooks sHooks;
1213 
1215 
1216  return SHPWriteTreeLL(tree, filename, &sHooks);
1217 }
1218 
1219 /************************************************************************/
1220 /* SHPWriteTreeLL() */
1221 /************************************************************************/
1222 
1223 int SHPWriteTreeLL(SHPTree *tree, const char *filename, SAHooks* psHooks )
1224 {
1225  char signature[4] = "SQT";
1226  int i;
1227  char abyBuf[32];
1228  SAFile fp;
1229 
1230  SAHooks sHooks;
1231  if (psHooks == SHPLIB_NULLPTR)
1232  {
1234  psHooks = &sHooks;
1235  }
1236 
1237 /* -------------------------------------------------------------------- */
1238 /* Open the output file. */
1239 /* -------------------------------------------------------------------- */
1240  fp = psHooks->FOpen(filename, "wb");
1241  if( fp == SHPLIB_NULLPTR )
1242  {
1243  return FALSE;
1244  }
1245 
1246 /* -------------------------------------------------------------------- */
1247 /* Establish the byte order on this machine. */
1248 /* -------------------------------------------------------------------- */
1249  i = 1;
1250  if( *REINTERPRET_CAST(unsigned char *, &i) == 1 )
1251  bBigEndian = FALSE;
1252  else
1253  bBigEndian = TRUE;
1254 
1255 /* -------------------------------------------------------------------- */
1256 /* Write the header. */
1257 /* -------------------------------------------------------------------- */
1258  memcpy( abyBuf+0, signature, 3 );
1259 
1260  if( bBigEndian )
1261  abyBuf[3] = 2; /* New MSB */
1262  else
1263  abyBuf[3] = 1; /* New LSB */
1264 
1265  abyBuf[4] = 1; /* version */
1266  abyBuf[5] = 0; /* next 3 reserved */
1267  abyBuf[6] = 0;
1268  abyBuf[7] = 0;
1269 
1270  psHooks->FWrite( abyBuf, 8, 1, fp );
1271 
1272  psHooks->FWrite( &(tree->nTotalCount), 4, 1, fp );
1273 
1274  /* write maxdepth */
1275 
1276  psHooks->FWrite( &(tree->nMaxDepth), 4, 1, fp );
1277 
1278 /* -------------------------------------------------------------------- */
1279 /* Write all the nodes "in order". */
1280 /* -------------------------------------------------------------------- */
1281 
1282  SHPWriteTreeNode( fp, tree->psRoot, psHooks );
1283 
1284  psHooks->FClose( fp );
1285 
1286  return TRUE;
1287 }
std::string filename
int size
int offset
a[190]
#define SEEK_SET
Definition: qioapi.cpp:38
#define SEEK_CUR
Definition: qioapi.cpp:30
void SASetupDefaultHooks(SAHooks *psHooks)
Definition: safileio.c:184
void SHPDestroyObject(SHPObject *psObject)
Definition: shpopen.c:2902
#define SHP_CVSID(string)
Definition: shapefil.h:267
#define MAX_DEFAULT_TREE_DEPTH
Definition: shapefil.h:486
int * SAFile
Definition: shapefil.h:287
void SHPGetInfo(SHPHandle hSHP, int *pnEntities, int *pnShapeType, double *padfMinBound, double *padfMaxBound)
Definition: shpopen.c:1217
#define SHPAPI_CALL
Definition: shapefil.h:250
#define SHPAPI_CALL1(x)
Definition: shapefil.h:255
SHPObject * SHPReadObject(SHPHandle hSHP, int iShape)
Definition: shpopen.c:2092
unsigned long SAOffset
Definition: shapefil.h:290
int SHPCheckBoundsOverlap(double *padfBox1Min, double *padfBox1Max, double *padfBox2Min, double *padfBox2Max, int nDimension)
Definition: shptree.c:378
static int compare_ints(const void *a, const void *b)
Definition: shptree.c:710
static int SHPSearchDiskTreeNode(SHPTreeDiskHandle hDiskTree, double *padfBoundsMin, double *padfBoundsMax, int **ppanResultBuffer, int *pnBufferMax, int *pnResultCount, int bNeedSwap, int nRecLevel)
Definition: shptree.c:887
static SAOffset SHPTreeSeekLibc(SAFile file, SAOffset offset, int whence)
Definition: shptree.c:1036
static int bBigEndian
Definition: shptree.c:139
int SHPWriteTreeLL(SHPTree *tree, const char *filename, SAHooks *psHooks)
Definition: shptree.c:1223
void SHPCloseDiskTree(SHPTreeDiskHandle hDiskTree)
Definition: shptree.c:873
int SHPTreeAddShapeId(SHPTree *psTree, SHPObject *psObject)
Definition: shptree.c:631
static SHPTreeNode * SHPTreeNodeCreate(double *padfBoundsMin, double *padfBoundsMax)
Definition: shptree.c:186
static SAOffset SHPTreeReadLibc(void *p, SAOffset size, SAOffset nmemb, SAFile file)
Definition: shptree.c:1023
static void SHPDestroyTreeNode(SHPTreeNode *psTreeNode)
Definition: shptree.c:329
static void SwapWord(int length, void *wordP)
Definition: shptree.c:822
static int SHPGetSubNodeOffset(SHPTreeNode *node)
Definition: shptree.c:1139
static int SHPTreeNodeAddShapeId(SHPTreeNode *psTreeNode, SHPObject *psObject, int nMaxDepth, int nDimension)
Definition: shptree.c:483
#define STATIC_CAST(type, x)
Definition: shptree.c:158
int SHPWriteTree(SHPTree *tree, const char *filename)
Definition: shptree.c:1210
#define SHP_SPLIT_RATIO
Definition: shptree.c:150
SHPTree * SHPCreateTree(SHPHandle hSHP, int nDimension, int nMaxDepth, double *padfBoundsMin, double *padfBoundsMax)
Definition: shptree.c:217
void SHPTreeTrimExtraNodes(SHPTree *hTree)
Definition: shptree.c:810
SHPTreeDiskHandle SHPOpenDiskTree(const char *pszQIXFilename, SAHooks *psHooks)
Definition: shptree.c:847
#define REINTERPRET_CAST(type, x)
Definition: shptree.c:159
static int SHPCheckObjectContained(SHPObject *psObject, int nDimension, double *padfBoundsMin, double *padfBoundsMax)
Definition: shptree.c:403
void SHPDestroyTree(SHPTree *psTree)
Definition: shptree.c:364
static void SHPTreeCollectShapeIds(SHPTree *hTree, SHPTreeNode *psTreeNode, double *padfBoundsMin, double *padfBoundsMax, int *pnShapeCount, int *pnMaxShapes, int **ppanShapeList)
Definition: shptree.c:648
int * SHPSearchDiskTree(FILE *fp, double *padfBoundsMin, double *padfBoundsMax, int *pnShapeCount)
Definition: shptree.c:1048
#define TRUE
Definition: shptree.c:135
#define FALSE
Definition: shptree.c:136
static int SHPTreeNodeTrim(SHPTreeNode *psTreeNode)
Definition: shptree.c:750
int * SHPSearchDiskTreeEx(SHPTreeDiskHandle hDiskTree, double *padfBoundsMin, double *padfBoundsMax, int *pnShapeCount)
Definition: shptree.c:1070
static void * SfRealloc(void *pMem, int nNewSize)
Definition: shptree.c:171
#define SHPLIB_NULLPTR
Definition: shptree.c:161
static void SHPTreeSplitBounds(double *padfBoundsMinIn, double *padfBoundsMaxIn, double *padfBoundsMin1, double *padfBoundsMax1, double *padfBoundsMin2, double *padfBoundsMax2)
Definition: shptree.c:440
int * SHPTreeFindLikelyShapes(SHPTree *hTree, double *padfBoundsMin, double *padfBoundsMax, int *pnShapeCount)
Definition: shptree.c:716
static void SHPWriteTreeNode(SAFile fp, SHPTreeNode *node, SAHooks *psHooks)
Definition: shptree.c:1161
void(* Error)(const char *message)
Definition: shapefil.h:303
SAFile(* FOpen)(const char *filename, const char *access)
Definition: shapefil.h:294
SAOffset(* FWrite)(void *p, SAOffset size, SAOffset nmemb, SAFile file)
Definition: shapefil.h:296
int(* FClose)(SAFile file)
Definition: shapefil.h:300
SAOffset(* FRead)(void *p, SAOffset size, SAOffset nmemb, SAFile file)
Definition: shapefil.h:295
SAOffset(* FSeek)(SAFile file, SAOffset offset, int whence)
Definition: shapefil.h:297
SAFile fpQIX
Definition: shptree.c:840
SAHooks sHooks
Definition: shptree.c:839
SHPTreeNode * psRoot
Definition: shapefil.h:511
SHPHandle hSHP
Definition: shapefil.h:505
int nDimension
Definition: shapefil.h:508
int nMaxDepth
Definition: shapefil.h:507
int nTotalCount
Definition: shapefil.h:509
SHPObject ** papsShapeObj
Definition: shapefil.h:497
int * panShapeIds
Definition: shapefil.h:496
struct shape_tree_node * apsSubNode[4]
Definition: shapefil.h:500
double adfBoundsMax[4]
Definition: shapefil.h:491
double adfBoundsMin[4]
Definition: shapefil.h:490
double dfYMax
Definition: shapefil.h:403
double dfXMin
Definition: shapefil.h:397
int nShapeId
Definition: shapefil.h:385
double dfYMin
Definition: shapefil.h:398
double dfMMax
Definition: shapefil.h:405
double dfZMax
Definition: shapefil.h:404
double dfXMax
Definition: shapefil.h:402
double dfMMin
Definition: shapefil.h:400
double dfZMin
Definition: shapefil.h:399