Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Fix tests
  • Loading branch information
nyalldawson committed Apr 7, 2020
1 parent bf6f017 commit 66959cd
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 6 deletions.
29 changes: 26 additions & 3 deletions src/core/processing/qgsprocessingutils.cpp
Expand Up @@ -731,7 +731,22 @@ QgsFeatureSink *QgsProcessingUtils::createFeatureSink( QString &destination, Qgs
saveOptions.datasourceOptions = QgsVectorFileWriter::defaultDatasetOptions( format );
saveOptions.layerOptions = QgsVectorFileWriter::defaultLayerOptions( format );
saveOptions.symbologyExport = QgsVectorFileWriter::NoSymbology;
saveOptions.actionOnExistingFile = remappingDefinition ? QgsVectorFileWriter::AppendToLayerNoNewFields : QgsVectorFileWriter::CreateOrOverwriteFile;
if ( remappingDefinition )
{
saveOptions.actionOnExistingFile = QgsVectorFileWriter::AppendToLayerNoNewFields;
// sniff destination file to get correct wkb type and crs
std::unique_ptr< QgsVectorLayer > vl = qgis::make_unique< QgsVectorLayer >( destination );
if ( vl->isValid() )
{
remappingDefinition->setDestinationWkbType( vl->wkbType() );
remappingDefinition->setDestinationCrs( vl->crs() );
}
context.expressionContext().setFields( fields );
}
else
{
saveOptions.actionOnExistingFile = QgsVectorFileWriter::CreateOrOverwriteFile;
}
std::unique_ptr< QgsVectorFileWriter > writer( QgsVectorFileWriter::create( destination, newFields, geometryType, crs, context.transformContext(), saveOptions, sinkFlags, &finalFileName ) );
if ( writer->hasError() )
{
Expand All @@ -756,15 +771,23 @@ QgsFeatureSink *QgsProcessingUtils::createFeatureSink( QString &destination, Qgs
//write to existing layer

// use destination string as layer name (eg "postgis:..." )
if ( !layerName.isEmpty() )
uri += QStringLiteral( "|layername=%1" ).arg( layerName );

std::unique_ptr< QgsVectorLayer > layer = qgis::make_unique<QgsVectorLayer>( uri, destination, providerKey, layerOptions );
// update destination to layer ID
destination = layer->id();

context.temporaryLayerStore()->addMapLayer( layer.release() );
if ( layer->isValid() )
{
remappingDefinition->setDestinationWkbType( layer->wkbType() );
remappingDefinition->setDestinationCrs( layer->crs() );
}

std::unique_ptr< QgsRemappingProxyFeatureSink > remapSink = qgis::make_unique< QgsRemappingProxyFeatureSink >( *remappingDefinition, layer->dataProvider(), false );
context.temporaryLayerStore()->addMapLayer( layer.release() );
remapSink->setExpressionContext( context.expressionContext() );
remapSink->setTransformContext( context.transformContext() );
context.expressionContext().setFields( fields );
return new QgsProcessingFeatureSink( remapSink.release(), destination, context, true );
}
else
Expand Down
1 change: 0 additions & 1 deletion src/core/qgsremappingproxyfeaturesink.cpp
Expand Up @@ -46,7 +46,6 @@ QgsFeatureList QgsRemappingProxyFeatureSink::remapFeature( const QgsFeature &fea
QgsFeatureList res;

mContext.setFeature( feature );
mContext.setFields( feature.fields() );

// remap fields first
QgsFeature f;
Expand Down
24 changes: 22 additions & 2 deletions tests/src/analysis/testqgsprocessing.cpp
Expand Up @@ -1820,7 +1820,6 @@ void TestQgsProcessing::createFeatureSink()
remapDef.setDestinationCrs( layer->crs() );
remapDef.setSourceCrs( QgsCoordinateReferenceSystem( "EPSG:4326" ) );
remapDef.setDestinationWkbType( QgsWkbTypes::Polygon );
remapDef.addMappedField( QStringLiteral( "fid" ), QgsProperty::fromValue( 3 ) );
remapDef.addMappedField( QStringLiteral( "my_field" ), QgsProperty::fromExpression( QStringLiteral( "field2 || @extra" ) ) );
QgsFields fields2;
fields2.append( QgsField( "field2", QVariant::String ) );
Expand All @@ -1842,7 +1841,6 @@ void TestQgsProcessing::createFeatureSink()
QCOMPARE( f.attributes().at( 1 ).toString(), QStringLiteral( "val" ) );
QCOMPARE( f.geometry().asWkt( 1 ), QStringLiteral( "PointZ (1 2 3)" ) );
QVERIFY( it.nextFeature( f ) );
QCOMPARE( f.attributes().at( 0 ).toInt(), 3 );
QCOMPARE( f.attributes().at( 1 ).toString(), QStringLiteral( "val2" ) );
QCOMPARE( f.geometry().asWkt( 0 ), QStringLiteral( "Point (-10199761 -4017774)" ) );
delete layer;
Expand Down Expand Up @@ -1890,6 +1888,28 @@ void TestQgsProcessing::createFeatureSink()
QCOMPARE( layer->wkbType(), QgsWkbTypes::Polygon );
QVERIFY( layer->getFeatures().nextFeature( f ) );
QCOMPARE( f.attribute( "my_field" ).toString(), QStringLiteral( "val" ) );

// now append to that second layer
remapDef.setDestinationFields( layer->fields() );
remapDef.setDestinationCrs( layer->crs() );

remapDef.setSourceCrs( QgsCoordinateReferenceSystem( "EPSG:4326" ) );
remapDef.setDestinationWkbType( QgsWkbTypes::Point );
remapDef.addMappedField( QStringLiteral( "my_field" ), QgsProperty::fromExpression( QStringLiteral( "field2 || @extra" ) ) );
destination2 = QStringLiteral( "ogr:dbname='%1' table=\"points\" (geom) sql=" ).arg( geopackagePath );
sink.reset( QgsProcessingUtils::createFeatureSink( destination2, context, fields2, QgsWkbTypes::PointZ, QgsCoordinateReferenceSystem::fromEpsgId( 4326 ), QVariantMap(), nullptr, &remapDef ) );
QVERIFY( sink.get() );
f = QgsFeature( fields );
f.setGeometry( QgsGeometry::fromWkt( QStringLiteral( "PointZ(3 4 5)" ) ) );
f.setAttributes( QgsAttributes() << "v" );
QVERIFY( sink->addFeature( f ) );
sink.reset( nullptr );
layer = qobject_cast< QgsVectorLayer *>( QgsProcessingUtils::mapLayerFromString( destination2, context, true ) );
QVERIFY( layer->isValid() );
QCOMPARE( layer->wkbType(), QgsWkbTypes::Point );
QCOMPARE( layer->featureCount(), 2L );
QVERIFY( layer->getFeatures().nextFeature( f ) );
QCOMPARE( f.attribute( "my_field" ).toString(), QStringLiteral( "val2" ) );
}

void TestQgsProcessing::source()
Expand Down

0 comments on commit 66959cd

Please sign in to comment.