Skip to content

Commit

Permalink
createFeature on move and copy
Browse files Browse the repository at this point in the history
using of the createFeature function with tool move and copy and there the logic of unique not null fields (like primary keys) is used.

This would fix #20347
  • Loading branch information
signedav committed Nov 7, 2018
1 parent 5ae2bae commit 4ac0631
Showing 1 changed file with 10 additions and 12 deletions.
22 changes: 10 additions & 12 deletions src/core/qgsvectorlayertools.cpp
Expand Up @@ -18,6 +18,7 @@
#include "qgsvectorlayertools.h"
#include "qgsfeaturerequest.h"
#include "qgslogger.h"
#include "qgsvectorlayerutils.h"


QgsVectorLayerTools::QgsVectorLayerTools()
Expand All @@ -34,7 +35,6 @@ bool QgsVectorLayerTools::copyMoveFeatures( QgsVectorLayer *layer, QgsFeatureReq

QgsFeatureIterator fi = layer->getFeatures( request );
QgsFeature f;
QgsAttributeList pkAttrList = layer->primaryKeyAttributes();

int browsedFeatureCount = 0;
int couldNotWriteCount = 0;
Expand All @@ -45,29 +45,27 @@ bool QgsVectorLayerTools::copyMoveFeatures( QgsVectorLayer *layer, QgsFeatureReq
while ( fi.nextFeature( f ) )
{
browsedFeatureCount++;
// remove pkey values
Q_FOREACH ( auto idx, pkAttrList )
{
f.setAttribute( idx, QVariant() );
}

QgsFeature newFeature = QgsVectorLayerUtils::createFeature( layer, f.geometry(), f.attributes().toMap() );

// translate
if ( f.hasGeometry() )
if ( newFeature.hasGeometry() )
{
QgsGeometry geom = f.geometry();
QgsGeometry geom = newFeature.geometry();
geom.translate( dx, dy );
f.setGeometry( geom );
newFeature.setGeometry( geom );
#ifdef QGISDEBUG
const QgsFeatureId fid = f.id();
const QgsFeatureId fid = newFeature.id();
#endif
// paste feature
if ( !layer->addFeature( f ) )
if ( !layer->addFeature( newFeature ) )
{
couldNotWriteCount++;
QgsDebugMsg( QStringLiteral( "Could not add new feature. Original copied feature id: %1" ).arg( fid ) );
}
else
{
fidList.insert( f.id() );
fidList.insert( newFeature.id() );
}
}
else
Expand Down

0 comments on commit 4ac0631

Please sign in to comment.