QGIS API Documentation  3.2.0-Bonn (bc43194)
qgslinestring.h
Go to the documentation of this file.
1 /***************************************************************************
2  qgslinestring.h
3  -----------------
4  begin : September 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 #ifndef QGSLINESTRINGV2_H
19 #define QGSLINESTRINGV2_H
20 
21 
22 #include <QPolygonF>
23 
24 #include "qgis_core.h"
25 #include "qgis.h"
26 #include "qgscurve.h"
27 #include "qgscompoundcurve.h"
28 
29 class QgsLineSegment2D;
30 
31 /***************************************************************************
32  * This class is considered CRITICAL and any change MUST be accompanied with
33  * full unit tests in testqgsgeometry.cpp.
34  * See details in QEP #17
35  ****************************************************************************/
36 
43 class CORE_EXPORT QgsLineString: public QgsCurve
44 {
45  public:
46  QgsLineString();
47 
54  QgsLineString( const QVector<QgsPoint> &points );
55 
64  QgsLineString( const QVector<double> &x, const QVector<double> &y,
65  const QVector<double> &z = QVector<double>(),
66  const QVector<double> &m = QVector<double>() );
67 
72  QgsLineString( const QgsPoint &p1, const QgsPoint &p2 );
73 
80  QgsLineString( const QVector<QgsPointXY> &points );
81 
86  explicit QgsLineString( const QgsLineSegment2D &segment );
87 
88  bool equals( const QgsCurve &other ) const override;
89 
94  QgsPoint pointN( int i ) const;
95 
96  double xAt( int index ) const override;
97  double yAt( int index ) const override;
98 
105  const double *xData() const SIP_SKIP
106  {
107  return mX.constData();
108  }
109 
116  const double *yData() const SIP_SKIP
117  {
118  return mY.constData();
119  }
120 
129  const double *zData() const SIP_SKIP
130  {
131  if ( mZ.empty() )
132  return nullptr;
133  else
134  return mZ.constData();
135  }
136 
145  const double *mData() const SIP_SKIP
146  {
147  if ( mM.empty() )
148  return nullptr;
149  else
150  return mM.constData();
151  }
152 
160  double zAt( int index ) const
161  {
162  if ( index >= 0 && index < mZ.size() )
163  return mZ.at( index );
164  else
165  return std::numeric_limits<double>::quiet_NaN();
166  }
167 
175  double mAt( int index ) const
176  {
177  if ( index >= 0 && index < mM.size() )
178  return mM.at( index );
179  else
180  return std::numeric_limits<double>::quiet_NaN();
181  }
182 
190  void setXAt( int index, double x );
191 
199  void setYAt( int index, double y );
200 
208  void setZAt( int index, double z )
209  {
210  if ( index >= 0 && index < mZ.size() )
211  mZ[ index ] = z;
212  }
213 
221  void setMAt( int index, double m )
222  {
223  if ( index >= 0 && index < mM.size() )
224  mM[ index ] = m;
225  }
226 
232  void setPoints( const QgsPointSequence &points );
233 
238  void append( const QgsLineString *line );
239 
244  void addVertex( const QgsPoint &pt );
245 
247  void close();
248 
252  QgsCompoundCurve *toCurveType() const override SIP_FACTORY;
253 
260  void extend( double startDistance, double endDistance );
261 
262  //reimplemented methods
263 
264  QString geometryType() const override;
265  int dimension() const override;
266  QgsLineString *clone() const override SIP_FACTORY;
267  void clear() override;
268  bool isEmpty() const override;
269  QgsLineString *snappedToGrid( double hSpacing, double vSpacing, double dSpacing = 0, double mSpacing = 0 ) const override SIP_FACTORY;
270  bool removeDuplicateNodes( double epsilon = 4 * std::numeric_limits<double>::epsilon(), bool useZValues = false ) override;
271  QPolygonF asQPolygonF() const override;
272 
273  bool fromWkb( QgsConstWkbPtr &wkb ) override;
274  bool fromWkt( const QString &wkt ) override;
275 
276  QByteArray asWkb() const override;
277  QString asWkt( int precision = 17 ) const override;
278  QDomElement asGml2( QDomDocument &doc, int precision = 17, const QString &ns = "gml", QgsAbstractGeometry::AxisOrder axisOrder = QgsAbstractGeometry::AxisOrder::XY ) const override;
279  QDomElement asGml3( QDomDocument &doc, int precision = 17, const QString &ns = "gml", QgsAbstractGeometry::AxisOrder axisOrder = QgsAbstractGeometry::AxisOrder::XY ) const override;
280  QString asJson( int precision = 17 ) const override;
281 
282  //curve interface
283  double length() const override;
284  QgsPoint startPoint() const override;
285  QgsPoint endPoint() const override;
286 
292  QgsLineString *curveToLine( double tolerance = M_PI_2 / 90, SegmentationToleranceType toleranceType = MaximumAngle ) const override SIP_FACTORY;
293 
294  int numPoints() const override;
295  int nCoordinates() const override;
296  void points( QgsPointSequence &pt SIP_OUT ) const override;
297 
298  void draw( QPainter &p ) const override;
299 
301  void transform( const QTransform &t, double zTranslate = 0.0, double zScale = 1.0, double mTranslate = 0.0, double mScale = 1.0 ) override;
302 
303  void addToPainterPath( QPainterPath &path ) const override;
304  void drawAsPolygon( QPainter &p ) const override;
305 
306  bool insertVertex( QgsVertexId position, const QgsPoint &vertex ) override;
307  bool moveVertex( QgsVertexId position, const QgsPoint &newPos ) override;
308  bool deleteVertex( QgsVertexId position ) override;
309 
310  QgsLineString *reversed() const override SIP_FACTORY;
311 
312  double closestSegment( const QgsPoint &pt, QgsPoint &segmentPt SIP_OUT, QgsVertexId &vertexAfter SIP_OUT, int *leftOf SIP_OUT = nullptr, double epsilon = 4 * std::numeric_limits<double>::epsilon() ) const override;
313  bool pointAt( int node, QgsPoint &point, QgsVertexId::VertexType &type ) const override;
314 
315  QgsPoint centroid() const override;
316 
317  void sumUpArea( double &sum SIP_OUT ) const override;
318  double vertexAngle( QgsVertexId vertex ) const override;
319  double segmentLength( QgsVertexId startVertex ) const override;
320  bool addZValue( double zValue = 0 ) override;
321  bool addMValue( double mValue = 0 ) override;
322 
323  bool dropZValue() override;
324  bool dropMValue() override;
325  void swapXy() override;
326 
327  bool convertTo( QgsWkbTypes::Type type ) override;
328 
329 #ifndef SIP_RUN
330  void filterVertices( const std::function< bool( const QgsPoint & ) > &filter ) override;
331 
339  inline const QgsLineString *cast( const QgsAbstractGeometry *geom ) const
340  {
341  if ( geom && QgsWkbTypes::flatType( geom->wkbType() ) == QgsWkbTypes::LineString )
342  return static_cast<const QgsLineString *>( geom );
343  return nullptr;
344  }
345 #endif
346 
348 
349 #ifdef SIP_RUN
350  SIP_PYOBJECT __repr__();
351  % MethodCode
352  QString str = QStringLiteral( "<QgsLineString: %1>" ).arg( sipCpp->asWkt() );
353  sipRes = PyUnicode_FromString( str.toUtf8().data() );
354  % End
355 #endif
356 
357  protected:
358 
359  QgsRectangle calculateBoundingBox() const override;
360 
361  private:
362  QVector<double> mX;
363  QVector<double> mY;
364  QVector<double> mZ;
365  QVector<double> mM;
366 
367  void importVerticesFromWkb( const QgsConstWkbPtr &wkb );
368 
374  void fromWkbPoints( QgsWkbTypes::Type type, const QgsConstWkbPtr &wkb )
375  {
376  mWkbType = type;
377  importVerticesFromWkb( wkb );
378  }
379 
380  friend class QgsPolygon;
381  friend class QgsTriangle;
382 
383 };
384 
385 // clazy:excludeall=qstring-allocations
386 
387 #endif // QGSLINESTRINGV2_H
virtual QgsCurve * reversed() const =0
Returns a reversed copy of the curve, where the direction of the curve has been flipped.
virtual QString asWkt(int precision=17) const =0
Returns a WKT representation of the geometry.
A rectangle specified with double values.
Definition: qgsrectangle.h:40
virtual QgsAbstractGeometry * snappedToGrid(double hSpacing, double vSpacing, double dSpacing=0, double mSpacing=0) const =0
Makes a new geometry with all the points or vertices snapped to the closest point of the grid...
virtual bool isEmpty() const
Returns true if the geometry is empty.
virtual bool deleteVertex(QgsVertexId position)=0
Deletes a vertex within the geometry.
virtual void transform(const QgsCoordinateTransform &ct, QgsCoordinateTransform::TransformDirection d=QgsCoordinateTransform::ForwardTransform, bool transformZ=false) SIP_THROW(QgsCsException)=0
Transforms the geometry using a coordinate transform.
double zAt(int index) const
Returns the z-coordinate of the specified node in the line string.
virtual QByteArray asWkb() const =0
Returns a WKB representation of the geometry.
const double * mData() const
Returns a const pointer to the m vertex data, or a nullptr if the linestring does not have m values...
virtual bool insertVertex(QgsVertexId position, const QgsPoint &vertex)=0
Inserts a vertex into the geometry.
TransformDirection
Enum used to indicate the direction (forward or inverse) of the transform.
virtual double vertexAngle(QgsVertexId vertex) const =0
Returns approximate angle at a vertex.
virtual bool addMValue(double mValue=0)=0
Adds a measure to the geometry, initialized to a preset value.
virtual void sumUpArea(double &sum) const =0
Sums up the area of the curve by iterating over the vertices (shoelace formula).
virtual QgsAbstractGeometry * createEmptyWithSameType() const =0
Creates a new geometry with the same class and same WKB type as the original and transfers ownership...
virtual QString asJson(int precision=17) const =0
Returns a GeoJSON representation of the geometry.
virtual QDomElement asGml2(QDomDocument &doc, int precision=17, const QString &ns="gml", AxisOrder axisOrder=QgsAbstractGeometry::AxisOrder::XY) const =0
Returns a GML2 representation of the geometry.
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.
Triangle geometry type.
Definition: qgstriangle.h:33
virtual QgsPoint centroid() const
Returns the centroid of the geometry.
virtual QgsPoint endPoint() const =0
Returns the end point of the curve.
const double * xData() const
Returns a const pointer to the x vertex data.
virtual QPolygonF asQPolygonF() const
Returns a QPolygonF representing the points.
Definition: qgscurve.cpp:191
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:67
virtual double closestSegment(const QgsPoint &pt, QgsPoint &segmentPt, QgsVertexId &vertexAfter, int *leftOf=nullptr, double epsilon=4 *std::numeric_limits< double >::epsilon()) const =0
Searches for the closest segment of the geometry to a given point.
virtual double segmentLength(QgsVertexId startVertex) const =0
Returns the length of the segment of the geometry which begins at startVertex.
virtual void filterVertices(const std::function< bool(const QgsPoint &) > &filter)
Filters the vertices from the geometry in place, removing any which do not return true for the filter...
Utility class for identifying a unique vertex within a geometry.
#define SIP_SKIP
Definition: qgis_sip.h:119
double mAt(int index) const
Returns the m value of the specified node in the line string.
virtual double xAt(int index) const =0
Returns the x-coordinate of the specified node in the line string.
Abstract base class for curved geometry type.
Definition: qgscurve.h:35
#define SIP_FACTORY
Definition: qgis_sip.h:69
Abstract base class for all geometries.
virtual int dimension() const =0
Returns the inherent dimension of the geometry.
void setZAt(int index, double z)
Sets the z-coordinate of the specified node in the line string.
QgsWkbTypes::Type wkbType() const
Returns the WKB type of the geometry.
Point geometry type, with support for z-dimension and m-values.
Definition: qgspoint.h:37
const double * yData() const
Returns a const pointer to the y vertex data.
AxisOrder
Axis order for GML generation.
Represents a single 2D line segment, consisting of a 2D start and end vertex only.
const double * zData() const
Returns a const pointer to the z vertex data, or a nullptr if the linestring does not have z values...
void setMAt(int index, double m)
Sets the m value of the specified node in the line string.
QVector< QgsPoint > QgsPointSequence
virtual void addToPainterPath(QPainterPath &path) const =0
Adds a curve to a painter path.
QgsCurve * toCurveType() const override
Returns the geometry converted to the more generic curve type.
Definition: qgscurve.cpp:177
virtual void clear()=0
Clears the geometry, ie reset it to a null geometry.
virtual bool moveVertex(QgsVertexId position, const QgsPoint &newPos)=0
Moves a vertex within the geometry.
QgsCurve * clone() const override=0
Clones the geometry by performing a deep copy.
virtual void draw(QPainter &p) const =0
Draws the geometry using the specified QPainter.
#define SIP_OUT
Definition: qgis_sip.h:51
const QgsLineString * cast(const QgsAbstractGeometry *geom) const
Cast the geom to a QgsLineString.
virtual bool addZValue(double zValue=0)=0
Adds a z-dimension to the geometry, initialized to a preset value.
Line string geometry type, with support for z-dimension and m-values.
Definition: qgslinestring.h:43
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...
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.
Class for doing transforms between two map coordinate systems.
#define SIP_THROW(name)
Definition: qgis_sip.h:177
virtual bool convertTo(QgsWkbTypes::Type type)
Converts the geometry to a specified type.
Transform from source to destination CRS.
Compound curve geometry type.
virtual bool removeDuplicateNodes(double epsilon=4 *std::numeric_limits< double >::epsilon(), bool useZValues=false)=0
Removes duplicate nodes from the geometry, wherever removing the nodes does not result in a degenerat...
Custom exception class for Coordinate Reference System related exceptions.
Definition: qgsexception.h:65
virtual void drawAsPolygon(QPainter &p) const =0
Draws the curve as a polygon on the specified QPainter.
virtual bool dropMValue()=0
Drops any measure values which exist in the geometry.
virtual bool fromWkt(const QString &wkt)=0
Sets the geometry from a WKT string.
virtual QgsPoint startPoint() const =0
Returns the starting point of the curve.
Polygon geometry type.
Definition: qgspolygon.h:31
virtual int nCoordinates() const
Returns the number of nodes contained in the geometry.
static Type flatType(Type type)
Returns the flat type for a WKB type.
Definition: qgswkbtypes.h:427
virtual void swapXy()=0
Swaps the x and y coordinates from the geometry.
virtual int numPoints() const =0
Returns the number of points in the curve.
double ANALYSIS_EXPORT leftOf(const QgsPoint &thepoint, const QgsPoint *p1, const QgsPoint *p2)
Returns whether &#39;thepoint&#39; is left or right of the line from &#39;p1&#39; to &#39;p2&#39;. Negativ values mean left a...
Definition: MathUtils.cpp:292
virtual bool dropZValue()=0
Drops any z-dimensions which exist in the geometry.
virtual void points(QgsPointSequence &pt) const =0
Returns a list of points within the curve.
virtual QDomElement asGml3(QDomDocument &doc, int precision=17, const QString &ns="gml", AxisOrder axisOrder=QgsAbstractGeometry::AxisOrder::XY) const =0
Returns a GML3 representation of the geometry.
virtual QString geometryType() const =0
Returns a unique string representing the geometry type.
virtual bool fromWkb(QgsConstWkbPtr &wkb)=0
Sets the geometry from a WKB string.