Skip to content

Commit

Permalink
Merge pull request #8184 from rouault/fix_20098
Browse files Browse the repository at this point in the history
[OGR provider] Make again a feature iterator to be resetable on a OSM layer (fixes #20098)
  • Loading branch information
rouault authored Oct 15, 2018
2 parents 530397c + 906f802 commit b777ab2
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 12 deletions.
30 changes: 18 additions & 12 deletions src/providers/ogr/qgsogrfeatureiterator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ bool QgsOgrFeatureIterator::fetchFeature( QgsFeature &feature )
// see more details here: https://trac.osgeo.org/gdal/wiki/rfc66_randomlayerreadwrite

#if GDAL_VERSION_NUM >= GDAL_COMPUTE_VERSION(2,2,0)
if ( mSource->mDriverName == QLatin1String( "OSM" ) )
if ( !QgsOgrProviderUtils::canDriverShareSameDatasetAmongLayers( mSource->mDriverName ) )
{
OGRLayerH nextFeatureBelongingLayer;
while ( fet.reset( GDALDatasetGetNextFeature( mConn->ds, &nextFeatureBelongingLayer, nullptr, nullptr, nullptr ) ), fet )
Expand All @@ -323,6 +323,7 @@ bool QgsOgrFeatureIterator::fetchFeature( QgsFeature &feature )
}
}
else
#endif
{

while ( fet.reset( OGR_L_GetNextFeature( mOgrLayer ) ), fet )
Expand All @@ -333,20 +334,25 @@ bool QgsOgrFeatureIterator::fetchFeature( QgsFeature &feature )
}
}
}
#else
while ( fet.reset( OGR_L_GetNextFeature( mOgrLayer ) ), fet )
{
if ( checkFeature( fet, feature ) )
{
return true;
}
}
#endif

close();
return false;
}

void QgsOgrFeatureIterator::resetReading()
{
#if GDAL_VERSION_NUM >= GDAL_COMPUTE_VERSION(2,2,0)
if ( !QgsOgrProviderUtils::canDriverShareSameDatasetAmongLayers( mSource->mDriverName ) )
{
GDALDatasetResetReading( mConn->ds );
}
else
#endif
{
OGR_L_ResetReading( mOgrLayer );
}
}


bool QgsOgrFeatureIterator::rewind()
{
Expand All @@ -355,7 +361,7 @@ bool QgsOgrFeatureIterator::rewind()
if ( mClosed || !mOgrLayer )
return false;

OGR_L_ResetReading( mOgrLayer );
resetReading();

mFilterFidsIt = mFilterFids.constBegin();

Expand Down Expand Up @@ -387,7 +393,7 @@ bool QgsOgrFeatureIterator::close()
// Will for example release SQLite3 statements
if ( mOgrLayer )
{
OGR_L_ResetReading( mOgrLayer );
resetReading();
}

if ( mOgrOrigLayer )
Expand Down
2 changes: 2 additions & 0 deletions src/providers/ogr/qgsogrfeatureiterator.h
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ class QgsOgrFeatureIterator : public QgsAbstractFeatureIteratorFromSource<QgsOgr
QgsFields mFieldsWithoutFid;

bool fetchFeatureWithId( QgsFeatureId id, QgsFeature &feature ) const;

void resetReading();
};

#endif // QGSOGRFEATUREITERATOR_H
4 changes: 4 additions & 0 deletions tests/src/python/test_provider_ogr.py
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,10 @@ def testOSM(self):
self.assertTrue(iter_multipolygons.nextFeature(f))
self.assertFalse(iter_multipolygons.nextFeature(f))

# Re-start an iterator (tests #20098)
iter_multipolygons = vl_multipolygons.getFeatures(QgsFeatureRequest())
self.assertTrue(iter_multipolygons.nextFeature(f))


if __name__ == '__main__':
unittest.main()

0 comments on commit b777ab2

Please sign in to comment.