QGIS API Documentation  3.0.2-Girona (307d082)
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 
22 
24  : QObject( nullptr )
25 {}
26 
27 bool QgsVectorLayerTools::copyMoveFeatures( QgsVectorLayer *layer, QgsFeatureRequest &request, double dx, double dy, QString *errorMsg ) const
28 {
29  bool res = false;
30  if ( !layer || !layer->isEditable() )
31  {
32  return false;
33  }
34 
35  QgsFeatureIterator fi = layer->getFeatures( request );
36  QgsFeature f;
37  QgsAttributeList pkAttrList = layer->primaryKeyAttributes();
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  // remove pkey values
49  Q_FOREACH ( auto idx, pkAttrList )
50  {
51  f.setAttribute( idx, QVariant() );
52  }
53  // translate
54  if ( f.hasGeometry() )
55  {
56  QgsGeometry geom = f.geometry();
57  geom.translate( dx, dy );
58  f.setGeometry( geom );
59 #ifdef QGISDEBUG
60  const QgsFeatureId fid = f.id();
61 #endif
62  // paste feature
63  if ( !layer->addFeature( f ) )
64  {
65  couldNotWriteCount++;
66  QgsDebugMsg( QString( "Could not add new feature. Original copied feature id: %1" ).arg( fid ) );
67  }
68  else
69  {
70  fidList.insert( f.id() );
71  }
72  }
73  else
74  {
75  noGeometryCount++;
76  }
77  }
78 
79  request = QgsFeatureRequest();
80  request.setFilterFids( fidList );
81 
82  if ( !couldNotWriteCount && !noGeometryCount )
83  {
84  res = true;
85  }
86  else if ( errorMsg )
87  {
88  errorMsg = new QString( QString( tr( "Only %1 out of %2 features were copied." ) )
89  .arg( browsedFeatureCount - couldNotWriteCount - noGeometryCount, browsedFeatureCount ) );
90  if ( noGeometryCount )
91  {
92  errorMsg->append( " " );
93  errorMsg->append( tr( "Some features have no geometry." ) );
94  }
95  if ( couldNotWriteCount )
96  {
97  errorMsg->append( " " );
98  errorMsg->append( tr( "Some could not be created on the layer." ) );
99  }
100  }
101  return res;
102 }
QgsFeatureId id
Definition: qgsfeature.h:71
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.
#define QgsDebugMsg(str)
Definition: qgslogger.h:38
QSet< QgsFeatureId > QgsFeatureIds
Definition: qgsfeature.h:544
A geometry is the spatial representation of a feature.
Definition: qgsgeometry.h:111
bool setAttribute(int field, const QVariant &attr)
Set an attribute&#39;s value by field index.
Definition: qgsfeature.cpp:204
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Definition: qgsfeature.h:62
bool hasGeometry() const
Returns true if the feature has an associated geometry.
Definition: qgsfeature.cpp:190
bool isEditable() const override
Returns true if the provider is in editing mode.
This class wraps a request for features to a vector layer (or directly its vector data provider)...
QgsAttributeList primaryKeyAttributes() const
Returns the list of attributes which make up the layer&#39;s primary keys.
QgsGeometry geometry() const
Returns the geometry associated with this feature.
Definition: qgsfeature.cpp:101
QgsFeatureIterator getFeatures(const QgsFeatureRequest &request=QgsFeatureRequest()) const override
Query the layer for features specified in request.
QgsFeatureRequest & setFilterFids(const QgsFeatureIds &fids)
Set feature IDs that should be fetched.
void setGeometry(const QgsGeometry &geometry)
Set the feature&#39;s geometry.
Definition: qgsfeature.cpp:137
OperationResult translate(double dx, double dy, double dz=0.0, double dm=0.0)
Translates this geometry by dx, dy, dz and dm.
qint64 QgsFeatureId
Definition: qgsfeature.h:37
QList< int > QgsAttributeList
Definition: qgsfield.h:27
bool nextFeature(QgsFeature &f)
Represents a vector layer which manages a vector based data sets.
bool addFeature(QgsFeature &feature, QgsFeatureSink::Flags flags=nullptr) override
Adds a single feature to the sink.