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 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682
|
/******************************************************************************
* $Id: sdtstransfer.cpp 21298 2010-12-20 10:58:34Z rouault $
*
* Project: SDTS Translator
* Purpose: Implementation of SDTSTransfer class.
* Author: Frank Warmerdam, warmerdam@pobox.com
*
******************************************************************************
* Copyright (c) 1999, Frank Warmerdam
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
****************************************************************************/
#include "sdts_al.h"
CPL_CVSID("$Id: sdtstransfer.cpp 21298 2010-12-20 10:58:34Z rouault $");
/************************************************************************/
/* SDTSTransfer() */
/************************************************************************/
SDTSTransfer::SDTSTransfer()
{
nLayers = 0;
panLayerCATDEntry = NULL;
papoLayerReader = NULL;
}
/************************************************************************/
/* ~SDTSTransfer() */
/************************************************************************/
SDTSTransfer::~SDTSTransfer()
{
Close();
}
/************************************************************************/
/* Open() */
/************************************************************************/
/**
* Open an SDTS transfer, and establish a list of data layers in the
* transfer.
*
* @param pszFilename The name of the CATD file within the transfer.
*
* @return TRUE if the open success, or FALSE if it fails.
*/
int SDTSTransfer::Open( const char * pszFilename )
{
/* -------------------------------------------------------------------- */
/* Open the catalog. */
/* -------------------------------------------------------------------- */
if( !oCATD.Read( pszFilename ) )
return FALSE;
/* -------------------------------------------------------------------- */
/* Read the IREF file. */
/* -------------------------------------------------------------------- */
if( oCATD.GetModuleFilePath( "IREF" ) == NULL )
{
CPLError( CE_Failure, CPLE_AppDefined,
"Can't find IREF module in transfer `%s'.\n",
pszFilename );
return FALSE;
}
if( !oIREF.Read( oCATD.GetModuleFilePath( "IREF" ) ) )
return FALSE;
/* -------------------------------------------------------------------- */
/* Read the XREF file. */
/* -------------------------------------------------------------------- */
if( oCATD.GetModuleFilePath( "XREF" ) == NULL )
{
CPLError( CE_Warning, CPLE_AppDefined,
"Can't find XREF module in transfer `%s'.\n",
pszFilename );
}
else if( !oXREF.Read( oCATD.GetModuleFilePath( "XREF" ) ) )
{
CPLError( CE_Warning, CPLE_AppDefined,
"Can't read XREF module, even though found in transfer `%s'.\n",
pszFilename );
}
/* -------------------------------------------------------------------- */
/* Build an index of layer types we recognise and care about. */
/* -------------------------------------------------------------------- */
int iCATDLayer;
panLayerCATDEntry = (int *) CPLMalloc(sizeof(int) * oCATD.GetEntryCount());
for( iCATDLayer = 0; iCATDLayer < oCATD.GetEntryCount(); iCATDLayer++ )
{
switch( oCATD.GetEntryType(iCATDLayer) )
{
case SLTPoint:
case SLTLine:
case SLTAttr:
case SLTPoly:
case SLTRaster:
panLayerCATDEntry[nLayers++] = iCATDLayer;
break;
default:
/* ignore */
break;
}
}
/* -------------------------------------------------------------------- */
/* Initialized the related indexed readers list. */
/* -------------------------------------------------------------------- */
papoLayerReader = (SDTSIndexedReader **)
CPLCalloc(sizeof(SDTSIndexedReader*),oCATD.GetEntryCount());
return TRUE;
}
/************************************************************************/
/* Close() */
/************************************************************************/
void SDTSTransfer::Close()
{
for( int i = 0; i < nLayers; i++ )
{
if( papoLayerReader[i] != NULL )
delete papoLayerReader[i];
}
CPLFree( papoLayerReader );
papoLayerReader = NULL;
CPLFree( panLayerCATDEntry );
panLayerCATDEntry = NULL;
nLayers = 0;
}
/************************************************************************/
/* GetLayerType() */
/************************************************************************/
/**
Fetch type of requested feature layer.
@param iEntry the index of the layer to fetch information on. A value
from zero to GetLayerCount()-1.
@return the layer type.
<ul>
<li> SLTPoint: A point layer. An SDTSPointReader is returned by
SDTSTransfer::GetLayerIndexedReader().
<li> SLTLine: A line layer. An SDTSLineReader is returned by
SDTSTransfer::GetLayerIndexedReader().
<li> SLTAttr: An attribute primary or secondary layer. An SDTSAttrReader
is returned by SDTSTransfer::GetLayerIndexedReader().
<li> SLTPoly: A polygon layer. An SDTSPolygonReader is returned by
SDTSTransfer::GetLayerIndexedReader().
<li> SLTRaster: A raster layer. SDTSTransfer::GetLayerIndexedReader()
is not implemented. Use SDTSTransfer::GetLayerRasterReader() instead.
</ul>
*/
SDTSLayerType SDTSTransfer::GetLayerType( int iEntry )
{
if( iEntry < 0 || iEntry >= nLayers )
return SLTUnknown;
return oCATD.GetEntryType( panLayerCATDEntry[iEntry] );
}
/************************************************************************/
/* GetLayerCATDEntry() */
/************************************************************************/
/**
Fetch the CATD module index for a layer. This can be used to fetch
details about the layer/module from the SDTS_CATD object, such as it's
filename, and description.
@param iEntry the layer index from 0 to GetLayerCount()-1.
@return the module index suitable for use with the various SDTS_CATD
methods.
*/
int SDTSTransfer::GetLayerCATDEntry( int iEntry )
{
if( iEntry < 0 || iEntry >= nLayers )
return -1;
return panLayerCATDEntry[iEntry];
}
/************************************************************************/
/* GetLayerLineReader() */
/************************************************************************/
SDTSLineReader *SDTSTransfer::GetLayerLineReader( int iEntry )
{
SDTSLineReader *poLineReader;
if( iEntry < 0
|| iEntry >= nLayers
|| oCATD.GetEntryType( panLayerCATDEntry[iEntry] ) != SLTLine )
{
return NULL;
}
poLineReader = new SDTSLineReader( &oIREF );
if( !poLineReader->Open(
oCATD.GetEntryFilePath( panLayerCATDEntry[iEntry] ) ) )
{
delete poLineReader;
return NULL;
}
else
{
return poLineReader;
}
}
/************************************************************************/
/* GetLayerPointReader() */
/************************************************************************/
SDTSPointReader *SDTSTransfer::GetLayerPointReader( int iEntry )
{
SDTSPointReader *poPointReader;
if( iEntry < 0
|| iEntry >= nLayers
|| oCATD.GetEntryType( panLayerCATDEntry[iEntry] ) != SLTPoint )
{
return NULL;
}
poPointReader = new SDTSPointReader( &oIREF );
if( !poPointReader->Open(
oCATD.GetEntryFilePath( panLayerCATDEntry[iEntry] ) ) )
{
delete poPointReader;
return NULL;
}
else
{
return poPointReader;
}
}
/************************************************************************/
/* GetLayerPolygonReader() */
/************************************************************************/
SDTSPolygonReader *SDTSTransfer::GetLayerPolygonReader( int iEntry )
{
SDTSPolygonReader *poPolyReader;
if( iEntry < 0
|| iEntry >= nLayers
|| oCATD.GetEntryType( panLayerCATDEntry[iEntry] ) != SLTPoly )
{
return NULL;
}
poPolyReader = new SDTSPolygonReader();
if( !poPolyReader->Open(
oCATD.GetEntryFilePath( panLayerCATDEntry[iEntry] ) ) )
{
delete poPolyReader;
return NULL;
}
else
{
return poPolyReader;
}
}
/************************************************************************/
/* GetLayerAttrReader() */
/************************************************************************/
SDTSAttrReader *SDTSTransfer::GetLayerAttrReader( int iEntry )
{
SDTSAttrReader *poAttrReader;
if( iEntry < 0
|| iEntry >= nLayers
|| oCATD.GetEntryType( panLayerCATDEntry[iEntry] ) != SLTAttr )
{
return NULL;
}
poAttrReader = new SDTSAttrReader( &oIREF );
if( !poAttrReader->Open(
oCATD.GetEntryFilePath( panLayerCATDEntry[iEntry] ) ) )
{
delete poAttrReader;
return NULL;
}
else
{
return poAttrReader;
}
}
/************************************************************************/
/* GetLayerRasterReader() */
/************************************************************************/
/**
Instantiate an SDTSRasterReader for the indicated layer.
@param iEntry the index of the layer to instantiate a reader for. A
value between 0 and GetLayerCount()-1.
@return a pointer to a new SDTSRasterReader object, or NULL if the method
fails.
NOTE: The reader returned from GetLayerRasterReader() becomes the
responsibility of the caller to delete, and isn't automatically deleted
when the SDTSTransfer is destroyed. This method is different from
the GetLayerIndexedReader() method in this regard.
*/
SDTSRasterReader *SDTSTransfer::GetLayerRasterReader( int iEntry )
{
SDTSRasterReader *poRasterReader;
if( iEntry < 0
|| iEntry >= nLayers
|| oCATD.GetEntryType( panLayerCATDEntry[iEntry] ) != SLTRaster )
{
return NULL;
}
poRasterReader = new SDTSRasterReader();
if( !poRasterReader->Open( &oCATD, &oIREF,
oCATD.GetEntryModule(panLayerCATDEntry[iEntry] ) ) )
{
delete poRasterReader;
return NULL;
}
else
{
return poRasterReader;
}
}
/************************************************************************/
/* GetLayerModuleReader() */
/************************************************************************/
DDFModule *SDTSTransfer::GetLayerModuleReader( int iEntry )
{
DDFModule *poModuleReader;
if( iEntry < 0 || iEntry >= nLayers )
{
return NULL;
}
poModuleReader = new DDFModule;
if( !poModuleReader->Open(
oCATD.GetEntryFilePath( panLayerCATDEntry[iEntry] ) ) )
{
delete poModuleReader;
return NULL;
}
else
{
return poModuleReader;
}
}
/************************************************************************/
/* GetLayerIndexedReader() */
/************************************************************************/
/**
Returns a pointer to a reader of the appropriate type to the requested
layer.
Notes:
<ul>
<li> The returned reader remains owned by the SDTSTransfer, and will be
destroyed when the SDTSTransfer is destroyed. It should not be
destroyed by the application.
<li> If an indexed reader was already created for this layer using
GetLayerIndexedReader(), it will be returned instead of creating a new
reader. Amoung other things this means that the returned reader may not
be positioned to read from the beginning of the module, and may already
have it's index filled.
<li> The returned reader will be of a type appropriate to the layer.
See SDTSTransfer::GetLayerType() to see what reader classes correspond
to what layer types, so it can be cast accordingly (if necessary).
</ul>
@param iEntry the index of the layer to instantiate a reader for. A
value between 0 and GetLayerCount()-1.
@return a pointer to an appropriate reader or NULL if the method fails.
*/
SDTSIndexedReader *SDTSTransfer::GetLayerIndexedReader( int iEntry )
{
if( papoLayerReader[iEntry] == NULL )
{
switch( oCATD.GetEntryType( panLayerCATDEntry[iEntry] ) )
{
case SLTAttr:
papoLayerReader[iEntry] = GetLayerAttrReader( iEntry );
break;
case SLTPoint:
papoLayerReader[iEntry] = GetLayerPointReader( iEntry );
break;
case SLTLine:
papoLayerReader[iEntry] = GetLayerLineReader( iEntry );
break;
case SLTPoly:
papoLayerReader[iEntry] = GetLayerPolygonReader( iEntry );
break;
default:
break;
}
}
return papoLayerReader[iEntry];
}
/************************************************************************/
/* FindLayer() */
/************************************************************************/
/**
Fetch the SDTSTransfer layer number corresponding to a module name.
@param pszModule the name of the module to search for, such as "PC01".
@return the layer number (between 0 and GetLayerCount()-1 corresponding to
the module, or -1 if it doesn't correspond to a layer.
*/
int SDTSTransfer::FindLayer( const char * pszModule )
{
int iLayer;
for( iLayer = 0; iLayer < nLayers; iLayer++ )
{
if( EQUAL(pszModule,
oCATD.GetEntryModule( panLayerCATDEntry[iLayer] ) ) )
{
return iLayer;
}
}
return -1;
}
/************************************************************************/
/* GetIndexedFeatureRef() */
/************************************************************************/
SDTSFeature *SDTSTransfer::GetIndexedFeatureRef( SDTSModId *poModId,
SDTSLayerType *peType )
{
/* -------------------------------------------------------------------- */
/* Find the desired layer ... this is likely a significant slow */
/* point in the whole process ... perhaps the last found could */
/* be cached or something. */
/* -------------------------------------------------------------------- */
int iLayer = FindLayer( poModId->szModule );
if( iLayer == -1 )
return NULL;
/* -------------------------------------------------------------------- */
/* Get the reader, and read a feature from it. */
/* -------------------------------------------------------------------- */
SDTSIndexedReader *poReader;
poReader = GetLayerIndexedReader( iLayer );
if( poReader == NULL )
return NULL;
/* -------------------------------------------------------------------- */
/* return type, if requested. */
/* -------------------------------------------------------------------- */
if( peType != NULL )
*peType = GetLayerType(iLayer);
return poReader->GetIndexedFeatureRef( poModId->nRecord );
}
/************************************************************************/
/* GetAttr() */
/* */
/* Fetch the attribute information corresponding to a given */
/* SDTSModId. */
/************************************************************************/
/**
Fetch the attribute fields given a particular module/record id.
@param poModId an attribute record identifer, normally taken from the
aoATID[] array of an SDTSIndexedFeature.
@return a pointer to the DDFField containing the user attribute values as
subfields.
*/
DDFField *SDTSTransfer::GetAttr( SDTSModId *poModId )
{
SDTSAttrRecord *poAttrRecord;
poAttrRecord = (SDTSAttrRecord *) GetIndexedFeatureRef( poModId );
if( poAttrRecord == NULL )
return NULL;
return poAttrRecord->poATTR;
}
/************************************************************************/
/* GetBounds() */
/************************************************************************/
/**
Fetch approximate bounds for a transfer by scanning all point layers
and raster layers.
For TVP datasets (where point layers are scanned) the results can, in
theory miss some lines that go outside the bounds of the point layers.
However, this isn't common since most TVP sets contain a bounding rectangle
whose corners will define the most extreme extents.
@param pdfMinX western edge of dataset
@param pdfMinY southern edge of dataset
@param pdfMaxX eastern edge of dataset
@param pdfMaxY northern edge of dataset
@return TRUE if success, or FALSE on a failure.
*/
int SDTSTransfer::GetBounds( double *pdfMinX, double *pdfMinY,
double *pdfMaxX, double *pdfMaxY )
{
int bFirst = TRUE;
for( int iLayer = 0; iLayer < GetLayerCount(); iLayer++ )
{
if( GetLayerType( iLayer ) == SLTPoint )
{
SDTSPointReader *poLayer;
SDTSRawPoint *poPoint;
poLayer = (SDTSPointReader *) GetLayerIndexedReader( iLayer );
if( poLayer == NULL )
continue;
poLayer->Rewind();
while( (poPoint = (SDTSRawPoint*) poLayer->GetNextFeature()) != NULL )
{
if( bFirst )
{
*pdfMinX = *pdfMaxX = poPoint->dfX;
*pdfMinY = *pdfMaxY = poPoint->dfY;
bFirst = FALSE;
}
else
{
*pdfMinX = MIN(*pdfMinX,poPoint->dfX);
*pdfMaxX = MAX(*pdfMaxX,poPoint->dfX);
*pdfMinY = MIN(*pdfMinY,poPoint->dfY);
*pdfMaxY = MAX(*pdfMaxY,poPoint->dfY);
}
if( !poLayer->IsIndexed() )
delete poPoint;
}
}
else if( GetLayerType( iLayer ) == SLTRaster )
{
SDTSRasterReader *poRL;
double adfGeoTransform[6];
double dfMinX, dfMaxX, dfMinY, dfMaxY;
poRL = GetLayerRasterReader( iLayer );
if( poRL == NULL )
continue;
poRL->GetTransform( adfGeoTransform );
dfMinX = adfGeoTransform[0];
dfMaxY = adfGeoTransform[3];
dfMaxX = adfGeoTransform[0] + poRL->GetXSize()*adfGeoTransform[1];
dfMinY = adfGeoTransform[3] + poRL->GetYSize()*adfGeoTransform[5];
if( bFirst )
{
*pdfMinX = dfMinX;
*pdfMaxX = dfMaxX;
*pdfMinY = dfMinY;
*pdfMaxY = dfMaxY;
bFirst = FALSE;
}
else
{
*pdfMinX = MIN(dfMinX,*pdfMinX);
*pdfMaxX = MAX(dfMaxX,*pdfMaxX);
*pdfMinY = MIN(dfMinY,*pdfMinY);
*pdfMaxY = MAX(dfMaxY,*pdfMaxY);
}
delete poRL;
}
}
return !bFirst;
}
|