Skip to content
Permalink
Browse files

Merge pull request #8436 from signedav/copy_2_pg

Using of QgsVectorLayerUtils::createFeature on move and copy tool
  • Loading branch information
m-kuhn committed Nov 7, 2018
2 parents 4cdde31 + 913719f commit 3e14761d791e9a3976c662f6a67d3e37788c94fc
Showing with 11 additions and 21 deletions.
  1. +10 −12 src/core/qgsvectorlayertools.cpp
  2. +1 −9 src/core/qgsvectorlayerutils.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
@@ -464,15 +464,7 @@ QgsFeature QgsVectorLayerUtils::duplicateFeature( QgsVectorLayer *layer, const Q
QgsExpressionContext context = layer->createExpressionContext();
context.setFeature( feature );

//create the attribute map
QgsAttributes srcAttr = feature.attributes();
QgsAttributeMap dstAttr;
for ( int src = 0; src < srcAttr.count(); ++src )
{
dstAttr[ src ] = srcAttr.at( src );
}

QgsFeature newFeature = createFeature( layer, feature.geometry(), dstAttr, &context );
QgsFeature newFeature = createFeature( layer, feature.geometry(), feature.attributes().toMap(), &context );

const QList<QgsRelation> relations = project->relationManager()->referencedRelations( layer );

0 comments on commit 3e14761

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