Skip to content

Commit

Permalink
-Patch for building pyramids
Browse files Browse the repository at this point in the history
-Closes ticket #1264

git-svn-id: http://svn.osgeo.org/qgis/trunk@9306 c8812cc2-4d05-0410-92ff-de0c093fc19c
  • Loading branch information
ersts committed Sep 12, 2008
1 parent 2106fc5 commit b356365
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 28 deletions.
5 changes: 4 additions & 1 deletion python/core/qgsrasterpyramid.sip
Expand Up @@ -13,6 +13,9 @@ class QgsRasterPyramid
/** \brief YDimension for this pyramid layer */
int yDim;
/** \brief Whether the pyramid layer has been built yet */
bool existsFlag;
bool exists;
/** \brief Whether the pyramid should be built */
bool build;

QgsRasterPyramid()
};
21 changes: 17 additions & 4 deletions src/app/qgsrasterlayerproperties.cpp
Expand Up @@ -276,7 +276,7 @@ QgsRasterLayerProperties::QgsRasterLayerProperties( QgsMapLayer *lyr, QWidget *p
myRasterPyramidIterator != myPyramidList.end();
++myRasterPyramidIterator )
{
if (( *myRasterPyramidIterator ).existsFlag == true )
if (( *myRasterPyramidIterator ).exists == true )
{
lbxPyramidResolutions->addItem( new QListWidgetItem( myPyramidPixmap,
QString::number(( *myRasterPyramidIterator ).xDim ) + QString( " x " ) +
Expand Down Expand Up @@ -1546,10 +1546,14 @@ void QgsRasterLayerProperties::on_buttonBuildPyramids_clicked()
for ( int myCounterInt = 0; myCounterInt < lbxPyramidResolutions->count(); myCounterInt++ )
{
QListWidgetItem *myItem = lbxPyramidResolutions->item( myCounterInt );
if ( myItem->isSelected() )
if ( myItem->isSelected() || myPyramidList[myCounterInt].exists )
{
//mark to be pyramided
myPyramidList[myCounterInt].existsFlag = true;
myPyramidList[myCounterInt].build = true;
}
else
{
myPyramidList[myCounterInt].build = false;
}
}
//
Expand Down Expand Up @@ -1583,13 +1587,22 @@ void QgsRasterLayerProperties::on_buttonBuildPyramids_clicked()
QMessageBox::warning( this, tr( "Building pyramids failed." ),
tr( "Building pyramid overviews is not supported on this type of raster." ) );
}
else if ( res == "ERROR_VIRTUAL" )
{
QMessageBox::warning( this, tr( "Building pyramids failed." ),
tr( "Building pyramid overviews is not supported on this type of raster." ) );
//TODO: should really read -- Building pyramid overviews is not supported for 'warped virtual dataset'. -- But in feature freeze, and translation requests have already gone out PJE20080912
}

}


//
// repopulate the pyramids list
//
lbxPyramidResolutions->clear();
// Need to rebuild list as some or all pyramids may have failed to build
myPyramidList = mRasterLayer->buildRasterPyramidList();
QIcon myPyramidPixmap( QgisApp::getThemeIcon( "/mIconPyramid.png" ) );
QIcon myNoPyramidPixmap( QgisApp::getThemeIcon( "/mIconNoPyramid.png" ) );

Expand All @@ -1598,7 +1611,7 @@ void QgsRasterLayerProperties::on_buttonBuildPyramids_clicked()
myRasterPyramidIterator != myPyramidList.end();
++myRasterPyramidIterator )
{
if (( *myRasterPyramidIterator ).existsFlag == true )
if (( *myRasterPyramidIterator ).exists == true )
{
lbxPyramidResolutions->addItem( new QListWidgetItem( myPyramidPixmap,
QString::number(( *myRasterPyramidIterator ).xDim ) + QString( " x " ) +
Expand Down
55 changes: 35 additions & 20 deletions src/core/raster/qgsrasterlayer.cpp
Expand Up @@ -542,6 +542,7 @@ bool QgsRasterLayer::readFile( QString const & fileName )
{
rasterLayerType = MULTIBAND;
}
//TODO hasBand is really obsolete and only used in the Palette instance, change to new function hasPalette(int)
else if ( hasBand( "Palette" ) ) //dont tr() this its a gdal word!
{
rasterLayerType = PALETTE;
Expand Down Expand Up @@ -3433,6 +3434,9 @@ QString QgsRasterLayer::getMetadata()
QString QgsRasterLayer::buildPyramids( RasterPyramidList const & theRasterPyramidList,
QString const & theResamplingMethod, bool theTryInternalFlag )
{
//TODO: Consider making theRasterPyramidList modifyable by this method to indicate if the pyramid exists after build attempt
//without requiring the user to rebuild the pyramid list to get the updated infomation

//
// Note: Make sure the raster is not opened in write mode
// in order to force overviews to be written to a separate file.
Expand All @@ -3456,20 +3460,22 @@ QString QgsRasterLayer::buildPyramids( RasterPyramidList const & theRasterPyrami
return "ERROR_VIRTUAL";
}


if ( theTryInternalFlag )
{
//close the gdal dataset and reopen it in read / write mode
GDALClose( mGdalDataset );
mGdalDataset = GDALOpen( QFile::encodeName( mDataSource ).constData(), GA_Update );
mGdalBaseDataset = GDALOpen( QFile::encodeName( mDataSource ).constData(), GA_Update );

// if the dataset couldn't be opened in read / write mode, tell the user
if ( !mGdalDataset )
if ( !mGdalBaseDataset )
{
mGdalDataset = GDALOpen( QFile::encodeName( mDataSource ).constData(), GA_ReadOnly );
mGdalBaseDataset = GDALOpen( QFile::encodeName( mDataSource ).constData(), GA_ReadOnly );
//Since we are not a virtual warped dataset, mGdalDataSet and mGdalBaseDataset are supposed to be the same
mGdalDataset = mGdalBaseDataset;
return "ERROR_WRITE_FORMAT";
}
}

//
// Iterate through the Raster Layer Pyramid Vector, building any pyramid
// marked as exists in eaxh RasterPyramid struct.
Expand All @@ -3486,9 +3492,9 @@ QString QgsRasterLayer::buildPyramids( RasterPyramidList const & theRasterPyrami
QgsLogger::debug( "Build pyramids:: Level", ( *myRasterPyramidIterator ).level, 1, __FILE__, __FUNCTION__, __LINE__ );
QgsLogger::debug( "x", ( *myRasterPyramidIterator ).xDim, 1, __FILE__, __FUNCTION__, __LINE__ );
QgsLogger::debug( "y", ( *myRasterPyramidIterator ).yDim, 1, __FILE__, __FUNCTION__, __LINE__ );
QgsLogger::debug( "exists :", ( *myRasterPyramidIterator ).existsFlag, 1, __FILE__, __FUNCTION__, __LINE__ );
QgsLogger::debug( "exists :", ( *myRasterPyramidIterator ).exists, 1, __FILE__, __FUNCTION__, __LINE__ );
#endif
if (( *myRasterPyramidIterator ).existsFlag )
if (( *myRasterPyramidIterator ).build )
{
QgsDebugMsg( "Building....." );
emit drawingProgress( myCount, myTotal );
Expand All @@ -3513,46 +3519,58 @@ QString QgsRasterLayer::buildPyramids( RasterPyramidList const & theRasterPyrami
//see ticket #284
if ( theResamplingMethod == tr( "Average Magphase" ) )
{
myError = GDALBuildOverviews( mGdalDataset, "MODE", 1, myOverviewLevelsArray, 0, NULL,
myError = GDALBuildOverviews( mGdalBaseDataset, "MODE", 1, myOverviewLevelsArray, 0, NULL,
progressCallback, this ); //this is the arg for the gdal progress callback
}
else if ( theResamplingMethod == tr( "Average" ) )

{
myError = GDALBuildOverviews( mGdalDataset, "AVERAGE", 1, myOverviewLevelsArray, 0, NULL,
myError = GDALBuildOverviews( mGdalBaseDataset, "AVERAGE", 1, myOverviewLevelsArray, 0, NULL,
progressCallback, this ); //this is the arg for the gdal progress callback
}
else // fall back to nearest neighbor
{
myError = GDALBuildOverviews( mGdalDataset, "NEAREST", 1, myOverviewLevelsArray, 0, NULL,
myError = GDALBuildOverviews( mGdalBaseDataset, "NEAREST", 1, myOverviewLevelsArray, 0, NULL,
progressCallback, this ); //this is the arg for the gdal progress callback
}

if ( myError == CE_Failure || CPLGetLastErrorNo() == CPLE_NotSupported )
{
//something bad happenend
//QString myString = QString (CPLGetLastError());
GDALClose( mGdalDataset );
mGdalDataset = GDALOpen( QFile::encodeName( mDataSource ).constData(), GA_ReadOnly );
GDALClose( mGdalBaseDataset );
mGdalBaseDataset = GDALOpen( QFile::encodeName( mDataSource ).constData(), GA_ReadOnly );
//Since we are not a virtual warped dataset, mGdalDataSet and mGdalBaseDataset are supposed to be the same
mGdalDataset = mGdalBaseDataset;

emit drawingProgress( 0, 0 );
return "FAILED_NOT_SUPPORTED";
}
else
{
//make sure the raster knows it has pyramids
hasPyramidsFlag = true;
}
myCount++;
//make sure the raster knows it has pyramids
hasPyramidsFlag = true;

}
catch ( CPLErr )
{
QgsLogger::warning( "Pyramid overview building failed!" );
}
}
}

QgsDebugMsg( "Pyramid overviews built" );
if ( theTryInternalFlag )
{
//close the gdal dataset and reopen it in read only mode
GDALClose( mGdalDataset );
mGdalDataset = GDALOpen( QFile::encodeName( mDataSource ).constData(), GA_ReadOnly );
GDALClose( mGdalBaseDataset );
mGdalBaseDataset = GDALOpen( QFile::encodeName( mDataSource ).constData(), GA_ReadOnly );
//Since we are not a virtual warped dataset, mGdalDataSet and mGdalBaseDataset are supposed to be the same
mGdalDataset = mGdalBaseDataset;
}

emit drawingProgress( 0, 0 );
return NULL; // returning null on success
}
Expand All @@ -3576,16 +3594,13 @@ QgsRasterLayer::RasterPyramidList QgsRasterLayer::buildRasterPyramidList()
myRasterPyramid.level = myDivisor;
myRasterPyramid.xDim = ( int )( 0.5 + ( myWidth / ( double )myDivisor ) );
myRasterPyramid.yDim = ( int )( 0.5 + ( myHeight / ( double )myDivisor ) );
myRasterPyramid.existsFlag = false;
myRasterPyramid.exists = false;
#ifdef QGISDEBUG
QgsLogger::debug( "Pyramid", myRasterPyramid.level, 1, __FILE__, __FUNCTION__, __LINE__ );
QgsLogger::debug( "xDim", myRasterPyramid.xDim, 1, __FILE__, __FUNCTION__, __LINE__ );
QgsLogger::debug( "yDim", myRasterPyramid.yDim, 1, __FILE__, __FUNCTION__, __LINE__ );
#endif




//
// Now we check if it actually exists in the raster layer
// and also adjust the dimensions if the dimensions calculated
Expand Down Expand Up @@ -3620,7 +3635,7 @@ QgsRasterLayer::RasterPyramidList QgsRasterLayer::buildRasterPyramidList()
//right we have a match so adjust the a / y before they get added to the list
myRasterPyramid.xDim = myOverviewXDim;
myRasterPyramid.yDim = myOverviewYDim;
myRasterPyramid.existsFlag = true;
myRasterPyramid.exists = true;
QgsDebugMsg( ".....YES!" );
}
else
Expand Down
13 changes: 12 additions & 1 deletion src/core/raster/qgsrasterpyramid.h
Expand Up @@ -30,7 +30,18 @@ class CORE_EXPORT QgsRasterPyramid
/** \brief YDimension for this pyramid layer */
int yDim;
/** \brief Whether the pyramid layer has been built yet */
bool existsFlag;
bool exists;
/** \brief Whether the pyramid should be built */
bool build;

QgsRasterPyramid()
{
level = 0;
xDim = 0;
yDim = 0;
exists = false;
build = false;
}

};
#endif
4 changes: 2 additions & 2 deletions tests/src/core/testqgsrasterlayer.cpp
Expand Up @@ -178,7 +178,7 @@ void TestQgsRasterLayer::buildExternalOverviews()
for ( int myCounterInt = 0; myCounterInt < myPyramidList.count(); myCounterInt++ )
{
//mark to be pyramided
myPyramidList[myCounterInt].existsFlag = true;
myPyramidList[myCounterInt].build = true;
}
//now actually make the pyramids
QString myResult = mypLayer->buildPyramids(
Expand All @@ -194,7 +194,7 @@ void TestQgsRasterLayer::buildExternalOverviews()
for ( int myCounterInt = 0; myCounterInt < myPyramidList.count(); myCounterInt++ )
{
//mark to be pyramided
QVERIFY( myPyramidList.at( myCounterInt ).existsFlag );
QVERIFY( myPyramidList.at( myCounterInt ).exists );
}

//
Expand Down

0 comments on commit b356365

Please sign in to comment.