QGIS API Documentation  3.14.0-Pi (9f7028fd23)
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 *
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
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
96 void QgsCurve::adjacentVertices( QgsVertexId vertex, QgsVertexId &previousVertex, QgsVertexId &nextVertex ) const
97 {
98  int n = numPoints();
99  if ( vertex.vertex < 0 || vertex.vertex >= n )
100  {
101  previousVertex = 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  {
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 QString QgsCurve::asKml( int precision ) const
149 {
150  std::unique_ptr<QgsLineString> lineString( curveToLine() );
151  if ( !lineString )
152  {
153  return QString();
154  }
155  QString kml = lineString->asKml( precision );
156  return kml;
157 }
158
159 QgsCurve *QgsCurve::segmentize( double tolerance, SegmentationToleranceType toleranceType ) const
160 {
161  return curveToLine( tolerance, toleranceType );
162 }
163
164 int QgsCurve::vertexCount( int part, int ring ) const
165 {
166  Q_UNUSED( part )
167  Q_UNUSED( ring )
168  return numPoints();
169 }
170
171 int QgsCurve::ringCount( int part ) const
172 {
173  Q_UNUSED( part )
174  return numPoints() > 0 ? 1 : 0;
175 }
176
178 {
179  return numPoints() > 0 ? 1 : 0;
180 }
181
183 {
184  QgsPoint v;
186  pointAt( id.vertex, v, type );
187  return v;
188 }
189
191 {
192  return clone();
193 }
194
196 {
197  if ( mBoundingBox.isNull() )
198  {
199  mBoundingBox = calculateBoundingBox();
200  }
201  return mBoundingBox;
202 }
203
204 bool QgsCurve::isValid( QString &error, int flags ) const
205 {
206  if ( flags == 0 && mHasCachedValidity )
207  {
208  // use cached validity results
209  error = mValidityFailureReason;
210  return error.isEmpty();
211  }
212
213  QgsGeos geos( this );
214  bool res = geos.isValid( &error, flags & QgsGeometry::FlagAllowSelfTouchingHoles, nullptr );
215  if ( flags == 0 )
216  {
217  mValidityFailureReason = !res ? error : QString();
218  mHasCachedValidity = true;
219  }
220  return res;
221 }
222
223 QPolygonF QgsCurve::asQPolygonF() const
224 {
225  const int nb = numPoints();
226  QPolygonF points;
227  points.reserve( nb );
228  for ( int i = 0; i < nb; ++i )
229  {
230  points << QPointF( xAt( i ), yAt( i ) );
231  }
232  return points;
233 }
234
236 {
237  return startPoint().distance( endPoint() );
238 }
239
240 double QgsCurve::sinuosity() const
241 {
242  double d = straightDistance2d();
243  if ( qgsDoubleNear( d, 0.0 ) )
244  return std::numeric_limits<double>::quiet_NaN();
245
246  return length() / d;
247 }
248
250 {
251  double a = 0;
252  sumUpArea( a );
253  return a < 0 ? Clockwise : CounterClockwise;
254 }
255
257 {
258  mBoundingBox = QgsRectangle();
259  mHasCachedValidity = false;
260  mValidityFailureReason.clear();
262 }
263
265 {
266  return numPoints();
267 }
268
269 QgsPoint QgsCurve::childPoint( int index ) const
270 {
271  QgsPoint point;
273  bool res = pointAt( index, point, type );
274  Q_ASSERT( res );
275  Q_UNUSED( res )
276  return point;
277 }
278
279 bool QgsCurve::snapToGridPrivate( double hSpacing, double vSpacing, double dSpacing, double mSpacing,
280  const QVector<double> &srcX, const QVector<double> &srcY, const QVector<double> &srcZ, const QVector<double> &srcM,
281  QVector<double> &outX, QVector<double> &outY, QVector<double> &outZ, QVector<double> &outM ) const
282 {
283  int length = numPoints();
284
285  if ( length <= 0 )
286  return false;
287
288  bool hasZ = is3D();
289  bool hasM = isMeasure();
290
291  // helper functions
292  auto roundVertex = [hSpacing, vSpacing, dSpacing, mSpacing, hasZ, hasM, &srcX, &srcY, &srcZ, &srcM]( QgsPoint & out, int i )
293  {
294  if ( hSpacing > 0 )
295  out.setX( std::round( srcX.at( i ) / hSpacing ) * hSpacing );
296  else
297  out.setX( srcX.at( i ) );
298
299  if ( vSpacing > 0 )
300  out.setY( std::round( srcY.at( i ) / vSpacing ) * vSpacing );
301  else
302  out.setY( srcY.at( i ) );
303
304  if ( hasZ )
305  {
306  if ( dSpacing > 0 )
307  out.setZ( std::round( srcZ.at( i ) / dSpacing ) * dSpacing );
308  else
309  out.setZ( srcZ.at( i ) );
310  }
311
312  if ( hasM )
313  {
314  if ( mSpacing > 0 )
315  out.setM( std::round( srcM.at( i ) / mSpacing ) * mSpacing );
316  else
317  out.setM( srcM.at( i ) );
318  }
319  };
320
321
322  auto append = [hasZ, hasM, &outX, &outY, &outM, &outZ]( QgsPoint const & point )
323  {
324  outX.append( point.x() );
325
326  outY.append( point.y() );
327
328  if ( hasZ )
329  outZ.append( point.z() );
330
331  if ( hasM )
332  outM.append( point.m() );
333  };
334
335  auto isPointEqual = [dSpacing, mSpacing, hasZ, hasM]( const QgsPoint & a, const QgsPoint & b )
336  {
337  return ( a.x() == b.x() )
338  && ( a.y() == b.y() )
339  && ( !hasZ || dSpacing <= 0 || a.z() == b.z() )
340  && ( !hasM || mSpacing <= 0 || a.m() == b.m() );
341  };
342
343  // temporary values
344  QgsWkbTypes::Type pointType = QgsWkbTypes::zmType( QgsWkbTypes::Point, hasZ, hasM );
345  QgsPoint last( pointType );
346  QgsPoint current( pointType );
347
348  // Actual code (what does all the work)
349  roundVertex( last, 0 );
350  append( last );
351
352  for ( int i = 1; i < length; ++i )
353  {
354  roundVertex( current, i );
355  if ( !isPointEqual( current, last ) )
356  {
357  append( current );
358  last = current;
359  }
360  }
361
362  // if it's not closed, with 2 points you get a correct line
363  // if it is, you need at least 4 (3 + the vertex that closes)
364  if ( outX.length() < 2 || ( isClosed() && outX.length() < 4 ) )
365  return false;
366
367  return true;
368 }
QgsCurve::isValid
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:204
QgsCurve
Abstract base class for curved geometry type.
Definition: qgscurve.h:35
QgsCurve::nextVertex
bool nextVertex(QgsVertexId &id, QgsPoint &vertex) const override
Returns next vertex id and coordinates.
Definition: qgscurve.cpp:71
QgsVertexId::part
int part
Definition: qgsabstractgeometry.h:1080
QgsCurve::pointAt
virtual bool pointAt(int node, QgsPoint &point, QgsVertexId::VertexType &type) const =0
Returns the point and vertex id of a point within the curve.
QgsCoordinateSequence
QVector< QgsRingSequence > QgsCoordinateSequence
Definition: qgsabstractgeometry.h:49
QgsPoint::distance
double distance(double x, double y) const
Returns the Cartesian 2D distance between this point and a specified x, y coordinate.
Definition: qgspoint.h:350
QgsVertexId::vertex
int vertex
Definition: qgsabstractgeometry.h:1082
QgsAbstractGeometry::clearCache
virtual void clearCache() const
Clears any cached parameters associated with the geometry, e.g., bounding boxes.
Definition: qgsabstractgeometry.cpp:112
QgsWkbTypes::Point
@ Point
Definition: qgswkbtypes.h:71
qgslinestring.h
QgsCurve::startPoint
virtual QgsPoint startPoint() const =0
Returns the starting point of the curve.
QgsPoint
Point geometry type, with support for z-dimension and m-values.
Definition: qgspoint.h:37
QgsGeometryCollection::reserve
void reserve(int size)
Attempts to allocate memory for at least size geometries.
Definition: qgsgeometrycollection.cpp:202
QgsCurve::operator==
bool operator==(const QgsAbstractGeometry &other) const override
Definition: qgscurve.cpp:26
Adds a geometry and takes ownership. Returns true in case of success.
Definition: qgsmultipoint.cpp:138
geos
Contains geos related utilities and functions.
Definition: qgsgeos.h:41
qgsmultipoint.h
QgsCurve::childCount
int childCount() const override
Returns number of child geometries (for geometries with child geometries) or child points (for geomet...
Definition: qgscurve.cpp:264
QgsPoint::z
double z
Definition: qgspoint.h:60
QgsAbstractGeometry::length
virtual double length() const
Returns the planar, 2-dimensional length of the geometry.
Definition: qgsabstractgeometry.cpp:132
QgsWkbTypes::Type
Type
The WKB type describes the number of dimensions a geometry has.
Definition: qgswkbtypes.h:68
QgsAbstractGeometry::SegmentationToleranceType
SegmentationToleranceType
Segmentation tolerance as maximum angle or maximum difference between approximation and circle.
Definition: qgsabstractgeometry.h:112
qgspoint.h
QgsCurve::sumUpArea
virtual void sumUpArea(double &sum) const =0
Sums up the area of the curve by iterating over the vertices (shoelace formula).
QgsRectangle
Definition: qgsrectangle.h:41
QgsCurve::partCount
int partCount() const override
Returns count of parts contained in the geometry.
Definition: qgscurve.cpp:177
QgsCurve::clearCache
void clearCache() const override
Clears any cached parameters associated with the geometry, e.g., bounding boxes.
Definition: qgscurve.cpp:256
Returns a KML representation of the geometry.
Definition: qgscurve.cpp:148
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
QgsPoint::y
double y
Definition: qgspoint.h:59
precision
int precision
Definition: qgswfsgetfeature.cpp:103
QgsCurve::childPoint
QgsPoint childPoint(int index) const override
Returns point at index (for geometries without child geometries - i.e.
Definition: qgscurve.cpp:269
QgsCurve::endPoint
virtual QgsPoint endPoint() const =0
Returns the end point of the curve.
QgsCurve::vertexNumberFromVertexId
int vertexNumberFromVertexId(QgsVertexId id) const override
Returns the vertex number corresponding to a vertex id.
Definition: qgscurve.cpp:124
QgsCurve::asQPolygonF
virtual QPolygonF asQPolygonF() const
Returns a QPolygonF representing the points.
Definition: qgscurve.cpp:223
qgsDoubleNear
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
QgsGeometry::FlagAllowSelfTouchingHoles
@ FlagAllowSelfTouchingHoles
Indicates that self-touching holes are permitted. OGC validity states that self-touching holes are NO...
Definition: qgsgeometry.h:368
QgsCurve::Clockwise
@ Clockwise
Clockwise orientation.
Definition: qgscurve.h:238
QgsCurve::equals
virtual bool equals(const QgsCurve &other) const =0
Checks whether this curve exactly equals another curve.
QgsCurve::isRing
virtual bool isRing() const
Returns true if the curve is a ring.
Definition: qgscurve.cpp:56
QgsGeos
Definition: qgsgeos.h:103
QgsCurve::CounterClockwise
@ CounterClockwise
Counter-clockwise orientation.
Definition: qgscurve.h:239
QgsCurve::yAt
virtual double yAt(int index) const =0
Returns the y-coordinate of the specified node in the line string.
QgsAbstractGeometry::is3D
bool is3D() const
Returns true if the geometry is 3D and contains a z-value.
Definition: qgsabstractgeometry.h:202
QgsCurve::vertexAt
QgsPoint vertexAt(QgsVertexId id) const override
Returns the point corresponding to a specified vertex id.
Definition: qgscurve.cpp:182
QgsRingSequence
QVector< QgsPointSequence > QgsRingSequence
Definition: qgsabstractgeometry.h:48
QgsCurve::snapToGridPrivate
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:279
QgsAbstractGeometry::isEmpty
virtual bool isEmpty() const
Returns true if the geometry is empty.
Definition: qgsabstractgeometry.cpp:298
QgsCurve::coordinateSequence
QgsCoordinateSequence coordinateSequence() const override
Retrieves the sequence of geometries, rings and nodes.
Definition: qgscurve.cpp:61
QgsMultiPoint
Multi point geometry collection.
Definition: qgsmultipoint.h:29
QgsAbstractGeometry
Abstract base class for all geometries.
Definition: qgsabstractgeometry.h:71
QgsCurve::xAt
virtual double xAt(int index) const =0
Returns the x-coordinate of the specified node in the line string.
QgsCurve::clone
QgsCurve * clone() const override=0
Clones the geometry by performing a deep copy.
QgsWkbTypes::zmType
static Type zmType(Type type, bool hasZ, bool hasM)
Returns the modified input geometry type according to hasZ / hasM.
Definition: qgswkbtypes.h:800
QgsCurve::isClosed
virtual bool isClosed() const
Returns true if the curve is closed.
Definition: qgscurve.cpp:40
QgsCurve::vertexCount
int vertexCount(int part=0, int ring=0) const override
Returns the number of vertices of which this geometry is built.
Definition: qgscurve.cpp:164
QgsPointSequence
QVector< QgsPoint > QgsPointSequence
Definition: qgsabstractgeometry.h:44
qgscurve.h
QgsCurve::ringCount
int ringCount(int part=0) const override
Returns the number of rings of which this geometry is built.
Definition: qgscurve.cpp:171
QgsCurve::toCurveType
QgsCurve * toCurveType() const override
Returns the geometry converted to the more generic curve type.
Definition: qgscurve.cpp:190
QgsCurve::boundingBox
QgsRectangle boundingBox() const override
Returns the minimal bounding box for the geometry.
Definition: qgscurve.cpp:195
QgsVertexId
Utility class for identifying a unique vertex within a geometry.
Definition: qgsabstractgeometry.h:1033
QgsCurve::boundary
QgsAbstractGeometry * boundary() const override
Returns the closure of the combinatorial boundary of the geometry (ie the topological boundary of the...
Definition: qgscurve.cpp:133
QgsVertexId::ring
int ring
Definition: qgsabstractgeometry.h:1081
QgsAbstractGeometry::isMeasure
bool isMeasure() const
Returns true if the geometry contains m values.
Definition: qgsabstractgeometry.h:211
QgsVertexId::VertexType
VertexType
Definition: qgsabstractgeometry.h:1035
QgsCurve::straightDistance2d
double straightDistance2d() const
Returns the straight distance of the curve, i.e.
Definition: qgscurve.cpp:235
QgsCurve::points
virtual void points(QgsPointSequence &pt) const =0
Returns a list of points within the curve.
QgsCurve::numPoints
virtual int numPoints() const =0
Returns the number of points in the curve.
QgsCurve::segmentize
QgsCurve * segmentize(double tolerance=M_PI_2/90, SegmentationToleranceType toleranceType=MaximumAngle) const override
Returns a geometry without curves.
Definition: qgscurve.cpp:159
QgsCurve::orientation
Orientation orientation() const
Returns the curve's orientation, e.g.
Definition: qgscurve.cpp:249
QgsCurve::Orientation
Orientation
Curve orientation.
Definition: qgscurve.h:236
QgsCurve::sinuosity
double sinuosity() const
Returns the curve sinuosity, which is the ratio of the curve length() to curve straightDistance2d().
Definition: qgscurve.cpp:240
QgsCurve::curveToLine
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.
QgsCurve::operator!=
bool operator!=(const QgsAbstractGeometry &other) const override
Definition: qgscurve.cpp:35
QgsPoint::x
double x
Definition: qgspoint.h:58
QgsRectangle::isNull
bool isNull() const
Test if the rectangle is null (all coordinates zero or after call to setMinimal()).
Definition: qgsrectangle.h:447
qgsgeos.h
QgsAbstractGeometry::calculateBoundingBox
virtual QgsRectangle calculateBoundingBox() const
Default calculator for the minimal bounding box for the geometry.
Definition: qgsabstractgeometry.cpp:85