QGIS API Documentation  3.8.0-Zanzibar (11aff65)
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->addGeometry( new QgsPoint( startPoint() ) );
143  multiPoint->addGeometry( new QgsPoint( endPoint() ) );
144  return multiPoint;
145 }
146 
147 QgsCurve *QgsCurve::segmentize( double tolerance, SegmentationToleranceType toleranceType ) const
148 {
149  return curveToLine( tolerance, toleranceType );
150 }
151 
152 int QgsCurve::vertexCount( int part, int ring ) const
153 {
154  Q_UNUSED( part )
155  Q_UNUSED( ring )
156  return numPoints();
157 }
158 
159 int QgsCurve::ringCount( int part ) const
160 {
161  Q_UNUSED( part )
162  return numPoints() > 0 ? 1 : 0;
163 }
164 
166 {
167  return numPoints() > 0 ? 1 : 0;
168 }
169 
171 {
172  QgsPoint v;
174  pointAt( id.vertex, v, type );
175  return v;
176 }
177 
179 {
180  return clone();
181 }
182 
184 {
185  if ( mBoundingBox.isNull() )
186  {
187  mBoundingBox = calculateBoundingBox();
188  }
189  return mBoundingBox;
190 }
191 
192 bool QgsCurve::isValid( QString &error, int flags ) const
193 {
194  if ( flags == 0 && mHasCachedValidity )
195  {
196  // use cached validity results
197  error = mValidityFailureReason;
198  return error.isEmpty();
199  }
200 
201  QgsGeos geos( this );
202  bool res = geos.isValid( &error, flags & QgsGeometry::FlagAllowSelfTouchingHoles, nullptr );
203  if ( flags == 0 )
204  {
205  mValidityFailureReason = !res ? error : QString();
206  mHasCachedValidity = true;
207  }
208  return res;
209 }
210 
211 QPolygonF QgsCurve::asQPolygonF() const
212 {
213  const int nb = numPoints();
214  QPolygonF points;
215  points.reserve( nb );
216  for ( int i = 0; i < nb; ++i )
217  {
218  points << QPointF( xAt( i ), yAt( i ) );
219  }
220  return points;
221 }
222 
224 {
225  return startPoint().distance( endPoint() );
226 }
227 
228 double QgsCurve::sinuosity() const
229 {
230  double d = straightDistance2d();
231  if ( qgsDoubleNear( d, 0.0 ) )
232  return std::numeric_limits<double>::quiet_NaN();
233 
234  return length() / d;
235 }
236 
238 {
239  double a = 0;
240  sumUpArea( a );
241  return a < 0 ? Clockwise : CounterClockwise;
242 }
243 
245 {
246  mBoundingBox = QgsRectangle();
247  mHasCachedValidity = false;
248  mValidityFailureReason.clear();
250 }
251 
253 {
254  return numPoints();
255 }
256 
257 QgsPoint QgsCurve::childPoint( int index ) const
258 {
259  QgsPoint point;
261  bool res = pointAt( index, point, type );
262  Q_ASSERT( res );
263  Q_UNUSED( res )
264  return point;
265 }
266 
267 bool QgsCurve::snapToGridPrivate( double hSpacing, double vSpacing, double dSpacing, double mSpacing,
268  const QVector<double> &srcX, const QVector<double> &srcY, const QVector<double> &srcZ, const QVector<double> &srcM,
269  QVector<double> &outX, QVector<double> &outY, QVector<double> &outZ, QVector<double> &outM ) const
270 {
271  int length = numPoints();
272 
273  if ( length <= 0 )
274  return false;
275 
276  bool hasZ = is3D();
277  bool hasM = isMeasure();
278 
279  // helper functions
280  auto roundVertex = [hSpacing, vSpacing, dSpacing, mSpacing, hasZ, hasM, &srcX, &srcY, &srcZ, &srcM]( QgsPoint & out, int i )
281  {
282  if ( hSpacing > 0 )
283  out.setX( std::round( srcX.at( i ) / hSpacing ) * hSpacing );
284  else
285  out.setX( srcX.at( i ) );
286 
287  if ( vSpacing > 0 )
288  out.setY( std::round( srcY.at( i ) / vSpacing ) * vSpacing );
289  else
290  out.setY( srcY.at( i ) );
291 
292  if ( hasZ )
293  {
294  if ( dSpacing > 0 )
295  out.setZ( std::round( srcZ.at( i ) / dSpacing ) * dSpacing );
296  else
297  out.setZ( srcZ.at( i ) );
298  }
299 
300  if ( hasM )
301  {
302  if ( mSpacing > 0 )
303  out.setM( std::round( srcM.at( i ) / mSpacing ) * mSpacing );
304  else
305  out.setM( srcM.at( i ) );
306  }
307  };
308 
309 
310  auto append = [hasZ, hasM, &outX, &outY, &outM, &outZ]( QgsPoint const & point )
311  {
312  outX.append( point.x() );
313 
314  outY.append( point.y() );
315 
316  if ( hasZ )
317  outZ.append( point.z() );
318 
319  if ( hasM )
320  outM.append( point.m() );
321  };
322 
323  auto isPointEqual = [dSpacing, mSpacing, hasZ, hasM]( const QgsPoint & a, const QgsPoint & b )
324  {
325  return ( a.x() == b.x() )
326  && ( a.y() == b.y() )
327  && ( !hasZ || dSpacing <= 0 || a.z() == b.z() )
328  && ( !hasM || mSpacing <= 0 || a.m() == b.m() );
329  };
330 
331  // temporary values
332  QgsWkbTypes::Type pointType = QgsWkbTypes::zmType( QgsWkbTypes::Point, hasZ, hasM );
333  QgsPoint last( pointType );
334  QgsPoint current( pointType );
335 
336  // Actual code (what does all the work)
337  roundVertex( last, 0 );
338  append( last );
339 
340  for ( int i = 1; i < length; ++i )
341  {
342  roundVertex( current, i );
343  if ( !isPointEqual( current, last ) )
344  {
345  append( current );
346  last = current;
347  }
348  }
349 
350  // if it's not closed, with 2 points you get a correct line
351  // if it is, you need at least 4 (3 + the vertex that closes)
352  if ( outX.length() < 2 || ( isClosed() && outX.length() < 4 ) )
353  return false;
354 
355  return true;
356 }
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:165
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:228
Multi point geometry collection.
Definition: qgsmultipoint.h:29
QVector< QgsRingSequence > QgsCoordinateSequence
double distance(double x, double y) const
Returns the distance between this point and a specified x, y coordinate.
Definition: qgspoint.h:276
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:265
bool isValid(QString *errorMsg=nullptr, bool allowSelfTouchingHoles=false, QgsGeometry *errorLoc=nullptr) const override
Returns true if the geometry is valid.
Definition: qgsgeos.cpp:1661
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:244
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:257
int childCount() const override
Returns number of child geometries (for geometries with child geometries) or child points (for geomet...
Definition: qgscurve.cpp:252
Indicates that self-touching holes are permitted. OGC validity states that self-touching holes are NO...
Definition: qgsgeometry.h:356
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:211
virtual double length() const
Returns the 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:152
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:170
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:267
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:237
QVector< QgsPoint > QgsPointSequence
QgsCurve * segmentize(double tolerance=M_PI_2/90, SegmentationToleranceType toleranceType=MaximumAngle) const override
Returns a geometry without curves.
Definition: qgscurve.cpp:147
bool addGeometry(QgsAbstractGeometry *g) override
Adds a geometry and takes ownership. Returns true in case of success.
QVector< QgsPointSequence > QgsRingSequence
QgsCurve * toCurveType() const override
Returns the geometry converted to the more generic curve type.
Definition: qgscurve.cpp:178
QgsRectangle boundingBox() const override
Returns the minimal bounding box for the geometry.
Definition: qgscurve.cpp:183
double straightDistance2d() const
Returns the straight distance of the curve, i.e.
Definition: qgscurve.cpp:223
int ringCount(int part=0) const override
Returns the number of rings of which this geometry is built.
Definition: qgscurve.cpp:159
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:192
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:529
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