QGIS API Documentation  3.12.1-BucureČ™ti (121cc00ff0)
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 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 }
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:177
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:240
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:332
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:315
bool isValid(QString *errorMsg=nullptr, bool allowSelfTouchingHoles=false, QgsGeometry *errorLoc=nullptr) const override
Returns true if the geometry is valid.
Definition: qgsgeos.cpp:1679
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:256
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:269
int childCount() const override
Returns number of child geometries (for geometries with child geometries) or child points (for geomet...
Definition: qgscurve.cpp:264
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:223
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:148
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:164
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:236
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:182
Counter-clockwise orientation.
Definition: qgscurve.h:239
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:279
QgsCoordinateSequence coordinateSequence() const override
Retrieves the sequence of geometries, rings and nodes.
Definition: qgscurve.cpp:61
Clockwise orientation.
Definition: qgscurve.h:238
Orientation orientation() const
Returns the curve&#39;s orientation, e.g.
Definition: qgscurve.cpp:249
QVector< QgsPoint > QgsPointSequence
QgsCurve * segmentize(double tolerance=M_PI_2/90, SegmentationToleranceType toleranceType=MaximumAngle) const override
Returns a geometry without curves.
Definition: qgscurve.cpp:159
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:190
QgsRectangle boundingBox() const override
Returns the minimal bounding box for the geometry.
Definition: qgscurve.cpp:195
double straightDistance2d() const
Returns the straight distance of the curve, i.e.
Definition: qgscurve.cpp:235
int ringCount(int part=0) const override
Returns the number of rings of which this geometry is built.
Definition: qgscurve.cpp:171
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:204
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