| 1 |
|
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 |
|
|---|
| 20 |
|
|---|
| 21 |
|
|---|
| 22 |
|
|---|
| 23 |
|
|---|
| 24 |
|
|---|
| 25 |
|
|---|
| 26 |
|
|---|
| 27 |
|
|---|
| 28 |
|
|---|
| 29 |
|
|---|
| 30 |
|
|---|
| 31 |
|
|---|
| 32 |
|
|---|
| 33 |
|
|---|
| 34 |
|
|---|
| 35 |
|
|---|
| 36 |
|
|---|
| 37 |
|
|---|
| 38 |
|
|---|
| 39 |
|
|---|
| 40 |
|
|---|
| 41 |
|
|---|
| 42 |
|
|---|
| 43 |
|
|---|
| 44 |
|
|---|
| 45 |
|
|---|
| 46 |
|
|---|
| 47 |
|
|---|
| 48 |
|
|---|
| 49 |
|
|---|
| 50 |
|
|---|
| 51 |
|
|---|
| 52 |
|
|---|
| 53 |
|
|---|
| 54 |
|
|---|
| 55 |
|
|---|
| 56 |
|
|---|
| 57 |
|
|---|
| 58 |
|
|---|
| 59 |
|
|---|
| 60 |
|
|---|
| 61 |
|
|---|
| 62 |
|
|---|
| 63 |
|
|---|
| 64 |
|
|---|
| 65 |
|
|---|
| 66 |
|
|---|
| 67 |
|
|---|
| 68 |
|
|---|
| 69 |
|
|---|
| 70 |
|
|---|
| 71 |
|
|---|
| 72 |
|
|---|
| 73 |
|
|---|
| 74 |
|
|---|
| 75 |
#include "shapefil.h" |
|---|
| 76 |
|
|---|
| 77 |
#include <math.h> |
|---|
| 78 |
#include <assert.h> |
|---|
| 79 |
#include <stdlib.h> |
|---|
| 80 |
#include <string.h> |
|---|
| 81 |
#ifdef USE_CPL |
|---|
| 82 |
#include <cpl_error.h> |
|---|
| 83 |
#endif |
|---|
| 84 |
|
|---|
| 85 |
SHP_CVSID("$Id: shptree.c,v 1.12 2008/11/12 15:39:50 fwarmerdam Exp $") |
|---|
| 86 |
|
|---|
| 87 |
#ifndef TRUE |
|---|
| 88 |
# define TRUE 1 |
|---|
| 89 |
# define FALSE 0 |
|---|
| 90 |
#endif |
|---|
| 91 |
|
|---|
| 92 |
static int bBigEndian = 0; |
|---|
| 93 |
|
|---|
| 94 |
|
|---|
| 95 |
|
|---|
| 96 |
|
|---|
| 97 |
|
|---|
| 98 |
|
|---|
| 99 |
|
|---|
| 100 |
|
|---|
| 101 |
|
|---|
| 102 |
|
|---|
| 103 |
#define SHP_SPLIT_RATIO 0.55 |
|---|
| 104 |
|
|---|
| 105 |
|
|---|
| 106 |
|
|---|
| 107 |
|
|---|
| 108 |
|
|---|
| 109 |
|
|---|
| 110 |
|
|---|
| 111 |
|
|---|
| 112 |
static void * SfRealloc( void * pMem, int nNewSize ) |
|---|
| 113 |
|
|---|
| 114 |
{ |
|---|
| 115 |
if( pMem == NULL ) |
|---|
| 116 |
return( (void *) malloc(nNewSize) ); |
|---|
| 117 |
else |
|---|
| 118 |
return( (void *) realloc(pMem,nNewSize) ); |
|---|
| 119 |
} |
|---|
| 120 |
|
|---|
| 121 |
|
|---|
| 122 |
|
|---|
| 123 |
|
|---|
| 124 |
|
|---|
| 125 |
|
|---|
| 126 |
|
|---|
| 127 |
static SHPTreeNode *SHPTreeNodeCreate( double * padfBoundsMin, |
|---|
| 128 |
double * padfBoundsMax ) |
|---|
| 129 |
|
|---|
| 130 |
{ |
|---|
| 131 |
SHPTreeNode *psTreeNode; |
|---|
| 132 |
|
|---|
| 133 |
psTreeNode = (SHPTreeNode *) malloc(sizeof(SHPTreeNode)); |
|---|
| 134 |
if( NULL == psTreeNode ) |
|---|
| 135 |
{ |
|---|
| 136 |
#ifdef USE_CPL |
|---|
| 137 |
CPLError( CE_Fatal, CPLE_OutOfMemory, "Memory allocation failure"); |
|---|
| 138 |
#endif |
|---|
| 139 |
return NULL; |
|---|
| 140 |
} |
|---|
| 141 |
|
|---|
| 142 |
psTreeNode->nShapeCount = 0; |
|---|
| 143 |
psTreeNode->panShapeIds = NULL; |
|---|
| 144 |
psTreeNode->papsShapeObj = NULL; |
|---|
| 145 |
|
|---|
| 146 |
psTreeNode->nSubNodes = 0; |
|---|
| 147 |
|
|---|
| 148 |
if( padfBoundsMin != NULL ) |
|---|
| 149 |
memcpy( psTreeNode->adfBoundsMin, padfBoundsMin, sizeof(double) * 4 ); |
|---|
| 150 |
|
|---|
| 151 |
if( padfBoundsMax != NULL ) |
|---|
| 152 |
memcpy( psTreeNode->adfBoundsMax, padfBoundsMax, sizeof(double) * 4 ); |
|---|
| 153 |
|
|---|
| 154 |
return psTreeNode; |
|---|
| 155 |
} |
|---|
| 156 |
|
|---|
| 157 |
|
|---|
| 158 |
|
|---|
| 159 |
|
|---|
| 160 |
|
|---|
| 161 |
|
|---|
| 162 |
SHPTree SHPAPI_CALL1(*) |
|---|
| 163 |
SHPCreateTree( SHPHandle hSHP, int nDimension, int nMaxDepth, |
|---|
| 164 |
double *padfBoundsMin, double *padfBoundsMax ) |
|---|
| 165 |
|
|---|
| 166 |
{ |
|---|
| 167 |
SHPTree *psTree; |
|---|
| 168 |
|
|---|
| 169 |
if( padfBoundsMin == NULL && hSHP == NULL ) |
|---|
| 170 |
return NULL; |
|---|
| 171 |
|
|---|
| 172 |
|
|---|
| 173 |
|
|---|
| 174 |
|
|---|
| 175 |
psTree = (SHPTree *) malloc(sizeof(SHPTree)); |
|---|
| 176 |
if( NULL == psTree ) |
|---|
| 177 |
{ |
|---|
| 178 |
#ifdef USE_CPL |
|---|
| 179 |
CPLError( CE_Fatal, CPLE_OutOfMemory, "Memory allocation failure"); |
|---|
| 180 |
#endif |
|---|
| 181 |
return NULL; |
|---|
| 182 |
} |
|---|
| 183 |
|
|---|
| 184 |
psTree->hSHP = hSHP; |
|---|
| 185 |
psTree->nMaxDepth = nMaxDepth; |
|---|
| 186 |
psTree->nDimension = nDimension; |
|---|
| 187 |
psTree->nTotalCount = 0; |
|---|
| 188 |
|
|---|
| 189 |
|
|---|
| 190 |
|
|---|
| 191 |
|
|---|
| 192 |
|
|---|
| 193 |
if( psTree->nMaxDepth == 0 && hSHP != NULL ) |
|---|
| 194 |
{ |
|---|
| 195 |
int nMaxNodeCount = 1; |
|---|
| 196 |
int nShapeCount; |
|---|
| 197 |
|
|---|
| 198 |
SHPGetInfo( hSHP, &nShapeCount, NULL, NULL, NULL ); |
|---|
| 199 |
while( nMaxNodeCount*4 < nShapeCount ) |
|---|
| 200 |
{ |
|---|
| 201 |
psTree->nMaxDepth += 1; |
|---|
| 202 |
nMaxNodeCount = nMaxNodeCount * 2; |
|---|
| 203 |
} |
|---|
| 204 |
|
|---|
| 205 |
#ifdef USE_CPL |
|---|
| 206 |
CPLDebug( "Shape", |
|---|
| 207 |
"Estimated spatial index tree depth: %d", |
|---|
| 208 |
psTree->nMaxDepth ); |
|---|
| 209 |
#endif |
|---|
| 210 |
|
|---|
| 211 |
|
|---|
| 212 |
|
|---|
| 213 |
|
|---|
| 214 |
|
|---|
| 215 |
if( psTree->nMaxDepth > MAX_DEFAULT_TREE_DEPTH ) |
|---|
| 216 |
{ |
|---|
| 217 |
psTree->nMaxDepth = MAX_DEFAULT_TREE_DEPTH; |
|---|
| 218 |
|
|---|
| 219 |
#ifdef USE_CPL |
|---|
| 220 |
CPLDebug( "Shape", |
|---|
| 221 |
"Falling back to max number of allowed index tree levels (%d).", |
|---|
| 222 |
MAX_DEFAULT_TREE_DEPTH ); |
|---|
| 223 |
#endif |
|---|
| 224 |
} |
|---|
| 225 |
} |
|---|
| 226 |
|
|---|
| 227 |
|
|---|
| 228 |
|
|---|
| 229 |
|
|---|
| 230 |
psTree->psRoot = SHPTreeNodeCreate( padfBoundsMin, padfBoundsMax ); |
|---|
| 231 |
if( NULL == psTree->psRoot ) |
|---|
| 232 |
{ |
|---|
| 233 |
return NULL; |
|---|
| 234 |
} |
|---|
| 235 |
|
|---|
| 236 |
|
|---|
| 237 |
|
|---|
| 238 |
|
|---|
| 239 |
|
|---|
| 240 |
|
|---|
| 241 |
assert( NULL != psTree ); |
|---|
| 242 |
assert( NULL != psTree->psRoot ); |
|---|
| 243 |
|
|---|
| 244 |
if( padfBoundsMin == NULL ) |
|---|
| 245 |
{ |
|---|
| 246 |
SHPGetInfo( hSHP, NULL, NULL, |
|---|
| 247 |
psTree->psRoot->adfBoundsMin, |
|---|
| 248 |
psTree->psRoot->adfBoundsMax ); |
|---|
| 249 |
} |
|---|
| 250 |
|
|---|
| 251 |
|
|---|
| 252 |
|
|---|
| 253 |
|
|---|
| 254 |
if( hSHP != NULL ) |
|---|
| 255 |
{ |
|---|
| 256 |
int iShape, nShapeCount; |
|---|
| 257 |
|
|---|
| 258 |
SHPGetInfo( hSHP, &nShapeCount, NULL, NULL, NULL ); |
|---|
| 259 |
|
|---|
| 260 |
for( iShape = 0; iShape < nShapeCount; iShape++ ) |
|---|
| 261 |
{ |
|---|
| 262 |
SHPObject *psShape; |
|---|
| 263 |
|
|---|
| 264 |
psShape = SHPReadObject( hSHP, iShape ); |
|---|
| 265 |
if( psShape != NULL ) |
|---|
| 266 |
{ |
|---|
| 267 |
SHPTreeAddShapeId( psTree, psShape ); |
|---|
| 268 |
SHPDestroyObject( psShape ); |
|---|
| 269 |
} |
|---|
| 270 |
} |
|---|
| 271 |
} |
|---|
| 272 |
|
|---|
| 273 |
return psTree; |
|---|
| 274 |
} |
|---|
| 275 |
|
|---|
| 276 |
|
|---|
| 277 |
|
|---|
| 278 |
|
|---|
| 279 |
|
|---|
| 280 |
static void SHPDestroyTreeNode( SHPTreeNode * psTreeNode ) |
|---|
| 281 |
|
|---|
| 282 |
{ |
|---|
| 283 |
int i; |
|---|
| 284 |
|
|---|
| 285 |
assert( NULL != psTreeNode ); |
|---|
| 286 |
|
|---|
| 287 |
for( i = 0; i < psTreeNode->nSubNodes; i++ ) |
|---|
| 288 |
{ |
|---|
| 289 |
if( psTreeNode->apsSubNode[i] != NULL ) |
|---|
| 290 |
SHPDestroyTreeNode( psTreeNode->apsSubNode[i] ); |
|---|
| 291 |
} |
|---|
| 292 |
|
|---|
| 293 |
if( psTreeNode->panShapeIds != NULL ) |
|---|
| 294 |
free( psTreeNode->panShapeIds ); |
|---|
| 295 |
|
|---|
| 296 |
if( psTreeNode->papsShapeObj != NULL ) |
|---|
| 297 |
{ |
|---|
| 298 |
for( i = 0; i < psTreeNode->nShapeCount; i++ ) |
|---|
| 299 |
{ |
|---|
| 300 |
if( psTreeNode->papsShapeObj[i] != NULL ) |
|---|
| 301 |
SHPDestroyObject( psTreeNode->papsShapeObj[i] ); |
|---|
| 302 |
} |
|---|
| 303 |
|
|---|
| 304 |
free( psTreeNode->papsShapeObj ); |
|---|
| 305 |
} |
|---|
| 306 |
|
|---|
| 307 |
free( psTreeNode ); |
|---|
| 308 |
} |
|---|
| 309 |
|
|---|
| 310 |
|
|---|
| 311 |
|
|---|
| 312 |
|
|---|
| 313 |
|
|---|
| 314 |
void SHPAPI_CALL |
|---|
| 315 |
SHPDestroyTree( SHPTree * psTree ) |
|---|
| 316 |
|
|---|
| 317 |
{ |
|---|
| 318 |
SHPDestroyTreeNode( psTree->psRoot ); |
|---|
| 319 |
free( psTree ); |
|---|
| 320 |
} |
|---|
| 321 |
|
|---|
| 322 |
|
|---|
| 323 |
|
|---|
| 324 |
|
|---|
| 325 |
|
|---|
| 326 |
|
|---|
| 327 |
|
|---|
| 328 |
int SHPAPI_CALL |
|---|
| 329 |
SHPCheckBoundsOverlap( double * padfBox1Min, double * padfBox1Max, |
|---|
| 330 |
double * padfBox2Min, double * padfBox2Max, |
|---|
| 331 |
int nDimension ) |
|---|
| 332 |
|
|---|
| 333 |
{ |
|---|
| 334 |
int iDim; |
|---|
| 335 |
|
|---|
| 336 |
for( iDim = 0; iDim < nDimension; iDim++ ) |
|---|
| 337 |
{ |
|---|
| 338 |
if( padfBox2Max[iDim] < padfBox1Min[iDim] ) |
|---|
| 339 |
return FALSE; |
|---|
| 340 |
|
|---|
| 341 |
if( padfBox1Max[iDim] < padfBox2Min[iDim] ) |
|---|
| 342 |
return FALSE; |
|---|
| 343 |
} |
|---|
| 344 |
|
|---|
| 345 |
return TRUE; |
|---|
| 346 |
} |
|---|
| 347 |
|
|---|
| 348 |
|
|---|
| 349 |
|
|---|
| 350 |
|
|---|
| 351 |
|
|---|
| 352 |
|
|---|
| 353 |
|
|---|
| 354 |
static int SHPCheckObjectContained( SHPObject * psObject, int nDimension, |
|---|
| 355 |
double * padfBoundsMin, double * padfBoundsMax ) |
|---|
| 356 |
|
|---|
| 357 |
{ |
|---|
| 358 |
if( psObject->dfXMin < padfBoundsMin[0] |
|---|
| 359 |
|| psObject->dfXMax > padfBoundsMax[0] ) |
|---|
| 360 |
return FALSE; |
|---|
| 361 |
|
|---|
| 362 |
if( psObject->dfYMin < padfBoundsMin[1] |
|---|
| 363 |
|| psObject->dfYMax > padfBoundsMax[1] ) |
|---|
| 364 |
return FALSE; |
|---|
| 365 |
|
|---|
| 366 |
if( nDimension == 2 ) |
|---|
| 367 |
return TRUE; |
|---|
| 368 |
|
|---|
| 369 |
if( psObject->dfZMin < padfBoundsMin[2] |
|---|
| 370 |
|| psObject->dfZMax < padfBoundsMax[2] ) |
|---|
| 371 |
return FALSE; |
|---|
| 372 |
|
|---|
| 373 |
if( nDimension == 3 ) |
|---|
| 374 |
return TRUE; |
|---|
| 375 |
|
|---|
| 376 |
if( psObject->dfMMin < padfBoundsMin[3] |
|---|
| 377 |
|| psObject->dfMMax < padfBoundsMax[3] ) |
|---|
| 378 |
return FALSE; |
|---|
| 379 |
|
|---|
| 380 |
return TRUE; |
|---|
| 381 |
} |
|---|
| 382 |
|
|---|
| 383 |
|
|---|
| 384 |
|
|---|
| 385 |
|
|---|
| 386 |
|
|---|
| 387 |
|
|---|
| 388 |
|
|---|
| 389 |
|
|---|
| 390 |
void SHPAPI_CALL |
|---|
| 391 |
SHPTreeSplitBounds( double *padfBoundsMinIn, double *padfBoundsMaxIn, |
|---|
| 392 |
double *padfBoundsMin1, double * padfBoundsMax1, |
|---|
| 393 |
double *padfBoundsMin2, double * padfBoundsMax2 ) |
|---|
| 394 |
|
|---|
| 395 |
{ |
|---|
| 396 |
|
|---|
| 397 |
|
|---|
| 398 |
|
|---|
| 399 |
|
|---|
| 400 |
memcpy( padfBoundsMin1, padfBoundsMinIn, sizeof(double) * 4 ); |
|---|
| 401 |
memcpy( padfBoundsMax1, padfBoundsMaxIn, sizeof(double) * 4 ); |
|---|
| 402 |
memcpy( padfBoundsMin2, padfBoundsMinIn, sizeof(double) * 4 ); |
|---|
| 403 |
memcpy( padfBoundsMax2, padfBoundsMaxIn, sizeof(double) * 4 ); |
|---|
| 404 |
|
|---|
| 405 |
|
|---|
| 406 |
|
|---|
| 407 |
|
|---|
| 408 |
if( (padfBoundsMaxIn[0] - padfBoundsMinIn[0]) |
|---|
| 409 |
> (padfBoundsMaxIn[1] - padfBoundsMinIn[1]) ) |
|---|
| 410 |
{ |
|---|
| 411 |
double dfRange = padfBoundsMaxIn[0] - padfBoundsMinIn[0]; |
|---|
| 412 |
|
|---|
| 413 |
padfBoundsMax1[0] = padfBoundsMinIn[0] + dfRange * SHP_SPLIT_RATIO; |
|---|
| 414 |
padfBoundsMin2[0] = padfBoundsMaxIn[0] - dfRange * SHP_SPLIT_RATIO; |
|---|
| 415 |
} |
|---|
| 416 |
|
|---|
| 417 |
|
|---|
| 418 |
|
|---|
| 419 |
|
|---|
| 420 |
else |
|---|
| 421 |
{ |
|---|
| 422 |
double dfRange = padfBoundsMaxIn[1] - padfBoundsMinIn[1]; |
|---|
| 423 |
|
|---|
| 424 |
padfBoundsMax1[1] = padfBoundsMinIn[1] + dfRange * SHP_SPLIT_RATIO; |
|---|
| 425 |
padfBoundsMin2[1] = padfBoundsMaxIn[1] - dfRange * SHP_SPLIT_RATIO; |
|---|
| 426 |
} |
|---|
| 427 |
} |
|---|
| 428 |
|
|---|
| 429 |
|
|---|
| 430 |
|
|---|
| 431 |
|
|---|
| 432 |
|
|---|
| 433 |
static int |
|---|
| 434 |
SHPTreeNodeAddShapeId( SHPTreeNode * psTreeNode, SHPObject * psObject, |
|---|
| 435 |
int nMaxDepth, int nDimension ) |
|---|
| 436 |
|
|---|
| 437 |
{ |
|---|
| 438 |
int i; |
|---|
| 439 |
|
|---|
| 440 |
|
|---|
| 441 |
|
|---|
| 442 |
|
|---|
| 443 |
|
|---|
| 444 |
if( nMaxDepth > 1 && psTreeNode->nSubNodes > 0 ) |
|---|
| 445 |
{ |
|---|
| 446 |
for( i = 0; i < psTreeNode->nSubNodes; i++ ) |
|---|
| 447 |
{ |
|---|
| 448 |
if( SHPCheckObjectContained(psObject, nDimension, |
|---|
| 449 |
psTreeNode->apsSubNode[i]->adfBoundsMin, |
|---|
| 450 |
psTreeNode->apsSubNode[i]->adfBoundsMax)) |
|---|
| 451 |
{ |
|---|
| 452 |
return SHPTreeNodeAddShapeId( psTreeNode->apsSubNode[i], |
|---|
| 453 |
psObject, nMaxDepth-1, |
|---|
| 454 |
nDimension ); |
|---|
| 455 |
} |
|---|
| 456 |
} |
|---|
| 457 |
} |
|---|
| 458 |
|
|---|
| 459 |
|
|---|
| 460 |
|
|---|
| 461 |
|
|---|
| 462 |
|
|---|
| 463 |
#if MAX_SUBNODE == 4 |
|---|
| 464 |
else if( nMaxDepth > 1 && psTreeNode->nSubNodes == 0 ) |
|---|
| 465 |
{ |
|---|
| 466 |
double adfBoundsMinH1[4], adfBoundsMaxH1[4]; |
|---|
| 467 |
double adfBoundsMinH2[4], adfBoundsMaxH2[4]; |
|---|
| 468 |
double adfBoundsMin1[4], adfBoundsMax1[4]; |
|---|
| 469 |
double adfBoundsMin2[4], adfBoundsMax2[4]; |
|---|
| 470 |
double adfBoundsMin3[4], adfBoundsMax3[4]; |
|---|
| 471 |
double adfBoundsMin4[4], adfBoundsMax4[4]; |
|---|
| 472 |
|
|---|
| 473 |
SHPTreeSplitBounds( psTreeNode->adfBoundsMin, |
|---|
| 474 |
psTreeNode->adfBoundsMax, |
|---|
| 475 |
adfBoundsMinH1, adfBoundsMaxH1, |
|---|
| 476 |
adfBoundsMinH2, adfBoundsMaxH2 ); |
|---|
| 477 |
|
|---|
| 478 |
SHPTreeSplitBounds( adfBoundsMinH1, adfBoundsMaxH1, |
|---|
| 479 |
adfBoundsMin1, adfBoundsMax1, |
|---|
| 480 |
adfBoundsMin2, adfBoundsMax2 ); |
|---|
| 481 |
|
|---|
| 482 |
SHPTreeSplitBounds( adfBoundsMinH2, adfBoundsMaxH2, |
|---|
| 483 |
adfBoundsMin3, adfBoundsMax3, |
|---|
| 484 |
adfBoundsMin4, adfBoundsMax4 ); |
|---|
| 485 |
|
|---|
| 486 |
if( SHPCheckObjectContained(psObject, nDimension, |
|---|
| 487 |
adfBoundsMin1, adfBoundsMax1) |
|---|
| 488 |
|| SHPCheckObjectContained(psObject, nDimension, |
|---|
| 489 |
adfBoundsMin2, adfBoundsMax2) |
|---|
| 490 |
|| SHPCheckObjectContained(psObject, nDimension, |
|---|
| 491 |
adfBoundsMin3, adfBoundsMax3) |
|---|
| 492 |
|| SHPCheckObjectContained(psObject, nDimension, |
|---|
| 493 |
adfBoundsMin4, adfBoundsMax4) ) |
|---|
| 494 |
{ |
|---|
| 495 |
psTreeNode->nSubNodes = 4; |
|---|
| 496 |
psTreeNode->apsSubNode[0] = SHPTreeNodeCreate( adfBoundsMin1, |
|---|
| 497 |
adfBoundsMax1 ); |
|---|
| 498 |
psTreeNode->apsSubNode[1] = SHPTreeNodeCreate( adfBoundsMin2, |
|---|
| 499 |
adfBoundsMax2 ); |
|---|
| 500 |
psTreeNode->apsSubNode[2] = SHPTreeNodeCreate( adfBoundsMin3, |
|---|
| 501 |
adfBoundsMax3 ); |
|---|
| 502 |
psTreeNode->apsSubNode[3] = SHPTreeNodeCreate( adfBoundsMin4, |
|---|
| 503 |
adfBoundsMax4 ); |
|---|
| 504 |
|
|---|
| 505 |
|
|---|
| 506 |
return( SHPTreeNodeAddShapeId( psTreeNode, psObject, |
|---|
| 507 |
nMaxDepth, nDimension ) ); |
|---|
| 508 |
} |
|---|
| 509 |
} |
|---|
| 510 |
#endif |
|---|
| 511 |
|
|---|
| 512 |
|
|---|
| 513 |
|
|---|
| 514 |
|
|---|
| 515 |
|
|---|
| 516 |
#if MAX_SUBNODE == 2 |
|---|
| 517 |
else if( nMaxDepth > 1 && psTreeNode->nSubNodes == 0 ) |
|---|
| 518 |
{ |
|---|
| 519 |
double adfBoundsMin1[4], adfBoundsMax1[4]; |
|---|
| 520 |
double adfBoundsMin2[4], adfBoundsMax2[4]; |
|---|
| 521 |
|
|---|
| 522 |
SHPTreeSplitBounds( psTreeNode->adfBoundsMin, psTreeNode->adfBoundsMax, |
|---|
| 523 |
adfBoundsMin1, adfBoundsMax1, |
|---|
| 524 |
adfBoundsMin2, adfBoundsMax2 ); |
|---|
| 525 |
|
|---|
| 526 |
if( SHPCheckObjectContained(psObject, nDimension, |
|---|
| 527 |
adfBoundsMin1, adfBoundsMax1)) |
|---|
| 528 |
{ |
|---|
| 529 |
psTreeNode->nSubNodes = 2; |
|---|
| 530 |
psTreeNode->apsSubNode[0] = SHPTreeNodeCreate( adfBoundsMin1, |
|---|
| 531 |
adfBoundsMax1 ); |
|---|
| 532 |
psTreeNode->apsSubNode[1] = SHPTreeNodeCreate( adfBoundsMin2, |
|---|
| 533 |
adfBoundsMax2 ); |
|---|
| 534 |
|
|---|
| 535 |
return( SHPTreeNodeAddShapeId( psTreeNode->apsSubNode[0], psObject, |
|---|
| 536 |
nMaxDepth - 1, nDimension ) ); |
|---|
| 537 |
} |
|---|
| 538 |
else if( SHPCheckObjectContained(psObject, nDimension, |
|---|
| 539 |
adfBoundsMin2, adfBoundsMax2) ) |
|---|
| 540 |
{ |
|---|
| 541 |
psTreeNode->nSubNodes = 2; |
|---|
| 542 |
psTreeNode->apsSubNode[0] = SHPTreeNodeCreate( adfBoundsMin1, |
|---|
| 543 |
adfBoundsMax1 ); |
|---|
| 544 |
psTreeNode->apsSubNode[1] = SHPTreeNodeCreate( adfBoundsMin2, |
|---|
| 545 |
adfBoundsMax2 ); |
|---|
| 546 |
|
|---|
| 547 |
return( SHPTreeNodeAddShapeId( psTreeNode->apsSubNode[1], psObject, |
|---|
| 548 |
nMaxDepth - 1, nDimension ) ); |
|---|
| 549 |
} |
|---|
| 550 |
} |
|---|
| 551 |
#endif |
|---|
| 552 |
|
|---|
| 553 |
|
|---|
| 554 |
|
|---|
| 555 |
|
|---|
| 556 |
psTreeNode->nShapeCount++; |
|---|
| 557 |
|
|---|
| 558 |
psTreeNode->panShapeIds = (int *) |
|---|
| 559 |
SfRealloc( psTreeNode->panShapeIds, |
|---|
| 560 |
sizeof(int) * psTreeNode->nShapeCount ); |
|---|
| 561 |
psTreeNode->panShapeIds[psTreeNode->nShapeCount-1] = psObject->nShapeId; |
|---|
| 562 |
|
|---|
| 563 |
if( psTreeNode->papsShapeObj != NULL ) |
|---|
| 564 |
{ |
|---|
| 565 |
psTreeNode->papsShapeObj = (SHPObject **) |
|---|
| 566 |
SfRealloc( psTreeNode->papsShapeObj, |
|---|
| 567 |
sizeof(void *) * psTreeNode->nShapeCount ); |
|---|
| 568 |
psTreeNode->papsShapeObj[psTreeNode->nShapeCount-1] = NULL; |
|---|
| 569 |
} |
|---|
| 570 |
|
|---|
| 571 |
return TRUE; |
|---|
| 572 |
} |
|---|
| 573 |
|
|---|
| 574 |
|
|---|
| 575 |
|
|---|
| 576 |
|
|---|
| 577 |
|
|---|
| 578 |
|
|---|
| 579 |
|
|---|
| 580 |
|
|---|
| 581 |
int SHPAPI_CALL |
|---|
| 582 |
SHPTreeAddShapeId( SHPTree * psTree, SHPObject * psObject ) |
|---|
| 583 |
|
|---|
| 584 |
{ |
|---|
| 585 |
psTree->nTotalCount++; |
|---|
| 586 |
|
|---|
| 587 |
return( SHPTreeNodeAddShapeId( psTree->psRoot, psObject, |
|---|
| 588 |
psTree->nMaxDepth, psTree->nDimension ) ); |
|---|
| 589 |
} |
|---|
| 590 |
|
|---|
| 591 |
|
|---|
| 592 |
|
|---|
| 593 |
|
|---|
| 594 |
|
|---|
| 595 |
|
|---|
| 596 |
|
|---|
| 597 |
|
|---|
| 598 |
void SHPAPI_CALL |
|---|
| 599 |
SHPTreeCollectShapeIds( SHPTree *hTree, SHPTreeNode * psTreeNode, |
|---|
| 600 |
double * padfBoundsMin, double * padfBoundsMax, |
|---|
| 601 |
int * pnShapeCount, int * pnMaxShapes, |
|---|
| 602 |
int ** ppanShapeList ) |
|---|
| 603 |
|
|---|
| 604 |
{ |
|---|
| 605 |
int i; |
|---|
| 606 |
|
|---|
| 607 |
|
|---|
| 608 |
|
|---|
| 609 |
|
|---|
| 610 |
|
|---|
| 611 |
if( !SHPCheckBoundsOverlap( psTreeNode->adfBoundsMin, |
|---|
| 612 |
psTreeNode->adfBoundsMax, |
|---|
| 613 |
padfBoundsMin, |
|---|
| 614 |
padfBoundsMax, |
|---|
| 615 |
hTree->nDimension ) ) |
|---|
| 616 |
return; |
|---|
| 617 |
|
|---|
| 618 |
|
|---|
| 619 |
|
|---|
| 620 |
|
|---|
| 621 |
if( *pnShapeCount + psTreeNode->nShapeCount > *pnMaxShapes ) |
|---|
| 622 |
{ |
|---|
| 623 |
*pnMaxShapes = (*pnShapeCount + psTreeNode->nShapeCount) * 2 + 20; |
|---|
| 624 |
*ppanShapeList = (int *) |
|---|
| 625 |
SfRealloc(*ppanShapeList,sizeof(int) * *pnMaxShapes); |
|---|
| 626 |
} |
|---|
| 627 |
|
|---|
| 628 |
|
|---|
| 629 |
|
|---|
| 630 |
|
|---|
| 631 |
for( i = 0; i < psTreeNode->nShapeCount; i++ ) |
|---|
| 632 |
{ |
|---|
| 633 |
(*ppanShapeList)[(*pnShapeCount)++] = psTreeNode->panShapeIds[i]; |
|---|
| 634 |
} |
|---|
| 635 |
|
|---|
| 636 |
|
|---|
| 637 |
|
|---|
| 638 |
|
|---|
| 639 |
for( i = 0; i < psTreeNode->nSubNodes; i++ ) |
|---|
| 640 |
{ |
|---|
| 641 |
if( psTreeNode->apsSubNode[i] != NULL ) |
|---|
| 642 |
SHPTreeCollectShapeIds( hTree, psTreeNode->apsSubNode[i], |
|---|
| 643 |
padfBoundsMin, padfBoundsMax, |
|---|
| 644 |
pnShapeCount, pnMaxShapes, |
|---|
| 645 |
ppanShapeList ); |
|---|
| 646 |
} |
|---|
| 647 |
} |
|---|
| 648 |
|
|---|
| 649 |
|
|---|
| 650 |
|
|---|
| 651 |
|
|---|
| 652 |
|
|---|
| 653 |
|
|---|
| 654 |
|
|---|
| 655 |
|
|---|
| 656 |
|
|---|
| 657 |
|
|---|
| 658 |
|
|---|
| 659 |
|
|---|
| 660 |
static int |
|---|
| 661 |
compare_ints( const void * a, const void * b) |
|---|
| 662 |
{ |
|---|
| 663 |
return (*(int*)a) - (*(int*)b); |
|---|
| 664 |
} |
|---|
| 665 |
|
|---|
| 666 |
int SHPAPI_CALL1(*) |
|---|
| 667 |
SHPTreeFindLikelyShapes( SHPTree * hTree, |
|---|
| 668 |
double * padfBoundsMin, double * padfBoundsMax, |
|---|
| 669 |
int * pnShapeCount ) |
|---|
| 670 |
|
|---|
| 671 |
{ |
|---|
| 672 |
int *panShapeList=NULL, nMaxShapes = 0; |
|---|
| 673 |
|
|---|
| 674 |
|
|---|
| 675 |
|
|---|
| 676 |
|
|---|
| 677 |
*pnShapeCount = 0; |
|---|
| 678 |
|
|---|
| 679 |
SHPTreeCollectShapeIds( hTree, hTree->psRoot, |
|---|
| 680 |
padfBoundsMin, padfBoundsMax, |
|---|
| 681 |
pnShapeCount, &nMaxShapes, |
|---|
| 682 |
&panShapeList ); |
|---|
| 683 |
|
|---|
| 684 |
|
|---|
| 685 |
|
|---|
| 686 |
|
|---|
| 687 |
|
|---|
| 688 |
qsort(panShapeList, *pnShapeCount, sizeof(int), compare_ints); |
|---|
| 689 |
|
|---|
| 690 |
return panShapeList; |
|---|
| 691 |
} |
|---|
| 692 |
|
|---|
| 693 |
|
|---|
| 694 |
|
|---|
| 695 |
|
|---|
| 696 |
|
|---|
| 697 |
|
|---|
| 698 |
|
|---|
| 699 |
|
|---|
| 700 |
static int SHPTreeNodeTrim( SHPTreeNode * psTreeNode ) |
|---|
| 701 |
|
|---|
| 702 |
{ |
|---|
| 703 |
int i; |
|---|
| 704 |
|
|---|
| 705 |
|
|---|
| 706 |
|
|---|
| 707 |
|
|---|
| 708 |
for( i = 0; i < psTreeNode->nSubNodes; i++ ) |
|---|
| 709 |
{ |
|---|
| 710 |
if( SHPTreeNodeTrim( psTreeNode->apsSubNode[i] ) ) |
|---|
| 711 |
{ |
|---|
| 712 |
SHPDestroyTreeNode( psTreeNode->apsSubNode[i] ); |
|---|
| 713 |
|
|---|
| 714 |
psTreeNode->apsSubNode[i] = |
|---|
| 715 |
psTreeNode->apsSubNode[psTreeNode->nSubNodes-1]; |
|---|
| 716 |
|
|---|
| 717 |
psTreeNode->nSubNodes--; |
|---|
| 718 |
|
|---|
| 719 |
i--; |
|---|
| 720 |
} |
|---|
| 721 |
} |
|---|
| 722 |
|
|---|
| 723 |
|
|---|
| 724 |
|
|---|
| 725 |
|
|---|
| 726 |
return( psTreeNode->nSubNodes == 0 && psTreeNode->nShapeCount == 0 ); |
|---|
| 727 |
} |
|---|
| 728 |
|
|---|
| 729 |
|
|---|
| 730 |
|
|---|
| 731 |
|
|---|
| 732 |
|
|---|
| 733 |
|
|---|
| 734 |
|
|---|
| 735 |
|
|---|
| 736 |
void SHPAPI_CALL |
|---|
| 737 |
SHPTreeTrimExtraNodes( SHPTree * hTree ) |
|---|
| 738 |
|
|---|
| 739 |
{ |
|---|
| 740 |
SHPTreeNodeTrim( hTree->psRoot ); |
|---|
| 741 |
} |
|---|
| 742 |
|
|---|
| 743 |
|
|---|
| 744 |
|
|---|
| 745 |
|
|---|
| 746 |
|
|---|
| 747 |
|
|---|
| 748 |
|
|---|
| 749 |
static void SwapWord( int length, void * wordP ) |
|---|
| 750 |
|
|---|
| 751 |
{ |
|---|
| 752 |
int i; |
|---|
| 753 |
unsigned char temp; |
|---|
| 754 |
|
|---|
| 755 |
for( i=0; i < length/2; i++ ) |
|---|
| 756 |
{ |
|---|
| 757 |
temp = ((unsigned char *) wordP)[i]; |
|---|
| 758 |
((unsigned char *)wordP)[i] = ((unsigned char *) wordP)[length-i-1]; |
|---|
| 759 |
((unsigned char *) wordP)[length-i-1] = temp; |
|---|
| 760 |
} |
|---|
| 761 |
} |
|---|
| 762 |
|
|---|
| 763 |
|
|---|
| 764 |
|
|---|
| 765 |
|
|---|
| 766 |
|
|---|
| 767 |
static int |
|---|
| 768 |
SHPSearchDiskTreeNode( FILE *fp, double *padfBoundsMin, double *padfBoundsMax, |
|---|
| 769 |
int **ppanResultBuffer, int *pnBufferMax, |
|---|
| 770 |
int *pnResultCount, int bNeedSwap ) |
|---|
| 771 |
|
|---|
| 772 |
{ |
|---|
| 773 |
int i; |
|---|
| 774 |
int offset; |
|---|
| 775 |
int numshapes, numsubnodes; |
|---|
| 776 |
double adfNodeBoundsMin[2], adfNodeBoundsMax[2]; |
|---|
| 777 |
|
|---|
| 778 |
|
|---|
| 779 |
|
|---|
| 780 |
|
|---|
| 781 |
fread( &offset, 4, 1, fp ); |
|---|
| 782 |
if ( bNeedSwap ) SwapWord ( 4, &offset ); |
|---|
| 783 |
|
|---|
| 784 |
fread( adfNodeBoundsMin, sizeof(double), 2, fp ); |
|---|
| 785 |
fread( adfNodeBoundsMax, sizeof(double), 2, fp ); |
|---|
| 786 |
if ( bNeedSwap ) |
|---|
| 787 |
{ |
|---|
| 788 |
SwapWord( 8, adfNodeBoundsMin + 0 ); |
|---|
| 789 |
SwapWord( 8, adfNodeBoundsMin + 1 ); |
|---|
| 790 |
SwapWord( 8, adfNodeBoundsMax + 0 ); |
|---|
| 791 |
SwapWord( 8, adfNodeBoundsMax + 1 ); |
|---|
| 792 |
} |
|---|
| 793 |
|
|---|
| 794 |
fread( &numshapes, 4, 1, fp ); |
|---|
| 795 |
if ( bNeedSwap ) SwapWord ( 4, &numshapes ); |
|---|
| 796 |
|
|---|
| 797 |
|
|---|
| 798 |
|
|---|
| 799 |
|
|---|
| 800 |
|
|---|
| 801 |
if( !SHPCheckBoundsOverlap( adfNodeBoundsMin, adfNodeBoundsMax, |
|---|
| 802 |
padfBoundsMin, padfBoundsMax, 2 ) ) |
|---|
| 803 |
{ |
|---|
| 804 |
offset += numshapes*sizeof(int) + sizeof(int); |
|---|
| 805 |
fseek(fp, offset, SEEK_CUR); |
|---|
| 806 |
return TRUE; |
|---|
| 807 |
} |
|---|
| 808 |
|
|---|
| 809 |
|
|---|
| 810 |
|
|---|
| 811 |
|
|---|
| 812 |
if(numshapes > 0) |
|---|
| 813 |
{ |
|---|
| 814 |
if( *pnResultCount + numshapes > *pnBufferMax ) |
|---|
| 815 |
{ |
|---|
| 816 |
*pnBufferMax = (int) ((*pnResultCount + numshapes + 100) * 1.25); |
|---|
| 817 |
*ppanResultBuffer = (int *) |
|---|
| 818 |
SfRealloc( *ppanResultBuffer, *pnBufferMax * sizeof(int) ); |
|---|
| 819 |
} |
|---|
| 820 |
|
|---|
| 821 |
fread( *ppanResultBuffer + *pnResultCount, |
|---|
| 822 |
sizeof(int), numshapes, fp ); |
|---|
| 823 |
|
|---|
| 824 |
if (bNeedSwap ) |
|---|
| 825 |
{ |
|---|
| 826 |
for( i=0; i<numshapes; i++ ) |
|---|
| 827 |
SwapWord( 4, *ppanResultBuffer + *pnResultCount + i ); |
|---|
| 828 |
} |
|---|
| 829 |
|
|---|
| 830 |
*pnResultCount += numshapes; |
|---|
| 831 |
} |
|---|
| 832 |
|
|---|
| 833 |
|
|---|
| 834 |
|
|---|
| 835 |
|
|---|
| 836 |
fread( &numsubnodes, 4, 1, fp ); |
|---|
| 837 |
if ( bNeedSwap ) SwapWord ( 4, &numsubnodes ); |
|---|
| 838 |
|
|---|
| 839 |
for(i=0; i<numsubnodes; i++) |
|---|
| 840 |
&n |
|---|