86 changes: 53 additions & 33 deletions src/providers/gdal/qgsgdalprovider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -172,15 +172,29 @@ QgsGdalProvider::QgsGdalProvider( QString const & uri )
GDALRasterBandH myGDALBand = GDALGetRasterBand( mGdalDataset, 1 ); //just use the first band
if ( myGDALBand == NULL )
{
QMessageBox::warning( 0, QObject::tr( "Warning" ),
QObject::tr( "Cannot get GDAL raster band: %1" ).arg( QString::fromUtf8( CPLGetLastErrorMsg() ) ) );
QString msg = QString::fromUtf8( CPLGetLastErrorMsg() );
QStringList layers = subLayers();

GDALDereferenceDataset( mGdalBaseDataset );
mGdalBaseDataset = NULL;
// if there are no subdatasets, then close the dataset
if ( layers.size() == 0 )
{
QMessageBox::warning( 0, QObject::tr( "Warning" ),
QObject::tr( "Cannot get GDAL raster band: %1" ).arg( msg ) );

GDALClose( mGdalDataset );
mGdalDataset = NULL;
return;
GDALDereferenceDataset( mGdalBaseDataset );
mGdalBaseDataset = NULL;

GDALClose( mGdalDataset );
mGdalDataset = NULL;
return;
}
// if there are subdatasets, leave the dataset open for subsequent queries
else
{
QgsDebugMsg( QObject::tr( "Cannot get GDAL raster band: %1" ).arg( msg ) +
QString( " but dataset has %1 subdatasets" ).arg( layers.size() ) );
return;
}
}

mHasPyramids = GDALGetOverviewCount( myGDALBand ) > 0;
Expand Down Expand Up @@ -500,7 +514,7 @@ void QgsGdalProvider::readBlock( int theBandNo, int xBlock, int yBlock, void *bl
GDALRasterBandH myGdalBand = GDALGetRasterBand( mGdalDataset, theBandNo );
//GDALReadBlock( myGdalBand, xBlock, yBlock, block );

/* We have to read with correct data type consistent with other readBlock functions */
// We have to read with correct data type consistent with other readBlock functions
int xOff = xBlock * mXBlockSize;
int yOff = yBlock * mYBlockSize;
GDALRasterIO( myGdalBand, GF_Read, xOff, yOff, mXBlockSize, mYBlockSize, block, mXBlockSize, mYBlockSize, ( GDALDataType ) mGdalDataType[theBandNo-1], 0, 0 );
Expand Down Expand Up @@ -743,12 +757,12 @@ void QgsGdalProvider::readBlock( int theBandNo, QgsRectangle const & theExtent,
//GDALSetProjection( myGdalMemDataset, theDestCRS.toWkt().toAscii().constData() );

double myMemGeoTransform[6];
myMemGeoTransform[0] = theExtent.xMinimum(); /* top left x */
myMemGeoTransform[1] = theExtent.width() / thePixelWidth; /* w-e pixel resolution */
myMemGeoTransform[2] = 0; /* rotation, 0 if image is "north up" */
myMemGeoTransform[3] = theExtent.yMaximum(); /* top left y */
myMemGeoTransform[4] = 0; /* rotation, 0 if image is "north up" */
myMemGeoTransform[5] = -1. * theExtent.height() / thePixelHeight; /* n-s pixel resolution */
myMemGeoTransform[0] = theExtent.xMinimum(); // top left x
myMemGeoTransform[1] = theExtent.width() / thePixelWidth; // w-e pixel resolution
myMemGeoTransform[2] = 0; // rotation, 0 if image is "north up"
myMemGeoTransform[3] = theExtent.yMaximum(); // top left y
myMemGeoTransform[4] = 0; // rotation, 0 if image is "north up"
myMemGeoTransform[5] = -1. * theExtent.height() / thePixelHeight; // n-s pixel resolution

double myGeoTransform[6];
GDALGetGeoTransform( mGdalDataset, myGeoTransform );
Expand Down Expand Up @@ -792,14 +806,14 @@ void QgsGdalProvider::readBlock( int theBandNo, QgsRectangle const & theExtent,
NULL,
FALSE, 0.0, 1
);
/*
#if 0
myWarpOptions->pTransformerArg =
GDALCreateGenImgProjTransformer2(
mGdalDataset,
myGdalMemDataset,
NULL
);
*/
#endif
if ( !myWarpOptions->pTransformerArg )
{
QMessageBox::warning( 0, QObject::tr( "Warning" ),
Expand Down Expand Up @@ -1036,7 +1050,7 @@ bool QgsGdalProvider::identify( const QgsPoint& thePoint, QMap<QString, QString>
double x = thePoint.x();
double y = thePoint.y();

/* Calculate the row / column where the point falls */
// Calculate the row / column where the point falls
double xres = ( mExtent.xMaximum() - mExtent.xMinimum() ) / mWidth;
double yres = ( mExtent.yMaximum() - mExtent.yMinimum() ) / mHeight;

Expand Down Expand Up @@ -1228,17 +1242,23 @@ QString QgsGdalProvider::description() const
}

// This is used also by global isValidRasterFileName
QStringList subLayers_( GDALDatasetH dataset )
QStringList QgsGdalProvider::subLayers( GDALDatasetH dataset )
{
QStringList subLayers;

if ( !dataset )
{
QgsDebugMsg( "dataset is NULL" );
return subLayers;
}

char **metadata = GDALGetMetadata( dataset, "SUBDATASETS" );

if ( metadata )
{
for ( int i = 0; metadata[i] != NULL; i++ )
{
QString layer = QString::fromUtf8( metadata[i] );

int pos = layer.indexOf( "_NAME=" );
if ( pos >= 0 )
{
Expand Down Expand Up @@ -1271,18 +1291,18 @@ void QgsGdalProvider::populateHistogram( int theBandNo, QgsRasterBandStats & t
theBandStats.isHistogramOutOfRange = theIgnoreOutOfRangeFlag;
int *myHistogramArray = new int[theBinCount];

/*
* CPLErr GDALRasterBand::GetHistogram (
* double dfMin,
* double dfMax,
* int nBuckets,
* int * panHistogram,
* int bIncludeOutOfRange,
* int bApproxOK,
* GDALProgressFunc pfnProgress,
* void * pProgressData
* )
*/
#if 0
CPLErr GDALRasterBand::GetHistogram(
double dfMin,
double dfMax,
int nBuckets,
int * panHistogram,
int bIncludeOutOfRange,
int bApproxOK,
GDALProgressFunc pfnProgress,
void * pProgressData
)
#endif

QgsGdalProgress myProg;
myProg.type = ProgressHistogram;
Expand Down Expand Up @@ -1567,7 +1587,7 @@ QList<QgsRasterPyramid> QgsGdalProvider::buildPyramidList()

QStringList QgsGdalProvider::subLayers() const
{
return subLayers_( mGdalDataset );
return subLayers( mGdalDataset );
}

void QgsGdalProvider::emitProgress( int theType, double theProgress, QString theMessage )
Expand Down Expand Up @@ -1813,7 +1833,7 @@ QGISEXTERN bool isValidRasterFileName( QString const & theFileNameQString, QStri
}
else if ( GDALGetRasterCount( myDataset ) == 0 )
{
QStringList layers = subLayers_( myDataset );
QStringList layers = QgsGdalProvider::subLayers( myDataset );
if ( layers.size() == 0 )
{
GDALClose( myDataset );
Expand Down
2 changes: 2 additions & 0 deletions src/providers/gdal/qgsgdalprovider.h
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,8 @@ class QgsGdalProvider : public QgsRasterDataProvider

/** \brief Returns the sublayers of this layer - Useful for providers that manage their own layers, such as WMS */
QStringList subLayers() const;
static QStringList subLayers( GDALDatasetH dataset );

/** \brief If the provider supports it, return band stats for the
given band.
@note added in QGIS 1.7
Expand Down
42 changes: 42 additions & 0 deletions src/ui/qgsoptionsbase.ui
Original file line number Diff line number Diff line change
Expand Up @@ -553,6 +553,48 @@
</item>
</layout>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout_12">
<item>
<widget class="QLabel" name="textLabel1_13">
<property name="sizePolicy">
<sizepolicy hsizetype="Fixed" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string>Prompt for raster sublayers</string>
</property>
</widget>
</item>
<item>
<spacer name="horizontalSpacer_10">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item>
<widget class="QComboBox" name="cmbPromptRasterSublayers">
<property name="duplicatesEnabled">
<bool>false</bool>
</property>
<item>
<property name="text">
<string/>
</property>
</item>
</widget>
</item>
</layout>
</item>
</layout>
</widget>
</item>
Expand Down