Skip to content
Permalink
Browse files

Fixes #19826 - Use subset layer to manage fid

  • Loading branch information
pblottiere authored and nyalldawson committed Oct 7, 2018
1 parent 2b71c9c commit 5a9067e722be19cd0ceb3c4417f034ad40c02bb4
Showing with 17 additions and 3 deletions.
  1. +14 −3 src/providers/ogr/qgsogrfeatureiterator.cpp
  2. +3 −0 src/providers/ogr/qgsogrfeatureiterator.h
@@ -44,6 +44,8 @@ QgsOgrFeatureIterator::QgsOgrFeatureIterator( QgsOgrFeatureSource *source, bool
, mFilterFids( mRequest.filterFids() )
, mFilterFidsIt( mFilterFids.constBegin() )
, mSharedDS( source->mSharedDS )
, mFirstFieldIsFid( source->mFirstFieldIsFid )
, mFieldsWithoutFid( source->mFieldsWithoutFid )
{
// Since connection timeout for OGR connections is problematic and can lead to crashes, disable for now.
mRequest.setTimeout( -1 );
@@ -82,6 +84,15 @@ QgsOgrFeatureIterator::QgsOgrFeatureIterator( QgsOgrFeatureSource *source, bool
mOgrOrigLayer = mOgrLayer;
mOgrLayerWithFid = QgsOgrProviderUtils::setSubsetString( mOgrLayer, mConn->ds, mSource->mEncoding, QString(), true, &mOrigFidAdded );
mOgrLayer = QgsOgrProviderUtils::setSubsetString( mOgrLayer, mConn->ds, mSource->mEncoding, mSource->mSubsetString, true, &mOrigFidAdded );

OGRFeatureDefnH fdef = OGR_L_GetLayerDefn( mOgrLayer );
QByteArray fidColumn( OGR_L_GetFIDColumn( mOgrLayer ) );
mFirstFieldIsFid = !fidColumn.isEmpty() && OGR_FD_GetFieldIndex( fdef, fidColumn ) < 0;

mFieldsWithoutFid.clear();
for ( int i = ( mFirstFieldIsFid ) ? 1 : 0; i < mSource->mFields.size(); i++ )
mFieldsWithoutFid.append( mSource->mFields.at( i ) );

if ( !mOgrLayer )
{
close();
@@ -404,15 +415,15 @@ bool QgsOgrFeatureIterator::close()

void QgsOgrFeatureIterator::getFeatureAttribute( OGRFeatureH ogrFet, QgsFeature &f, int attindex ) const
{
if ( mSource->mFirstFieldIsFid && attindex == 0 )
if ( mFirstFieldIsFid && attindex == 0 )
{
f.setAttribute( 0, static_cast<qint64>( OGR_F_GetFID( ogrFet ) ) );
return;
}

int attindexWithoutFid = ( mSource->mFirstFieldIsFid ) ? attindex - 1 : attindex;
int attindexWithoutFid = ( mFirstFieldIsFid ) ? attindex - 1 : attindex;
bool ok = false;
QVariant value = QgsOgrUtils::getOgrFeatureAttribute( ogrFet, mSource->mFieldsWithoutFid, attindexWithoutFid, mSource->mEncoding, &ok );
QVariant value = QgsOgrUtils::getOgrFeatureAttribute( ogrFet, mFieldsWithoutFid, attindexWithoutFid, mSource->mEncoding, &ok );
if ( !ok )
return;

@@ -96,6 +96,9 @@ class QgsOgrFeatureIterator : public QgsAbstractFeatureIteratorFromSource<QgsOgr
QgsCoordinateTransform mTransform;
QgsOgrDatasetSharedPtr mSharedDS = nullptr;

bool mFirstFieldIsFid = false;
QgsFields mFieldsWithoutFid;

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

0 comments on commit 5a9067e

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