QGIS API Documentation 4.0.0-Norrköping (1ddcee3d0e4)
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 "qgsvectorlayertools.h"
18
19#include "qgsfeaturerequest.h"
20#include "qgslogger.h"
21#include "qgsproject.h"
22#include "qgsvectorlayer.h"
23#include "qgsvectorlayerutils.h"
24
25#include <QString>
26
27#include "moc_qgsvectorlayertools.cpp"
28
29using namespace Qt::StringLiterals;
30
32 : QObject( nullptr )
33{}
34
36 QgsVectorLayer *layer, QgsFeatureRequest &request, double dx, double dy, QString *errorMsg, const bool topologicalEditing, QgsVectorLayer *topologicalLayer, QString *childrenInfoMsg
37) const
38{
39 bool res = false;
40 if ( !layer || !layer->isEditable() )
41 {
42 request = QgsFeatureRequest();
43 return false;
44 }
45
46 QgsFeatureIterator fi = layer->getFeatures( request );
47 QgsFeature f;
48
49 int browsedFeatureCount = 0;
50 int couldNotWriteCount = 0;
51 int noGeometryCount = 0;
52
53 QgsFeatureIds fidList;
55 QMap<QString, int> duplicateFeatureCount;
56 while ( fi.nextFeature( f ) )
57 {
58 browsedFeatureCount++;
59
60 if ( f.hasGeometry() )
61 {
62 QgsGeometry geom = f.geometry();
63 geom.translate( dx, dy );
64 f.setGeometry( geom );
65 }
66
67 QgsFeature newFeature;
68 if ( mProject )
69 {
70 newFeature = QgsVectorLayerUtils::duplicateFeature( layer, f, mProject, duplicateFeatureContext );
71 if ( !newFeature.isValid() )
72 {
73 couldNotWriteCount++;
74 QgsDebugError( u"Could not add new feature. Original copied feature id: %1"_s.arg( f.id() ) );
75 }
76 else
77 {
78 fidList.insert( newFeature.id() );
79 }
80
81 const auto duplicateFeatureContextLayers = duplicateFeatureContext.layers();
82 for ( QgsVectorLayer *chl : duplicateFeatureContextLayers )
83 {
84 if ( duplicateFeatureCount.contains( chl->name() ) )
85 {
86 duplicateFeatureCount[chl->name()] += duplicateFeatureContext.duplicatedFeatures( chl ).size();
87 }
88 else
89 {
90 duplicateFeatureCount[chl->name()] = duplicateFeatureContext.duplicatedFeatures( chl ).size();
91 }
92 }
93 }
94 else
95 {
96 newFeature = QgsVectorLayerUtils::createFeature( layer, f.geometry(), f.attributes().toMap() );
97 if ( !layer->addFeature( newFeature ) )
98 {
99 couldNotWriteCount++;
100 QgsDebugError( u"Could not add new feature. Original copied feature id: %1"_s.arg( f.id() ) );
101 }
102 else
103 {
104 fidList.insert( newFeature.id() );
105 }
106 }
107
108 // translate
109 if ( newFeature.hasGeometry() )
110 {
111 QgsGeometry geom = newFeature.geometry();
112 if ( topologicalEditing )
113 {
114 if ( topologicalLayer )
115 {
116 topologicalLayer->addTopologicalPoints( geom );
117 }
118 layer->addTopologicalPoints( geom );
119 }
120 }
121 else
122 {
123 noGeometryCount++;
124 }
125 }
126
127 QString childrenInfo;
128 for ( auto it = duplicateFeatureCount.constBegin(); it != duplicateFeatureCount.constEnd(); ++it )
129 {
130 childrenInfo += ( tr( "\n%n children on layer %1 duplicated", nullptr, it.value() ).arg( it.key() ) );
131 }
132
133 request = QgsFeatureRequest();
134 request.setFilterFids( fidList );
135
136 if ( childrenInfoMsg && !childrenInfo.isEmpty() )
137 {
138 childrenInfoMsg->append( childrenInfo );
139 }
140
141 if ( !couldNotWriteCount && !noGeometryCount )
142 {
143 res = true;
144 }
145 else if ( errorMsg )
146 {
147 errorMsg = new QString( tr( "Only %1 out of %2 features were copied." ).arg( browsedFeatureCount - couldNotWriteCount - noGeometryCount, browsedFeatureCount ) );
148 if ( noGeometryCount )
149 {
150 errorMsg->append( " " );
151 errorMsg->append( tr( "Some features have no geometry." ) );
152 }
153 if ( couldNotWriteCount )
154 {
155 errorMsg->append( " " );
156 errorMsg->append( tr( "Some could not be created on the layer." ) );
157 }
158 }
159 return res;
160}
161
163{
164 return mForceSuppressFormPopup;
165}
166
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.
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:60
QgsAttributes attributes
Definition qgsfeature.h:69
QgsFeatureId id
Definition qgsfeature.h:68
QgsGeometry geometry
Definition qgsfeature.h:71
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 which 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 dataset.
bool isEditable() const final
Returns true if the provider is in editing mode.
QgsFeatureIterator getFeatures(const QgsFeatureRequest &request=QgsFeatureRequest()) const final
Queries the layer for features specified in request.
int addTopologicalPoints(const QgsGeometry &geom)
Adds topological points for every vertex of the geometry.
bool addFeature(QgsFeature &feature, QgsFeatureSink::Flags flags=QgsFeatureSink::Flags()) final
Adds a single feature to the sink.
QSet< QgsFeatureId > QgsFeatureIds
#define QgsDebugError(str)
Definition qgslogger.h:59