Skip to content

Commit

Permalink
Close gdal/ogr data sources when finished in dataItems implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
wonder-sk committed Aug 17, 2011
1 parent 42418b1 commit 22f5ba2
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 15 deletions.
32 changes: 17 additions & 15 deletions src/providers/gdal/qgsgdalprovider.cpp
Expand Up @@ -884,12 +884,12 @@ void QgsGdalProvider::computeMinMax( int theBandNo )
return;
}
GDALRasterBandH myGdalBand = GDALGetRasterBand( mGdalDataset, theBandNo );
int bApproxOK=false;
int bApproxOK = false;
int bGotMin, bGotMax;
double adfMinMax[2];
adfMinMax[0] = GDALGetRasterMinimum( myGdalBand, &bGotMin );
adfMinMax[1] = GDALGetRasterMaximum( myGdalBand, &bGotMax );
if( ! ( bGotMin && bGotMax ) )
if ( !( bGotMin && bGotMax ) )
{
GDALComputeRasterMinMax( myGdalBand, TRUE, adfMinMax );
}
Expand Down Expand Up @@ -1838,7 +1838,7 @@ QgsRasterBandStats QgsGdalProvider::bandStatistics( int theBandNo )
{
GDALRasterBandH myGdalBand = GDALGetRasterBand( mGdalDataset, theBandNo );
QgsRasterBandStats myRasterBandStats;
int bApproxOK=false;
int bApproxOK = false;
double pdfMin;
double pdfMax;
double pdfMean;
Expand All @@ -1847,26 +1847,26 @@ QgsRasterBandStats QgsGdalProvider::bandStatistics( int theBandNo )
myProg.type = ProgressHistogram;
myProg.provider = this;

// double myerval =
// GDALComputeRasterStatistics (
// myGdalBand, bApproxOK, &pdfMin, &pdfMax, &pdfMean, &pdfStdDev,
// progressCallback, &myProg ) ;
// double myerval =
// double myerval =
// GDALComputeRasterStatistics (
// myGdalBand, bApproxOK, &pdfMin, &pdfMax, &pdfMean, &pdfStdDev,
// progressCallback, &myProg ) ;
// double myerval =
// GDALGetRasterStatistics ( myGdalBand, bApproxOK, TRUE, &pdfMin, &pdfMax, &pdfMean, &pdfStdDev);
// double myerval =
// double myerval =
// GDALGetRasterStatisticsProgress ( myGdalBand, bApproxOK, TRUE, &pdfMin, &pdfMax, &pdfMean, &pdfStdDev,
// progressCallback, &myProg );
// progressCallback, &myProg );

// try to fetch the cached stats (bForce=FALSE)
CPLErr myerval =
GDALGetRasterStatistics ( myGdalBand, bApproxOK, FALSE, &pdfMin, &pdfMax, &pdfMean, &pdfStdDev);
CPLErr myerval =
GDALGetRasterStatistics( myGdalBand, bApproxOK, FALSE, &pdfMin, &pdfMax, &pdfMean, &pdfStdDev );

// if cached stats are not found, compute them
if ( CE_Warning == myerval )
{
myerval = GDALComputeRasterStatistics ( myGdalBand, bApproxOK,
&pdfMin, &pdfMax, &pdfMean, &pdfStdDev,
progressCallback, &myProg ) ;
myerval = GDALComputeRasterStatistics( myGdalBand, bApproxOK,
&pdfMin, &pdfMax, &pdfMean, &pdfStdDev,
progressCallback, &myProg ) ;
}

// if stats are found populate the QgsRasterBandStats object
Expand Down Expand Up @@ -2010,6 +2010,8 @@ QGISEXTERN QgsDataItem * dataItem( QString thePath, QgsDataItem* parentItem )
if ( !hDS )
return 0;

GDALClose( hDS );

QgsDebugMsg( "GdalDataset opened " + thePath );

QString name = info.fileName();
Expand Down
7 changes: 7 additions & 0 deletions src/providers/ogr/qgsogrprovider.cpp
Expand Up @@ -2288,7 +2288,10 @@ QGISEXTERN QgsDataItem * dataItem( QString thePath, QgsDataItem* parentItem )
int numLayers = OGR_DS_GetLayerCount( hDataSource );

if ( numLayers == 0 )
{
OGR_DS_Destroy( hDataSource );
return 0;
}

QgsDataCollectionItem * collection = 0;
if ( numLayers > 1 )
Expand Down Expand Up @@ -2352,10 +2355,14 @@ QGISEXTERN QgsDataItem * dataItem( QString thePath, QgsDataItem* parentItem )

QgsOgrLayerItem * item = new QgsOgrLayerItem( collection ? collection : parentItem, name, path, layerUri, layerType );
if ( numLayers == 1 )
{
OGR_DS_Destroy( hDataSource );
return item;
}
collection->addChild( item );
}
collection->setPopulated();
OGR_DS_Destroy( hDataSource );
return collection;
}

Expand Down

0 comments on commit 22f5ba2

Please sign in to comment.