Skip to content

Commit 2f2d6ac

Browse files
committedMar 3, 2014
Merge pull request #1120 from 3nids/pasteconvertgeom
copy/paste: try to convert geometries (fixes #3604)
2 parents bb7cd40 + 9c5c065 commit 2f2d6ac

File tree

5 files changed

+551
-7
lines changed

5 files changed

+551
-7
lines changed
 

‎python/core/qgsgeometry.sip

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,14 @@ class QgsGeometry
356356
*/
357357
QString exportToGeoJSON() const;
358358

359+
/** try to convert the geometry to the requested type
360+
* @param destType the geometry type to be converted to
361+
* @param destMultipart determines if the output geometry will be multipart or not
362+
* @return the converted geometry or NULL pointer if the conversion fails.
363+
* @note added in 2.2
364+
*/
365+
QgsGeometry* convertToType( QGis::GeometryType destType, bool destMultipart = false ) /Factory/;
366+
359367
/* Accessor functions for getting geometry data */
360368

361369
/** return contents of the geometry as a point

‎src/app/qgisapp.cpp

Lines changed: 46 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5580,6 +5580,7 @@ void QgisApp::editPaste( QgsMapLayer *destinationLayer )
55805580
{
55815581
features = clipboard()->copyOf( pasteVectorLayer->pendingFields() );
55825582
}
5583+
int nTotalFeatures = features.count();
55835584

55845585
QHash<int, int> remap;
55855586
const QgsFields &fields = clipboard()->fields();
@@ -5594,10 +5595,11 @@ void QgisApp::editPaste( QgsMapLayer *destinationLayer )
55945595
}
55955596

55965597
int dstAttrCount = pasteVectorLayer->pendingFields().count();
5597-
for ( int i = 0; i < features.size(); i++ )
5598+
5599+
QgsFeatureList::iterator featureIt = features.begin();
5600+
while ( featureIt != features.end() )
55985601
{
5599-
QgsFeature &f = features[i];
5600-
const QgsAttributes &srcAttr = f.attributes();
5602+
const QgsAttributes &srcAttr = featureIt->attributes();
56015603
QgsAttributes dstAttr( dstAttrCount );
56025604

56035605
for ( int src = 0; src < srcAttr.count(); ++src )
@@ -5617,17 +5619,54 @@ void QgisApp::editPaste( QgsMapLayer *destinationLayer )
56175619
dstAttr[ dst ] = srcAttr[ src ];
56185620
}
56195621

5620-
f.setAttributes( dstAttr );
5622+
featureIt->setAttributes( dstAttr );
56215623

5622-
//avoid intersection if enabled in digitize settings
5623-
if ( f.geometry() )
5624+
if ( featureIt->geometry() )
56245625
{
5625-
f.geometry()->avoidIntersections();
5626+
// convert geometry to match destination layer
5627+
QGis::GeometryType destType = pasteVectorLayer->geometryType();
5628+
bool destIsMulti = QGis::isMultiType( pasteVectorLayer->wkbType() );
5629+
if ( destType != QGis::UnknownGeometry )
5630+
{
5631+
QgsGeometry* newGeometry = featureIt->geometry()->convertToType( destType, destIsMulti );
5632+
if ( !newGeometry )
5633+
{
5634+
featureIt = features.erase( featureIt );
5635+
continue;
5636+
}
5637+
featureIt->setGeometry( newGeometry );
5638+
}
5639+
// avoid intersection if enabled in digitize settings
5640+
featureIt->geometry()->avoidIntersections();
56265641
}
5642+
5643+
++featureIt;
56275644
}
56285645

56295646
pasteVectorLayer->addFeatures( features );
56305647
pasteVectorLayer->endEditCommand();
5648+
5649+
int nCopiedFeatures = features.count();
5650+
if ( nCopiedFeatures == 0 )
5651+
{
5652+
messageBar()->pushMessage( tr( "Paste features" ),
5653+
tr( "no features could be successfully pasted." ),
5654+
QgsMessageBar::WARNING , messageTimeout() );
5655+
5656+
}
5657+
else if ( nCopiedFeatures == nTotalFeatures )
5658+
{
5659+
messageBar()->pushMessage( tr( "Paste features" ),
5660+
tr( "%1 features were successfully pasted." ).arg( nCopiedFeatures ),
5661+
QgsMessageBar::INFO , messageTimeout() );
5662+
}
5663+
else
5664+
{
5665+
messageBar()->pushMessage( tr( "Paste features" ),
5666+
tr( "%1 of %2 features could be successfully pasted." ).arg( nCopiedFeatures ).arg( nTotalFeatures ),
5667+
QgsMessageBar::WARNING , messageTimeout() );
5668+
}
5669+
56315670
mMapCanvas->refresh();
56325671
}
56335672

0 commit comments

Comments
 (0)
Please sign in to comment.