Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
PG raster: fix crash on invalid extent
  • Loading branch information
github-actions[bot] authored and nyalldawson committed Nov 22, 2020
1 parent 84b9cbf commit 054f8da
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 10 deletions.
11 changes: 8 additions & 3 deletions src/core/qgsgdalutils.cpp
Expand Up @@ -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;
Expand All @@ -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 )
Expand Down
3 changes: 2 additions & 1 deletion src/core/qgsgdalutils.h
Expand Up @@ -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.
Expand Down
12 changes: 9 additions & 3 deletions src/providers/postgres/raster/qgspostgresrasterprovider.cpp
Expand Up @@ -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 )
Expand Down Expand Up @@ -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" )
Expand All @@ -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();
Expand Down Expand Up @@ -1368,6 +1373,7 @@ bool QgsPostgresRasterProvider::init()
QStringLiteral( "PostGIS" ), Qgis::Critical );
return false;
}

return initFieldsAndTemporal( );
}

Expand Down
3 changes: 0 additions & 3 deletions src/providers/postgres/raster/qgspostgresrasterprovider.h
Expand Up @@ -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
Expand Down Expand Up @@ -234,7 +232,6 @@ class QgsPostgresRasterProvider : public QgsRasterDataProvider

QStringList parseUriKey( const QString &key );

public:
};


Expand Down

0 comments on commit 054f8da

Please sign in to comment.