QGIS API Documentation 3.41.0-Master (fda2aa46e9a)
Loading...
Searching...
No Matches
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 "moc_qgsvectorlayertools.cpp"
20#include "qgsfeaturerequest.h"
21#include "qgslogger.h"
22#include "qgsvectorlayerutils.h"
23#include "qgsproject.h"
24
25
27 : QObject( nullptr )
28{}
29
30bool QgsVectorLayerTools::copyMoveFeatures( QgsVectorLayer *layer, QgsFeatureRequest &request, double dx, double dy, QString *errorMsg, const bool topologicalEditing, QgsVectorLayer *topologicalLayer, QString *childrenInfoMsg ) const
31{
32 bool res = false;
33 if ( !layer || !layer->isEditable() )
34 {
35 return false;
36 }
37
38 QgsFeatureIterator fi = layer->getFeatures( request );
39 QgsFeature f;
40
41 int browsedFeatureCount = 0;
42 int couldNotWriteCount = 0;
43 int noGeometryCount = 0;
44
45 QgsFeatureIds fidList;
47 QMap<QString, int> duplicateFeatureCount;
48 while ( fi.nextFeature( f ) )
49 {
50 browsedFeatureCount++;
51
52 if ( f.hasGeometry() )
53 {
54 QgsGeometry geom = f.geometry();
55 geom.translate( dx, dy );
56 f.setGeometry( geom );
57 }
58
59 QgsFeature newFeature;
60 if ( mProject )
61 {
62 newFeature = QgsVectorLayerUtils::duplicateFeature( layer, f, mProject, duplicateFeatureContext );
63 if ( !newFeature.isValid() )
64 {
65 couldNotWriteCount++;
66 QgsDebugError( QStringLiteral( "Could not add new feature. Original copied feature id: %1" ).arg( f.id() ) );
67 }
68 else
69 {
70 fidList.insert( newFeature.id() );
71 }
72
73 const auto duplicateFeatureContextLayers = duplicateFeatureContext.layers();
74 for ( QgsVectorLayer *chl : duplicateFeatureContextLayers )
75 {
76 if ( duplicateFeatureCount.contains( chl->name() ) )
77 {
78 duplicateFeatureCount[chl->name()] += duplicateFeatureContext.duplicatedFeatures( chl ).size();
79 }
80 else
81 {
82 duplicateFeatureCount[chl->name()] = duplicateFeatureContext.duplicatedFeatures( chl ).size();
83 }
84 }
85 }
86 else
87 {
88 newFeature = QgsVectorLayerUtils::createFeature( layer, f.geometry(), f.attributes().toMap() );
89 if ( !layer->addFeature( newFeature ) )
90 {
91 couldNotWriteCount++;
92 QgsDebugError( QStringLiteral( "Could not add new feature. Original copied feature id: %1" ).arg( f.id() ) );
93 }
94 else
95 {
96 fidList.insert( newFeature.id() );
97 }
98 }
99
100 // translate
101 if ( newFeature.hasGeometry() )
102 {
103 QgsGeometry geom = newFeature.geometry();
104 if ( topologicalEditing )
105 {
106 if ( topologicalLayer )
107 {
108 topologicalLayer->addTopologicalPoints( geom );
109 }
110 layer->addTopologicalPoints( geom );
111 }
112 }
113 else
114 {
115 noGeometryCount++;
116 }
117 }
118
119 QString childrenInfo;
120 for ( auto it = duplicateFeatureCount.constBegin(); it != duplicateFeatureCount.constEnd(); ++it )
121 {
122 childrenInfo += ( tr( "\n%n children on layer %1 duplicated", nullptr, it.value() ).arg( it.key() ) );
123 }
124
125 request = QgsFeatureRequest();
126 request.setFilterFids( fidList );
127
128 if ( childrenInfoMsg && !childrenInfo.isEmpty() )
129 {
130 childrenInfoMsg->append( childrenInfo );
131 }
132
133 if ( !couldNotWriteCount && !noGeometryCount )
134 {
135 res = true;
136 }
137 else if ( errorMsg )
138 {
139 errorMsg = new QString( tr( "Only %1 out of %2 features were copied." )
140 .arg( browsedFeatureCount - couldNotWriteCount - noGeometryCount, browsedFeatureCount ) );
141 if ( noGeometryCount )
142 {
143 errorMsg->append( " " );
144 errorMsg->append( tr( "Some features have no geometry." ) );
145 }
146 if ( couldNotWriteCount )
147 {
148 errorMsg->append( " " );
149 errorMsg->append( tr( "Some could not be created on the layer." ) );
150 }
151 }
152 return res;
153}
154
156{
157 return mForceSuppressFormPopup;
158}
159
160void QgsVectorLayerTools::setForceSuppressFormPopup( bool forceSuppressFormPopup )
161{
162 mForceSuppressFormPopup = forceSuppressFormPopup;
163}
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)
Fetch next feature and stores in f, returns true on success.
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:58
QgsAttributes attributes
Definition qgsfeature.h:67
QgsFeatureId id
Definition qgsfeature.h:66
QgsGeometry geometry
Definition qgsfeature.h:69
bool hasGeometry() const
Returns true if the feature has an associated geometry.
bool isValid() const
Returns the validity of this feature.
void setGeometry(const QgsGeometry &geometry)
Set the feature's geometry.
A geometry is the spatial representation of a feature.
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, QString *childrenInfoMsg=nullptr) const
Copy and move features with defined translation.
void setForceSuppressFormPopup(bool forceSuppressFormPopup)
Sets force suppress form popup status to forceSuppressFormPopup.
Contains mainly the QMap with QgsVectorLayer and QgsFeatureIds do list all the duplicated features.
QgsFeatureIds duplicatedFeatures(QgsVectorLayer *layer) const
Returns the duplicated features in the given layer.
QList< QgsVectorLayer * > layers() const
Returns all the layers on which features have been duplicated.
static QgsFeature duplicateFeature(QgsVectorLayer *layer, const QgsFeature &feature, QgsProject *project, QgsDuplicateFeatureContext &duplicateFeatureContext, const int maxDepth=0, int depth=0, QList< QgsVectorLayer * > referencedLayersBranch=QList< QgsVectorLayer * >())
Duplicates a feature and it's children (one level deep).
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
#define QgsDebugError(str)
Definition qgslogger.h:38