Skip to content
Permalink
Browse files

Remove bounds check and don't crash either

  • Loading branch information
elpaso committed Nov 13, 2020
1 parent 1c22933 commit 787181ad1132e16687f5c008ab853d717ad23664
@@ -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.
@@ -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();
@@ -1146,11 +1152,6 @@ bool QgsPostgresRasterProvider::init()
// is tiled?
mIsTiled = ( mWidth != mTileWidth ) || ( mHeight != mTileHeight );

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

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

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

return initFieldsAndTemporal( );
}

@@ -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() )
@@ -232,9 +232,6 @@ class QgsPostgresRasterProvider : public QgsRasterDataProvider

QStringList parseUriKey( const QString &key );

bool checkExtent();

public:
};


0 comments on commit 787181a

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