Skip to content

Commit

Permalink
Remove bounds check and don't crash either
Browse files Browse the repository at this point in the history
  • Loading branch information
elpaso committed Nov 13, 2020
1 parent 1c22933 commit 787181a
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 35 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
35 changes: 7 additions & 28 deletions src/providers/postgres/raster/qgspostgresrasterprovider.cpp
Expand Up @@ -492,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 @@ -1146,11 +1152,6 @@ bool QgsPostgresRasterProvider::init()
// is tiled?
mIsTiled = ( mWidth != mTileWidth ) || ( mHeight != mTileHeight );

if ( ! checkExtent() )
{
return false;
}

// Detect overviews
findOverviews();
return initFieldsAndTemporal( );
Expand Down Expand Up @@ -1373,11 +1374,6 @@ bool QgsPostgresRasterProvider::init()
return false;
}

if ( ! checkExtent() )
{
return false;
}

return initFieldsAndTemporal( );
}

Expand Down Expand Up @@ -1919,23 +1915,6 @@ QStringList QgsPostgresRasterProvider::parseUriKey( const QString &key )
return cols;
}

bool QgsPostgresRasterProvider::checkExtent()
{
// Last check for extent and crs (see GH #39779 crash)
if ( ! mCrs.bounds().isEmpty() )
{
const QgsCoordinateTransform transformer { mCrs, QgsCoordinateReferenceSystem( 4326 ), transformContext() };
const QgsRectangle transformedExtent { transformer.transform( mExtent ) };
if ( ! mCrs.bounds().contains( transformedExtent ) )
{
QgsMessageLog::logMessage( tr( "The extent is outside the CRS bounds" ),
QStringLiteral( "PostGIS" ), Qgis::Critical );
return false;
}
}
return true;
}

QgsPostgresProvider::Relkind QgsPostgresRasterProvider::relkind() const
{
if ( mIsQuery || !connectionRO() )
Expand Down
3 changes: 0 additions & 3 deletions src/providers/postgres/raster/qgspostgresrasterprovider.h
Expand Up @@ -232,9 +232,6 @@ class QgsPostgresRasterProvider : public QgsRasterDataProvider

QStringList parseUriKey( const QString &key );

bool checkExtent();

public:
};


Expand Down

0 comments on commit 787181a

Please sign in to comment.