bool QgsVectorLayerTools::copyMoveFeatures( QgsVectorLayer* layer, QgsFeatureRequest& request, double dx, double dy, QString* errorMsg ) const { bool res = false; if ( !layer || !layer->isEditable() ) { return false; } QgsFeatureIterator fi = layer->getFeatures( request ); QgsFeature f; QgsAttributeList pkAttrList = layer->pkAttributeList(); int browsedFeatureCount = 0; int couldNotWriteCount = 0; int noGeometryCount = 0; QgsFeatureIds fidList; while ( fi.nextFeature( f ) ) { browsedFeatureCount++; // remove pkey values Q_FOREACH ( auto idx, pkAttrList ) { f.setAttribute( idx, QVariant() ); } // translate if ( f.hasGeometry() ) { QgsGeometry geom = f.geometry(); geom.translate( dx, dy ); f.setGeometry( geom ); #ifdef QGISDEBUG const QgsFeatureId fid = f.id(); #endif // paste feature if ( !layer->addFeature( f, false ) ) { couldNotWriteCount++; QgsDebugMsg( QString( "Could not add new feature. Original copied feature id: %1" ).arg( fid ) ); } else { fidList.insert( f.id() ); } } else { noGeometryCount++; } }
int QgsVectorLayerEditUtils::translateFeature( QgsFeatureId featureId, double dx, double dy ) { if ( !mLayer->isSpatial() ) return 1; QgsFeature f; if ( !mLayer->getFeatures( QgsFeatureRequest().setFilterFid( featureId ).setSubsetOfAttributes( QgsAttributeList() ) ).nextFeature( f ) || !f.hasGeometry() ) return 1; //geometry not found QgsGeometry geometry = f.geometry(); int errorCode = geometry.translate( dx, dy ); if ( errorCode == 0 ) { mLayer->editBuffer()->changeGeometry( featureId, geometry ); } return errorCode; }
int QgsVectorLayerEditUtils::translateFeature( QgsFeatureId featureId, double dx, double dy ) { if ( !L->hasGeometryType() ) return 1; QgsGeometry geometry; if ( !cache()->geometry( featureId, geometry ) ) // maybe it's in cache { // it's not in cache: let's fetch it from layer QgsFeature f; if ( !L->getFeatures( QgsFeatureRequest().setFilterFid( featureId ).setSubsetOfAttributes( QgsAttributeList() ) ).nextFeature( f ) || !f.constGeometry() ) return 1; //geometry not found geometry = *f.constGeometry(); } int errorCode = geometry.translate( dx, dy ); if ( errorCode == 0 ) { L->editBuffer()->changeGeometry( featureId, &geometry ); } return errorCode; }