QGIS API Documentation  2.12.0-Lyon
qgsgeometryeditutils.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsgeometryeditutils.cpp
3  -------------------------------------------------------------------
4 Date : 21 Jan 2015
5 Copyright : (C) 2015 by Marco Hugentobler
6 email : marco.hugentobler at sourcepole dot com
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 #include "qgsgeometryeditutils.h"
17 #include "qgscurvev2.h"
18 #include "qgscurvepolygonv2.h"
19 #include "qgspolygonv2.h"
20 #include "qgsgeometryutils.h"
21 #include "qgsgeometry.h"
22 #include "qgsgeos.h"
23 #include "qgsmaplayerregistry.h"
24 #include "qgsmultisurfacev2.h"
25 #include "qgsproject.h"
26 #include "qgsvectorlayer.h"
27 #include <limits>
28 
30 {
31  if ( !ring )
32  {
33  return 1;
34  }
35 
36  QList< QgsCurvePolygonV2* > polygonList;
37  QgsCurvePolygonV2* curvePoly = dynamic_cast< QgsCurvePolygonV2* >( geom );
38  QgsGeometryCollectionV2* multiGeom = dynamic_cast< QgsGeometryCollectionV2* >( geom );
39  if ( curvePoly )
40  {
41  polygonList.append( curvePoly );
42  }
43  else if ( multiGeom )
44  {
45  polygonList.reserve( multiGeom->numGeometries() );
46  for ( int i = 0; i < multiGeom->numGeometries(); ++i )
47  {
48  polygonList.append( dynamic_cast< QgsCurvePolygonV2* >( multiGeom->geometryN( i ) ) );
49  }
50  }
51  else
52  {
53  delete ring; return 1; //not polygon / multipolygon;
54  }
55 
56  //ring must be closed
57  if ( !ring->isClosed() )
58  {
59  delete ring; return 2;
60  }
61  else if ( !ring->isRing() )
62  {
63  delete ring; return 3;
64  }
65 
67  ringGeom->prepareGeometry();
68 
69  //for each polygon, test if inside outer ring and no intersection with other interior ring
70  QList< QgsCurvePolygonV2* >::iterator polyIter = polygonList.begin();
71  for ( ; polyIter != polygonList.end(); ++polyIter )
72  {
73  if ( ringGeom->within( **polyIter ) )
74  {
75  //check if disjoint with other interior rings
76  int nInnerRings = ( *polyIter )->numInteriorRings();
77  for ( int i = 0; i < nInnerRings; ++i )
78  {
79  if ( !ringGeom->disjoint( *( *polyIter )->interiorRing( i ) ) )
80  {
81  delete ring; return 4;
82  }
83  }
84 
85  //make sure dimensionality of ring matches geometry
86  if ( QgsWKBTypes::hasZ( geom->wkbType() ) )
87  ring->addZValue( 0 );
88  if ( QgsWKBTypes::hasM( geom->wkbType() ) )
89  ring->addMValue( 0 );
90 
91  ( *polyIter )->addInteriorRing( ring );
92  return 0; //success
93  }
94  }
95  delete ring; return 5; //not contained in any outer ring
96 }
97 
99 {
100  if ( !geom )
101  {
102  return 1;
103  }
104 
105  if ( !part )
106  {
107  return 2;
108  }
109 
110  //multitype?
111  QgsGeometryCollectionV2* geomCollection = dynamic_cast<QgsGeometryCollectionV2*>( geom );
112  if ( !geomCollection )
113  {
114  return 1;
115  }
116 
117  bool added = false;
118  if ( geom->geometryType() == "MultiSurface" || geom->geometryType() == "MultiPolygon" )
119  {
120  QgsCurveV2* curve = dynamic_cast<QgsCurveV2*>( part );
121  if ( curve && curve->isClosed() && curve->numPoints() >= 4 )
122  {
123  QgsCurvePolygonV2 *poly = 0;
124  if ( curve->geometryType() == "LineString" )
125  {
126  poly = new QgsPolygonV2();
127  }
128  else
129  {
130  poly = new QgsCurvePolygonV2();
131  }
132  poly->setExteriorRing( curve );
133  added = geomCollection->addGeometry( poly );
134  }
135  else if ( part->geometryType() == "Polygon" )
136  {
137  added = geomCollection->addGeometry( part );
138  }
139  else if ( part->geometryType() == "MultiPolygon" )
140  {
141  QgsGeometryCollectionV2 *parts = static_cast<QgsGeometryCollectionV2*>( part );
142 
143  int i;
144  int n = geomCollection->numGeometries();
145  for ( i = 0; i < parts->numGeometries() && geomCollection->addGeometry( parts->geometryN( i )->clone() ); i++ )
146  ;
147 
148  added = i == parts->numGeometries();
149  if ( !added )
150  {
151  while ( geomCollection->numGeometries() > n )
152  geomCollection->removeGeometry( n );
153  delete part; return 2;
154  }
155 
156  delete part;
157  }
158  else
159  {
160  delete part; return 2;
161  }
162  }
163  else
164  {
165  added = geomCollection->addGeometry( part );
166  }
167  return added ? 0 : 2;
168 }
169 
170 bool QgsGeometryEditUtils::deleteRing( QgsAbstractGeometryV2* geom, int ringNum, int partNum )
171 {
172  if ( !geom || partNum < 0 )
173  {
174  return false;
175  }
176 
177  if ( ringNum < 1 ) //cannot remove exterior ring
178  {
179  return false;
180  }
181 
182  QgsAbstractGeometryV2* g = geom;
183  QgsGeometryCollectionV2* c = dynamic_cast<QgsGeometryCollectionV2*>( geom );
184  if ( c )
185  {
186  g = c->geometryN( partNum );
187  }
188  else if ( partNum > 0 )
189  {
190  //part num specified, but not a multi part geometry type
191  return false;
192  }
193 
194  QgsCurvePolygonV2* cpoly = dynamic_cast<QgsCurvePolygonV2*>( g );
195  if ( !cpoly )
196  {
197  return false;
198  }
199 
200  return cpoly->removeInteriorRing( ringNum - 1 );
201 }
202 
204 {
205  if ( !geom )
206  {
207  return false;
208  }
209 
210  QgsGeometryCollectionV2* c = dynamic_cast<QgsGeometryCollectionV2*>( geom );
211  if ( !c )
212  {
213  return false;
214  }
215 
216  return c->removeGeometry( partNum );
217 }
218 
220 {
222  if ( geomEngine.isNull() )
223  {
224  return 0;
225  }
226  QgsWKBTypes::Type geomTypeBeforeModification = geom.wkbType();
227 
228 
229  //check if g has polygon type
230  if ( QgsWKBTypes::geometryType( geomTypeBeforeModification ) != QgsWKBTypes::PolygonGeometry )
231  {
232  return 0;
233  }
234 
235  //read avoid intersections list from project properties
236  bool listReadOk;
237  QStringList avoidIntersectionsList = QgsProject::instance()->readListEntry( "Digitizing", "/AvoidIntersectionsList", QStringList(), &listReadOk );
238  if ( !listReadOk )
239  return 0; //no intersections stored in project does not mean error
240 
242 
243  //go through list, convert each layer to vector layer and call QgsVectorLayer::removePolygonIntersections for each
244  QgsVectorLayer* currentLayer = 0;
245  QStringList::const_iterator aIt = avoidIntersectionsList.constBegin();
246  for ( ; aIt != avoidIntersectionsList.constEnd(); ++aIt )
247  {
248  currentLayer = dynamic_cast<QgsVectorLayer*>( QgsMapLayerRegistry::instance()->mapLayer( *aIt ) );
249  if ( currentLayer )
250  {
251  QgsFeatureIds ignoreIds;
252  QMap<QgsVectorLayer*, QSet<qint64> >::const_iterator ignoreIt = ignoreFeatures.find( currentLayer );
253  if ( ignoreIt != ignoreFeatures.constEnd() )
254  ignoreIds = ignoreIt.value();
255 
256  QgsFeatureIterator fi = currentLayer->getFeatures( QgsFeatureRequest( geom.boundingBox() )
259  QgsFeature f;
260  while ( fi.nextFeature( f ) )
261  {
262  if ( ignoreIds.contains( f.id() ) )
263  continue;
264 
265  if ( !f.geometry() )
266  continue;
267 
268  nearGeometries << f.geometry()->geometry()->clone();
269  }
270  }
271  }
272 
273  if ( nearGeometries.isEmpty() )
274  {
275  return 0;
276  }
277 
278 
279  QgsAbstractGeometryV2* combinedGeometries = geomEngine.data()->combine( nearGeometries );
280  qDeleteAll( nearGeometries );
281  if ( !combinedGeometries )
282  {
283  return 0;
284  }
285 
286  QgsAbstractGeometryV2* diffGeom = geomEngine.data()->difference( *combinedGeometries );
287 
288  delete combinedGeometries;
289  return diffGeom;
290 }
QgsFeatureId id() const
Get the feature ID for this feature.
Definition: qgsfeature.cpp:53
bool removeInteriorRing(int nr)
Removes ring.
Wrapper for iterator of features from vector data provider or vector layer.
QgsWKBTypes::Type wkbType() const
Returns the WKB type of the geometry.
Use exact geometry intersection (slower) instead of bounding boxes.
QgsAbstractGeometryV2 * geometry() const
Returns the underlying geometry store.
void reserve(int alloc)
QgsFeatureIterator getFeatures(const QgsFeatureRequest &request=QgsFeatureRequest())
Query the provider for features specified in request.
virtual bool isRing() const
Returns true if the curve is a ring.
Definition: qgscurvev2.cpp:40
QgsFeatureRequest & setSubsetOfAttributes(const QgsAttributeList &attrs)
Set a subset of attributes that will be fetched.
Abstract base class for all geometries.
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Definition: qgsfeature.h:176
static int addPart(QgsAbstractGeometryV2 *geom, QgsAbstractGeometryV2 *part)
Adds part to multi type geometry (taking ownership)
QgsMapLayer * mapLayer(const QString &theLayerId)
Retrieve a pointer to a loaded layer by id.
virtual bool addMValue(double mValue=0)=0
Adds a measure to the geometry, initialized to a preset value.
virtual int numPoints() const =0
Returns the number of points in the curve.
Polygon geometry type.
Definition: qgspolygonv2.h:29
void append(const T &value)
static bool hasM(Type type)
Tests whether a WKB type contains m values.
static GeometryType geometryType(Type type)
Definition: qgswkbtypes.cpp:99
bool isEmpty() const
const_iterator constEnd() const
This class wraps a request for features to a vector layer (or directly its vector data provider)...
static bool hasZ(Type type)
Tests whether a WKB type contains the z-dimension.
QList< int > QgsAttributeList
virtual bool addZValue(double zValue=0)=0
Adds a z-dimension to the geometry, initialized to a preset value.
T * data() const
iterator end()
static QgsGeometryEngine * createGeometryEngine(const QgsAbstractGeometryV2 *geometry)
Creates and returns a new geometry engine.
virtual QString geometryType() const =0
Returns a unique string representing the geometry type.
QgsGeometry * geometry()
Get the geometry object associated with this feature.
Definition: qgsfeature.cpp:64
bool contains(const T &value) const
static bool deletePart(QgsAbstractGeometryV2 *geom, int partNum)
Deletes a part from a geometry.
bool isNull() const
static bool deleteRing(QgsAbstractGeometryV2 *geom, int ringNum, int partNum=0)
Deletes a ring from a geometry.
static QgsAbstractGeometryV2 * avoidIntersections(const QgsAbstractGeometryV2 &geom, QMap< QgsVectorLayer *, QSet< QgsFeatureId > > ignoreFeatures=(QMap< QgsVectorLayer *, QSet< QgsFeatureId > >()))
Alters a geometry so that it avoids intersections with features from all open vector layers...
int numGeometries() const
Returns the number of geometries within the collection.
static QgsMapLayerRegistry * instance()
Returns the instance pointer, creating the object on the first call.
void setExteriorRing(QgsCurveV2 *ring)
Sets exterior ring (takes ownership)
virtual bool isClosed() const
Returns true if the curve is closed.
Definition: qgscurvev2.cpp:27
static QgsProject * instance()
access to canonical QgsProject instance
Definition: qgsproject.cpp:353
virtual bool addGeometry(QgsAbstractGeometryV2 *g)
Adds a geometry and takes ownership.
const QgsAbstractGeometryV2 * geometryN(int n) const
Returns a const reference to a geometry from within the collection.
virtual bool removeGeometry(int nr)
Removes a geometry from the collection.
static int addRing(QgsAbstractGeometryV2 *geom, QgsCurveV2 *ring)
Adds interior ring (taking ownership).
Curve polygon geometry type.
const_iterator constEnd() const
bool nextFeature(QgsFeature &f)
const_iterator constBegin() const
QgsRectangle boundingBox() const
Returns the minimal bounding box for the geometry.
virtual QgsAbstractGeometryV2 * clone() const =0
Clones the geometry by performing a deep copy.
Abstract base class for curved geometry type.
Definition: qgscurvev2.h:32
Represents a vector layer which manages a vector based data sets.
iterator find(const Key &key)
iterator begin()
QStringList readListEntry(const QString &scope, const QString &key, const QStringList &def=QStringList(), bool *ok=0) const
Key value accessors.
const T value(const Key &key) const