Skip to content

Commit 054f8da

Browse files
github-actions[bot]nyalldawson
authored andcommittedNov 22, 2020
PG raster: fix crash on invalid extent
1 parent 84b9cbf commit 054f8da

File tree

4 files changed

+19
-10
lines changed

4 files changed

+19
-10
lines changed
 

‎src/core/qgsgdalutils.cpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ gdal::dataset_unique_ptr QgsGdalUtils::imageToMemoryDataset( const QImage &image
145145
return hSrcDS;
146146
}
147147

148-
void QgsGdalUtils::resampleSingleBandRaster( GDALDatasetH hSrcDS, GDALDatasetH hDstDS, GDALResampleAlg resampleAlg )
148+
bool QgsGdalUtils::resampleSingleBandRaster( GDALDatasetH hSrcDS, GDALDatasetH hDstDS, GDALResampleAlg resampleAlg )
149149
{
150150
gdal::warp_options_unique_ptr psWarpOptions( GDALCreateWarpOptions() );
151151
psWarpOptions->hSrcDS = hSrcDS;
@@ -164,15 +164,20 @@ void QgsGdalUtils::resampleSingleBandRaster( GDALDatasetH hSrcDS, GDALDatasetH h
164164
GDALCreateGenImgProjTransformer( hSrcDS, GDALGetProjectionRef( hSrcDS ),
165165
hDstDS, GDALGetProjectionRef( hDstDS ),
166166
FALSE, 0.0, 1 );
167+
if ( ! psWarpOptions->pTransformerArg )
168+
{
169+
return false;
170+
}
171+
167172
psWarpOptions->pfnTransformer = GDALGenImgProjTransform;
168173

169174
// Initialize and execute the warp operation.
170175
GDALWarpOperation oOperation;
171176
oOperation.Initialize( psWarpOptions.get() );
172177

173-
oOperation.ChunkAndWarpImage( 0, 0, GDALGetRasterXSize( hDstDS ), GDALGetRasterYSize( hDstDS ) );
174-
178+
const bool retVal { oOperation.ChunkAndWarpImage( 0, 0, GDALGetRasterXSize( hDstDS ), GDALGetRasterYSize( hDstDS ) ) != CE_None };
175179
GDALDestroyGenImgProjTransformer( psWarpOptions->pTransformerArg );
180+
return retVal;
176181
}
177182

178183
QImage QgsGdalUtils::resampleImage( const QImage &image, QSize outputSize, GDALRIOResampleAlg resampleAlg )

‎src/core/qgsgdalutils.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,10 @@ class CORE_EXPORT QgsGdalUtils
6363
/**
6464
* Resamples a single band raster to the destination dataset with different resolution (and possibly with different CRS).
6565
* Ideally the source dataset should cover the whole area or the destination dataset.
66+
* \returns TRUE on success
6667
* \since QGIS 3.8
6768
*/
68-
static void resampleSingleBandRaster( GDALDatasetH hSrcDS, GDALDatasetH hDstDS, GDALResampleAlg resampleAlg );
69+
static bool resampleSingleBandRaster( GDALDatasetH hSrcDS, GDALDatasetH hDstDS, GDALResampleAlg resampleAlg );
6970

7071
/**
7172
* Resamples a QImage \a image using GDAL resampler.

‎src/providers/postgres/raster/qgspostgresrasterprovider.cpp

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,6 @@ QgsPostgresRasterProvider::QgsPostgresRasterProvider( const QgsPostgresRasterPro
131131
, mRasterColumn( other.mRasterColumn )
132132
, mSchemaName( other.mSchemaName )
133133
, mSqlWhereClause( other.mSqlWhereClause )
134-
, mExtent( other.mExtent )
135134
, mUseEstimatedMetadata( other.mUseEstimatedMetadata )
136135
, mDataTypes( other.mDataTypes )
137136
, mDataSizes( other.mDataSizes )
@@ -484,7 +483,7 @@ bool QgsPostgresRasterProvider::readBlock( int bandNo, const QgsRectangle &viewE
484483
// Write data to the output block
485484
gdal::dataset_unique_ptr dstDS { QgsGdalUtils::createSingleBandMemoryDataset(
486485
gdalDataType, viewExtent, width, height, mCrs ) };
487-
if ( ! dstDS )
486+
if ( ! dstDS )
488487
{
489488
const QString lastError = QString::fromUtf8( CPLGetLastErrorMsg() ) ;
490489
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
493492
}
494493

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

498503
// Copy to result buffer
499504
CPLErrorReset();
@@ -1368,6 +1373,7 @@ bool QgsPostgresRasterProvider::init()
13681373
QStringLiteral( "PostGIS" ), Qgis::Critical );
13691374
return false;
13701375
}
1376+
13711377
return initFieldsAndTemporal( );
13721378
}
13731379

‎src/providers/postgres/raster/qgspostgresrasterprovider.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,6 @@ class QgsPostgresRasterProvider : public QgsRasterDataProvider
9393
QString mSchemaName;
9494
//! SQL statement used to limit the features retrieved (subset string)
9595
QString mSqlWhereClause;
96-
//! Rectangle that contains the extent (bounding box) of the layer
97-
mutable QgsRectangle mExtent;
9896
//! Use estimated metadata. Uses fast table counts, geometry type and extent determination
9997
bool mUseEstimatedMetadata = true;
10098
//! Error information
@@ -234,7 +232,6 @@ class QgsPostgresRasterProvider : public QgsRasterDataProvider
234232

235233
QStringList parseUriKey( const QString &key );
236234

237-
public:
238235
};
239236

240237

0 commit comments

Comments
 (0)
Please sign in to comment.