QGIS API Documentation  3.6.0-Noosa (5873452)
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
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  }
70  }
71  else
72  {
73  noGeometryCount++;
74  }
75  }
76 
77  request = QgsFeatureRequest();
78  request.setFilterFids( fidList );
79 
80  if ( !couldNotWriteCount && !noGeometryCount )
81  {
82  res = true;
83  }
84  else if ( errorMsg )
85  {
86  errorMsg = new QString( QString( tr( "Only %1 out of %2 features were copied." ) )
87  .arg( browsedFeatureCount - couldNotWriteCount - noGeometryCount, browsedFeatureCount ) );
88  if ( noGeometryCount )
89  {
90  errorMsg->append( " " );
91  errorMsg->append( tr( "Some features have no geometry." ) );
92  }
93  if ( couldNotWriteCount )
94  {
95  errorMsg->append( " " );
96  errorMsg->append( tr( "Some could not be created on the layer." ) );
97  }
98  }
99  return res;
100 }
QgsFeatureId id
Definition: qgsfeature.h:64
Wrapper for iterator of features from vector data provider or vector layer.
virtual bool copyMoveFeatures(QgsVectorLayer *layer, QgsFeatureRequest &request, double dx=0, double dy=0, QString *errorMsg=nullptr) const
Copy and move features with defined translation.
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:106
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.
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.
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
Query 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