QGIS API Documentation  3.26.3-Buenos Aires (65e4edfdad)
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( 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 }
109 
111 {
112  return mForceSuppressFormPopup;
113 }
114 
115 void QgsVectorLayerTools::setForceSuppressFormPopup( bool forceSuppressFormPopup )
116 {
117  mForceSuppressFormPopup = forceSuppressFormPopup;
118 }
QgsVectorLayer::getFeatures
QgsFeatureIterator getFeatures(const QgsFeatureRequest &request=QgsFeatureRequest()) const FINAL
Queries the layer for features specified in request.
Definition: qgsvectorlayer.cpp:1052
qgsfeaturerequest.h
QgsVectorLayerTools::forceSuppressFormPopup
bool forceSuppressFormPopup() const
Returns force suppress form popup status.
Definition: qgsvectorlayertools.cpp:110
QgsFeature::geometry
QgsGeometry geometry
Definition: qgsfeature.h:71
QgsVectorLayerTools::copyMoveFeatures
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.
Definition: qgsvectorlayertools.cpp:28
QgsVectorLayerTools::setForceSuppressFormPopup
void setForceSuppressFormPopup(bool forceSuppressFormPopup)
Sets force suppress form popup status to forceSuppressFormPopup.
Definition: qgsvectorlayertools.cpp:115
QgsDebugMsg
#define QgsDebugMsg(str)
Definition: qgslogger.h:38
QgsVectorLayer::isEditable
bool isEditable() const FINAL
Returns true if the provider is in editing mode.
Definition: qgsvectorlayer.cpp:3728
QgsFeature::id
QgsFeatureId id
Definition: qgsfeature.h:68
QgsFeatureRequest
This class wraps a request for features to a vector layer (or directly its vector data provider).
Definition: qgsfeaturerequest.h:83
qgsvectorlayertools.h
QgsVectorLayer::addTopologicalPoints
int addTopologicalPoints(const QgsGeometry &geom)
Adds topological points for every vertex of the geometry.
Definition: qgsvectorlayer.cpp:1450
QgsFeature::setGeometry
void setGeometry(const QgsGeometry &geometry)
Set the feature's geometry.
Definition: qgsfeature.cpp:170
qgsvectorlayerutils.h
QgsFeatureRequest::setFilterFids
QgsFeatureRequest & setFilterFids(const QgsFeatureIds &fids)
Sets the feature IDs that should be fetched.
Definition: qgsfeaturerequest.cpp:148
QgsAttributes::toMap
CORE_EXPORT QgsAttributeMap toMap() const
Returns a QgsAttributeMap of the attribute values.
Definition: qgsattributes.cpp:21
QgsFeatureIds
QSet< QgsFeatureId > QgsFeatureIds
Definition: qgsfeatureid.h:37
QgsGeometry::translate
Qgis::GeometryOperationResult translate(double dx, double dy, double dz=0.0, double dm=0.0)
Translates this geometry by dx, dy, dz and dm.
Definition: qgsgeometry.cpp:897
QgsFeature::attributes
QgsAttributes attributes
Definition: qgsfeature.h:69
QgsVectorLayerUtils::createFeature
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.
Definition: qgsvectorlayerutils.cpp:478
qgsvectorlayer.h
QgsFeatureIterator::nextFeature
bool nextFeature(QgsFeature &f)
Definition: qgsfeatureiterator.h:399
QgsVectorLayerTools::QgsVectorLayerTools
QgsVectorLayerTools()
Definition: qgsvectorlayertools.cpp:24
QgsGeometry
A geometry is the spatial representation of a feature.
Definition: qgsgeometry.h:124
QgsVectorLayer
Represents a vector layer which manages a vector based data sets.
Definition: qgsvectorlayer.h:391
QgsFeature::hasGeometry
bool hasGeometry() const
Returns true if the feature has an associated geometry.
Definition: qgsfeature.cpp:230
QgsFeature
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Definition: qgsfeature.h:55
qgslogger.h
QgsFeatureIterator
Wrapper for iterator of features from vector data provider or vector layer.
Definition: qgsfeatureiterator.h:289
QgsVectorLayer::addFeature
bool addFeature(QgsFeature &feature, QgsFeatureSink::Flags flags=QgsFeatureSink::Flags()) FINAL
Adds a single feature to the sink.
Definition: qgsvectorlayer.cpp:1070
QgsFeatureId
qint64 QgsFeatureId
64 bit feature ids negative numbers are used for uncommitted/newly added features
Definition: qgsfeatureid.h:28