Skip to content

Commit

Permalink
Follow up a1e57a7 : fix handling of filtered layers
Browse files Browse the repository at this point in the history
  • Loading branch information
nirvn committed Mar 16, 2021
1 parent 48d8442 commit 8731c7e
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 17 deletions.
19 changes: 10 additions & 9 deletions src/core/providers/ogr/qgsogrprovider.cpp
Expand Up @@ -1901,21 +1901,22 @@ bool QgsOgrProvider::addFeatures( QgsFeatureList &flist, Flags flags )
const bool inTransaction = startTransaction();

QgsFeatureId incrementalFeatureId = -1;
OGRGeometryH filter;
if ( !( flags & QgsFeatureSink::FastInsert ) &&
( mGDALDriverName == QLatin1String( "CSV" ) || mGDALDriverName == QLatin1String( "XLSX" ) || mGDALDriverName == QLatin1String( "ODS" ) ) )
{
filter = mOgrLayer->GetSpatialFilter();
if ( filter )
QMutex *mutex = nullptr;
OGRLayerH layer = mOgrOrigLayer->getHandleAndMutex( mutex );
{
filter = OGR_G_Clone( filter );
mOgrLayer->SetSpatialFilter( nullptr );
}
QMutexLocker locker( mutex );

incrementalFeatureId = static_cast< QgsFeatureId >( mOgrLayer->GetFeatureCount() ) + 1;
if ( !mSubsetString.isEmpty() )
OGR_L_SetAttributeFilter( layer, nullptr );

incrementalFeatureId = static_cast< QgsFeatureId >( OGR_L_GetFeatureCount( layer, false ) ) + 1;

if ( filter )
mOgrLayer->SetSpatialFilter( filter );
if ( !mSubsetString.isEmpty() )
OGR_L_SetAttributeFilter( layer, textEncoding()->fromUnicode( mSubsetString ).constData() );
}
}

bool returnvalue = true;
Expand Down
27 changes: 19 additions & 8 deletions tests/src/core/testqgsogrprovider.cpp
Expand Up @@ -357,29 +357,40 @@ void TestQgsOgrProvider::testCsvFeatureAddition()
{
QString csvFilename = QDir::tempPath() + "/csvfeatureadditiontest.csv";
QFile csvFile( csvFilename );
if ( csvFile.open( QIODevice::WriteOnly | QIODevice::Append ) )
if ( csvFile.open( QIODevice::WriteOnly | QIODevice::Truncate ) )
{
QTextStream textStream( &csvFile );
textStream << QLatin1String( "col1,col2\n1,2\n" );
textStream << QLatin1String( "col1,col2,col3\n0,0,\"csv0\"\n" );
csvFile.close();
}

QgsVectorLayer *csvLayer = new QgsVectorLayer( csvFilename, QStringLiteral( "csv" ) );
QVERIFY( csvLayer->isValid() );
QCOMPARE( csvLayer->featureCount(), 1 );


QgsFeature feature( csvLayer->fields() );
feature.setAttribute( 0, 1 );
feature.setAttribute( 1, 1 );
feature.setAttribute( 2, QLatin1String( "csv" ) );
QgsFeature f1( csvLayer->fields() );
f1.setAttribute( 0, 1 );
f1.setAttribute( 1, 1 );
f1.setAttribute( 2, QLatin1String( "csv1" ) );
QgsFeature f2( csvLayer->fields() );
f2.setAttribute( 0, 2 );
f2.setAttribute( 1, 2 );
f2.setAttribute( 2, QLatin1String( "csv2" ) );

QgsFeatureList features;
features << feature << feature;
features << f1 << f2;
csvLayer->dataProvider()->addFeatures( features );
QCOMPARE( features.at( 0 ).id(), 2 );
QCOMPARE( features.at( 1 ).id(), 3 );

csvLayer->setSubsetString( QStringLiteral( "col1 = '2'" ) );
QCOMPARE( csvLayer->featureCount(), 1 );

features.clear();
features << f1;
csvLayer->dataProvider()->addFeatures( features );
QCOMPARE( features.at( 0 ).id(), 4 );

delete csvLayer;
QFile::remove( csvFilename );
}
Expand Down

0 comments on commit 8731c7e

Please sign in to comment.