Skip to content
Permalink
Browse files
cache gdal sublayers list for subsequent access
  • Loading branch information
etiennesky committed Apr 2, 2012
1 parent 5101155 commit 8b351e2ef542b6f8cb7197c669bbdfa24b160fd9
Showing with 65 additions and 34 deletions.
  1. +5 −1 src/app/qgisapp.cpp
  2. +41 −27 src/providers/gdal/qgsgdaldataitems.cpp
  3. +9 −1 src/providers/gdal/qgsgdaldataitems.h
  4. +8 −5 src/providers/gdal/qgsgdalprovider.cpp
  5. +2 −0 src/providers/gdal/qgsgdalprovider.h
@@ -2314,8 +2314,10 @@ void QgisApp::askUserForGDALSublayers( QgsRasterLayer *layer )
return;

QStringList sublayers = layer->subLayers();
QgsDebugMsg( QString( "raster has %1 sublayers" ).arg( layer->subLayers().size() ) );

QgsDebugMsg( "sublayers:\n " + sublayers.join( " \n" ) + "\n" );
if ( sublayers.size() < 1 )
return;

// if promptLayers=Load all, load all sublayers without prompting
QSettings settings;
@@ -2390,6 +2392,7 @@ void QgisApp::loadGDALSublayers( QString uri, QStringList list )
else
delete subLayer;
}

}
}

@@ -6773,6 +6776,7 @@ QgsRasterLayer* QgisApp::addRasterLayer(
// draw the map
mMapCanvas->freeze( false );
mMapCanvas->refresh();

return layer;

// Let render() do its own cursor management
@@ -9,11 +9,20 @@ void buildSupportedRasterFileFilterAndExtensions( QString & theFileFiltersString


QgsGdalLayerItem::QgsGdalLayerItem( QgsDataItem* parent,
QString name, QString path, QString uri )
QString name, QString path, QString uri,
QStringList *theSublayers )
: QgsLayerItem( parent, name, path, uri, QgsLayerItem::Raster, "gdal" )
{
mToolTip = uri;
mPopulated = true; // children are not expected
// save sublayers for subsequent access
// if there are sublayers, set populated=false so item can be populated on demand
if ( theSublayers && theSublayers->size() > 0 )
{
sublayers = *theSublayers;
mPopulated = false;
}
else
mPopulated = true;
}

QgsGdalLayerItem::~QgsGdalLayerItem()
@@ -22,7 +31,7 @@ QgsGdalLayerItem::~QgsGdalLayerItem()

QgsLayerItem::Capability QgsGdalLayerItem::capabilities()
{
// Check if data sour can be opened for update
// Check if data source can be opened for update
QgsDebugMsg( "mPath = " + mPath );
GDALAllRegister();
GDALDatasetH hDS = GDALOpen( TO8F( mPath ), GA_Update );
@@ -52,6 +61,32 @@ bool QgsGdalLayerItem::setCrs( QgsCoordinateReferenceSystem crs )
return true;
}

QVector<QgsDataItem*> QgsGdalLayerItem::createChildren( )
{
QgsDebugMsg( "Entered, path=" + path() );
QVector<QgsDataItem*> children;

// get children from sublayers
if ( sublayers.count() > 0 )
{
QgsDataItem * childItem = NULL;
QgsDebugMsg( QString( "got %1 sublayers" ).arg( sublayers.count() ) );
for ( int i = 0; i < sublayers.count(); i++ )
{
QString name = sublayers[i];
// replace full path with basename+extension
name.replace( mPath, mName );
// use subdataset name only - perhaps only if name is long
if ( name.length() > 50 )
name = name.split( mName )[1].mid( 2 );
childItem = new QgsGdalLayerItem( this, name, sublayers[i], sublayers[i] );
if ( childItem )
this->addChildItem( childItem );
}
}

return children;
}

// ---------------------------------------------------------------------------

@@ -107,6 +142,7 @@ QGISEXTERN QgsDataItem * dataItem( QString thePath, QgsDataItem* parentItem )
if ( !hDS )
return 0;

// get layers list now so we can pass it to item
QStringList sublayers = QgsGdalProvider::subLayers( hDS );

GDALClose( hDS );
@@ -117,30 +153,8 @@ QGISEXTERN QgsDataItem * dataItem( QString thePath, QgsDataItem* parentItem )
QString name = info.completeBaseName() + "." + QFileInfo( thePath ).suffix();
QString uri = thePath;

QgsLayerItem * item = new QgsGdalLayerItem( parentItem, name, thePath, uri );

QgsDataItem * childItem = NULL;
GDALDatasetH hChildDS = NULL;

if ( item && sublayers.count() > 1 )
{
QgsDebugMsg( QString( "dataItem() got %1 sublayers" ).arg( sublayers.count() ) );
for ( int i = 0; i < sublayers.count(); i++ )
{
hChildDS = GDALOpen( TO8F( sublayers[i] ), GA_ReadOnly );
if ( hChildDS )
{
GDALClose( hChildDS );

QString name = sublayers[i];
//replace full path with basename+extension
name.replace( thePath, QFileInfo( thePath ).completeBaseName() + "." + QFileInfo( thePath ).suffix() );
childItem = new QgsGdalLayerItem( item, name, thePath + "/" + name, sublayers[i] );
if ( childItem )
item->addChildItem( childItem );
}
}
}
QgsLayerItem * item = new QgsGdalLayerItem( parentItem, name, thePath, uri,
&sublayers );

return item;
}
@@ -5,13 +5,21 @@

class QgsGdalLayerItem : public QgsLayerItem
{
private:

QStringList sublayers;

public:
QgsGdalLayerItem( QgsDataItem* parent,
QString name, QString path, QString uri );
QString name, QString path, QString uri,
QStringList *theSublayers = NULL );
~QgsGdalLayerItem();

bool setCrs( QgsCoordinateReferenceSystem crs );
Capability capabilities();

QVector<QgsDataItem*> createChildren();

};


@@ -167,16 +167,18 @@ QgsGdalProvider::QgsGdalProvider( QString const & uri )
mGeoTransform[5] = -1;
}

//check if this file has pyramids
// get sublayers
mSubLayers = QgsGdalProvider::subLayers( mGdalDataset );

// check if this file has bands or subdatasets
CPLErrorReset();
GDALRasterBandH myGDALBand = GDALGetRasterBand( mGdalDataset, 1 ); //just use the first band
if ( myGDALBand == NULL )
{
QString msg = QString::fromUtf8( CPLGetLastErrorMsg() );
QStringList layers = subLayers();

// if there are no subdatasets, then close the dataset
if ( layers.size() == 0 )
if ( mSubLayers.size() == 0 )
{
QMessageBox::warning( 0, QObject::tr( "Warning" ),
QObject::tr( "Cannot get GDAL raster band: %1" ).arg( msg ) );
@@ -192,11 +194,12 @@ QgsGdalProvider::QgsGdalProvider( QString const & uri )
else
{
QgsDebugMsg( QObject::tr( "Cannot get GDAL raster band: %1" ).arg( msg ) +
QString( " but dataset has %1 subdatasets" ).arg( layers.size() ) );
QString( " but dataset has %1 subdatasets" ).arg( mSubLayers.size() ) );
return;
}
}

// check if this file has pyramids
mHasPyramids = GDALGetOverviewCount( myGDALBand ) > 0;

// Get the layer's projection info and set up the
@@ -1590,7 +1593,7 @@ QList<QgsRasterPyramid> QgsGdalProvider::buildPyramidList()

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

void QgsGdalProvider::emitProgress( int theType, double theProgress, QString theMessage )
@@ -308,6 +308,8 @@ class QgsGdalProvider : public QgsRasterDataProvider

QList<QgsRasterPyramid> mPyramidList;

/** \brief sublayers list saved for subsequent access */
QStringList mSubLayers;
};

#endif

0 comments on commit 8b351e2

Please sign in to comment.