Skip to content
Permalink
Browse files

createFeature on move and copy

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

(cherry-picked from 4ac0631)
  • Loading branch information
signedav committed Nov 7, 2018
1 parent bf10953 commit 672b994edfaae36d24c1718df61557189234badf
Showing with 10 additions and 12 deletions.
  1. +10 −12 src/core/qgsvectorlayertools.cpp
@@ -18,6 +18,7 @@
#include "qgsvectorlayertools.h"
#include "qgsfeaturerequest.h"
#include "qgslogger.h"
#include "qgsvectorlayerutils.h"


QgsVectorLayerTools::QgsVectorLayerTools()
@@ -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;
@@ -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

0 comments on commit 672b994

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