Skip to content
Permalink
Browse files

Merge branch 'iterator'

Semi fix #7472.
Semi fix #7862
  • Loading branch information
NathanW2 committed Jun 21, 2013
2 parents 9bbc647 + 6876540 commit 4164d6ed0b8498f3e3e45cfa40b639674c8c5074
@@ -32,17 +32,31 @@


QgsOgrFeatureIterator::QgsOgrFeatureIterator( QgsOgrProvider* p, const QgsFeatureRequest& request )
: QgsAbstractFeatureIterator( request ), P( p )
: QgsAbstractFeatureIterator( request ), P( p ), ogrDataSource(0), ogrLayer(0), mSubsetStringSet(false)
{
// make sure that only one iterator is active
if ( P->mActiveIterator )
mFeatureFetched = false;
P->mActiveIterators.insert( this );

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

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

mFeatureFetched = false;
if ( !P->subsetString().isEmpty() )
{
QString sql = QString( "SELECT * FROM %1 WHERE %2" )
.arg( P->quotedIdentifier( FROM8( OGR_FD_GetName( OGR_L_GetLayerDefn( ogrLayer ) ) ) ) )
.arg( P->subsetString() );
QgsDebugMsg( QString( "SQL: %1" ).arg( sql ) );
ogrLayer = OGR_DS_ExecuteSQL( ogrDataSource, P->textEncoding()->fromUnicode( sql ).constData(), NULL, NULL );
mSubsetStringSet = true;
}

ensureRelevantFields();

@@ -56,12 +70,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 +108,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 +125,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 +149,7 @@ bool QgsOgrFeatureIterator::rewind()
if ( mClosed )
return false;

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

return true;
}
@@ -146,10 +160,17 @@ bool QgsOgrFeatureIterator::close()
if ( mClosed )
return false;

// tell provider that this iterator is not active anymore
P->mActiveIterator = 0;
P->mActiveIterators.remove( this );

if (mSubsetStringSet)
{
OGR_DS_ReleaseResultSet(ogrDataSource, ogrLayer );
}

OGR_DS_Destroy( ogrDataSource );

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

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

bool mFeatureFetched;

OGRDataSourceH ogrDataSource;
OGRLayerH ogrLayer;

bool mSubsetStringSet;
};


@@ -209,7 +209,6 @@ QgsOgrProvider::QgsOgrProvider( QString const & uri )
, ogrDriver( 0 )
, valid( false )
, featuresCounted( -1 )
, mActiveIterator( 0 )
{
QgsCPLErrorHandler handler;

@@ -360,8 +359,12 @@ QgsOgrProvider::QgsOgrProvider( QString const & uri )

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

foreach ( QgsOgrFeatureIterator* it, mActiveIterators )
{
it->close();
}

if ( ogrLayer != ogrOrigLayer )
{
@@ -15,6 +15,8 @@ email : sherman at mrcc.com
* *
***************************************************************************/

#include "QTextCodec"

#include "qgsrectangle.h"
#include "qgsvectordataprovider.h"
#include "qgsvectorfilewriter.h"
@@ -250,6 +252,16 @@ 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; }

QTextCodec* textEncoding() { return mEncoding; }

QString quotedIdentifier( QString field );

protected:
/** loads fields from input file to member attributeFields */
void loadFields();
@@ -323,11 +335,9 @@ class QgsOgrProvider : public QgsVectorDataProvider
/**Deletes one feature*/
bool deleteFeature( QgsFeatureId id );

QString quotedIdentifier( QString field );

/**Calls OGR_L_SyncToDisk and recreates the spatial index if present*/
bool syncToDisc();

friend class QgsOgrFeatureIterator;
QgsOgrFeatureIterator* mActiveIterator; //!< pointer to currently active iterator (0 if none)
QSet< QgsOgrFeatureIterator*> mActiveIterators;
};

0 comments on commit 4164d6e

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