QGIS API Documentation  3.10.0-A Coruña (6c816b4204)
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
6  email : [email protected]
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 
28 bool 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 
49  QgsFeature newFeature = QgsVectorLayerUtils::createFeature( layer, f.geometry(), f.attributes().toMap() );
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  QgsDebugMsg( 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( 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 }
QgsFeatureId id
Definition: qgsfeature.h:64
Wrapper for iterator of features from vector data provider or vector layer.
QSet< QgsFeatureId > QgsFeatureIds
Definition: qgsfeatureid.h:34
#define QgsDebugMsg(str)
Definition: qgslogger.h:38
qint64 QgsFeatureId
Definition: qgsfeatureid.h:25
A geometry is the spatial representation of a feature.
Definition: qgsgeometry.h:122
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Definition: qgsfeature.h:55
bool hasGeometry() const
Returns true if the feature has an associated geometry.
Definition: qgsfeature.cpp:197
bool isEditable() const FINAL
Returns true if the provider is in editing mode.
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.
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.
This class wraps a request for features to a vector layer (or directly its vector data provider)...
QgsAttributeMap toMap() const
Returns a QgsAttributeMap of the attribute values.
int addTopologicalPoints(const QgsGeometry &geom)
Adds topological points for every vertex of the geometry.
QgsFeatureRequest & setFilterFids(const QgsFeatureIds &fids)
Sets feature IDs that should be fetched.
void setGeometry(const QgsGeometry &geometry)
Set the feature&#39;s geometry.
Definition: qgsfeature.cpp:137
bool addFeature(QgsFeature &feature, QgsFeatureSink::Flags flags=nullptr) FINAL
Adds a single feature to the sink.
OperationResult translate(double dx, double dy, double dz=0.0, double dm=0.0)
Translates this geometry by dx, dy, dz and dm.
QgsFeatureIterator getFeatures(const QgsFeatureRequest &request=QgsFeatureRequest()) const FINAL
Queries the layer for features specified in request.
QgsGeometry geometry
Definition: qgsfeature.h:67
bool nextFeature(QgsFeature &f)
Represents a vector layer which manages a vector based data sets.
QgsAttributes attributes
Definition: qgsfeature.h:65