Skip to content

Commit 0b9d1dc

Browse files
committed
Use a new ogr layer per iterator
1 parent 9bbc647 commit 0b9d1dc

File tree

4 files changed

+29
-15
lines changed

4 files changed

+29
-15
lines changed

src/providers/ogr/qgsogrfeatureiterator.cpp

+18-13
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,19 @@
3232

3333

3434
QgsOgrFeatureIterator::QgsOgrFeatureIterator( QgsOgrProvider* p, const QgsFeatureRequest& request )
35-
: QgsAbstractFeatureIterator( request ), P( p )
35+
: QgsAbstractFeatureIterator( request ), P( p ), ogrDataSource(0), ogrLayer(0), ogrDriver(0)
3636
{
37-
// make sure that only one iterator is active
38-
if ( P->mActiveIterator )
37+
38+
ogrDataSource = OGROpen( TO8F( P->filePath() ), false, &ogrDriver );
39+
40+
if ( P->layerName().isNull() )
41+
{
42+
ogrLayer = OGR_DS_GetLayer( ogrDataSource, P->layerIndex() );
43+
}
44+
else
3945
{
40-
QgsMessageLog::logMessage( QObject::tr( "Already active iterator on this provider was closed." ), QObject::tr( "OGR" ) );
41-
P->mActiveIterator->close();
46+
ogrLayer = OGR_DS_GetLayerByName( ogrDataSource, TO8( p->layerName() ) );
4247
}
43-
P->mActiveIterator = this;
4448

4549
mFeatureFetched = false;
4650

@@ -56,12 +60,12 @@ QgsOgrFeatureIterator::QgsOgrFeatureIterator( QgsOgrProvider* p, const QgsFeatur
5660

5761
OGR_G_CreateFromWkt(( char ** )&wktText, NULL, &filter );
5862
QgsDebugMsg( "Setting spatial filter using " + wktExtent );
59-
OGR_L_SetSpatialFilter( P->ogrLayer, filter );
63+
OGR_L_SetSpatialFilter( ogrLayer, filter );
6064
OGR_G_DestroyGeometry( filter );
6165
}
6266
else
6367
{
64-
OGR_L_SetSpatialFilter( P->ogrLayer, 0 );
68+
OGR_L_SetSpatialFilter( ogrLayer, 0 );
6569
}
6670

6771
//start with first feature
@@ -94,7 +98,7 @@ bool QgsOgrFeatureIterator::nextFeature( QgsFeature& feature )
9498

9599
if ( mRequest.filterType() == QgsFeatureRequest::FilterFid )
96100
{
97-
OGRFeatureH fet = OGR_L_GetFeature( P->ogrLayer, FID_TO_NUMBER( mRequest.filterFid() ) );
101+
OGRFeatureH fet = OGR_L_GetFeature( ogrLayer, FID_TO_NUMBER( mRequest.filterFid() ) );
98102
if ( !fet )
99103
{
100104
close();
@@ -111,7 +115,7 @@ bool QgsOgrFeatureIterator::nextFeature( QgsFeature& feature )
111115

112116
OGRFeatureH fet;
113117

114-
while (( fet = OGR_L_GetNextFeature( P->ogrLayer ) ) )
118+
while (( fet = OGR_L_GetNextFeature( ogrLayer ) ) )
115119
{
116120
if ( !readFeature( fet, feature ) )
117121
continue;
@@ -135,7 +139,7 @@ bool QgsOgrFeatureIterator::rewind()
135139
if ( mClosed )
136140
return false;
137141

138-
OGR_L_ResetReading( P->ogrLayer );
142+
OGR_L_ResetReading( ogrLayer );
139143

140144
return true;
141145
}
@@ -146,10 +150,11 @@ bool QgsOgrFeatureIterator::close()
146150
if ( mClosed )
147151
return false;
148152

149-
// tell provider that this iterator is not active anymore
150-
P->mActiveIterator = 0;
153+
OGR_DS_ReleaseResultSet( ogrDataSource, ogrLayer );
154+
OGR_DS_Destroy( ogrDataSource );
151155

152156
mClosed = true;
157+
ogrDataSource = 0;
153158
return true;
154159
}
155160

src/providers/ogr/qgsogrfeatureiterator.h

+4
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@ class QgsOgrFeatureIterator : public QgsAbstractFeatureIterator
4848
void getFeatureAttribute( OGRFeatureH ogrFet, QgsFeature & f, int attindex );
4949

5050
bool mFeatureFetched;
51+
52+
OGRDataSourceH ogrDataSource;
53+
OGRLayerH ogrLayer;
54+
OGRSFDriverH ogrDriver;
5155
};
5256

5357

src/providers/ogr/qgsogrprovider.cpp

+1-2
Original file line numberDiff line numberDiff line change
@@ -360,8 +360,7 @@ QgsOgrProvider::QgsOgrProvider( QString const & uri )
360360

361361
QgsOgrProvider::~QgsOgrProvider()
362362
{
363-
if ( mActiveIterator )
364-
mActiveIterator->close();
363+
// Do we need to close all active iterators here?
365364

366365
if ( ogrLayer != ogrOrigLayer )
367366
{

src/providers/ogr/qgsogrprovider.h

+6
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,12 @@ class QgsOgrProvider : public QgsVectorDataProvider
250250
/** Get single flatten geometry type */
251251
static OGRwkbGeometryType ogrWkbSingleFlatten( OGRwkbGeometryType type );
252252

253+
QString layerName() { return mLayerName; }
254+
255+
QString filePath() { return mFilePath; }
256+
257+
int layerIndex() { return mLayerIndex; }
258+
253259
protected:
254260
/** loads fields from input file to member attributeFields */
255261
void loadFields();

0 commit comments

Comments
 (0)