QGIS API Documentation 3.40.0-Bratislava (b56115d8743)
Loading...
Searching...
No Matches
qgscurve.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgscurve.cpp
3 --------------
4 begin : November 2014
5 copyright : (C) 2014 by Marco Hugentobler
6 email : marco at sourcepole dot ch
7 ***************************************************************************/
8
9/***************************************************************************
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 ***************************************************************************/
17
18#include <memory>
19
20#include "qgscurve.h"
21#include "qgslinestring.h"
22#include "qgspoint.h"
23#include "qgsmultipoint.h"
24#include "qgsgeos.h"
25#include "qgsvertexid.h"
26
27bool QgsCurve::operator==( const QgsAbstractGeometry &other ) const
28{
29 const QgsCurve *otherCurve = qgsgeometry_cast< const QgsCurve * >( &other );
30 if ( !otherCurve )
31 return false;
32
33 return equals( *otherCurve );
34}
35
36bool QgsCurve::operator!=( const QgsAbstractGeometry &other ) const
37{
38 return !operator==( other );
39}
40
42{
43 if ( numPoints() == 0 )
44 return false;
45
46 //don't consider M-coordinates when testing closedness
47 const QgsPoint start = startPoint();
48 const QgsPoint end = endPoint();
49
50 return qgsDoubleNear( start.x(), end.x() ) &&
51 qgsDoubleNear( start.y(), end.y() );
52}
54{
55 bool closed = isClosed2D();
56 if ( is3D() && closed )
57 {
58 const QgsPoint start = startPoint();
59 const QgsPoint end = endPoint();
60 closed &= qgsDoubleNear( start.z(), end.z() ) || ( std::isnan( start.z() ) && std::isnan( end.z() ) );
61 }
62 return closed;
63}
64
65bool QgsCurve::isRing() const
66{
67 return ( isClosed() && numPoints() >= 4 );
68}
69
70QPainterPath QgsCurve::asQPainterPath() const
71{
72 QPainterPath p;
74 return p;
75}
76
78{
79 QgsCoordinateSequence sequence;
80 sequence.append( QgsRingSequence() );
81 sequence.back().append( QgsPointSequence() );
82 points( sequence.back().back() );
83
84 return sequence;
85}
86
87bool QgsCurve::nextVertex( QgsVertexId &id, QgsPoint &vertex ) const
88{
89 if ( id.vertex < 0 )
90 {
91 id.vertex = 0;
92 if ( id.part < 0 )
93 {
94 id.part = 0;
95 }
96 if ( id.ring < 0 )
97 {
98 id.ring = 0;
99 }
100 }
101 else
102 {
103 if ( id.vertex + 1 >= numPoints() )
104 {
105 return false;
106 }
107 ++id.vertex;
108 }
109 return pointAt( id.vertex, vertex, id.type );
110}
111
112void QgsCurve::adjacentVertices( QgsVertexId vertex, QgsVertexId &previousVertex, QgsVertexId &nextVertex ) const
113{
114 const int n = numPoints();
115 if ( vertex.vertex < 0 || vertex.vertex >= n )
116 {
117 previousVertex = QgsVertexId();
119 return;
120 }
121
122 if ( vertex.vertex == 0 )
123 {
124 previousVertex = QgsVertexId();
125 }
126 else
127 {
128 previousVertex = QgsVertexId( vertex.part, vertex.ring, vertex.vertex - 1 );
129 }
130 if ( vertex.vertex == n - 1 )
131 {
133 }
134 else
135 {
136 nextVertex = QgsVertexId( vertex.part, vertex.ring, vertex.vertex + 1 );
137 }
138}
139
141{
142 if ( id.part != 0 || id.ring != 0 )
143 return -1;
144 if ( id.vertex < 0 || id.vertex >= numPoints() )
145 return -1;
146 return id.vertex;
147}
148
150{
151 if ( isEmpty() )
152 return nullptr;
153
154 if ( isClosed() )
155 return nullptr;
156
157 QgsMultiPoint *multiPoint = new QgsMultiPoint();
158 multiPoint->reserve( 2 );
159 multiPoint->addGeometry( new QgsPoint( startPoint() ) );
160 multiPoint->addGeometry( new QgsPoint( endPoint() ) );
161 return multiPoint;
162}
163
164QString QgsCurve::asKml( int precision ) const
165{
166 std::unique_ptr<QgsLineString> lineString( curveToLine() );
167 if ( !lineString )
168 {
169 return QString();
170 }
171 QString kml = lineString->asKml( precision );
172 return kml;
173}
174
175QgsCurve *QgsCurve::segmentize( double tolerance, SegmentationToleranceType toleranceType ) const
176{
177 return curveToLine( tolerance, toleranceType );
178}
179
180int QgsCurve::vertexCount( int part, int ring ) const
181{
182 Q_UNUSED( part )
183 Q_UNUSED( ring )
184 return numPoints();
185}
186
187int QgsCurve::ringCount( int part ) const
188{
189 Q_UNUSED( part )
190 return numPoints() > 0 ? 1 : 0;
191}
192
194{
195 return numPoints() > 0 ? 1 : 0;
196}
197
199{
200 QgsPoint v;
201 Qgis::VertexType type;
202 pointAt( id.vertex, v, type );
203 return v;
204}
205
207{
208 return clone();
209}
210
212{
213 if ( isEmpty() )
214 return;
215
216 if ( !isClosed() )
217 {
218 return;
219 }
220
221 int minCoordinateIndex = 0;
222 QgsPoint minCoord;
223 int i = 0;
224 for ( auto it = vertices_begin(); it != vertices_end(); ++it )
225 {
226 const QgsPoint vertex = *it;
227 if ( minCoord.isEmpty() || minCoord.compareTo( &vertex ) > 0 )
228 {
229 minCoord = vertex;
230 minCoordinateIndex = i;
231 }
232 i++;
233 }
234
235 scroll( minCoordinateIndex );
236}
237
239{
240 if ( mBoundingBox.isNull() )
241 {
243 }
244 return mBoundingBox;
245}
246
247bool QgsCurve::isValid( QString &error, Qgis::GeometryValidityFlags flags ) const
248{
249 if ( flags == 0 && mHasCachedValidity )
250 {
251 // use cached validity results
252 error = mValidityFailureReason;
253 return error.isEmpty();
254 }
255
256 const QgsGeos geos( this );
257 const bool res = geos.isValid( &error, flags & Qgis::GeometryValidityFlag::AllowSelfTouchingHoles, nullptr );
258 if ( flags == 0 )
259 {
260 mValidityFailureReason = !res ? error : QString();
261 mHasCachedValidity = true;
262 }
263 return res;
264}
265
266QPolygonF QgsCurve::asQPolygonF() const
267{
268 std::unique_ptr< QgsLineString > segmentized( curveToLine() );
269 return segmentized->asQPolygonF();
270}
271
273{
274 return startPoint().distance( endPoint() );
275}
276
278{
279 const double d = straightDistance2d();
280 if ( qgsDoubleNear( d, 0.0 ) )
281 return std::numeric_limits<double>::quiet_NaN();
282
283 return length() / d;
284}
285
292
294{
296 mHasCachedValidity = false;
297 mValidityFailureReason.clear();
300}
301
303{
304 return numPoints();
305}
306
308{
309 QgsPoint point;
310 Qgis::VertexType type;
311 const bool res = pointAt( index, point, type );
312 Q_ASSERT( res );
313 Q_UNUSED( res )
314 return point;
315}
316
317bool QgsCurve::snapToGridPrivate( double hSpacing, double vSpacing, double dSpacing, double mSpacing,
318 const QVector<double> &srcX, const QVector<double> &srcY, const QVector<double> &srcZ, const QVector<double> &srcM,
319 QVector<double> &outX, QVector<double> &outY, QVector<double> &outZ, QVector<double> &outM, bool removeRedundantPoints ) const
320{
321 const int length = numPoints();
322 if ( length < 2 )
323 return false;
324
325 const bool hasZ = is3D();
326 const bool hasM = isMeasure();
327
328 outX.reserve( length );
329 outY.reserve( length );
330 if ( hasZ )
331 outZ.reserve( length );
332 if ( hasM )
333 outM.reserve( length );
334
335 const double *xIn = srcX.constData();
336 const double *yIn = srcY.constData();
337 const double *zIn = hasZ ? srcZ.constData() : nullptr;
338 const double *mIn = hasM ? srcM.constData() : nullptr;
339
340 double previousX = 0;
341 double previousY = 0;
342 double previousZ = 0;
343 double previousM = 0;
344 int outSize = 0;
345 for ( int i = 0; i < length; ++i )
346 {
347 const double currentX = *xIn++;
348 const double currentY = *yIn++;
349 const double currentZ = zIn ? *zIn++ : 0;
350 const double currentM = mIn ? *mIn++ : 0;
351
352 const double roundedX = hSpacing > 0 ? ( std::round( currentX / hSpacing ) * hSpacing ) : currentX;
353 const double roundedY = vSpacing > 0 ? ( std::round( currentY / vSpacing ) * vSpacing ) : currentY;
354 const double roundedZ = hasZ && dSpacing > 0 ? ( std::round( currentZ / dSpacing ) * dSpacing ) : currentZ;
355 const double roundedM = hasM && mSpacing > 0 ? ( std::round( currentM / mSpacing ) * mSpacing ) : currentM;
356
357 if ( i == 0 )
358 {
359 outX.append( roundedX );
360 outY.append( roundedY );
361 if ( hasZ )
362 outZ.append( roundedZ );
363 if ( hasM )
364 outM.append( roundedM );
365 outSize++;
366 }
367 else
368 {
369 const bool isPointEqual = qgsDoubleNear( roundedX, previousX )
370 && qgsDoubleNear( roundedY, previousY )
371 && ( !hasZ || dSpacing <= 0 || qgsDoubleNear( roundedZ, previousZ ) )
372 && ( !hasM || mSpacing <= 0 || qgsDoubleNear( roundedM, previousM ) );
373 if ( isPointEqual )
374 continue;
375
376 // maybe previous point is redundant and is just a midpoint on a straight line -- let's check
377 bool previousPointRedundant = false;
378 if ( removeRedundantPoints && outSize > 1 && !hasZ && !hasM )
379 {
380 previousPointRedundant = QgsGeometryUtilsBase::leftOfLine( outX.at( outSize - 1 ),
381 outY.at( outSize - 1 ),
382 outX.at( outSize - 2 ),
383 outY.at( outSize - 2 ),
384 roundedX, roundedY ) == 0;
385 }
386 if ( previousPointRedundant )
387 {
388 outX[ outSize - 1 ] = roundedX;
389 outY[ outSize - 1 ] = roundedY;
390 }
391 else
392 {
393 outX.append( roundedX );
394 outY.append( roundedY );
395 if ( hasZ )
396 outZ.append( roundedZ );
397 if ( hasM )
398 outM.append( roundedM );
399 outSize++;
400 }
401 }
402
403 previousX = roundedX;
404 previousY = roundedY;
405 previousZ = roundedZ;
406 previousM = roundedM;
407 }
408
409 if ( removeRedundantPoints && isClosed() && outSize > 4 && !hasZ && !hasM )
410 {
411 // maybe first/last vertex is redundant, let's try to remove that too
412 const bool firstVertexIsRedundant = QgsGeometryUtilsBase::leftOfLine( outX.at( 0 ),
413 outY.at( 0 ),
414 outX.at( outSize - 2 ),
415 outY.at( outSize - 2 ),
416 outX.at( 1 ), outY.at( 1 ) ) == 0;
417 if ( firstVertexIsRedundant )
418 {
419 outX.removeAt( 0 );
420 outY.removeAt( 0 );
421 outX[ outSize - 2 ] = outX.at( 0 );
422 outY[ outSize - 2 ] = outY.at( 0 );
423 }
424 }
425
426 // we previously reserved size based on a worst case scenario, let's free
427 // unnecessary memory reservation now
428 outX.squeeze();
429 outY.squeeze();
430 if ( hasZ )
431 outZ.squeeze();
432 if ( hasM )
433 outM.squeeze();
434
435 return outSize >= 4 || ( !isClosed() && outSize >= 2 );
436}
@ AllowSelfTouchingHoles
Indicates that self-touching holes are permitted. OGC validity states that self-touching holes are NO...
AngularDirection
Angular directions.
Definition qgis.h:3175
@ CounterClockwise
Counter-clockwise direction.
@ Clockwise
Clockwise direction.
QFlags< GeometryValidityFlag > GeometryValidityFlags
Geometry validity flags.
Definition qgis.h:1922
VertexType
Types of vertex.
Definition qgis.h:2835
Abstract base class for all geometries.
SegmentationToleranceType
Segmentation tolerance as maximum angle or maximum difference between approximation and circle.
vertex_iterator vertices_end() const
Returns STL-style iterator pointing to the imaginary vertex after the last vertex of the geometry.
virtual QgsBox3D calculateBoundingBox3D() const
Calculates the minimal 3D bounding box for the geometry.
bool isMeasure() const
Returns true if the geometry contains m values.
bool is3D() const
Returns true if the geometry is 3D and contains a z-value.
virtual void clearCache() const
Clears any cached parameters associated with the geometry, e.g., bounding boxes.
virtual double length() const
Returns the planar, 2-dimensional length of the geometry.
virtual bool isEmpty() const
Returns true if the geometry is empty.
vertex_iterator vertices_begin() const
Returns STL-style iterator pointing to the first vertex of the geometry.
virtual int compareTo(const QgsAbstractGeometry *other) const
Comparator for sorting of geometry.
A 3-dimensional box composed of x, y, z coordinates.
Definition qgsbox3d.h:43
bool isNull() const
Test if the box is null (holding no spatial information).
Definition qgsbox3d.cpp:289
Abstract base class for curved geometry type.
Definition qgscurve.h:35
virtual bool equals(const QgsCurve &other) const =0
Checks whether this curve exactly equals another curve.
Qgis::AngularDirection orientation() const
Returns the curve's orientation, e.g.
Definition qgscurve.cpp:286
QgsCoordinateSequence coordinateSequence() const override
Retrieves the sequence of geometries, rings and nodes.
Definition qgscurve.cpp:77
virtual int numPoints() const =0
Returns the number of points in the curve.
void clearCache() const override
Clears any cached parameters associated with the geometry, e.g., bounding boxes.
Definition qgscurve.cpp:293
double sinuosity() const
Returns the curve sinuosity, which is the ratio of the curve length() to curve straightDistance2d().
Definition qgscurve.cpp:277
bool mHasCachedSummedUpArea
Definition qgscurve.h:354
void normalize() final
Reorganizes the geometry into a normalized form (or "canonical" form).
Definition qgscurve.cpp:211
QPainterPath asQPainterPath() const override
Returns the geometry represented as a QPainterPath.
Definition qgscurve.cpp:70
QgsPoint childPoint(int index) const override
Returns point at index (for geometries without child geometries - i.e.
Definition qgscurve.cpp:307
int vertexNumberFromVertexId(QgsVertexId id) const override
Returns the vertex number corresponding to a vertex id.
Definition qgscurve.cpp:140
virtual void scroll(int firstVertexIndex)=0
Scrolls the curve vertices so that they start with the vertex at the given index.
int partCount() const override
Returns count of parts contained in the geometry.
Definition qgscurve.cpp:193
QgsCurve * segmentize(double tolerance=M_PI_2/90, SegmentationToleranceType toleranceType=MaximumAngle) const override
Returns a geometry without curves.
Definition qgscurve.cpp:175
virtual bool isRing() const
Returns true if the curve is a ring.
Definition qgscurve.cpp:65
virtual bool pointAt(int node, QgsPoint &point, Qgis::VertexType &type) const =0
Returns the point and vertex id of a point within the curve.
int childCount() const override
Returns number of child geometries (for geometries with child geometries) or child points (for geomet...
Definition qgscurve.cpp:302
QgsBox3D boundingBox3D() const override
Returns the 3D bounding box for the geometry.
Definition qgscurve.cpp:238
virtual bool isClosed() const
Returns true if the curve is closed.
Definition qgscurve.cpp:53
bool isValid(QString &error, Qgis::GeometryValidityFlags flags=Qgis::GeometryValidityFlags()) const override
Checks validity of the geometry, and returns true if the geometry is valid.
Definition qgscurve.cpp:247
virtual bool isClosed2D() const
Returns true if the curve is closed.
Definition qgscurve.cpp:41
int vertexCount(int part=0, int ring=0) const override
Returns the number of vertices of which this geometry is built.
Definition qgscurve.cpp:180
QgsPoint vertexAt(QgsVertexId id) const override
Returns the point corresponding to a specified vertex id.
Definition qgscurve.cpp:198
QgsAbstractGeometry * boundary() const override
Returns the closure of the combinatorial boundary of the geometry (ie the topological boundary of the...
Definition qgscurve.cpp:149
virtual QPolygonF asQPolygonF() const
Returns a QPolygonF representing the points.
Definition qgscurve.cpp:266
void adjacentVertices(QgsVertexId vertex, QgsVertexId &previousVertex, QgsVertexId &nextVertex) const override
Returns the vertices adjacent to a specified vertex within a geometry.
Definition qgscurve.cpp:112
virtual void addToPainterPath(QPainterPath &path) const =0
Adds a curve to a painter path.
QgsCurve * toCurveType() const override
Returns the geometry converted to the more generic curve type.
Definition qgscurve.cpp:206
virtual void sumUpArea(double &sum) const =0
Sums up the area of the curve by iterating over the vertices (shoelace formula).
bool operator==(const QgsAbstractGeometry &other) const override
Definition qgscurve.cpp:27
bool snapToGridPrivate(double hSpacing, double vSpacing, double dSpacing, double mSpacing, const QVector< double > &srcX, const QVector< double > &srcY, const QVector< double > &srcZ, const QVector< double > &srcM, QVector< double > &outX, QVector< double > &outY, QVector< double > &outZ, QVector< double > &outM, bool removeRedundantPoints) const
Helper function for QgsCurve subclasses to snap to grids.
Definition qgscurve.cpp:317
QString asKml(int precision=17) const override
Returns a KML representation of the geometry.
Definition qgscurve.cpp:164
QgsBox3D mBoundingBox
Cached bounding box.
Definition qgscurve.h:352
QgsCurve * clone() const override=0
Clones the geometry by performing a deep copy.
int ringCount(int part=0) const override
Returns the number of rings of which this geometry is built.
Definition qgscurve.cpp:187
virtual QgsPoint startPoint() const =0
Returns the starting point of the curve.
bool operator!=(const QgsAbstractGeometry &other) const override
Definition qgscurve.cpp:36
double straightDistance2d() const
Returns the straight distance of the curve, i.e.
Definition qgscurve.cpp:272
bool nextVertex(QgsVertexId &id, QgsPoint &vertex) const override
Returns next vertex id and coordinates.
Definition qgscurve.cpp:87
virtual QgsPoint endPoint() const =0
Returns the end point of the curve.
virtual void points(QgsPointSequence &pt) const =0
Returns a list of points within the curve.
virtual QgsLineString * curveToLine(double tolerance=M_PI_2/90, SegmentationToleranceType toleranceType=MaximumAngle) const =0
Returns a new line string geometry corresponding to a segmentized approximation of the curve.
void reserve(int size)
Attempts to allocate memory for at least size geometries.
static int leftOfLine(const double x, const double y, const double x1, const double y1, const double x2, const double y2)
Returns a value < 0 if the point (x, y) is left of the line from (x1, y1) -> (x2, y2).
Does vector analysis using the geos library and handles import, export, exception handling*.
Definition qgsgeos.h:137
Multi point geometry collection.
bool addGeometry(QgsAbstractGeometry *g) override
Adds a geometry and takes ownership. Returns true in case of success.
Point geometry type, with support for z-dimension and m-values.
Definition qgspoint.h:49
double z
Definition qgspoint.h:54
double x
Definition qgspoint.h:52
bool isEmpty() const override
Returns true if the geometry is empty.
Definition qgspoint.cpp:737
double distance(double x, double y) const
Returns the Cartesian 2D distance between this point and a specified x, y coordinate.
Definition qgspoint.h:393
double y
Definition qgspoint.h:53
Contains geos related utilities and functions.
Definition qgsgeos.h:75
bool qgsDoubleNear(double a, double b, double epsilon=4 *std::numeric_limits< double >::epsilon())
Compare two doubles (but allow some difference)
Definition qgis.h:5917
QVector< QgsRingSequence > QgsCoordinateSequence
QVector< QgsPointSequence > QgsRingSequence
QVector< QgsPoint > QgsPointSequence
int precision
Utility class for identifying a unique vertex within a geometry.
Definition qgsvertexid.h:30
int vertex
Vertex number.
Definition qgsvertexid.h:94
int part
Part number.
Definition qgsvertexid.h:88
int ring
Ring number.
Definition qgsvertexid.h:91