Skip to content
Permalink
Browse files

Follow up a1e57a7 : fix handling of filtered layers

  • Loading branch information
nirvn committed Mar 16, 2021
1 parent 935e0c6 commit 1802269103d6e7bf75a096d6f6abb5a61b7b3848
Showing with 29 additions and 17 deletions.
  1. +10 −9 src/core/providers/ogr/qgsogrprovider.cpp
  2. +19 −8 tests/src/core/testqgsogrprovider.cpp
@@ -1928,21 +1928,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;
@@ -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 );
}

0 comments on commit 1802269

Please sign in to comment.