Skip to content
Permalink
Browse files

Use a new ogr layer per iterator

  • Loading branch information
NathanW2 committed Jun 21, 2013
1 parent 9bbc647 commit 0b9d1dc49b3277a842d0a18a2f1ba99782965999
@@ -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;

@@ -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
@@ -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();
@@ -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;
@@ -135,7 +139,7 @@ bool QgsOgrFeatureIterator::rewind()
if ( mClosed )
return false;

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

return true;
}
@@ -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;
}

@@ -48,6 +48,10 @@ class QgsOgrFeatureIterator : public QgsAbstractFeatureIterator
void getFeatureAttribute( OGRFeatureH ogrFet, QgsFeature & f, int attindex );

bool mFeatureFetched;

OGRDataSourceH ogrDataSource;
OGRLayerH ogrLayer;
OGRSFDriverH ogrDriver;
};


@@ -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 )
{
@@ -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();

0 comments on commit 0b9d1dc

Please sign in to comment.
You can’t perform that action at this time.