int QgsGrassGisLib::G_get_cellhd( const char *name, const char *mapset, struct Cell_head *cellhd ) { Q_UNUSED( mapset ); initCellHead( cellhd ); Raster rast = raster( name ); QgsRasterDataProvider *provider = rast.provider; cellhd->rows = provider->ySize(); cellhd->cols = provider->xSize(); cellhd->ew_res = provider->extent().width() / provider->xSize(); cellhd->ns_res = provider->extent().height() / provider->ySize(); cellhd->north = provider->extent().yMaximum(); cellhd->south = provider->extent().yMinimum(); cellhd->east = provider->extent().yMaximum(); cellhd->west = provider->extent().xMinimum(); return 0; }
bool QgsRasterChecker::runTest( QString theVerifiedKey, QString theVerifiedUri, QString theExpectedKey, QString theExpectedUri ) { bool ok = true; mReport += "\n\n"; //QgsRasterDataProvider* verifiedProvider = QgsRasterLayer::loadProvider( theVerifiedKey, theVerifiedUri ); QgsRasterDataProvider* verifiedProvider = ( QgsRasterDataProvider* ) QgsProviderRegistry::instance()->provider( theVerifiedKey, theVerifiedUri ); if ( !verifiedProvider || !verifiedProvider->isValid() ) { error( QString( "Cannot load provider %1 with URI: %2" ).arg( theVerifiedKey ).arg( theVerifiedUri ), mReport ); ok = false; } //QgsRasterDataProvider* expectedProvider = QgsRasterLayer::loadProvider( theExpectedKey, theExpectedUri ); QgsRasterDataProvider* expectedProvider = ( QgsRasterDataProvider* ) QgsProviderRegistry::instance()->provider( theExpectedKey, theExpectedUri ); if ( !expectedProvider || !expectedProvider->isValid() ) { error( QString( "Cannot load provider %1 with URI: %2" ).arg( theExpectedKey ).arg( theExpectedUri ), mReport ); ok = false; } if ( !ok ) return false; mReport += QString( "Verified URI: %1<br>" ).arg( theVerifiedUri.replace( "&", "&" ) ); mReport += QString( "Expected URI: %1<br>" ).arg( theExpectedUri.replace( "&", "&" ) ); mReport += "<br>"; mReport += QString( "<table style='%1'>\n" ).arg( mTabStyle ); mReport += compareHead(); compare( "Band count", verifiedProvider->bandCount(), expectedProvider->bandCount(), mReport, ok ); compare( "Width", verifiedProvider->xSize(), expectedProvider->xSize(), mReport, ok ); compare( "Height", verifiedProvider->ySize(), expectedProvider->ySize(), mReport, ok ); compareRow( "Extent", verifiedProvider->extent().toString(), expectedProvider->extent().toString(), mReport, verifiedProvider->extent() == expectedProvider->extent() ); if ( verifiedProvider->extent() != expectedProvider->extent() ) ok = false; mReport += "</table>\n"; if ( !ok ) return false; bool allOk = true; for ( int band = 1; band <= expectedProvider->bandCount(); band++ ) { bool bandOk = true; mReport += QString( "<h3>Band %1</h3>\n" ).arg( band ); mReport += QString( "<table style='%1'>\n" ).arg( mTabStyle ); mReport += compareHead(); // Data types may differ (?) bool typesOk = true; compare( "Source data type", verifiedProvider->srcDataType( band ), expectedProvider->srcDataType( band ), mReport, typesOk ); compare( "Data type", verifiedProvider->dataType( band ), expectedProvider->dataType( band ), mReport, typesOk ) ; // TODO: not yet sure if noDataValue() should exist at all //compare( "No data (NULL) value", verifiedProvider->noDataValue( band ), expectedProvider->noDataValue( band ), mReport, typesOk ); bool statsOk = true; QgsRasterBandStats verifiedStats = verifiedProvider->bandStatistics( band ); QgsRasterBandStats expectedStats = expectedProvider->bandStatistics( band ); // Min/max may 'slightly' differ, for big numbers however, the difference may // be quite big, for example for Float32 with max -3.332e+38, the difference is 1.47338e+24 double tol = tolerance( expectedStats.minimumValue ); compare( "Minimum value", verifiedStats.minimumValue, expectedStats.minimumValue, mReport, statsOk, tol ); tol = tolerance( expectedStats.maximumValue ); compare( "Maximum value", verifiedStats.maximumValue, expectedStats.maximumValue, mReport, statsOk, tol ); // TODO: enable once fixed (WCS excludes nulls but GDAL does not) //compare( "Cells count", verifiedStats.elementCount, expectedStats.elementCount, mReport, statsOk ); tol = tolerance( expectedStats.mean ); compare( "Mean", verifiedStats.mean, expectedStats.mean, mReport, statsOk, tol ); // stdDev usually differ significantly tol = tolerance( expectedStats.stdDev, 1 ); compare( "Standard deviation", verifiedStats.stdDev, expectedStats.stdDev, mReport, statsOk, tol ); mReport += "</table>"; mReport += "<br>"; if ( !bandOk ) { allOk = false; continue; } if ( !statsOk || !typesOk ) { allOk = false; // create values table anyway so that values are available } mReport += "<table><tr>"; mReport += "<td>Data comparison</td>"; mReport += QString( "<td style='%1 %2 border: 1px solid'>correct value</td>" ).arg( mCellStyle ).arg( mOkStyle ); mReport += "<td></td>"; mReport += QString( "<td style='%1 %2 border: 1px solid'>wrong value<br>expected value</td></tr>" ).arg( mCellStyle ).arg( mErrStyle ); mReport += "</tr></table>"; mReport += "<br>"; int width = expectedProvider->xSize(); int height = expectedProvider->ySize(); QgsRasterBlock *expectedBlock = expectedProvider->block( band, expectedProvider->extent(), width, height ); QgsRasterBlock *verifiedBlock = verifiedProvider->block( band, expectedProvider->extent(), width, height ); if ( !expectedBlock || !expectedBlock->isValid() || !verifiedBlock || !verifiedBlock->isValid() ) { allOk = false; mReport += "cannot read raster block"; continue; } // compare data values QString htmlTable = QString( "<table style='%1'>" ).arg( mTabStyle ); for ( int row = 0; row < height; row ++ ) { htmlTable += "<tr>"; for ( int col = 0; col < width; col ++ ) { bool cellOk = true; double verifiedVal = verifiedBlock->value( row, col ); double expectedVal = expectedBlock->value( row, col ); QString valStr; if ( compare( verifiedVal, expectedVal, 0 ) ) { valStr = QString( "%1" ).arg( verifiedVal ); } else { cellOk = false; allOk = false; valStr = QString( "%1<br>%2" ).arg( verifiedVal ).arg( expectedVal ); } htmlTable += QString( "<td style='%1 %2'>%3</td>" ).arg( mCellStyle ).arg( cellOk ? mOkStyle : mErrStyle ).arg( valStr ); } htmlTable += "</tr>"; } htmlTable += "</table>"; mReport += htmlTable; delete expectedBlock; delete verifiedBlock; } delete verifiedProvider; delete expectedProvider; return allOk; }
ProjectorData::ProjectorData( const QgsRectangle &extent, int width, int height, QgsRasterInterface *input, const QgsCoordinateTransform &inverseCt, QgsRasterProjector::Precision precision ) : mApproximate( false ) , mInverseCt( inverseCt ) , mDestExtent( extent ) , mDestRows( height ) , mDestCols( width ) , mDestXRes( 0.0 ) , mDestYRes( 0.0 ) , mSrcRows( 0 ) , mSrcCols( 0 ) , mSrcXRes( 0.0 ) , mSrcYRes( 0.0 ) , mDestRowsPerMatrixRow( 0.0 ) , mDestColsPerMatrixCol( 0.0 ) , mHelperTopRow( 0 ) , mCPCols( 0 ) , mCPRows( 0 ) , mSqrTolerance( 0.0 ) , mMaxSrcXRes( 0 ) , mMaxSrcYRes( 0 ) { QgsDebugMsgLevel( QStringLiteral( "Entered" ), 4 ); // Get max source resolution and extent if possible if ( input ) { QgsRasterDataProvider *provider = dynamic_cast<QgsRasterDataProvider *>( input->sourceInput() ); if ( provider ) { if ( provider->capabilities() & QgsRasterDataProvider::Size ) { mMaxSrcXRes = provider->extent().width() / provider->xSize(); mMaxSrcYRes = provider->extent().height() / provider->ySize(); } // Get source extent if ( mExtent.isEmpty() ) { mExtent = provider->extent(); } } } mDestXRes = mDestExtent.width() / ( mDestCols ); mDestYRes = mDestExtent.height() / ( mDestRows ); // Calculate tolerance // TODO: Think it over better // Note: we are checking on matrix each even point, that means that the real error // in that moment is approximately half size double myDestRes = mDestXRes < mDestYRes ? mDestXRes : mDestYRes; mSqrTolerance = myDestRes * myDestRes; if ( precision == QgsRasterProjector::Approximate ) { mApproximate = true; } else { mApproximate = false; } // Always try to calculate mCPMatrix, it is used in calcSrcExtent() for both Approximate and Exact // Initialize the matrix by corners and middle points mCPCols = mCPRows = 3; for ( int i = 0; i < mCPRows; i++ ) { QList<QgsPointXY> myRow; myRow.append( QgsPointXY() ); myRow.append( QgsPointXY() ); myRow.append( QgsPointXY() ); mCPMatrix.insert( i, myRow ); // And the legal points QList<bool> myLegalRow; myLegalRow.append( bool( false ) ); myLegalRow.append( bool( false ) ); myLegalRow.append( bool( false ) ); mCPLegalMatrix.insert( i, myLegalRow ); } for ( int i = 0; i < mCPRows; i++ ) { calcRow( i, inverseCt ); } while ( true ) { bool myColsOK = checkCols( inverseCt ); if ( !myColsOK ) { insertRows( inverseCt ); } bool myRowsOK = checkRows( inverseCt ); if ( !myRowsOK ) { insertCols( inverseCt ); } if ( myColsOK && myRowsOK ) { QgsDebugMsgLevel( QStringLiteral( "CP matrix within tolerance" ), 4 ); break; } // What is the maximum reasonable size of transformatio matrix? // TODO: consider better when to break - ratio if ( mCPRows * mCPCols > 0.25 * mDestRows * mDestCols ) //if ( mCPRows * mCPCols > mDestRows * mDestCols ) { QgsDebugMsgLevel( QStringLiteral( "Too large CP matrix" ), 4 ); mApproximate = false; break; } } QgsDebugMsgLevel( QStringLiteral( "CPMatrix size: mCPRows = %1 mCPCols = %2" ).arg( mCPRows ).arg( mCPCols ), 4 ); mDestRowsPerMatrixRow = static_cast< float >( mDestRows ) / ( mCPRows - 1 ); mDestColsPerMatrixCol = static_cast< float >( mDestCols ) / ( mCPCols - 1 ); QgsDebugMsgLevel( QStringLiteral( "CPMatrix:" ), 5 ); QgsDebugMsgLevel( cpToString(), 5 ); // init helper points pHelperTop = new QgsPointXY[mDestCols]; pHelperBottom = new QgsPointXY[mDestCols]; calcHelper( 0, pHelperTop ); calcHelper( 1, pHelperBottom ); mHelperTopRow = 0; // Calculate source dimensions calcSrcExtent(); calcSrcRowsCols(); mSrcYRes = mSrcExtent.height() / mSrcRows; mSrcXRes = mSrcExtent.width() / mSrcCols; }
QgsRasterBlock* QgsRasterResampleFilter::block2( int bandNo, QgsRectangle const & extent, int width, int height, QgsRasterBlockFeedback *feedback ) { Q_UNUSED( bandNo ); QgsDebugMsg( QString( "width = %1 height = %2 extent = %3" ).arg( width ).arg( height ).arg( extent.toString() ) ); QgsRasterBlock *outputBlock = new QgsRasterBlock(); if ( !mInput ) return outputBlock; double oversampling = 1.0; // approximate global oversampling factor if ( mZoomedInResampler || mZoomedOutResampler ) { QgsRasterDataProvider *provider = dynamic_cast<QgsRasterDataProvider*>( mInput->srcInput() ); if ( provider && ( provider->capabilities() & QgsRasterDataProvider::Size ) ) { double xRes = extent.width() / width; double providerXRes = provider->extent().width() / provider->xSize(); double pixelRatio = xRes / providerXRes; oversampling = ( pixelRatio > mMaxOversampling ) ? mMaxOversampling : pixelRatio; QgsDebugMsg( QString( "xRes = %1 providerXRes = %2 pixelRatio = %3 oversampling = %4" ).arg( xRes ).arg( providerXRes ).arg( pixelRatio ).arg( oversampling ) ); } else { // We don't know exact data source resolution (WMS) so we expect that // server data have higher resolution (which is not always true) and use // mMaxOversampling oversampling = mMaxOversampling; } } QgsDebugMsg( QString( "oversampling %1" ).arg( oversampling ) ); int bandNumber = 1; // Do no oversampling if no resampler for zoomed in / zoomed out (nearest neighbour) // We do mZoomedInResampler if oversampling == 1 (otherwise for example reprojected // zoom in rasters are never resampled because projector limits resolution. if ((( oversampling < 1.0 || qgsDoubleNear( oversampling, 1.0 ) ) && !mZoomedInResampler ) || ( oversampling > 1.0 && !mZoomedOutResampler ) ) { QgsDebugMsg( "No oversampling." ); delete outputBlock; return mInput->block2( bandNumber, extent, width, height, feedback ); } //effective oversampling factors are different to global one because of rounding double oversamplingX = (( double )width * oversampling ) / width; double oversamplingY = (( double )height * oversampling ) / height; // TODO: we must also increase the extent to get correct result on borders of parts int resWidth = width * oversamplingX; int resHeight = height * oversamplingY; QgsRasterBlock *inputBlock = mInput->block2( bandNumber, extent, resWidth, resHeight, feedback ); if ( !inputBlock || inputBlock->isEmpty() ) { QgsDebugMsg( "No raster data!" ); delete inputBlock; return outputBlock; } if ( !outputBlock->reset( QGis::ARGB32_Premultiplied, width, height ) ) { delete inputBlock; return outputBlock; } //resample image QImage img = inputBlock->image(); QImage dstImg = QImage( width, height, QImage::Format_ARGB32_Premultiplied ); if ( mZoomedInResampler && ( oversamplingX < 1.0 || qgsDoubleNear( oversampling, 1.0 ) ) ) { QgsDebugMsg( "zoomed in resampling" ); mZoomedInResampler->resample( img, dstImg ); } else if ( mZoomedOutResampler && oversamplingX > 1.0 ) { QgsDebugMsg( "zoomed out resampling" ); mZoomedOutResampler->resample( img, dstImg ); } else { // Should not happen QgsDebugMsg( "Unexpected resampling" ); dstImg = img.scaled( width, height ); } outputBlock->setImage( &dstImg ); delete inputBlock; return outputBlock; // No resampling }
QgsRasterBlock * QgsRasterResampleFilter::block( int bandNo, QgsRectangle const & extent, int width, int height ) { Q_UNUSED( bandNo ); QgsDebugMsg( "Entered" ); QgsRasterBlock *outputBlock = new QgsRasterBlock(); if ( !mInput ) return outputBlock; double oversampling = 1.0; // approximate global oversampling factor if ( mZoomedInResampler || mZoomedOutResampler ) { QgsRasterDataProvider *provider = dynamic_cast<QgsRasterDataProvider*>( mInput->srcInput() ); // Do not oversample if data source does not have fixed resolution (WMS) if ( provider && ( provider->capabilities() & QgsRasterDataProvider::ExactResolution ) ) { double xRes = extent.width() / width; double providerXRes = provider->extent().width() / provider->xSize(); double pixelRatio = xRes / providerXRes; oversampling = ( pixelRatio > mMaxOversampling ) ? mMaxOversampling : pixelRatio; QgsDebugMsg( QString( "xRes = %1 providerXRes = %2 pixelRatio = %3 oversampling = %4" ).arg( xRes ).arg( providerXRes ).arg( pixelRatio ).arg( oversampling ) ); } } //set oversampling back to 1.0 if no resampler for zoomed in / zoomed out (nearest neighbour) if (( oversampling < 1.0 && !mZoomedInResampler ) || ( oversampling > 1.0 && !mZoomedOutResampler ) ) { oversampling = 1.0; } QgsDebugMsg( QString( "oversampling %1" ).arg( oversampling ) ); //effective oversampling factors are different to global one because of rounding double oversamplingX = (( double )width * oversampling ) / width; double oversamplingY = (( double )height * oversampling ) / height; // TODO: we must also increase the extent to get correct result on borders of parts int resWidth = width * oversamplingX; int resHeight = height * oversamplingY; // At moment we know that we read rendered image int bandNumber = 1; //void *rasterData = mInput->block( bandNumber, extent, resWidth, resHeight ); QgsRasterBlock *inputBlock = mInput->block( bandNumber, extent, resWidth, resHeight ); if ( !inputBlock || inputBlock->isEmpty() ) { QgsDebugMsg( "No raster data!" ); delete inputBlock; return outputBlock; } if ( doubleNear( oversamplingX, 1.0 ) || doubleNear( oversamplingY, 1.0 ) ) { QgsDebugMsg( "No oversampling." ); delete outputBlock; return inputBlock; } if ( !outputBlock->reset( QgsRasterBlock::ARGB32_Premultiplied, width, height ) ) { delete inputBlock; return outputBlock; } //resample image QImage img = inputBlock->image(); QImage dstImg = QImage( width, height, QImage::Format_ARGB32_Premultiplied ); if ( mZoomedInResampler && oversamplingX < 1.0 ) { QgsDebugMsg( "zoomed in resampling" ); mZoomedInResampler->resample( img, dstImg ); } else if ( mZoomedOutResampler && oversamplingX > 1.0 ) { QgsDebugMsg( "zoomed out resampling" ); mZoomedOutResampler->resample( img, dstImg ); } else { // Should not happen QgsDebugMsg( "Unexpected resampling" ); dstImg = img.scaled( width, height ); } outputBlock->setImage( &dstImg ); delete inputBlock; return outputBlock; // No resampling }
void QgsRasterProjector::calc() { QgsDebugMsg( "Entered" ); mCPMatrix.clear(); mCPLegalMatrix.clear(); delete[] pHelperTop; pHelperTop = 0; delete[] pHelperBottom; pHelperBottom = 0; // Get max source resolution and extent if possible mMaxSrcXRes = 0; mMaxSrcYRes = 0; if ( mInput ) { QgsRasterDataProvider *provider = dynamic_cast<QgsRasterDataProvider*>( mInput->srcInput() ); if ( provider && ( provider->capabilities() & QgsRasterDataProvider::Size ) ) { mMaxSrcXRes = provider->extent().width() / provider->xSize(); mMaxSrcYRes = provider->extent().height() / provider->ySize(); } // Get source extent if ( mExtent.isEmpty() ) { mExtent = provider->extent(); } } mDestXRes = mDestExtent.width() / ( mDestCols ); mDestYRes = mDestExtent.height() / ( mDestRows ); // Calculate tolerance // TODO: Think it over better // Note: we are checking on matrix each even point, that means that the real error // in that moment is approximately half size double myDestRes = mDestXRes < mDestYRes ? mDestXRes : mDestYRes; mSqrTolerance = myDestRes * myDestRes; const QgsCoordinateTransform* ct = QgsCoordinateTransformCache::instance()->transform( mDestCRS.authid(), mSrcCRS.authid(), mDestDatumTransform, mSrcDatumTransform ); // Initialize the matrix by corners and middle points mCPCols = mCPRows = 3; for ( int i = 0; i < mCPRows; i++ ) { QList<QgsPoint> myRow; myRow.append( QgsPoint() ); myRow.append( QgsPoint() ); myRow.append( QgsPoint() ); mCPMatrix.insert( i, myRow ); // And the legal points QList<bool> myLegalRow; myLegalRow.append( bool( false ) ); myLegalRow.append( bool( false ) ); myLegalRow.append( bool( false ) ); mCPLegalMatrix.insert( i, myLegalRow ); } for ( int i = 0; i < mCPRows; i++ ) { calcRow( i, ct ); } while ( true ) { bool myColsOK = checkCols( ct ); if ( !myColsOK ) { insertRows( ct ); } bool myRowsOK = checkRows( ct ); if ( !myRowsOK ) { insertCols( ct ); } if ( myColsOK && myRowsOK ) { QgsDebugMsg( "CP matrix within tolerance" ); mApproximate = true; break; } // What is the maximum reasonable size of transformatio matrix? // TODO: consider better when to break - ratio if ( mCPRows * mCPCols > 0.25 * mDestRows * mDestCols ) { QgsDebugMsg( "Too large CP matrix" ); mApproximate = false; break; } } QgsDebugMsg( QString( "CPMatrix size: mCPRows = %1 mCPCols = %2" ).arg( mCPRows ).arg( mCPCols ) ); mDestRowsPerMatrixRow = ( float )mDestRows / ( mCPRows - 1 ); mDestColsPerMatrixCol = ( float )mDestCols / ( mCPCols - 1 ); QgsDebugMsgLevel( "CPMatrix:", 5 ); QgsDebugMsgLevel( cpToString(), 5 ); // Calculate source dimensions calcSrcExtent(); calcSrcRowsCols(); mSrcYRes = mSrcExtent.height() / mSrcRows; mSrcXRes = mSrcExtent.width() / mSrcCols; // init helper points pHelperTop = new QgsPoint[mDestCols]; pHelperBottom = new QgsPoint[mDestCols]; calcHelper( 0, pHelperTop ); calcHelper( 1, pHelperBottom ); mHelperTopRow = 0; }
QgsRasterFileWriter::WriterError QgsRasterFileWriter::writeDataRaster( const QgsRasterPipe* pipe, QgsRasterIterator* iter, int nCols, int nRows, const QgsRectangle& outputExtent, const QgsCoordinateReferenceSystem& crs, QProgressDialog* progressDialog ) { QgsDebugMsg( "Entered" ); if ( !iter ) { return SourceProviderError; } const QgsRasterInterface* iface = pipe->last(); if ( !iface ) { return SourceProviderError; } QgsRasterDataProvider* srcProvider = const_cast<QgsRasterDataProvider*>( dynamic_cast<const QgsRasterDataProvider*>( iface->srcInput() ) ); if ( !srcProvider ) { QgsDebugMsg( "Cannot get source data provider" ); return SourceProviderError; } iter->setMaximumTileWidth( mMaxTileWidth ); iter->setMaximumTileHeight( mMaxTileHeight ); int nBands = iface->bandCount(); if ( nBands < 1 ) { return SourceProviderError; } //check if all the bands have the same data type size, otherwise we cannot write it to the provider //(at least not with the current interface) int dataTypeSize = QgsRasterBlock::typeSize( srcProvider->srcDataType( 1 ) ); for ( int i = 2; i <= nBands; ++i ) { if ( QgsRasterBlock::typeSize( srcProvider->srcDataType( 1 ) ) != dataTypeSize ) { return DestProviderError; } } // Output data type - source data type is preferred but it may happen that we need // to set 'no data' value (which was not set on source data) if output extent // is larger than source extent (with or without reprojection) and there is no 'free' // (not used) value available QList<bool> destHasNoDataValueList; QList<double> destNoDataValueList; QList<QGis::DataType> destDataTypeList; for ( int bandNo = 1; bandNo <= nBands; bandNo++ ) { QgsRasterNuller *nuller = pipe->nuller(); bool srcHasNoDataValue = srcProvider->srcHasNoDataValue( bandNo ); bool destHasNoDataValue = false; double destNoDataValue = std::numeric_limits<double>::quiet_NaN(); QGis::DataType destDataType = srcProvider->srcDataType( bandNo ); // TODO: verify what happens/should happen if srcNoDataValue is disabled by setUseSrcNoDataValue QgsDebugMsg( QString( "srcHasNoDataValue = %1 srcNoDataValue = %2" ).arg( srcHasNoDataValue ).arg( srcProvider->srcNoDataValue( bandNo ) ) ); if ( srcHasNoDataValue ) { // If source has no data value, it is used by provider destNoDataValue = srcProvider->srcNoDataValue( bandNo ); destHasNoDataValue = true; } else if ( nuller && nuller->noData( bandNo ).size() > 0 ) { // Use one user defined no data value destNoDataValue = nuller->noData( bandNo ).value( 0 ).min(); destHasNoDataValue = true; } else { // Verify if we realy need no data value, i.e. QgsRectangle srcExtent = outputExtent; QgsRasterProjector *projector = pipe->projector(); if ( projector && projector->destCrs() != projector->srcCrs() ) { QgsCoordinateTransform ct( projector->destCrs(), projector->srcCrs() ); srcExtent = ct.transformBoundingBox( outputExtent ); } if ( !srcProvider->extent().contains( srcExtent ) ) { // Destination extent is larger than source extent, we need destination no data values // Get src sample statistics (estimation from sample) QgsRasterBandStats stats = srcProvider->bandStatistics( bandNo, QgsRasterBandStats::Min | QgsRasterBandStats::Max, srcExtent, 250000 ); // Test if we have free (not used) values double typeMinValue = QgsContrastEnhancement::maximumValuePossible(( QGis::DataType )srcProvider->srcDataType( bandNo ) ); double typeMaxValue = QgsContrastEnhancement::maximumValuePossible(( QGis::DataType )srcProvider->srcDataType( bandNo ) ); if ( stats.minimumValue > typeMinValue ) { destNoDataValue = typeMinValue; } else if ( stats.maximumValue < typeMaxValue ) { destNoDataValue = typeMaxValue; } else { // We have to use wider type destDataType = QgsRasterBlock::typeWithNoDataValue( destDataType, &destNoDataValue ); } destHasNoDataValue = true; } } if ( nuller && destHasNoDataValue ) { nuller->setOutputNoDataValue( bandNo, destNoDataValue ); } QgsDebugMsg( QString( "bandNo = %1 destDataType = %2 destHasNoDataValue = %3 destNoDataValue = %4" ).arg( bandNo ).arg( destDataType ).arg( destHasNoDataValue ).arg( destNoDataValue ) ); destDataTypeList.append( destDataType ); destHasNoDataValueList.append( destHasNoDataValue ); destNoDataValueList.append( destNoDataValue ); } QGis::DataType destDataType = destDataTypeList.value( 0 ); // Currently write API supports one output type for dataset only -> find the widest for ( int i = 1; i < nBands; i++ ) { if ( destDataTypeList.value( i ) > destDataType ) { destDataType = destDataTypeList.value( i ); // no data value may be left per band (for future) } } //create destProvider for whole dataset here QgsRasterDataProvider* destProvider = 0; double pixelSize; double geoTransform[6]; globalOutputParameters( outputExtent, nCols, nRows, geoTransform, pixelSize ); // initOutput() returns 0 in tile mode! destProvider = initOutput( nCols, nRows, crs, geoTransform, nBands, destDataType, destHasNoDataValueList, destNoDataValueList ); WriterError error = writeDataRaster( pipe, iter, nCols, nRows, outputExtent, crs, destDataType, destHasNoDataValueList, destNoDataValueList, destProvider, progressDialog ); if ( error == NoDataConflict ) { // The value used for no data was found in source data, we must use wider data type if ( destProvider ) // no tiles { destProvider->remove(); delete destProvider; destProvider = 0; } else // VRT { // TODO: remove created VRT } // But we don't know which band -> wider all for ( int i = 0; i < nBands; i++ ) { double destNoDataValue; QGis::DataType destDataType = QgsRasterBlock::typeWithNoDataValue( destDataTypeList.value( i ), &destNoDataValue ); destDataTypeList.replace( i, destDataType ); destNoDataValueList.replace( i, destNoDataValue ); } destDataType = destDataTypeList.value( 0 ); // Try again destProvider = initOutput( nCols, nRows, crs, geoTransform, nBands, destDataType, destHasNoDataValueList, destNoDataValueList ); error = writeDataRaster( pipe, iter, nCols, nRows, outputExtent, crs, destDataType, destHasNoDataValueList, destNoDataValueList, destProvider, progressDialog ); } if ( destProvider ) delete destProvider; return error; }