|
18 | 18 |
|
19 | 19 | #define CPL_SUPRESS_CPLUSPLUS //#spellok
|
20 | 20 | #include "gdal.h"
|
| 21 | +#include "gdalwarper.h" |
21 | 22 | #include "cpl_string.h"
|
22 | 23 |
|
23 | 24 | #include <QString>
|
@@ -277,3 +278,211 @@ QString QgsGdalUtils::validateCreationOptionsFormat( const QStringList &createOp
|
277 | 278 | return QStringLiteral( "Failed GDALValidateCreationOptions() test" );
|
278 | 279 | return QString();
|
279 | 280 | }
|
| 281 | + |
| 282 | +#if GDAL_VERSION_NUM < GDAL_COMPUTE_VERSION(2,3,0) |
| 283 | +// GDAL < 2.3 does not come with GDALWarpInitDefaultBandMapping and GDALWarpInitNoDataReal |
| 284 | +// in the public API so we define them here for rpcAwareAutoCreateWarpedVrt() |
| 285 | + |
| 286 | +static void GDALWarpInitDefaultBandMapping( GDALWarpOptions *psOptionsIn, int nBandCount ) |
| 287 | +{ |
| 288 | + if ( psOptionsIn->nBandCount != 0 ) { return; } |
| 289 | + |
| 290 | + psOptionsIn->nBandCount = nBandCount; |
| 291 | + |
| 292 | + psOptionsIn->panSrcBands = static_cast<int *>( |
| 293 | + CPLMalloc( sizeof( int ) * psOptionsIn->nBandCount ) ); |
| 294 | + psOptionsIn->panDstBands = static_cast<int *>( |
| 295 | + CPLMalloc( sizeof( int ) * psOptionsIn->nBandCount ) ); |
| 296 | + |
| 297 | + for ( int i = 0; i < psOptionsIn->nBandCount; i++ ) |
| 298 | + { |
| 299 | + psOptionsIn->panSrcBands[i] = i + 1; |
| 300 | + psOptionsIn->panDstBands[i] = i + 1; |
| 301 | + } |
| 302 | +} |
| 303 | + |
| 304 | +static void InitNoData( int nBandCount, double **ppdNoDataReal, double dDataReal ) |
| 305 | +{ |
| 306 | + if ( nBandCount <= 0 ) { return; } |
| 307 | + if ( *ppdNoDataReal != nullptr ) { return; } |
| 308 | + |
| 309 | + *ppdNoDataReal = static_cast<double *>( |
| 310 | + CPLMalloc( sizeof( double ) * nBandCount ) ); |
| 311 | + |
| 312 | + for ( int i = 0; i < nBandCount; ++i ) |
| 313 | + { |
| 314 | + ( *ppdNoDataReal )[i] = dDataReal; |
| 315 | + } |
| 316 | +} |
| 317 | + |
| 318 | +static void GDALWarpInitNoDataReal( GDALWarpOptions *psOptionsIn, double dNoDataReal ) |
| 319 | +{ |
| 320 | + InitNoData( psOptionsIn->nBandCount, &psOptionsIn->padfDstNoDataReal, dNoDataReal ); |
| 321 | + InitNoData( psOptionsIn->nBandCount, &psOptionsIn->padfSrcNoDataReal, dNoDataReal ); |
| 322 | + // older GDAL also requires imaginary values to be set |
| 323 | + InitNoData( psOptionsIn->nBandCount, &psOptionsIn->padfDstNoDataImag, 0 ); |
| 324 | + InitNoData( psOptionsIn->nBandCount, &psOptionsIn->padfSrcNoDataImag, 0 ); |
| 325 | +} |
| 326 | +#endif |
| 327 | + |
| 328 | +#if GDAL_VERSION_NUM < GDAL_COMPUTE_VERSION(3,2,0) |
| 329 | + |
| 330 | +GDALDatasetH GDALAutoCreateWarpedVRTEx( GDALDatasetH hSrcDS, const char *pszSrcWKT, const char *pszDstWKT, GDALResampleAlg eResampleAlg, |
| 331 | + double dfMaxError, const GDALWarpOptions *psOptionsIn, char **papszTransformerOptions ) |
| 332 | +{ |
| 333 | + VALIDATE_POINTER1( hSrcDS, "GDALAutoCreateWarpedVRT", nullptr ); |
| 334 | + |
| 335 | + /* -------------------------------------------------------------------- */ |
| 336 | + /* Populate the warp options. */ |
| 337 | + /* -------------------------------------------------------------------- */ |
| 338 | + GDALWarpOptions *psWO = nullptr; |
| 339 | + if ( psOptionsIn != nullptr ) |
| 340 | + psWO = GDALCloneWarpOptions( psOptionsIn ); |
| 341 | + else |
| 342 | + psWO = GDALCreateWarpOptions(); |
| 343 | + |
| 344 | + psWO->eResampleAlg = eResampleAlg; |
| 345 | + |
| 346 | + psWO->hSrcDS = hSrcDS; |
| 347 | + |
| 348 | + GDALWarpInitDefaultBandMapping( psWO, GDALGetRasterCount( hSrcDS ) ); |
| 349 | + |
| 350 | + /* -------------------------------------------------------------------- */ |
| 351 | + /* Setup no data values */ |
| 352 | + /* -------------------------------------------------------------------- */ |
| 353 | + for ( int i = 0; i < psWO->nBandCount; i++ ) |
| 354 | + { |
| 355 | + GDALRasterBandH rasterBand = GDALGetRasterBand( psWO->hSrcDS, psWO->panSrcBands[i] ); |
| 356 | + |
| 357 | + int hasNoDataValue; |
| 358 | + double noDataValue = GDALGetRasterNoDataValue( rasterBand, &hasNoDataValue ); |
| 359 | + |
| 360 | + if ( hasNoDataValue ) |
| 361 | + { |
| 362 | + // Check if the nodata value is out of range |
| 363 | + int bClamped = FALSE; |
| 364 | + int bRounded = FALSE; |
| 365 | + CPL_IGNORE_RET_VAL( |
| 366 | + GDALAdjustValueToDataType( GDALGetRasterDataType( rasterBand ), |
| 367 | + noDataValue, &bClamped, &bRounded ) ); |
| 368 | + if ( !bClamped ) |
| 369 | + { |
| 370 | + GDALWarpInitNoDataReal( psWO, -1e10 ); |
| 371 | + |
| 372 | + psWO->padfSrcNoDataReal[i] = noDataValue; |
| 373 | + psWO->padfDstNoDataReal[i] = noDataValue; |
| 374 | + } |
| 375 | + } |
| 376 | + } |
| 377 | + |
| 378 | + if ( psWO->padfDstNoDataReal != nullptr ) |
| 379 | + { |
| 380 | + if ( CSLFetchNameValue( psWO->papszWarpOptions, "INIT_DEST" ) == nullptr ) |
| 381 | + { |
| 382 | + psWO->papszWarpOptions = |
| 383 | + CSLSetNameValue( psWO->papszWarpOptions, "INIT_DEST", "NO_DATA" ); |
| 384 | + } |
| 385 | + } |
| 386 | + |
| 387 | + /* -------------------------------------------------------------------- */ |
| 388 | + /* Create the transformer. */ |
| 389 | + /* -------------------------------------------------------------------- */ |
| 390 | + psWO->pfnTransformer = GDALGenImgProjTransform; |
| 391 | + |
| 392 | + char **papszOptions = nullptr; |
| 393 | + if ( pszSrcWKT != nullptr ) |
| 394 | + papszOptions = CSLSetNameValue( papszOptions, "SRC_SRS", pszSrcWKT ); |
| 395 | + if ( pszDstWKT != nullptr ) |
| 396 | + papszOptions = CSLSetNameValue( papszOptions, "DST_SRS", pszDstWKT ); |
| 397 | + papszOptions = CSLMerge( papszOptions, papszTransformerOptions ); |
| 398 | + psWO->pTransformerArg = |
| 399 | + GDALCreateGenImgProjTransformer2( psWO->hSrcDS, nullptr, |
| 400 | + papszOptions ); |
| 401 | + CSLDestroy( papszOptions ); |
| 402 | + |
| 403 | + if ( psWO->pTransformerArg == nullptr ) |
| 404 | + { |
| 405 | + GDALDestroyWarpOptions( psWO ); |
| 406 | + return nullptr; |
| 407 | + } |
| 408 | + |
| 409 | + /* -------------------------------------------------------------------- */ |
| 410 | + /* Figure out the desired output bounds and resolution. */ |
| 411 | + /* -------------------------------------------------------------------- */ |
| 412 | + double adfDstGeoTransform[6] = { 0.0 }; |
| 413 | + int nDstPixels = 0; |
| 414 | + int nDstLines = 0; |
| 415 | + CPLErr eErr = |
| 416 | + GDALSuggestedWarpOutput( hSrcDS, psWO->pfnTransformer, |
| 417 | + psWO->pTransformerArg, |
| 418 | + adfDstGeoTransform, &nDstPixels, &nDstLines ); |
| 419 | + if ( eErr != CE_None ) |
| 420 | + { |
| 421 | + GDALDestroyTransformer( psWO->pTransformerArg ); |
| 422 | + GDALDestroyWarpOptions( psWO ); |
| 423 | + return nullptr; |
| 424 | + } |
| 425 | + |
| 426 | + /* -------------------------------------------------------------------- */ |
| 427 | + /* Update the transformer to include an output geotransform */ |
| 428 | + /* back to pixel/line coordinates. */ |
| 429 | + /* */ |
| 430 | + /* -------------------------------------------------------------------- */ |
| 431 | + GDALSetGenImgProjTransformerDstGeoTransform( |
| 432 | + psWO->pTransformerArg, adfDstGeoTransform ); |
| 433 | + |
| 434 | + /* -------------------------------------------------------------------- */ |
| 435 | + /* Do we want to apply an approximating transformation? */ |
| 436 | + /* -------------------------------------------------------------------- */ |
| 437 | + if ( dfMaxError > 0.0 ) |
| 438 | + { |
| 439 | + psWO->pTransformerArg = |
| 440 | + GDALCreateApproxTransformer( psWO->pfnTransformer, |
| 441 | + psWO->pTransformerArg, |
| 442 | + dfMaxError ); |
| 443 | + psWO->pfnTransformer = GDALApproxTransform; |
| 444 | + GDALApproxTransformerOwnsSubtransformer( psWO->pTransformerArg, TRUE ); |
| 445 | + } |
| 446 | + |
| 447 | + /* -------------------------------------------------------------------- */ |
| 448 | + /* Create the VRT file. */ |
| 449 | + /* -------------------------------------------------------------------- */ |
| 450 | + GDALDatasetH hDstDS |
| 451 | + = GDALCreateWarpedVRT( hSrcDS, nDstPixels, nDstLines, |
| 452 | + adfDstGeoTransform, psWO ); |
| 453 | + |
| 454 | + GDALDestroyWarpOptions( psWO ); |
| 455 | + |
| 456 | + if ( pszDstWKT != nullptr ) |
| 457 | + GDALSetProjection( hDstDS, pszDstWKT ); |
| 458 | + else if ( pszSrcWKT != nullptr ) |
| 459 | + GDALSetProjection( hDstDS, pszSrcWKT ); |
| 460 | + else if ( GDALGetGCPCount( hSrcDS ) > 0 ) |
| 461 | + GDALSetProjection( hDstDS, GDALGetGCPProjection( hSrcDS ) ); |
| 462 | + else |
| 463 | + GDALSetProjection( hDstDS, GDALGetProjectionRef( hSrcDS ) ); |
| 464 | + |
| 465 | + return hDstDS; |
| 466 | +} |
| 467 | +#endif |
| 468 | + |
| 469 | + |
| 470 | +GDALDatasetH QgsGdalUtils::rpcAwareAutoCreateWarpedVrt( |
| 471 | + GDALDatasetH hSrcDS, |
| 472 | + const char *pszSrcWKT, |
| 473 | + const char *pszDstWKT, |
| 474 | + GDALResampleAlg eResampleAlg, |
| 475 | + double dfMaxError, |
| 476 | + const GDALWarpOptions *psOptionsIn ) |
| 477 | +{ |
| 478 | + char **opts = nullptr; |
| 479 | + if ( GDALGetMetadata( hSrcDS, "RPC" ) ) |
| 480 | + { |
| 481 | + // well-behaved RPC should have height offset a good value for RPC_HEIGHT |
| 482 | + const char *heightOffStr = GDALGetMetadataItem( hSrcDS, "HEIGHT_OFF", "RPC" ); |
| 483 | + if ( heightOffStr ) |
| 484 | + opts = CSLAddNameValue( opts, "RPC_HEIGHT", heightOffStr ); |
| 485 | + } |
| 486 | + |
| 487 | + return GDALAutoCreateWarpedVRTEx( hSrcDS, pszSrcWKT, pszDstWKT, eResampleAlg, dfMaxError, psOptionsIn, opts ); |
| 488 | +} |
0 commit comments