QGIS API Documentation 3.99.0-Master (21b3aa880ba)
Loading...
Searching...
No Matches
qgsabstractgeometry.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsabstractgeometry.cpp
3 -------------------------------------------------------------------
4Date : 04 Sept 2014
5Copyright : (C) 2014 by Marco Hugentobler
6email : 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 "qgsabstractgeometry.h"
17
18#include <limits>
19#include <nlohmann/json.hpp>
20
21#include "qgsbox3d.h"
22#include "qgscurve.h"
24#include "qgspoint.h"
25#include "qgsvertexid.h"
26
27#include <QTransform>
28
29#include "moc_qgsabstractgeometry.cpp"
30
35
37{
38 if ( &geom != this )
39 {
40 clear();
41 mWkbType = geom.mWkbType;
42 }
43 return *this;
44}
45
47{
48 // compare to self
49 if ( this == other )
50 {
51 return 0;
52 }
53
54 if ( sortIndex() != other->sortIndex() )
55 {
56 //different geometry types
57 const int diff = sortIndex() - other->sortIndex();
58 return ( diff > 0 ) - ( diff < 0 );
59 }
60
61 // same types
62 if ( isEmpty() && other->isEmpty() )
63 {
64 return 0;
65 }
66
67 if ( isEmpty() )
68 {
69 return -1;
70 }
71 if ( other->isEmpty() )
72 {
73 return 1;
74 }
75
76 return compareToSameClass( other );
77}
78
80{
81 if ( !subgeom )
82 {
83 return;
84 }
85
86 //special handling for 25d types:
87 if ( baseGeomType == Qgis::WkbType::LineString &&
88 ( subgeom->wkbType() == Qgis::WkbType::Point25D || subgeom->wkbType() == Qgis::WkbType::LineString25D ) )
89 {
91 return;
92 }
93 else if ( baseGeomType == Qgis::WkbType::Polygon &&
94 ( subgeom->wkbType() == Qgis::WkbType::Point25D || subgeom->wkbType() == Qgis::WkbType::LineString25D ) )
95 {
97 return;
98 }
99
100 const bool hasZ = subgeom->is3D();
101 const bool hasM = subgeom->isMeasure();
102
103 if ( hasZ && hasM )
104 {
105 mWkbType = QgsWkbTypes::addM( QgsWkbTypes::addZ( baseGeomType ) );
106 }
107 else if ( hasZ )
108 {
109 mWkbType = QgsWkbTypes::addZ( baseGeomType );
110 }
111 else if ( hasM )
112 {
113 mWkbType = QgsWkbTypes::addM( baseGeomType );
114 }
115 else
116 {
117 mWkbType = baseGeomType;
118 }
119}
120
125
130
132{
133 double xmin = std::numeric_limits<double>::max();
134 double ymin = std::numeric_limits<double>::max();
135 double zmin = std::numeric_limits<double>::max();
136 double xmax = -std::numeric_limits<double>::max();
137 double ymax = -std::numeric_limits<double>::max();
138 double zmax = -std::numeric_limits<double>::max();
139
140 QgsVertexId id;
141 QgsPoint vertex;
142 double x, y, z;
143 if ( is3D() )
144 {
145 while ( nextVertex( id, vertex ) )
146 {
147 x = vertex.x();
148 y = vertex.y();
149 z = vertex.z();
150
151 xmin = std::min( xmin, x );
152 xmax = std::max( xmax, x );
153
154 ymin = std::min( ymin, y );
155 ymax = std::max( ymax, y );
156
157 zmin = std::min( zmin, z );
158 zmax = std::max( zmax, z );
159 }
160 }
161 else
162 {
163 while ( nextVertex( id, vertex ) )
164 {
165 x = vertex.x();
166 y = vertex.y();
167 xmin = std::min( xmin, x );
168 xmax = std::max( xmax, x );
169
170 ymin = std::min( ymin, y );
171 ymax = std::max( ymax, y );
172 }
173 zmin = std::numeric_limits<double>::quiet_NaN();
174 zmax = std::numeric_limits<double>::quiet_NaN();
175 }
176
177 return QgsBox3D( xmin, ymin, zmin, xmax, ymax, zmax );
178}
179
181{
182}
183
185{
186 int nCoords = 0;
187
189 for ( const QgsRingSequence &r : seq )
190 {
191 for ( const QgsPointSequence &p : r )
192 {
193 nCoords += p.size();
194 }
195 }
196
197 return nCoords;
198}
199
201{
202 return 0.0;
203}
204
206{
207 return 0.0;
208}
209
211{
212 return 0.0;
213}
214
216{
217 QString wkt = geometryType();
218 QString suffix;
219 if ( is3D() )
220 suffix += 'Z';
221 if ( isMeasure() )
222 suffix += 'M';
223 if ( !suffix.isEmpty() )
224 {
225 wkt += ' ' + suffix;
226 }
227 return wkt;
228}
229
230QString QgsAbstractGeometry::asJson( int precision )
231{
232 return QString::fromStdString( asJsonObject( precision ).dump() );
233}
234
235json QgsAbstractGeometry::asJsonObject( int precision ) const
236{
237 Q_UNUSED( precision ) return nullptr;
238}
239
241{
242 if ( isEmpty() )
243 return QgsPoint();
244
245 // http://en.wikipedia.org/wiki/Centroid#Centroid_of_polygon
246 // Pick the first ring of first part for the moment
247
248 const int n = vertexCount( 0, 0 );
249 if ( n == 1 )
250 {
251 return vertexAt( QgsVertexId( 0, 0, 0 ) );
252 }
253
254 double A = 0.;
255 double Cx = 0.;
256 double Cy = 0.;
257 const QgsPoint v0 = vertexAt( QgsVertexId( 0, 0, 0 ) );
258 int i = 0, j = 1;
259 if ( vertexAt( QgsVertexId( 0, 0, 0 ) ) != vertexAt( QgsVertexId( 0, 0, n - 1 ) ) )
260 {
261 i = n - 1;
262 j = 0;
263 }
264 for ( ; j < n; i = j++ )
265 {
266 QgsPoint vi = vertexAt( QgsVertexId( 0, 0, i ) );
267 QgsPoint vj = vertexAt( QgsVertexId( 0, 0, j ) );
268 vi.rx() -= v0.x();
269 vi.ry() -= v0.y();
270 vj.rx() -= v0.x();
271 vj.ry() -= v0.y();
272 const double d = vi.x() * vj.y() - vj.x() * vi.y();
273 A += d;
274 Cx += ( vi.x() + vj.x() ) * d;
275 Cy += ( vi.y() + vj.y() ) * d;
276 }
277
278 if ( A < 1E-12 )
279 {
280 Cx = Cy = 0.;
281 for ( int i = 0; i < n - 1; ++i )
282 {
283 const QgsPoint vi = vertexAt( QgsVertexId( 0, 0, i ) );
284 Cx += vi.x();
285 Cy += vi.y();
286 }
287 return QgsPoint( Cx / ( n - 1 ), Cy / ( n - 1 ) );
288 }
289 else
290 {
291 return QgsPoint( v0.x() + Cx / ( 3. * A ), v0.y() + Cy / ( 3. * A ) );
292 }
293}
294
296{
297 if ( type == mWkbType )
298 return true;
299
301 return false;
302
303 const bool needZ = QgsWkbTypes::hasZ( type );
304 const bool needM = QgsWkbTypes::hasM( type );
305 if ( !needZ )
306 {
307 dropZValue();
308 }
309 else if ( !is3D() )
310 {
311 addZValue( std::numeric_limits<double>::quiet_NaN() );
312 }
313
314 if ( !needM )
315 {
316 dropMValue();
317 }
318 else if ( !isMeasure() )
319 {
320 addMValue( std::numeric_limits<double>::quiet_NaN() );
321 }
322
323 return true;
324}
325
327{
328 return this;
329}
330
331void QgsAbstractGeometry::filterVertices( const std::function<bool ( const QgsPoint & )> & )
332{
333 // Ideally this would be pure virtual, but SIP has issues with that
334}
335
336void QgsAbstractGeometry::transformVertices( const std::function<QgsPoint( const QgsPoint & )> & )
337{
338 // Ideally this would be pure virtual, but SIP has issues with that
339}
340
346
351
356
362
367
369{
370 switch ( QgsWkbTypes::flatType( mWkbType ) )
371 {
373 return 0;
375 return 1;
377 return 2;
379 return 3;
381 return 4;
383 return 5;
385 return 6;
388 return 7;
390 return 8;
392 return 9;
394 return 10;
396 return 11;
398 return 12;
400 default:
401 break;
402 }
403 return 13;
404}
405
410
412{
413 Q_UNUSED( index )
414 return QgsPoint();
415}
416
418{
419 QgsVertexId vId;
420 QgsPoint vertex;
421 return !nextVertex( vId, vertex );
422}
423
425{
426 return false;
427}
428
430{
431 return boundingBox().intersects( rectangle );
432}
433
435{
436 return boundingBox3D().intersects( box3d );
437}
438
440{
441 Q_UNUSED( tolerance )
442 Q_UNUSED( toleranceType )
443 return clone();
444}
445
446
448 : depth( 0 )
449{
450 levels.fill( Level() );
451 levels[0].g = g;
452 levels[0].index = index;
453
454 digDown(); // go to the leaf level of the first vertex
455}
456
458{
459 if ( depth == 0 && levels[0].index >= levels[0].g->childCount() )
460 return *this; // end of geometry - nowhere else to go
461
462 Q_ASSERT( !levels[depth].g->hasChildGeometries() ); // we should be at a leaf level
463
464 ++levels[depth].index;
465
466 // traverse up if we are at the end in the current level
467 while ( depth > 0 && levels[depth].index >= levels[depth].g->childCount() )
468 {
469 --depth;
470 ++levels[depth].index;
471 }
472
473 digDown(); // go to the leaf level again
474
475 return *this;
476}
477
484
486{
487 Q_ASSERT( !levels[depth].g->hasChildGeometries() );
488 return levels[depth].g->childPoint( levels[depth].index );
489}
490
492{
493 int part = 0, ring = 0, vertex = levels[depth].index;
494 if ( depth == 0 )
495 {
496 // nothing else to do
497 }
498 else if ( depth == 1 )
499 {
500 if ( QgsWkbTypes::isMultiType( levels[0].g->wkbType() ) )
501 part = levels[0].index;
502 else
503 ring = levels[0].index;
504 }
505 else if ( depth == 2 )
506 {
507 part = levels[0].index;
508 ring = levels[1].index;
509 }
510 else
511 {
512 Q_ASSERT( false );
513 return QgsVertexId();
514 }
515
516 // get the vertex type: find out from the leaf geometry
518 if ( const QgsCurve *curve = dynamic_cast<const QgsCurve *>( levels[depth].g ) )
519 {
520 QgsPoint p;
521 curve->pointAt( vertex, p, vertexType );
522 }
523
524 return QgsVertexId( part, ring, vertex, vertexType );
525}
526
528{
529 if ( depth != other.depth )
530 return false;
531 return std::equal( std::begin( levels ), std::begin( levels ) + depth + 1, std::begin( other.levels ) );
532}
533
534void QgsAbstractGeometry::vertex_iterator::digDown()
535{
536 if ( levels[depth].g->hasChildGeometries() && levels[depth].index >= levels[depth].g->childCount() )
537 return; // first check we are not already at the end
538
539 // while not "final" depth for the geom: go one level down.
540 while ( levels[depth].g->hasChildGeometries() )
541 {
542 ++depth;
543 Q_ASSERT( depth < 3 ); // that's capacity of the levels array
544 levels[depth].index = 0;
545 levels[depth].g = levels[depth - 1].g->childGeometry( levels[depth - 1].index );
546 }
547}
548
550{
551 n = i++;
552 return *n;
553}
554
556 : mIndex( index )
557 , mGeometry( g )
558{
559}
560
562{
564 if ( !collection )
565 {
566 mIndex = 1;
567 return *this; // end of geometry -- nowhere else to go
568 }
569
570 if ( mIndex >= collection->partCount() )
571 return *this; // end of geometry - nowhere else to go
572
573 mIndex++;
574 return *this;
575}
576
578{
579 part_iterator it( *this );
580 ++*this;
581 return it;
582}
583
585{
587 if ( !collection )
588 {
589 return mGeometry;
590 }
591
592 return collection->geometryN( mIndex );
593}
594
596{
597 return mIndex;
598}
599
601{
602 return mGeometry == other.mGeometry && mIndex == other.mIndex;
603}
604
606{
607 n = i++;
608 return *n;
609}
610
611
612
614 : mIndex( index )
615 , mGeometry( g )
616{
617}
618
620{
622 if ( !collection )
623 {
624 mIndex = 1;
625 return *this; // end of geometry -- nowhere else to go
626 }
627
628 if ( mIndex >= collection->partCount() )
629 return *this; // end of geometry - nowhere else to go
630
631 mIndex++;
632 return *this;
633}
634
641
643{
645 if ( !collection )
646 {
647 return mGeometry;
648 }
649
650 return collection->geometryN( mIndex );
651}
652
654{
655 return mIndex;
656}
657
659{
660 return mGeometry == other.mGeometry && mIndex == other.mIndex;
661}
662
664{
665 n = i++;
666 return *n;
667}
668
669bool QgsAbstractGeometry::vertex_iterator::Level::operator==( const QgsAbstractGeometry::vertex_iterator::Level &other ) const
670{
671 return g == other.g && index == other.index;
672}
VertexType
Types of vertex.
Definition qgis.h:3066
@ Segment
The actual start or end point of a segment.
Definition qgis.h:3067
WkbType
The WKB type describes the number of dimensions a geometry has.
Definition qgis.h:277
@ LineString25D
LineString25D.
Definition qgis.h:341
@ CompoundCurve
CompoundCurve.
Definition qgis.h:288
@ Point
Point.
Definition qgis.h:279
@ LineString
LineString.
Definition qgis.h:280
@ MultiPoint
MultiPoint.
Definition qgis.h:283
@ Polygon
Polygon.
Definition qgis.h:281
@ MultiPolygon
MultiPolygon.
Definition qgis.h:285
@ Triangle
Triangle.
Definition qgis.h:282
@ NoGeometry
No geometry.
Definition qgis.h:294
@ MultiLineString
MultiLineString.
Definition qgis.h:284
@ Unknown
Unknown.
Definition qgis.h:278
@ CircularString
CircularString.
Definition qgis.h:287
@ GeometryCollection
GeometryCollection.
Definition qgis.h:286
@ MultiCurve
MultiCurve.
Definition qgis.h:290
@ CurvePolygon
CurvePolygon.
Definition qgis.h:289
@ Point25D
Point25D.
Definition qgis.h:340
@ MultiSurface
MultiSurface.
Definition qgis.h:291
@ Polygon25D
Polygon25D.
Definition qgis.h:342
The part_iterator class provides an STL-style iterator for const references to geometry parts.
const_part_iterator & operator++()
The prefix ++ operator (++it) advances the iterator to the next part and returns an iterator to the n...
const_part_iterator()=default
Create invalid iterator.
const QgsAbstractGeometry * operator*() const
Returns the current item.
int partNumber() const
Returns the part number of the current item.
bool operator==(const_part_iterator other) const
The part_iterator class provides an STL-style iterator for geometry parts.
part_iterator & operator++()
The prefix ++ operator (++it) advances the iterator to the next part and returns an iterator to the n...
part_iterator()=default
Create invalid iterator.
QgsAbstractGeometry * operator*() const
Returns the current item.
bool operator==(part_iterator other) const
int partNumber() const
Returns the part number of the current item.
The vertex_iterator class provides an STL-style iterator for vertices.
vertex_iterator()=default
Create invalid iterator.
bool operator==(const vertex_iterator &other) const
QgsPoint operator*() const
Returns the current item.
vertex_iterator & operator++()
The prefix ++ operator (++it) advances the iterator to the next vertex and returns an iterator to the...
QgsVertexId vertexId() const
Returns vertex ID of the current item.
Abstract base class for all geometries.
virtual QgsPoint childPoint(int index) const
Returns point at index (for geometries without child geometries - i.e.
virtual bool addZValue(double zValue=0)=0
Adds a z-dimension to the geometry, initialized to a preset value.
SegmentationToleranceType
Segmentation tolerance as maximum angle or maximum difference between approximation and circle.
virtual bool convertTo(Qgis::WkbType type)
Converts the geometry to a specified type.
virtual bool dropMValue()=0
Drops any measure values which exist in the geometry.
virtual QgsBox3D calculateBoundingBox3D() const
Calculates the minimal 3D bounding box for the geometry.
virtual const QgsAbstractGeometry * simplifiedTypeRef() const
Returns a reference to the simplest lossless representation of this geometry, e.g.
QgsVertexIterator vertices() const
Returns a read-only, Java-style iterator for traversal of vertices of all the geometry,...
virtual QgsAbstractGeometry * segmentize(double tolerance=M_PI/180., SegmentationToleranceType toleranceType=MaximumAngle) const
Returns a version of the geometry without curves.
virtual int vertexCount(int part=0, int ring=0) const =0
Returns the number of vertices of which this geometry is built.
bool isMeasure() const
Returns true if the geometry contains m values.
virtual QgsRectangle calculateBoundingBox() const
Default calculator for the minimal bounding box for the geometry.
virtual QgsRectangle boundingBox() const
Returns the minimal bounding box for the geometry.
virtual void transformVertices(const std::function< QgsPoint(const QgsPoint &) > &transform)
Transforms the vertices from the geometry in place, applying the transform function to every vertex.
bool is3D() const
Returns true if the geometry is 3D and contains a z-value.
virtual QgsBox3D boundingBox3D() const =0
Returns the 3D bounding box for the geometry.
virtual QString geometryType() const =0
Returns a unique string representing the geometry type.
int sortIndex() const
Returns the sort index for the geometry, used in the compareTo() method to compare geometries of diff...
QString wktTypeStr() const
Returns the WKT type string of the geometry.
virtual double perimeter() const
Returns the planar, 2-dimensional perimeter of the geometry.
virtual int nCoordinates() const
Returns the number of nodes contained in the geometry.
virtual QgsPoint vertexAt(QgsVertexId id) const =0
Returns the point corresponding to a specified vertex id.
virtual void clearCache() const
Clears any cached parameters associated with the geometry, e.g., bounding boxes.
QgsAbstractGeometry & operator=(const QgsAbstractGeometry &geom)
virtual bool addMValue(double mValue=0)=0
Adds a measure to the geometry, initialized to a preset value.
Qgis::WkbType wkbType() const
Returns the WKB type of the geometry.
QString asJson(int precision=17)
Returns a GeoJSON representation of the geometry as a QString.
part_iterator parts_end()
Returns STL-style iterator pointing to the imaginary part after the last part of the geometry.
void setZMTypeFromSubGeometry(const QgsAbstractGeometry *subggeom, Qgis::WkbType baseGeomType)
Updates the geometry type based on whether sub geometries contain z or m values.
virtual double length() const
Returns the planar, 2-dimensional length of the geometry.
const_part_iterator const_parts_end() const
Returns STL-style iterator pointing to the imaginary const part after the last part of the geometry.
virtual bool isEmpty() const
Returns true if the geometry is empty.
virtual json asJsonObject(int precision=17) const
Returns a json object representation of the geometry.
virtual QgsCoordinateSequence coordinateSequence() const =0
Retrieves the sequence of geometries, rings and nodes.
virtual bool hasChildGeometries() const
Returns whether the geometry has any child geometries (false for point / curve, true otherwise).
virtual bool boundingBoxIntersects(const QgsRectangle &rectangle) const
Returns true if the bounding box of this geometry intersects with a rectangle.
virtual bool hasCurvedSegments() const
Returns true if the geometry contains curved segments.
virtual void clear()=0
Clears the geometry, ie reset it to a null geometry.
virtual void filterVertices(const std::function< bool(const QgsPoint &) > &filter)
Filters the vertices from the geometry in place, removing any which do not return true for the filter...
virtual bool dropZValue()=0
Drops any z-dimensions which exist in the geometry.
virtual QgsPoint centroid() const
Returns the centroid of the geometry.
virtual int dimension() const =0
Returns the inherent dimension of the geometry.
virtual int compareTo(const QgsAbstractGeometry *other) const
Comparator for sorting of geometry.
QgsAbstractGeometry()=default
virtual double area() const
Returns the planar, 2-dimensional area of the geometry.
virtual int compareToSameClass(const QgsAbstractGeometry *other) const =0
Compares to an other geometry of the same class, and returns a integer for sorting of the two geometr...
QgsGeometryConstPartIterator parts() const
Returns Java-style iterator for traversal of parts of the geometry.
virtual bool nextVertex(QgsVertexId &id, QgsPoint &vertex) const =0
Returns next vertex id and coordinates.
virtual QgsAbstractGeometry * clone() const =0
Clones the geometry by performing a deep copy.
A 3-dimensional box composed of x, y, z coordinates.
Definition qgsbox3d.h:42
bool intersects(const QgsBox3D &other) const
Returns true if box intersects with another box.
Definition qgsbox3d.cpp:146
QgsRectangle toRectangle() const
Converts the box to a 2D rectangle.
Definition qgsbox3d.h:378
Abstract base class for curved geometry type.
Definition qgscurve.h:36
int partCount() const override
Returns count of parts contained in the geometry.
const QgsAbstractGeometry * geometryN(int n) const
Returns a const reference to a geometry from within the collection.
Java-style iterator for const traversal of parts of a geometry.
const QgsAbstractGeometry * next()
Returns next part of the geometry (undefined behavior if hasNext() returns false before calling next(...
Java-style iterator for traversal of parts of a geometry.
QgsAbstractGeometry * next()
Returns next part of the geometry (undefined behavior if hasNext() returns false before calling next(...
Point geometry type, with support for z-dimension and m-values.
Definition qgspoint.h:49
double & rx()
Returns a reference to the x-coordinate of this point.
Definition qgspoint.h:292
double z
Definition qgspoint.h:54
double x
Definition qgspoint.h:52
double & ry()
Returns a reference to the y-coordinate of this point.
Definition qgspoint.h:301
double y
Definition qgspoint.h:53
A rectangle specified with double values.
bool intersects(const QgsRectangle &rect) const
Returns true when rectangle intersects with other rectangle.
Java-style iterator for traversal of vertices of a geometry.
QgsPoint next()
Returns next vertex of the geometry (undefined behavior if hasNext() returns false before calling nex...
static Qgis::WkbType addM(Qgis::WkbType type)
Adds the m dimension to a WKB type and returns the new type.
static Qgis::WkbType addZ(Qgis::WkbType type)
Adds the z dimension to a WKB type and returns the new type.
static Q_INVOKABLE bool hasZ(Qgis::WkbType type)
Tests whether a WKB type contains the z-dimension.
static Q_INVOKABLE bool hasM(Qgis::WkbType type)
Tests whether a WKB type contains m values.
static Qgis::WkbType flatType(Qgis::WkbType type)
Returns the flat type for a WKB type.
static Q_INVOKABLE bool isMultiType(Qgis::WkbType type)
Returns true if the WKB type is a multi type.
T qgsgeometry_cast(QgsAbstractGeometry *geom)
QVector< QgsRingSequence > QgsCoordinateSequence
QVector< QgsPointSequence > QgsRingSequence
QVector< QgsPoint > QgsPointSequence
Utility class for identifying a unique vertex within a geometry.
Definition qgsvertexid.h:30