Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[OGR provider] Make again a feature iterator to be resetable on a OSM layer (fixes #20098) #8184

Merged
merged 1 commit into from Oct 15, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
30 changes: 18 additions & 12 deletions src/providers/ogr/qgsogrfeatureiterator.cpp
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
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
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()