QGIS API Documentation  3.15.0-Master (a49cb7c9f3)
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 
26 bool QgsCurve::operator==( const QgsAbstractGeometry &other ) const
27 {
28  const QgsCurve *otherCurve = qgsgeometry_cast< const QgsCurve * >( &other );
29  if ( !otherCurve )
30  return false;
31 
32  return equals( *otherCurve );
33 }
34 
35 bool QgsCurve::operator!=( const QgsAbstractGeometry &other ) const
36 {
37  return !operator==( other );
38 }
39 
40 bool QgsCurve::isClosed() const
41 {
42  if ( numPoints() == 0 )
43  return false;
44 
45  //don't consider M-coordinates when testing closedness
46  QgsPoint start = startPoint();
47  QgsPoint end = endPoint();
48 
49  bool closed = qgsDoubleNear( start.x(), end.x() ) &&
50  qgsDoubleNear( start.y(), end.y() );
51  if ( is3D() && closed )
52  closed &= qgsDoubleNear( start.z(), end.z() ) || ( std::isnan( start.z() ) && std::isnan( end.z() ) );
53  return closed;
54 }
55 
56 bool QgsCurve::isRing() const
57 {
58  return ( isClosed() && numPoints() >= 4 );
59 }
60 
61 QPainterPath QgsCurve::asQPainterPath() const
62 {
63  QPainterPath p;
64  addToPainterPath( p );
65  return p;
66 }
67 
69 {
70  QgsCoordinateSequence sequence;
71  sequence.append( QgsRingSequence() );
72  sequence.back().append( QgsPointSequence() );
73  points( sequence.back().back() );
74 
75  return sequence;
76 }
77 
78 bool QgsCurve::nextVertex( QgsVertexId &id, QgsPoint &vertex ) const
79 {
80  if ( id.vertex < 0 )
81  {
82  id.vertex = 0;
83  if ( id.part < 0 )
84  {
85  id.part = 0;
86  }
87  if ( id.ring < 0 )
88  {
89  id.ring = 0;
90  }
91  }
92  else
93  {
94  if ( id.vertex + 1 >= numPoints() )
95  {
96  return false;
97  }
98  ++id.vertex;
99  }
100  return pointAt( id.vertex, vertex, id.type );
101 }
102 
104 {
105  int n = numPoints();
106  if ( vertex.vertex < 0 || vertex.vertex >= n )
107  {
108  previousVertex = QgsVertexId();
109  nextVertex = QgsVertexId();
110  return;
111  }
112 
113  if ( vertex.vertex == 0 )
114  {
115  previousVertex = QgsVertexId();
116  }
117  else
118  {
119  previousVertex = QgsVertexId( vertex.part, vertex.ring, vertex.vertex - 1 );
120  }
121  if ( vertex.vertex == n - 1 )
122  {
123  nextVertex = QgsVertexId();
124  }
125  else
126  {
127  nextVertex = QgsVertexId( vertex.part, vertex.ring, vertex.vertex + 1 );
128  }
129 }
130 
132 {
133  if ( id.part != 0 || id.ring != 0 )
134  return -1;
135  if ( id.vertex < 0 || id.vertex >= numPoints() )
136  return -1;
137  return id.vertex;
138 }
139 
141 {
142  if ( isEmpty() )
143  return nullptr;
144 
145  if ( isClosed() )
146  return nullptr;
147 
148  QgsMultiPoint *multiPoint = new QgsMultiPoint();
149  multiPoint->reserve( 2 );
150  multiPoint->addGeometry( new QgsPoint( startPoint() ) );
151  multiPoint->addGeometry( new QgsPoint( endPoint() ) );
152  return multiPoint;
153 }
154 
155 QString QgsCurve::asKml( int precision ) const
156 {
157  std::unique_ptr<QgsLineString> lineString( curveToLine() );
158  if ( !lineString )
159  {
160  return QString();
161  }
162  QString kml = lineString->asKml( precision );
163  return kml;
164 }
165 
166 QgsCurve *QgsCurve::segmentize( double tolerance, SegmentationToleranceType toleranceType ) const
167 {
168  return curveToLine( tolerance, toleranceType );
169 }
170 
171 int QgsCurve::vertexCount( int part, int ring ) const
172 {
173  Q_UNUSED( part )
174  Q_UNUSED( ring )
175  return numPoints();
176 }
177 
178 int QgsCurve::ringCount( int part ) const
179 {
180  Q_UNUSED( part )
181  return numPoints() > 0 ? 1 : 0;
182 }
183 
185 {
186  return numPoints() > 0 ? 1 : 0;
187 }
188 
190 {
191  QgsPoint v;
193  pointAt( id.vertex, v, type );
194  return v;
195 }
196 
198 {
199  return clone();
200 }
201 
203 {
204  if ( mBoundingBox.isNull() )
205  {
206  mBoundingBox = calculateBoundingBox();
207  }
208  return mBoundingBox;
209 }
210 
211 bool QgsCurve::isValid( QString &error, int flags ) const
212 {
213  if ( flags == 0 && mHasCachedValidity )
214  {
215  // use cached validity results
216  error = mValidityFailureReason;
217  return error.isEmpty();
218  }
219 
220  QgsGeos geos( this );
221  bool res = geos.isValid( &error, flags & QgsGeometry::FlagAllowSelfTouchingHoles, nullptr );
222  if ( flags == 0 )
223  {
224  mValidityFailureReason = !res ? error : QString();
225  mHasCachedValidity = true;
226  }
227  return res;
228 }
229 
230 QPolygonF QgsCurve::asQPolygonF() const
231 {
232  const int nb = numPoints();
233  QPolygonF points;
234  points.reserve( nb );
235  for ( int i = 0; i < nb; ++i )
236  {
237  points << QPointF( xAt( i ), yAt( i ) );
238  }
239  return points;
240 }
241 
243 {
244  return startPoint().distance( endPoint() );
245 }
246 
247 double QgsCurve::sinuosity() const
248 {
249  double d = straightDistance2d();
250  if ( qgsDoubleNear( d, 0.0 ) )
251  return std::numeric_limits<double>::quiet_NaN();
252 
253  return length() / d;
254 }
255 
257 {
258  double a = 0;
259  sumUpArea( a );
260  return a < 0 ? Clockwise : CounterClockwise;
261 }
262 
264 {
265  mBoundingBox = QgsRectangle();
266  mHasCachedValidity = false;
267  mValidityFailureReason.clear();
269 }
270 
272 {
273  return numPoints();
274 }
275 
276 QgsPoint QgsCurve::childPoint( int index ) const
277 {
278  QgsPoint point;
280  bool res = pointAt( index, point, type );
281  Q_ASSERT( res );
282  Q_UNUSED( res )
283  return point;
284 }
285 
286 bool QgsCurve::snapToGridPrivate( double hSpacing, double vSpacing, double dSpacing, double mSpacing,
287  const QVector<double> &srcX, const QVector<double> &srcY, const QVector<double> &srcZ, const QVector<double> &srcM,
288  QVector<double> &outX, QVector<double> &outY, QVector<double> &outZ, QVector<double> &outM ) const
289 {
290  int length = numPoints();
291 
292  if ( length <= 0 )
293  return false;
294 
295  bool hasZ = is3D();
296  bool hasM = isMeasure();
297 
298  // helper functions
299  auto roundVertex = [hSpacing, vSpacing, dSpacing, mSpacing, hasZ, hasM, &srcX, &srcY, &srcZ, &srcM]( QgsPoint & out, int i )
300  {
301  if ( hSpacing > 0 )
302  out.setX( std::round( srcX.at( i ) / hSpacing ) * hSpacing );
303  else
304  out.setX( srcX.at( i ) );
305 
306  if ( vSpacing > 0 )
307  out.setY( std::round( srcY.at( i ) / vSpacing ) * vSpacing );
308  else
309  out.setY( srcY.at( i ) );
310 
311  if ( hasZ )
312  {
313  if ( dSpacing > 0 )
314  out.setZ( std::round( srcZ.at( i ) / dSpacing ) * dSpacing );
315  else
316  out.setZ( srcZ.at( i ) );
317  }
318 
319  if ( hasM )
320  {
321  if ( mSpacing > 0 )
322  out.setM( std::round( srcM.at( i ) / mSpacing ) * mSpacing );
323  else
324  out.setM( srcM.at( i ) );
325  }
326  };
327 
328 
329  auto append = [hasZ, hasM, &outX, &outY, &outM, &outZ]( QgsPoint const & point )
330  {
331  outX.append( point.x() );
332 
333  outY.append( point.y() );
334 
335  if ( hasZ )
336  outZ.append( point.z() );
337 
338  if ( hasM )
339  outM.append( point.m() );
340  };
341 
342  auto isPointEqual = [dSpacing, mSpacing, hasZ, hasM]( const QgsPoint & a, const QgsPoint & b )
343  {
344  return ( a.x() == b.x() )
345  && ( a.y() == b.y() )
346  && ( !hasZ || dSpacing <= 0 || a.z() == b.z() )
347  && ( !hasM || mSpacing <= 0 || a.m() == b.m() );
348  };
349 
350  // temporary values
351  QgsWkbTypes::Type pointType = QgsWkbTypes::zmType( QgsWkbTypes::Point, hasZ, hasM );
352  QgsPoint last( pointType );
353  QgsPoint current( pointType );
354 
355  // Actual code (what does all the work)
356  roundVertex( last, 0 );
357  append( last );
358 
359  for ( int i = 1; i < length; ++i )
360  {
361  roundVertex( current, i );
362  if ( !isPointEqual( current, last ) )
363  {
364  append( current );
365  last = current;
366  }
367  }
368 
369  // if it's not closed, with 2 points you get a correct line
370  // if it is, you need at least 4 (3 + the vertex that closes)
371  if ( outX.length() < 2 || ( isClosed() && outX.length() < 4 ) )
372  return false;
373 
374  return true;
375 }
bool isMeasure() const
Returns true if the geometry contains m values.
int precision
A rectangle specified with double values.
Definition: qgsrectangle.h:41
double y
Definition: qgspoint.h:42
virtual bool isEmpty() const
Returns true if the geometry is empty.
int partCount() const override
Returns count of parts contained in the geometry.
Definition: qgscurve.cpp:184
bool operator==(const QgsAbstractGeometry &other) const override
Definition: qgscurve.cpp:26
double sinuosity() const
Returns the curve sinuosity, which is the ratio of the curve length() to curve straightDistance2d().
Definition: qgscurve.cpp:247
Multi point geometry collection.
Definition: qgsmultipoint.h:29
QVector< QgsRingSequence > QgsCoordinateSequence
double distance(double x, double y) const
Returns the Cartesian 2D distance between this point and a specified x, y coordinate.
Definition: qgspoint.h:333
bool nextVertex(QgsVertexId &id, QgsPoint &vertex) const override
Returns next vertex id and coordinates.
Definition: qgscurve.cpp:78
bool qgsDoubleNear(double a, double b, double epsilon=4 *std::numeric_limits< double >::epsilon())
Compare two doubles (but allow some difference)
Definition: qgis.h:315
bool isValid(QString *errorMsg=nullptr, bool allowSelfTouchingHoles=false, QgsGeometry *errorLoc=nullptr) const override
Returns true if the geometry is valid.
Definition: qgsgeos.cpp:1680
virtual void sumUpArea(double &sum) const =0
Sums up the area of the curve by iterating over the vertices (shoelace formula).
virtual bool isRing() const
Returns true if the curve is a ring.
Definition: qgscurve.cpp:56
void clearCache() const override
Clears any cached parameters associated with the geometry, e.g., bounding boxes.
Definition: qgscurve.cpp:263
SegmentationToleranceType
Segmentation tolerance as maximum angle or maximum difference between approximation and circle...
virtual bool pointAt(int node, QgsPoint &point, QgsVertexId::VertexType &type) const =0
Returns the point and vertex id of a point within the curve.
virtual bool equals(const QgsCurve &other) const =0
Checks whether this curve exactly equals another curve.
QgsPoint childPoint(int index) const override
Returns point at index (for geometries without child geometries - i.e.
Definition: qgscurve.cpp:276
int childCount() const override
Returns number of child geometries (for geometries with child geometries) or child points (for geomet...
Definition: qgscurve.cpp:271
QPainterPath asQPainterPath() const override
Returns the geometry represented as a QPainterPath.
Definition: qgscurve.cpp:61
Indicates that self-touching holes are permitted. OGC validity states that self-touching holes are NO...
Definition: qgsgeometry.h:368
virtual QgsPoint endPoint() const =0
Returns the end point of the curve.
virtual QPolygonF asQPolygonF() const
Returns a QPolygonF representing the points.
Definition: qgscurve.cpp:230
virtual double length() const
Returns the planar, 2-dimensional length of the geometry.
Type
The WKB type describes the number of dimensions a geometry has.
Definition: qgswkbtypes.h:68
virtual void clearCache() const
Clears any cached parameters associated with the geometry, e.g., bounding boxes.
QString asKml(int precision=17) const override
Returns a KML representation of the geometry.
Definition: qgscurve.cpp:155
Utility class for identifying a unique vertex within a geometry.
int vertexCount(int part=0, int ring=0) const override
Returns the number of vertices of which this geometry is built.
Definition: qgscurve.cpp:171
T qgsgeometry_cast(const QgsAbstractGeometry *geom)
virtual double xAt(int index) const =0
Returns the x-coordinate of the specified node in the line string.
Orientation
Curve orientation.
Definition: qgscurve.h:237
Abstract base class for curved geometry type.
Definition: qgscurve.h:35
Abstract base class for all geometries.
Does vector analysis using the geos library and handles import, export, exception handling*...
Definition: qgsgeos.h:103
QgsPoint vertexAt(QgsVertexId id) const override
Returns the point corresponding to a specified vertex id.
Definition: qgscurve.cpp:189
Counter-clockwise orientation.
Definition: qgscurve.h:240
Point geometry type, with support for z-dimension and m-values.
Definition: qgspoint.h:37
int vertexNumberFromVertexId(QgsVertexId id) const override
Returns the vertex number corresponding to a vertex id.
Definition: qgscurve.cpp:131
QgsAbstractGeometry * boundary() const override
Returns the closure of the combinatorial boundary of the geometry (ie the topological boundary of the...
Definition: qgscurve.cpp:140
Contains geos related utilities and functions.
Definition: qgsgeos.h:41
virtual bool isClosed() const
Returns true if the curve is closed.
Definition: qgscurve.cpp:40
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) const
Helper function for QgsCurve subclasses to snap to grids.
Definition: qgscurve.cpp:286
QgsCoordinateSequence coordinateSequence() const override
Retrieves the sequence of geometries, rings and nodes.
Definition: qgscurve.cpp:68
Clockwise orientation.
Definition: qgscurve.h:239
Orientation orientation() const
Returns the curve&#39;s orientation, e.g.
Definition: qgscurve.cpp:256
QVector< QgsPoint > QgsPointSequence
QgsCurve * segmentize(double tolerance=M_PI_2/90, SegmentationToleranceType toleranceType=MaximumAngle) const override
Returns a geometry without curves.
Definition: qgscurve.cpp:166
bool addGeometry(QgsAbstractGeometry *g) override
Adds a geometry and takes ownership. Returns true in case of success.
virtual void addToPainterPath(QPainterPath &path) const =0
Adds a curve to a painter path.
QVector< QgsPointSequence > QgsRingSequence
void reserve(int size)
Attempts to allocate memory for at least size geometries.
QgsCurve * toCurveType() const override
Returns the geometry converted to the more generic curve type.
Definition: qgscurve.cpp:197
QgsRectangle boundingBox() const override
Returns the minimal bounding box for the geometry.
Definition: qgscurve.cpp:202
double straightDistance2d() const
Returns the straight distance of the curve, i.e.
Definition: qgscurve.cpp:242
int ringCount(int part=0) const override
Returns the number of rings of which this geometry is built.
Definition: qgscurve.cpp:178
void adjacentVertices(QgsVertexId vertex, QgsVertexId &previousVertex, QgsVertexId &nextVertex) const override
Returns the vertices adjacent to a specified vertex within a geometry.
Definition: qgscurve.cpp:103
QgsCurve * clone() const override=0
Clones the geometry by performing a deep copy.
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...
bool isNull() const
Test if the rectangle is null (all coordinates zero or after call to setMinimal()).
Definition: qgsrectangle.h:447
bool isValid(QString &error, int flags=0) const override
Checks validity of the geometry, and returns true if the geometry is valid.
Definition: qgscurve.cpp:211
virtual QgsRectangle calculateBoundingBox() const
Default calculator for the minimal bounding box for the geometry.
virtual double yAt(int index) const =0
Returns the y-coordinate of the specified node in the line string.
static Type zmType(Type type, bool hasZ, bool hasM)
Returns the modified input geometry type according to hasZ / hasM.
Definition: qgswkbtypes.h:800
double z
Definition: qgspoint.h:43
virtual QgsPoint startPoint() const =0
Returns the starting point of the curve.
bool operator!=(const QgsAbstractGeometry &other) const override
Definition: qgscurve.cpp:35
virtual int numPoints() const =0
Returns the number of points in the curve.
bool is3D() const
Returns true if the geometry is 3D and contains a z-value.
virtual void points(QgsPointSequence &pt) const =0
Returns a list of points within the curve.
double x
Definition: qgspoint.h:41