Skip to content

Commit 8b351e2

Browse files
committedApr 2, 2012
cache gdal sublayers list for subsequent access
1 parent 5101155 commit 8b351e2

File tree

5 files changed

+65
-34
lines changed

5 files changed

+65
-34
lines changed
 

‎src/app/qgisapp.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2314,8 +2314,10 @@ void QgisApp::askUserForGDALSublayers( QgsRasterLayer *layer )
23142314
return;
23152315

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

2318-
QgsDebugMsg( "sublayers:\n " + sublayers.join( " \n" ) + "\n" );
2319+
if ( sublayers.size() < 1 )
2320+
return;
23192321

23202322
// if promptLayers=Load all, load all sublayers without prompting
23212323
QSettings settings;
@@ -2390,6 +2392,7 @@ void QgisApp::loadGDALSublayers( QString uri, QStringList list )
23902392
else
23912393
delete subLayer;
23922394
}
2395+
23932396
}
23942397
}
23952398

@@ -6773,6 +6776,7 @@ QgsRasterLayer* QgisApp::addRasterLayer(
67736776
// draw the map
67746777
mMapCanvas->freeze( false );
67756778
mMapCanvas->refresh();
6779+
67766780
return layer;
67776781

67786782
// Let render() do its own cursor management

‎src/providers/gdal/qgsgdaldataitems.cpp

Lines changed: 41 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,20 @@ void buildSupportedRasterFileFilterAndExtensions( QString & theFileFiltersString
99

1010

1111
QgsGdalLayerItem::QgsGdalLayerItem( QgsDataItem* parent,
12-
QString name, QString path, QString uri )
12+
QString name, QString path, QString uri,
13+
QStringList *theSublayers )
1314
: QgsLayerItem( parent, name, path, uri, QgsLayerItem::Raster, "gdal" )
1415
{
1516
mToolTip = uri;
16-
mPopulated = true; // children are not expected
17+
// save sublayers for subsequent access
18+
// if there are sublayers, set populated=false so item can be populated on demand
19+
if ( theSublayers && theSublayers->size() > 0 )
20+
{
21+
sublayers = *theSublayers;
22+
mPopulated = false;
23+
}
24+
else
25+
mPopulated = true;
1726
}
1827

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

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

64+
QVector<QgsDataItem*> QgsGdalLayerItem::createChildren( )
65+
{
66+
QgsDebugMsg( "Entered, path=" + path() );
67+
QVector<QgsDataItem*> children;
68+
69+
// get children from sublayers
70+
if ( sublayers.count() > 0 )
71+
{
72+
QgsDataItem * childItem = NULL;
73+
QgsDebugMsg( QString( "got %1 sublayers" ).arg( sublayers.count() ) );
74+
for ( int i = 0; i < sublayers.count(); i++ )
75+
{
76+
QString name = sublayers[i];
77+
// replace full path with basename+extension
78+
name.replace( mPath, mName );
79+
// use subdataset name only - perhaps only if name is long
80+
if ( name.length() > 50 )
81+
name = name.split( mName )[1].mid( 2 );
82+
childItem = new QgsGdalLayerItem( this, name, sublayers[i], sublayers[i] );
83+
if ( childItem )
84+
this->addChildItem( childItem );
85+
}
86+
}
87+
88+
return children;
89+
}
5590

5691
// ---------------------------------------------------------------------------
5792

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

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

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

120-
QgsLayerItem * item = new QgsGdalLayerItem( parentItem, name, thePath, uri );
121-
122-
QgsDataItem * childItem = NULL;
123-
GDALDatasetH hChildDS = NULL;
124-
125-
if ( item && sublayers.count() > 1 )
126-
{
127-
QgsDebugMsg( QString( "dataItem() got %1 sublayers" ).arg( sublayers.count() ) );
128-
for ( int i = 0; i < sublayers.count(); i++ )
129-
{
130-
hChildDS = GDALOpen( TO8F( sublayers[i] ), GA_ReadOnly );
131-
if ( hChildDS )
132-
{
133-
GDALClose( hChildDS );
134-
135-
QString name = sublayers[i];
136-
//replace full path with basename+extension
137-
name.replace( thePath, QFileInfo( thePath ).completeBaseName() + "." + QFileInfo( thePath ).suffix() );
138-
childItem = new QgsGdalLayerItem( item, name, thePath + "/" + name, sublayers[i] );
139-
if ( childItem )
140-
item->addChildItem( childItem );
141-
}
142-
}
143-
}
156+
QgsLayerItem * item = new QgsGdalLayerItem( parentItem, name, thePath, uri,
157+
&sublayers );
144158

145159
return item;
146160
}

‎src/providers/gdal/qgsgdaldataitems.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,21 @@
55

66
class QgsGdalLayerItem : public QgsLayerItem
77
{
8+
private:
9+
10+
QStringList sublayers;
11+
812
public:
913
QgsGdalLayerItem( QgsDataItem* parent,
10-
QString name, QString path, QString uri );
14+
QString name, QString path, QString uri,
15+
QStringList *theSublayers = NULL );
1116
~QgsGdalLayerItem();
1217

1318
bool setCrs( QgsCoordinateReferenceSystem crs );
1419
Capability capabilities();
20+
21+
QVector<QgsDataItem*> createChildren();
22+
1523
};
1624

1725

‎src/providers/gdal/qgsgdalprovider.cpp

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -167,16 +167,18 @@ QgsGdalProvider::QgsGdalProvider( QString const & uri )
167167
mGeoTransform[5] = -1;
168168
}
169169

170-
//check if this file has pyramids
170+
// get sublayers
171+
mSubLayers = QgsGdalProvider::subLayers( mGdalDataset );
172+
173+
// check if this file has bands or subdatasets
171174
CPLErrorReset();
172175
GDALRasterBandH myGDALBand = GDALGetRasterBand( mGdalDataset, 1 ); //just use the first band
173176
if ( myGDALBand == NULL )
174177
{
175178
QString msg = QString::fromUtf8( CPLGetLastErrorMsg() );
176-
QStringList layers = subLayers();
177179

178180
// if there are no subdatasets, then close the dataset
179-
if ( layers.size() == 0 )
181+
if ( mSubLayers.size() == 0 )
180182
{
181183
QMessageBox::warning( 0, QObject::tr( "Warning" ),
182184
QObject::tr( "Cannot get GDAL raster band: %1" ).arg( msg ) );
@@ -192,11 +194,12 @@ QgsGdalProvider::QgsGdalProvider( QString const & uri )
192194
else
193195
{
194196
QgsDebugMsg( QObject::tr( "Cannot get GDAL raster band: %1" ).arg( msg ) +
195-
QString( " but dataset has %1 subdatasets" ).arg( layers.size() ) );
197+
QString( " but dataset has %1 subdatasets" ).arg( mSubLayers.size() ) );
196198
return;
197199
}
198200
}
199201

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

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

15911594
QStringList QgsGdalProvider::subLayers() const
15921595
{
1593-
return subLayers( mGdalDataset );
1596+
return mSubLayers;
15941597
}
15951598

15961599
void QgsGdalProvider::emitProgress( int theType, double theProgress, QString theMessage )

‎src/providers/gdal/qgsgdalprovider.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,8 @@ class QgsGdalProvider : public QgsRasterDataProvider
308308

309309
QList<QgsRasterPyramid> mPyramidList;
310310

311+
/** \brief sublayers list saved for subsequent access */
312+
QStringList mSubLayers;
311313
};
312314

313315
#endif

0 commit comments

Comments
 (0)
Please sign in to comment.