Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Use a new ogr layer per iterator
  • Loading branch information
NathanW2 committed Jun 21, 2013
1 parent 9bbc647 commit 0b9d1dc
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 15 deletions.
31 changes: 18 additions & 13 deletions src/providers/ogr/qgsogrfeatureiterator.cpp
Expand Up @@ -32,15 +32,19 @@


QgsOgrFeatureIterator::QgsOgrFeatureIterator( QgsOgrProvider* p, const QgsFeatureRequest& request )
: QgsAbstractFeatureIterator( request ), P( p )
: QgsAbstractFeatureIterator( request ), P( p ), ogrDataSource(0), ogrLayer(0), ogrDriver(0)
{
// make sure that only one iterator is active
if ( P->mActiveIterator )

ogrDataSource = OGROpen( TO8F( P->filePath() ), false, &ogrDriver );

if ( P->layerName().isNull() )
{
ogrLayer = OGR_DS_GetLayer( ogrDataSource, P->layerIndex() );
}
else
{
QgsMessageLog::logMessage( QObject::tr( "Already active iterator on this provider was closed." ), QObject::tr( "OGR" ) );
P->mActiveIterator->close();
ogrLayer = OGR_DS_GetLayerByName( ogrDataSource, TO8( p->layerName() ) );
}
P->mActiveIterator = this;

mFeatureFetched = false;

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

OGR_G_CreateFromWkt(( char ** )&wktText, NULL, &filter );
QgsDebugMsg( "Setting spatial filter using " + wktExtent );
OGR_L_SetSpatialFilter( P->ogrLayer, filter );
OGR_L_SetSpatialFilter( ogrLayer, filter );
OGR_G_DestroyGeometry( filter );
}
else
{
OGR_L_SetSpatialFilter( P->ogrLayer, 0 );
OGR_L_SetSpatialFilter( ogrLayer, 0 );
}

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

if ( mRequest.filterType() == QgsFeatureRequest::FilterFid )
{
OGRFeatureH fet = OGR_L_GetFeature( P->ogrLayer, FID_TO_NUMBER( mRequest.filterFid() ) );
OGRFeatureH fet = OGR_L_GetFeature( ogrLayer, FID_TO_NUMBER( mRequest.filterFid() ) );
if ( !fet )
{
close();
Expand All @@ -111,7 +115,7 @@ bool QgsOgrFeatureIterator::nextFeature( QgsFeature& feature )

OGRFeatureH fet;

while (( fet = OGR_L_GetNextFeature( P->ogrLayer ) ) )
while (( fet = OGR_L_GetNextFeature( ogrLayer ) ) )
{
if ( !readFeature( fet, feature ) )
continue;
Expand All @@ -135,7 +139,7 @@ bool QgsOgrFeatureIterator::rewind()
if ( mClosed )
return false;

OGR_L_ResetReading( P->ogrLayer );
OGR_L_ResetReading( ogrLayer );

return true;
}
Expand All @@ -146,10 +150,11 @@ bool QgsOgrFeatureIterator::close()
if ( mClosed )
return false;

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

mClosed = true;
ogrDataSource = 0;
return true;
}

Expand Down
4 changes: 4 additions & 0 deletions src/providers/ogr/qgsogrfeatureiterator.h
Expand Up @@ -48,6 +48,10 @@ class QgsOgrFeatureIterator : public QgsAbstractFeatureIterator
void getFeatureAttribute( OGRFeatureH ogrFet, QgsFeature & f, int attindex );

bool mFeatureFetched;

OGRDataSourceH ogrDataSource;
OGRLayerH ogrLayer;
OGRSFDriverH ogrDriver;
};


Expand Down
3 changes: 1 addition & 2 deletions src/providers/ogr/qgsogrprovider.cpp
Expand Up @@ -360,8 +360,7 @@ QgsOgrProvider::QgsOgrProvider( QString const & uri )

QgsOgrProvider::~QgsOgrProvider()
{
if ( mActiveIterator )
mActiveIterator->close();
// Do we need to close all active iterators here?

if ( ogrLayer != ogrOrigLayer )
{
Expand Down
6 changes: 6 additions & 0 deletions src/providers/ogr/qgsogrprovider.h
Expand Up @@ -250,6 +250,12 @@ class QgsOgrProvider : public QgsVectorDataProvider
/** Get single flatten geometry type */
static OGRwkbGeometryType ogrWkbSingleFlatten( OGRwkbGeometryType type );

QString layerName() { return mLayerName; }

QString filePath() { return mFilePath; }

int layerIndex() { return mLayerIndex; }

protected:
/** loads fields from input file to member attributeFields */
void loadFields();
Expand Down

0 comments on commit 0b9d1dc

Please sign in to comment.