Skip to content

Commit dbfbcca

Browse files
committedOct 6, 2018
Simplify paste by using QgsVectorLayerUtils::makeFeaturesCompatible
1 parent a3bf98d commit dbfbcca

File tree

1 file changed

+16
-51
lines changed

1 file changed

+16
-51
lines changed
 

‎src/app/qgisapp.cpp

Lines changed: 16 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -8597,70 +8597,35 @@ void QgisApp::pasteFromClipboard( QgsMapLayer *destinationLayer )
85978597
pasteVectorLayer->beginEditCommand( tr( "Features pasted" ) );
85988598
QgsFeatureList features = clipboard()->transformedCopyOf( pasteVectorLayer->crs(), pasteVectorLayer->fields() );
85998599
int nTotalFeatures = features.count();
8600-
8601-
QHash<int, int> remap;
8602-
QgsFields fields = clipboard()->fields();
8603-
for ( int idx = 0; idx < fields.count(); ++idx )
8604-
{
8605-
int dst = pasteVectorLayer->fields().lookupField( fields.at( idx ).name() );
8606-
if ( dst < 0 )
8607-
continue;
8608-
8609-
remap.insert( idx, dst );
8610-
}
8611-
86128600
QgsExpressionContext context = pasteVectorLayer->createExpressionContext();
86138601

8614-
int invalidGeometriesCount = 0;
8602+
QgsFeatureList compatibleFeatures( QgsVectorLayerUtils::makeFeaturesCompatible( features, pasteVectorLayer ) );
86158603
QgsFeatureList newFeatures;
8616-
QgsFeatureList::const_iterator featureIt = features.constBegin();
8617-
while ( featureIt != features.constEnd() )
8604+
// Count collapsed geometries
8605+
int invalidGeometriesCount = 0;
8606+
8607+
for ( const auto &feature : qgis::as_const( compatibleFeatures ) )
86188608
{
8619-
QgsAttributes srcAttr = featureIt->attributes();
8620-
QgsAttributeMap dstAttr;
86218609

8622-
for ( int src = 0; src < srcAttr.count(); ++src )
8623-
{
8624-
int dst = remap.value( src, -1 );
8625-
if ( dst < 0 )
8626-
continue;
8610+
QgsGeometry geom = feature.geometry();
86278611

8628-
dstAttr[ dst ] = srcAttr.at( src );
8629-
}
8630-
8631-
QgsGeometry geom = featureIt->geometry();
8632-
bool geomWasInvalid = geom.isEmpty() || geom.isNull( );
8633-
if ( featureIt->hasGeometry() )
8612+
if ( !( geom.isEmpty() || geom.isNull( ) ) )
86348613
{
8635-
// convert geometry to match destination layer
8636-
QgsWkbTypes::GeometryType destType = pasteVectorLayer->geometryType();
8637-
bool destIsMulti = QgsWkbTypes::isMultiType( pasteVectorLayer->wkbType() );
8638-
if ( pasteVectorLayer->dataProvider() &&
8639-
!pasteVectorLayer->dataProvider()->doesStrictFeatureTypeCheck() )
8640-
{
8641-
// force destination to multi if provider doesn't do a feature strict check
8642-
destIsMulti = true;
8643-
}
8644-
8645-
if ( destType != QgsWkbTypes::UnknownGeometry )
8646-
{
8647-
QgsGeometry newGeometry = geom.convertToType( destType, destIsMulti );
8648-
if ( newGeometry.isNull() )
8649-
{
8650-
continue;
8651-
}
8652-
geom = newGeometry;
8653-
}
86548614
// avoid intersection if enabled in digitize settings
86558615
geom.avoidIntersections( QgsProject::instance()->avoidIntersectionsLayers() );
8616+
// Count collapsed geometries
8617+
if ( geom.isEmpty() || geom.isNull( ) )
8618+
invalidGeometriesCount++;
86568619
}
8657-
if ( ! geomWasInvalid && ( geom.isEmpty() || geom.isNull( ) ) )
8658-
invalidGeometriesCount++;
86598620

8621+
QgsAttributeMap attrMap;
8622+
for ( int i = 0; i < feature.fields().count(); i++ )
8623+
{
8624+
attrMap[i] = feature.attribute( i );
8625+
}
86608626
// now create new feature using pasted feature as a template. This automatically handles default
86618627
// values and field constraints
8662-
newFeatures << QgsVectorLayerUtils::createFeature( pasteVectorLayer, geom, dstAttr, &context );
8663-
++featureIt;
8628+
newFeatures << QgsVectorLayerUtils::createFeature( pasteVectorLayer, geom, attrMap, &context );
86648629
}
86658630
pasteVectorLayer->addFeatures( newFeatures );
86668631
QgsFeatureIds newIds;

0 commit comments

Comments
 (0)
Please sign in to comment.