QGIS API Documentation  3.10.0-A Coruña (6c816b4204)
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(), 1E-8 ) &&
50  qgsDoubleNear( start.y(), end.y(), 1E-8 );
51  if ( is3D() && closed )
52  closed &= qgsDoubleNear( start.z(), end.z(), 1E-8 ) || ( 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 
62 {
63  QgsCoordinateSequence sequence;
64  sequence.append( QgsRingSequence() );
65  sequence.back().append( QgsPointSequence() );
66  points( sequence.back().back() );
67 
68  return sequence;
69 }
70 
71 bool QgsCurve::nextVertex( QgsVertexId &id, QgsPoint &vertex ) const
72 {
73  if ( id.vertex < 0 )
74  {
75  id.vertex = 0;
76  if ( id.part < 0 )
77  {
78  id.part = 0;
79  }
80  if ( id.ring < 0 )
81  {
82  id.ring = 0;
83  }
84  }
85  else
86  {
87  if ( id.vertex + 1 >= numPoints() )
88  {
89  return false;
90  }
91  ++id.vertex;
92  }
93  return pointAt( id.vertex, vertex, id.type );
94 }
95 
97 {
98  int n = numPoints();
99  if ( vertex.vertex < 0 || vertex.vertex >= n )
100  {
101  previousVertex = QgsVertexId();
102  nextVertex = QgsVertexId();
103  return;
104  }
105 
106  if ( vertex.vertex == 0 )
107  {
108  previousVertex = QgsVertexId();
109  }
110  else
111  {
112  previousVertex = QgsVertexId( vertex.part, vertex.ring, vertex.vertex - 1 );
113  }
114  if ( vertex.vertex == n - 1 )
115  {
116  nextVertex = QgsVertexId();
117  }
118  else
119  {
120  nextVertex = QgsVertexId( vertex.part, vertex.ring, vertex.vertex + 1 );
121  }
122 }
123 
125 {
126  if ( id.part != 0 || id.ring != 0 )
127  return -1;
128  if ( id.vertex < 0 || id.vertex >= numPoints() )
129  return -1;
130  return id.vertex;
131 }
132 
134 {
135  if ( isEmpty() )
136  return nullptr;
137 
138  if ( isClosed() )
139  return nullptr;
140 
141  QgsMultiPoint *multiPoint = new QgsMultiPoint();
142  multiPoint->reserve( 2 );
143  multiPoint->addGeometry( new QgsPoint( startPoint() ) );
144  multiPoint->addGeometry( new QgsPoint( endPoint() ) );
145  return multiPoint;
146 }
147 
148 QgsCurve *QgsCurve::segmentize( double tolerance, SegmentationToleranceType toleranceType ) const
149 {
150  return curveToLine( tolerance, toleranceType );
151 }
152 
153 int QgsCurve::vertexCount( int part, int ring ) const
154 {
155  Q_UNUSED( part )
156  Q_UNUSED( ring )
157  return numPoints();
158 }
159 
160 int QgsCurve::ringCount( int part ) const
161 {
162  Q_UNUSED( part )
163  return numPoints() > 0 ? 1 : 0;
164 }
165 
167 {
168  return numPoints() > 0 ? 1 : 0;
169 }
170 
172 {
173  QgsPoint v;
175  pointAt( id.vertex, v, type );
176  return v;
177 }
178 
180 {
181  return clone();
182 }
183 
185 {
186  if ( mBoundingBox.isNull() )
187  {
188  mBoundingBox = calculateBoundingBox();
189  }
190  return mBoundingBox;
191 }
192 
193 bool QgsCurve::isValid( QString &error, int flags ) const
194 {
195  if ( flags == 0 && mHasCachedValidity )
196  {
197  // use cached validity results
198  error = mValidityFailureReason;
199  return error.isEmpty();
200  }
201 
202  QgsGeos geos( this );
203  bool res = geos.isValid( &error, flags & QgsGeometry::FlagAllowSelfTouchingHoles, nullptr );
204  if ( flags == 0 )
205  {
206  mValidityFailureReason = !res ? error : QString();
207  mHasCachedValidity = true;
208  }
209  return res;
210 }
211 
212 QPolygonF QgsCurve::asQPolygonF() const
213 {
214  const int nb = numPoints();
215  QPolygonF points;
216  points.reserve( nb );
217  for ( int i = 0; i < nb; ++i )
218  {
219  points << QPointF( xAt( i ), yAt( i ) );
220  }
221  return points;
222 }
223 
225 {
226  return startPoint().distance( endPoint() );
227 }
228 
229 double QgsCurve::sinuosity() const
230 {
231  double d = straightDistance2d();
232  if ( qgsDoubleNear( d, 0.0 ) )
233  return std::numeric_limits<double>::quiet_NaN();
234 
235  return length() / d;
236 }
237 
239 {
240  double a = 0;
241  sumUpArea( a );
242  return a < 0 ? Clockwise : CounterClockwise;
243 }
244 
246 {
247  mBoundingBox = QgsRectangle();
248  mHasCachedValidity = false;
249  mValidityFailureReason.clear();
251 }
252 
254 {
255  return numPoints();
256 }
257 
258 QgsPoint QgsCurve::childPoint( int index ) const
259 {
260  QgsPoint point;
262  bool res = pointAt( index, point, type );
263  Q_ASSERT( res );
264  Q_UNUSED( res )
265  return point;
266 }
267 
268 bool QgsCurve::snapToGridPrivate( double hSpacing, double vSpacing, double dSpacing, double mSpacing,
269  const QVector<double> &srcX, const QVector<double> &srcY, const QVector<double> &srcZ, const QVector<double> &srcM,
270  QVector<double> &outX, QVector<double> &outY, QVector<double> &outZ, QVector<double> &outM ) const
271 {
272  int length = numPoints();
273 
274  if ( length <= 0 )
275  return false;
276 
277  bool hasZ = is3D();
278  bool hasM = isMeasure();
279 
280  // helper functions
281  auto roundVertex = [hSpacing, vSpacing, dSpacing, mSpacing, hasZ, hasM, &srcX, &srcY, &srcZ, &srcM]( QgsPoint & out, int i )
282  {
283  if ( hSpacing > 0 )
284  out.setX( std::round( srcX.at( i ) / hSpacing ) * hSpacing );
285  else
286  out.setX( srcX.at( i ) );
287 
288  if ( vSpacing > 0 )
289  out.setY( std::round( srcY.at( i ) / vSpacing ) * vSpacing );
290  else
291  out.setY( srcY.at( i ) );
292 
293  if ( hasZ )
294  {
295  if ( dSpacing > 0 )
296  out.setZ( std::round( srcZ.at( i ) / dSpacing ) * dSpacing );
297  else
298  out.setZ( srcZ.at( i ) );
299  }
300 
301  if ( hasM )
302  {
303  if ( mSpacing > 0 )
304  out.setM( std::round( srcM.at( i ) / mSpacing ) * mSpacing );
305  else
306  out.setM( srcM.at( i ) );
307  }
308  };
309 
310 
311  auto append = [hasZ, hasM, &outX, &outY, &outM, &outZ]( QgsPoint const & point )
312  {
313  outX.append( point.x() );
314 
315  outY.append( point.y() );
316 
317  if ( hasZ )
318  outZ.append( point.z() );
319 
320  if ( hasM )
321  outM.append( point.m() );
322  };
323 
324  auto isPointEqual = [dSpacing, mSpacing, hasZ, hasM]( const QgsPoint & a, const QgsPoint & b )
325  {
326  return ( a.x() == b.x() )
327  && ( a.y() == b.y() )
328  && ( !hasZ || dSpacing <= 0 || a.z() == b.z() )
329  && ( !hasM || mSpacing <= 0 || a.m() == b.m() );
330  };
331 
332  // temporary values
333  QgsWkbTypes::Type pointType = QgsWkbTypes::zmType( QgsWkbTypes::Point, hasZ, hasM );
334  QgsPoint last( pointType );
335  QgsPoint current( pointType );
336 
337  // Actual code (what does all the work)
338  roundVertex( last, 0 );
339  append( last );
340 
341  for ( int i = 1; i < length; ++i )
342  {
343  roundVertex( current, i );
344  if ( !isPointEqual( current, last ) )
345  {
346  append( current );
347  last = current;
348  }
349  }
350 
351  // if it's not closed, with 2 points you get a correct line
352  // if it is, you need at least 4 (3 + the vertex that closes)
353  if ( outX.length() < 2 || ( isClosed() && outX.length() < 4 ) )
354  return false;
355 
356  return true;
357 }
bool isMeasure() const
Returns true if the geometry contains m values.
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:166
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:229
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:325
bool nextVertex(QgsVertexId &id, QgsPoint &vertex) const override
Returns next vertex id and coordinates.
Definition: qgscurve.cpp:71
bool qgsDoubleNear(double a, double b, double epsilon=4 *std::numeric_limits< double >::epsilon())
Compare two doubles (but allow some difference)
Definition: qgis.h:280
bool isValid(QString *errorMsg=nullptr, bool allowSelfTouchingHoles=false, QgsGeometry *errorLoc=nullptr) const override
Returns true if the geometry is valid.
Definition: qgsgeos.cpp:1665
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:245
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:258
int childCount() const override
Returns number of child geometries (for geometries with child geometries) or child points (for geomet...
Definition: qgscurve.cpp:253
Indicates that self-touching holes are permitted. OGC validity states that self-touching holes are NO...
Definition: qgsgeometry.h:367
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:212
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.
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:153
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:234
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:171
Counter-clockwise orientation.
Definition: qgscurve.h:237
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:124
QgsAbstractGeometry * boundary() const override
Returns the closure of the combinatorial boundary of the geometry (ie the topological boundary of the...
Definition: qgscurve.cpp:133
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:268
QgsCoordinateSequence coordinateSequence() const override
Retrieves the sequence of geometries, rings and nodes.
Definition: qgscurve.cpp:61
Clockwise orientation.
Definition: qgscurve.h:236
Orientation orientation() const
Returns the curve&#39;s orientation, e.g.
Definition: qgscurve.cpp:238
QVector< QgsPoint > QgsPointSequence
QgsCurve * segmentize(double tolerance=M_PI_2/90, SegmentationToleranceType toleranceType=MaximumAngle) const override
Returns a geometry without curves.
Definition: qgscurve.cpp:148
bool addGeometry(QgsAbstractGeometry *g) override
Adds a geometry and takes ownership. Returns true in case of success.
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:179
QgsRectangle boundingBox() const override
Returns the minimal bounding box for the geometry.
Definition: qgscurve.cpp:184
double straightDistance2d() const
Returns the straight distance of the curve, i.e.
Definition: qgscurve.cpp:224
int ringCount(int part=0) const override
Returns the number of rings of which this geometry is built.
Definition: qgscurve.cpp:160
void adjacentVertices(QgsVertexId vertex, QgsVertexId &previousVertex, QgsVertexId &nextVertex) const override
Returns the vertices adjacent to a specified vertex within a geometry.
Definition: qgscurve.cpp:96
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:436
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:193
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:675
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