Skip to content
Permalink
Browse files

copy/paste: try to convert geometries

  • Loading branch information
3nids committed Feb 28, 2014
1 parent 3a317a0 commit 643e59a23d15172089df573f154f0d9b792e8361
Showing with 403 additions and 7 deletions.
  1. +8 −0 python/core/qgsgeometry.sip
  2. +46 −7 src/app/qgisapp.cpp
  3. +333 −0 src/core/qgsgeometry.cpp
  4. +16 −0 src/core/qgsgeometry.h
@@ -356,6 +356,14 @@ class QgsGeometry
*/
QString exportToGeoJSON() const;

/** try to convert the geometry to the requested type
* @param destType the geometry type to be converted to
* @param destMultipart determines if the output geometry will be multipart or not
* @return the converted geometry or NULL pointer if the conversion fails.
* @note added in 2.2
*/
QgsGeometry* convertToType( QGis::GeometryType destType, bool destMultipart = false ) /Factory/;

/* Accessor functions for getting geometry data */

/** return contents of the geometry as a point
@@ -5580,6 +5580,7 @@ void QgisApp::editPaste( QgsMapLayer *destinationLayer )
{
features = clipboard()->copyOf( pasteVectorLayer->pendingFields() );
}
int nTotalFeatures = features.count();

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

int dstAttrCount = pasteVectorLayer->pendingFields().count();
for ( int i = 0; i < features.size(); i++ )

QgsFeatureList::iterator featureIt = features.begin();
while ( featureIt != features.end() )
{
QgsFeature &f = features[i];
const QgsAttributes &srcAttr = f.attributes();
const QgsAttributes &srcAttr = featureIt->attributes();
QgsAttributes dstAttr( dstAttrCount );

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

f.setAttributes( dstAttr );
featureIt->setAttributes( dstAttr );

//avoid intersection if enabled in digitize settings
if ( f.geometry() )
if ( featureIt->geometry() )
{
// convert geometry to match destination layer
QgsGeometry* newGeometry = featureIt->geometry()->convertToType( pasteVectorLayer->geometryType(), QGis::isMultiType( pasteVectorLayer->wkbType() ) );
if ( newGeometry )
{
// avoid intersection if enabled in digitize settings
featureIt->setGeometry( newGeometry );
featureIt->geometry()->avoidIntersections();
++featureIt;
}
else
{
featureIt = features.erase( featureIt );
}
}
else
{
f.geometry()->avoidIntersections();
++featureIt;
}
}

pasteVectorLayer->addFeatures( features );
pasteVectorLayer->endEditCommand();

int nCopiedFeatures = features.count();
if ( nCopiedFeatures == 0 )
{
messageBar()->pushMessage( tr( "Paste features" ),
tr( "no features could be successfully pasted." ),
QgsMessageBar::WARNING , messageTimeout() );

}
else if ( nCopiedFeatures == nTotalFeatures )
{
messageBar()->pushMessage( tr( "Paste features" ),
tr( "%1 features were successfully pasted." ).arg( nCopiedFeatures ),
QgsMessageBar::INFO , messageTimeout() );
}
else
{
messageBar()->pushMessage( tr( "Paste features" ),
tr( "%1 of %2 features could be successfully pasted." ).arg( nCopiedFeatures ).arg( nTotalFeatures ),
QgsMessageBar::WARNING , messageTimeout() );
}

mMapCanvas->refresh();
}

0 comments on commit 643e59a

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