Skip to content
Permalink
Browse files

Merge pull request #39784 from elpaso/bugfix-gh39779-pgraster-crash-o…

…n-invalid-extent

PG raster: fix crash on invalid extent
  • Loading branch information
elpaso committed Nov 20, 2020
2 parents 0f97ac7 + 7da48ab commit 311a092164585a49e964469675ae0d0e62530e7c
@@ -145,7 +145,7 @@ gdal::dataset_unique_ptr QgsGdalUtils::imageToMemoryDataset( const QImage &image
return hSrcDS;
}

void QgsGdalUtils::resampleSingleBandRaster( GDALDatasetH hSrcDS, GDALDatasetH hDstDS, GDALResampleAlg resampleAlg )
bool QgsGdalUtils::resampleSingleBandRaster( GDALDatasetH hSrcDS, GDALDatasetH hDstDS, GDALResampleAlg resampleAlg )
{
gdal::warp_options_unique_ptr psWarpOptions( GDALCreateWarpOptions() );
psWarpOptions->hSrcDS = hSrcDS;
@@ -164,15 +164,20 @@ void QgsGdalUtils::resampleSingleBandRaster( GDALDatasetH hSrcDS, GDALDatasetH h
GDALCreateGenImgProjTransformer( hSrcDS, GDALGetProjectionRef( hSrcDS ),
hDstDS, GDALGetProjectionRef( hDstDS ),
FALSE, 0.0, 1 );
if ( ! psWarpOptions->pTransformerArg )
{
return false;
}

psWarpOptions->pfnTransformer = GDALGenImgProjTransform;

// Initialize and execute the warp operation.
GDALWarpOperation oOperation;
oOperation.Initialize( psWarpOptions.get() );

oOperation.ChunkAndWarpImage( 0, 0, GDALGetRasterXSize( hDstDS ), GDALGetRasterYSize( hDstDS ) );

const bool retVal { oOperation.ChunkAndWarpImage( 0, 0, GDALGetRasterXSize( hDstDS ), GDALGetRasterYSize( hDstDS ) ) != CE_None };
GDALDestroyGenImgProjTransformer( psWarpOptions->pTransformerArg );
return retVal;
}

QImage QgsGdalUtils::resampleImage( const QImage &image, QSize outputSize, GDALRIOResampleAlg resampleAlg )
@@ -63,9 +63,10 @@ class CORE_EXPORT QgsGdalUtils
/**
* Resamples a single band raster to the destination dataset with different resolution (and possibly with different CRS).
* Ideally the source dataset should cover the whole area or the destination dataset.
* \returns TRUE on success
* \since QGIS 3.8
*/
static void resampleSingleBandRaster( GDALDatasetH hSrcDS, GDALDatasetH hDstDS, GDALResampleAlg resampleAlg );
static bool resampleSingleBandRaster( GDALDatasetH hSrcDS, GDALDatasetH hDstDS, GDALResampleAlg resampleAlg );

/**
* Resamples a QImage \a image using GDAL resampler.
@@ -131,7 +131,6 @@ QgsPostgresRasterProvider::QgsPostgresRasterProvider( const QgsPostgresRasterPro
, mRasterColumn( other.mRasterColumn )
, mSchemaName( other.mSchemaName )
, mSqlWhereClause( other.mSqlWhereClause )
, mExtent( other.mExtent )
, mUseEstimatedMetadata( other.mUseEstimatedMetadata )
, mDataTypes( other.mDataTypes )
, mDataSizes( other.mDataSizes )
@@ -484,7 +483,7 @@ bool QgsPostgresRasterProvider::readBlock( int bandNo, const QgsRectangle &viewE
// Write data to the output block
gdal::dataset_unique_ptr dstDS { QgsGdalUtils::createSingleBandMemoryDataset(
gdalDataType, viewExtent, width, height, mCrs ) };
if ( ! dstDS )
if ( ! dstDS )
{
const QString lastError = QString::fromUtf8( CPLGetLastErrorMsg() ) ;
QgsMessageLog::logMessage( tr( "Unable to create destination raster for tiles from %1: %2" )
@@ -493,7 +492,13 @@ bool QgsPostgresRasterProvider::readBlock( int bandNo, const QgsRectangle &viewE
}

// Resample the raster to the final bounds and resolution
QgsGdalUtils::resampleSingleBandRaster( tmpDS.get(), dstDS.get(), GDALResampleAlg::GRA_NearestNeighbour );
if ( ! QgsGdalUtils::resampleSingleBandRaster( tmpDS.get(), dstDS.get(), GDALResampleAlg::GRA_NearestNeighbour ) )
{
const QString lastError = QString::fromUtf8( CPLGetLastErrorMsg() ) ;
QgsMessageLog::logMessage( tr( "Unable to resample and transform destination raster for tiles from %1: %2" )
.arg( tableToQuery, lastError ), tr( "PostGIS" ), Qgis::Critical );
return false;
}

// Copy to result buffer
CPLErrorReset();
@@ -1368,6 +1373,7 @@ bool QgsPostgresRasterProvider::init()
QStringLiteral( "PostGIS" ), Qgis::Critical );
return false;
}

return initFieldsAndTemporal( );
}

@@ -93,8 +93,6 @@ class QgsPostgresRasterProvider : public QgsRasterDataProvider
QString mSchemaName;
//! SQL statement used to limit the features retrieved (subset string)
QString mSqlWhereClause;
//! Rectangle that contains the extent (bounding box) of the layer
mutable QgsRectangle mExtent;
//! Use estimated metadata. Uses fast table counts, geometry type and extent determination
bool mUseEstimatedMetadata = true;
//! Error information
@@ -234,7 +232,6 @@ class QgsPostgresRasterProvider : public QgsRasterDataProvider

QStringList parseUriKey( const QString &key );

public:
};


0 comments on commit 311a092

Please sign in to comment.
You can’t perform that action at this time.