QGIS API Documentation 3.32.0-Lima (311a8cb8a6)
qgsvectorlayertools.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsvectorlayertools.cpp
3 ---------------------
4 begin : 09.11.2016
5 copyright : (C) 2016 by Denis Rouzaud
7 ***************************************************************************
8 * *
9 * This program is free software; you can redistribute it and/or modify *
10 * it under the terms of the GNU General Public License as published by *
11 * the Free Software Foundation; either version 2 of the License, or *
12 * (at your option) any later version. *
13 * *
14 ***************************************************************************/
15
16
17#include "qgsvectorlayer.h"
18#include "qgsvectorlayertools.h"
19#include "qgsfeaturerequest.h"
20#include "qgslogger.h"
21#include "qgsvectorlayerutils.h"
22
23
25 : QObject( nullptr )
26{}
27
28bool QgsVectorLayerTools::copyMoveFeatures( QgsVectorLayer *layer, QgsFeatureRequest &request, double dx, double dy, QString *errorMsg, const bool topologicalEditing, QgsVectorLayer *topologicalLayer ) const
29{
30 bool res = false;
31 if ( !layer || !layer->isEditable() )
32 {
33 return false;
34 }
35
36 QgsFeatureIterator fi = layer->getFeatures( request );
37 QgsFeature f;
38
39 int browsedFeatureCount = 0;
40 int couldNotWriteCount = 0;
41 int noGeometryCount = 0;
42
43 QgsFeatureIds fidList;
44
45 while ( fi.nextFeature( f ) )
46 {
47 browsedFeatureCount++;
48
50
51 // translate
52 if ( newFeature.hasGeometry() )
53 {
54 QgsGeometry geom = newFeature.geometry();
55 geom.translate( dx, dy );
56 newFeature.setGeometry( geom );
57#ifdef QGISDEBUG
58 const QgsFeatureId fid = newFeature.id();
59#endif
60 // paste feature
61 if ( !layer->addFeature( newFeature ) )
62 {
63 couldNotWriteCount++;
64 QgsDebugError( QStringLiteral( "Could not add new feature. Original copied feature id: %1" ).arg( fid ) );
65 }
66 else
67 {
68 fidList.insert( newFeature.id() );
69 if ( topologicalEditing )
70 {
71 if ( topologicalLayer )
72 {
73 topologicalLayer->addTopologicalPoints( geom );
74 }
75 layer->addTopologicalPoints( geom );
76 }
77 }
78 }
79 else
80 {
81 noGeometryCount++;
82 }
83 }
84
85 request = QgsFeatureRequest();
86 request.setFilterFids( fidList );
87
88 if ( !couldNotWriteCount && !noGeometryCount )
89 {
90 res = true;
91 }
92 else if ( errorMsg )
93 {
94 errorMsg = new QString( tr( "Only %1 out of %2 features were copied." )
95 .arg( browsedFeatureCount - couldNotWriteCount - noGeometryCount, browsedFeatureCount ) );
96 if ( noGeometryCount )
97 {
98 errorMsg->append( " " );
99 errorMsg->append( tr( "Some features have no geometry." ) );
100 }
101 if ( couldNotWriteCount )
102 {
103 errorMsg->append( " " );
104 errorMsg->append( tr( "Some could not be created on the layer." ) );
105 }
106 }
107 return res;
108}
109
111{
112 return mForceSuppressFormPopup;
113}
114
115void QgsVectorLayerTools::setForceSuppressFormPopup( bool forceSuppressFormPopup )
116{
117 mForceSuppressFormPopup = forceSuppressFormPopup;
118}
CORE_EXPORT QgsAttributeMap toMap() const
Returns a QgsAttributeMap of the attribute values.
Wrapper for iterator of features from vector data provider or vector layer.
bool nextFeature(QgsFeature &f)
This class wraps a request for features to a vector layer (or directly its vector data provider).
QgsFeatureRequest & setFilterFids(const QgsFeatureIds &fids)
Sets the feature IDs that should be fetched.
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Definition: qgsfeature.h:56
QgsAttributes attributes
Definition: qgsfeature.h:65
QgsGeometry geometry
Definition: qgsfeature.h:67
bool hasGeometry() const
Returns true if the feature has an associated geometry.
Definition: qgsfeature.cpp:230
void setGeometry(const QgsGeometry &geometry)
Set the feature's geometry.
Definition: qgsfeature.cpp:167
Q_GADGET QgsFeatureId id
Definition: qgsfeature.h:64
A geometry is the spatial representation of a feature.
Definition: qgsgeometry.h:164
Qgis::GeometryOperationResult translate(double dx, double dy, double dz=0.0, double dm=0.0)
Translates this geometry by dx, dy, dz and dm.
bool forceSuppressFormPopup() const
Returns force suppress form popup status.
virtual bool copyMoveFeatures(QgsVectorLayer *layer, QgsFeatureRequest &request, double dx=0, double dy=0, QString *errorMsg=nullptr, const bool topologicalEditing=false, QgsVectorLayer *topologicalLayer=nullptr) const
Copy and move features with defined translation.
void setForceSuppressFormPopup(bool forceSuppressFormPopup)
Sets force suppress form popup status to forceSuppressFormPopup.
static QgsFeature createFeature(const QgsVectorLayer *layer, const QgsGeometry &geometry=QgsGeometry(), const QgsAttributeMap &attributes=QgsAttributeMap(), QgsExpressionContext *context=nullptr)
Creates a new feature ready for insertion into a layer.
Represents a vector layer which manages a vector based data sets.
QgsFeatureIterator getFeatures(const QgsFeatureRequest &request=QgsFeatureRequest()) const FINAL
Queries the layer for features specified in request.
bool isEditable() const FINAL
Returns true if the provider is in editing mode.
bool addFeature(QgsFeature &feature, QgsFeatureSink::Flags flags=QgsFeatureSink::Flags()) FINAL
Adds a single feature to the sink.
int addTopologicalPoints(const QgsGeometry &geom)
Adds topological points for every vertex of the geometry.
QSet< QgsFeatureId > QgsFeatureIds
Definition: qgsfeatureid.h:37
qint64 QgsFeatureId
64 bit feature ids negative numbers are used for uncommitted/newly added features
Definition: qgsfeatureid.h:28
#define QgsDebugError(str)
Definition: qgslogger.h:38