QGIS API Documentation  3.21.0-Master (5b68dc587e)
qgsgeometryutils.h
Go to the documentation of this file.
1 /***************************************************************************
2  qgsgeometryutils.h
3  -------------------------------------------------------------------
4 Date : 21 Nov 2014
5 Copyright : (C) 2014 by Marco Hugentobler
6 email : marco.hugentobler at sourcepole dot com
7  ***************************************************************************
8  * *
9  * This program is free software; you can redistribute it and/or modify *
10  * it under the terms of the GNU General Public License as published by *
11  * the Free Software Foundation; either version 2 of the License, or *
12  * (at your option) any later version. *
13  * *
14  ***************************************************************************/
15 
16 #ifndef QGSGEOMETRYUTILS_H
17 #define QGSGEOMETRYUTILS_H
18 
19 #include <limits>
20 
21 #include "qgis_core.h"
22 #include "qgis_sip.h"
23 #include "qgspoint.h"
24 #include "qgsabstractgeometry.h"
25 #include "qgsgeometry.h"
26 #include "qgsvector3d.h"
27 
28 #include <QJsonArray>
29 
30 class QgsLineString;
31 
38 class CORE_EXPORT QgsGeometryUtils
39 {
40  public:
41 
46  static QVector<QgsLineString *> extractLineStrings( const QgsAbstractGeometry *geom ) SIP_FACTORY;
47 
52  static QgsPoint closestVertex( const QgsAbstractGeometry &geom, const QgsPoint &pt, QgsVertexId &id SIP_OUT );
53 
59  static QgsPoint closestPoint( const QgsAbstractGeometry &geometry, const QgsPoint &point );
60 
68  static double distanceToVertex( const QgsAbstractGeometry &geom, QgsVertexId id );
69 
81  static bool verticesAtDistance( const QgsAbstractGeometry &geometry,
82  double distance,
83  QgsVertexId &previousVertex SIP_OUT,
84  QgsVertexId &nextVertex SIP_OUT );
85 
89  static double sqrDistance2D( const QgsPoint &pt1, const QgsPoint &pt2 ) SIP_HOLDGIL;
90 
94  static double sqrDistToLine( double ptX, double ptY, double x1, double y1, double x2, double y2, double &minDistX SIP_OUT, double &minDistY SIP_OUT, double epsilon ) SIP_HOLDGIL;
95 
107  static bool lineIntersection( const QgsPoint &p1, QgsVector v1, const QgsPoint &p2, QgsVector v2, QgsPoint &intersection SIP_OUT ) SIP_HOLDGIL;
108 
145  static bool segmentIntersection( const QgsPoint &p1, const QgsPoint &p2, const QgsPoint &q1, const QgsPoint &q2, QgsPoint &intersectionPoint SIP_OUT, bool &isIntersection SIP_OUT, double tolerance = 1e-8, bool acceptImproperIntersection = false ) SIP_HOLDGIL;
146 
158  static bool lineCircleIntersection( const QgsPointXY &center, double radius,
159  const QgsPointXY &linePoint1, const QgsPointXY &linePoint2,
160  QgsPointXY &intersection SIP_INOUT ) SIP_HOLDGIL;
161 
172  static int circleCircleIntersections( const QgsPointXY &center1, double radius1,
173  const QgsPointXY &center2, double radius2,
174  QgsPointXY &intersection1 SIP_OUT, QgsPointXY &intersection2 SIP_OUT ) SIP_HOLDGIL;
175 
184  static bool tangentPointAndCircle( const QgsPointXY &center, double radius,
186 
204  static int circleCircleOuterTangents(
205  const QgsPointXY &center1, double radius1, const QgsPointXY &center2, double radius2,
206  QgsPointXY &line1P1 SIP_OUT, QgsPointXY &line1P2 SIP_OUT,
207  QgsPointXY &line2P1 SIP_OUT, QgsPointXY &line2P2 SIP_OUT ) SIP_HOLDGIL;
208 
227  static int circleCircleInnerTangents(
228  const QgsPointXY &center1, double radius1, const QgsPointXY &center2, double radius2,
229  QgsPointXY &line1P1 SIP_OUT, QgsPointXY &line1P2 SIP_OUT,
230  QgsPointXY &line2P1 SIP_OUT, QgsPointXY &line2P2 SIP_OUT ) SIP_HOLDGIL;
231 
239  static QgsPoint projectPointOnSegment( const QgsPoint &p, const QgsPoint &s1, const QgsPoint &s2 ) SIP_HOLDGIL
240  {
241  const double nx = s2.y() - s1.y();
242  const double ny = -( s2.x() - s1.x() );
243  const double t = ( p.x() * ny - p.y() * nx - s1.x() * ny + s1.y() * nx ) / ( ( s2.x() - s1.x() ) * ny - ( s2.y() - s1.y() ) * nx );
244  return t < 0. ? s1 : t > 1. ? s2 : QgsPoint( s1.x() + ( s2.x() - s1.x() ) * t, s1.y() + ( s2.y() - s1.y() ) * t );
245  }
246 
249  {
250  int segment1;
251  int segment2;
253  };
254 
265  static QVector<SelfIntersection> selfIntersections( const QgsAbstractGeometry *geom, int part, int ring, double tolerance ) SIP_SKIP;
266 
274  static int leftOfLine( const double x, const double y, const double x1, const double y1, const double x2, const double y2 ) SIP_HOLDGIL;
275 
285  static int leftOfLine( const QgsPoint &point, const QgsPoint &p1, const QgsPoint &p2 ) SIP_HOLDGIL;
286 
290  static QgsPoint pointOnLineWithDistance( const QgsPoint &startPoint, const QgsPoint &directionPoint, double distance ) SIP_HOLDGIL;
291 
301  static void pointOnLineWithDistance( double x1, double y1, double x2, double y2, double distance, double &x, double &y,
302  double *z1 = nullptr, double *z2 = nullptr, double *z = nullptr,
303  double *m1 = nullptr, double *m2 = nullptr, double *m = nullptr ) SIP_SKIP;
304 
332  static void perpendicularOffsetPointAlongSegment( double x1, double y1, double x2, double y2, double proportion, double offset, double *x SIP_OUT, double *y SIP_OUT );
333 
342  static QgsPoint interpolatePointOnArc( const QgsPoint &pt1, const QgsPoint &pt2, const QgsPoint &pt3, double distance ) SIP_HOLDGIL;
343 
345  static double ccwAngle( double dy, double dx ) SIP_HOLDGIL;
346 
348  static void circleCenterRadius( const QgsPoint &pt1, const QgsPoint &pt2, const QgsPoint &pt3, double &radius SIP_OUT,
349  double &centerX SIP_OUT, double &centerY SIP_OUT ) SIP_HOLDGIL;
350 
357  static bool circleClockwise( double angle1, double angle2, double angle3 ) SIP_HOLDGIL;
358 
360  static bool circleAngleBetween( double angle, double angle1, double angle2, bool clockwise ) SIP_HOLDGIL;
361 
366  static bool angleOnCircle( double angle, double angle1, double angle2, double angle3 ) SIP_HOLDGIL;
367 
369  static double circleLength( double x1, double y1, double x2, double y2, double x3, double y3 ) SIP_HOLDGIL;
370 
372  static double sweepAngle( double centerX, double centerY, double x1, double y1, double x2, double y2, double x3, double y3 ) SIP_HOLDGIL;
373 
380  static bool segmentMidPoint( const QgsPoint &p1, const QgsPoint &p2, QgsPoint &result SIP_OUT, double radius, const QgsPoint &mousePos ) SIP_HOLDGIL;
381 
393  static QgsPoint segmentMidPointFromCenter( const QgsPoint &p1, const QgsPoint &p2, const QgsPoint &center, bool useShortestArc = true ) SIP_HOLDGIL;
394 
396  static double circleTangentDirection( const QgsPoint &tangentPoint, const QgsPoint &cp1, const QgsPoint &cp2, const QgsPoint &cp3 ) SIP_HOLDGIL;
397 
402  static void segmentizeArc( const QgsPoint &p1, const QgsPoint &p2, const QgsPoint &p3,
403  QgsPointSequence SIP_PYALTERNATIVETYPE( QVector<QgsPoint> ) &points SIP_OUT, double tolerance = M_PI_2 / 90,
404  QgsAbstractGeometry::SegmentationToleranceType toleranceType = QgsAbstractGeometry::MaximumAngle,
405  bool hasZ = false, bool hasM = false );
406 
422  static bool pointContinuesArc( const QgsPoint &a1, const QgsPoint &a2, const QgsPoint &a3, const QgsPoint &b, double distanceTolerance,
423  double pointSpacingAngleTolerance ) SIP_HOLDGIL;
424 
430  static int segmentSide( const QgsPoint &pt1, const QgsPoint &pt3, const QgsPoint &pt2 ) SIP_HOLDGIL;
431 
436  static double interpolateArcValue( double angle, double a1, double a2, double a3, double zm1, double zm2, double zm3 ) SIP_HOLDGIL;
437 
442  static QgsPointSequence pointsFromWKT( const QString &wktCoordinateList, bool is3D, bool isMeasure ) SIP_SKIP;
443 
448  static void pointsToWKB( QgsWkbPtr &wkb, const QgsPointSequence &points, bool is3D, bool isMeasure ) SIP_SKIP;
449 
454  static QString pointsToWKT( const QgsPointSequence &points, int precision, bool is3D, bool isMeasure ) SIP_SKIP;
455 
460  static QDomElement pointsToGML2( const QgsPointSequence &points, QDomDocument &doc, int precision, const QString &ns, QgsAbstractGeometry::AxisOrder axisOrder = QgsAbstractGeometry::AxisOrder::XY ) SIP_SKIP;
461 
466  static QDomElement pointsToGML3( const QgsPointSequence &points, QDomDocument &doc, int precision, const QString &ns, bool is3D, QgsAbstractGeometry::AxisOrder axisOrder = QgsAbstractGeometry::AxisOrder::XY ) SIP_SKIP;
467 
472  static QString pointsToJSON( const QgsPointSequence &points, int precision ) SIP_SKIP;
473 
478  static json pointsToJson( const QgsPointSequence &points, int precision ) SIP_SKIP;
479 
485  static double normalizedAngle( double angle ) SIP_HOLDGIL;
486 
495  static double lineAngle( double x1, double y1, double x2, double y2 ) SIP_HOLDGIL;
496 
508  static double angleBetweenThreePoints( double x1, double y1, double x2, double y2,
509  double x3, double y3 ) SIP_HOLDGIL;
510 
520  static double linePerpendicularAngle( double x1, double y1, double x2, double y2 ) SIP_HOLDGIL;
521 
526  static double averageAngle( double x1, double y1, double x2, double y2, double x3, double y3 ) SIP_HOLDGIL;
527 
534  static double averageAngle( double a1, double a2 ) SIP_HOLDGIL;
535 
540  static QPair<QgsWkbTypes::Type, QString> wktReadBlock( const QString &wkt ) SIP_SKIP;
541 
549  static QStringList wktGetChildBlocks( const QString &wkt, const QString &defaultType = QString() ) SIP_SKIP;
550 
573  static int closestSideOfRectangle( double right, double bottom, double left, double top, double x, double y );
574 
598  static QgsPoint midpoint( const QgsPoint &pt1, const QgsPoint &pt2 ) SIP_HOLDGIL;
599 
613  static QgsPointXY interpolatePointOnLine( double x1, double y1, double x2, double y2, double fraction ) SIP_HOLDGIL;
614 
631  static QgsPoint interpolatePointOnLine( const QgsPoint &p1, const QgsPoint &p2, double fraction ) SIP_HOLDGIL;
632 
645  static QgsPointXY interpolatePointOnLineByValue( double x1, double y1, double v1, double x2, double y2, double v2, double value ) SIP_HOLDGIL;
646 
654  static double gradient( const QgsPoint &pt1, const QgsPoint &pt2 ) SIP_HOLDGIL;
655 
665  static void coefficients( const QgsPoint &pt1, const QgsPoint &pt2,
666  double &a SIP_OUT, double &b SIP_OUT, double &c SIP_OUT ) SIP_HOLDGIL;
667 
675  static QgsLineString perpendicularSegment( const QgsPoint &p, const QgsPoint &s1, const QgsPoint &s2 ) SIP_HOLDGIL;
676 
677 
686  static double skewLinesDistance( const QgsVector3D &P1, const QgsVector3D &P12,
687  const QgsVector3D &P2, const QgsVector3D &P22 ) SIP_HOLDGIL;
688 
699  static bool skewLinesProjection( const QgsVector3D &P1, const QgsVector3D &P12,
700  const QgsVector3D &P2, const QgsVector3D &P22,
701  QgsVector3D &X1 SIP_OUT,
702  double epsilon = 0.0001 ) SIP_HOLDGIL;
703 
740  static bool linesIntersection3D( const QgsVector3D &La1, const QgsVector3D &La2,
741  const QgsVector3D &Lb1, const QgsVector3D &Lb2,
742  QgsVector3D &intersection SIP_OUT ) SIP_HOLDGIL;
743 
750  static double triangleArea( double aX, double aY, double bX, double bY, double cX, double cY ) SIP_HOLDGIL;
751 
769  static void weightedPointInTriangle( double aX, double aY, double bX, double bY, double cX, double cY,
770  double weightB, double weightC, double &pointX SIP_OUT, double &pointY SIP_OUT ) SIP_HOLDGIL;
771 
788  Q_DECL_DEPRECATED static bool setZValueFromPoints( const QgsPointSequence &points, QgsPoint &point ) SIP_DEPRECATED
789  {
790  return transferFirstZValueToPoint( points, point );
791  }
792 
808  static bool transferFirstZValueToPoint( const QgsPointSequence &points, QgsPoint &point );
809 
825  static bool transferFirstMValueToPoint( const QgsPointSequence &points, QgsPoint &point );
826 
845  template <class Iterator> static bool transferFirstZOrMValueToPoint( Iterator verticesBegin, Iterator verticesEnd, QgsPoint &point ) SIP_SKIP
846  {
847  bool zFound = false;
848  bool mFound = false;
849 
850  for ( auto it = verticesBegin ; it != verticesEnd ; ++it )
851  {
852  if ( !mFound && ( *it ).isMeasure() )
853  {
854  point.convertTo( QgsWkbTypes::addM( point.wkbType() ) );
855  point.setM( ( *it ).m() );
856  mFound = true;
857  }
858  if ( !zFound && ( *it ).is3D() )
859  {
860  point.convertTo( QgsWkbTypes::addZ( point.wkbType() ) );
861  point.setZ( ( *it ).z() );
862  zFound = true;
863  }
864  if ( zFound && mFound )
865  break;
866  }
867 
868  return zFound || mFound;
869  }
870 
887  static bool transferFirstZOrMValueToPoint( const QgsPointSequence &points, QgsPoint &point )
888  {
889  return QgsGeometryUtils::transferFirstZOrMValueToPoint( points.constBegin(), points.constEnd(), point );
890  }
891 
908  static bool transferFirstZOrMValueToPoint( const QgsGeometry &geom, QgsPoint &point )
909  {
911  }
912 
934  static bool angleBisector( double aX, double aY, double bX, double bY, double cX, double cY, double dX, double dY,
935  double &pointX SIP_OUT, double &pointY SIP_OUT, double &angle SIP_OUT ) SIP_HOLDGIL;
936 
953  static bool bisector( double aX, double aY, double bX, double bY, double cX, double cY,
954  double &pointX SIP_OUT, double &pointY SIP_OUT ) SIP_HOLDGIL;
955 
958  {
961  Part
962  };
963 
965  template<class T> static double closestSegmentFromComponents( T &container, ComponentType ctype, const QgsPoint &pt, QgsPoint &segmentPt, QgsVertexId &vertexAfter, int *leftOf, double epsilon ) SIP_SKIP
966  {
967  double minDist = std::numeric_limits<double>::max();
968  double minDistSegmentX = 0.0, minDistSegmentY = 0.0;
969  QgsVertexId minDistVertexAfter;
970  int minDistLeftOf = 0;
971  double sqrDist = 0.0;
972  int vertexOffset = 0;
973  int ringOffset = 0;
974  int partOffset = 0;
975 
976  for ( int i = 0; i < container.size(); ++i )
977  {
978  sqrDist = container.at( i )->closestSegment( pt, segmentPt, vertexAfter, leftOf, epsilon );
979  if ( sqrDist >= 0 && sqrDist < minDist )
980  {
981  minDist = sqrDist;
982  minDistSegmentX = segmentPt.x();
983  minDistSegmentY = segmentPt.y();
984  minDistVertexAfter = vertexAfter;
985  minDistVertexAfter.vertex = vertexAfter.vertex + vertexOffset;
986  minDistVertexAfter.part = vertexAfter.part + partOffset;
987  minDistVertexAfter.ring = vertexAfter.ring + ringOffset;
988  if ( leftOf )
989  {
990  minDistLeftOf = *leftOf;
991  }
992  }
993 
994  if ( ctype == Vertex )
995  {
996  //-1 because compoundcurve counts duplicated vertices of neighbour curves as one node
997  vertexOffset += container.at( i )->nCoordinates() - 1;
998  }
999  else if ( ctype == Ring )
1000  {
1001  ringOffset += 1;
1002  }
1003  else if ( ctype == Part )
1004  {
1005  partOffset += 1;
1006  }
1007  }
1008 
1009  if ( minDist == std::numeric_limits<double>::max() )
1010  return -1; // error: no segments
1011 
1012  segmentPt.setX( minDistSegmentX );
1013  segmentPt.setY( minDistSegmentY );
1014  vertexAfter = minDistVertexAfter;
1015  if ( leftOf )
1016  {
1017  *leftOf = minDistLeftOf;
1018  }
1019  return minDist;
1020  }
1021 };
1022 
1023 #endif // QGSGEOMETRYUTILS_H
Abstract base class for all geometries.
QgsWkbTypes::Type wkbType() const SIP_HOLDGIL
Returns the WKB type of the geometry.
Contains various geometry utility functions.
static bool transferFirstZOrMValueToPoint(const QgsPointSequence &points, QgsPoint &point)
A Z or M dimension is added to point if one of the points in the list points contains Z or M value.
static QgsPoint projectPointOnSegment(const QgsPoint &p, const QgsPoint &s1, const QgsPoint &s2) SIP_HOLDGIL
Project the point on a segment.
static bool transferFirstZOrMValueToPoint(const QgsGeometry &geom, QgsPoint &point)
A Z or M dimension is added to point if one of the points in the list points contains Z or M value.
static double closestSegmentFromComponents(T &container, ComponentType ctype, const QgsPoint &pt, QgsPoint &segmentPt, QgsVertexId &vertexAfter, int *leftOf, double epsilon)
static bool transferFirstZOrMValueToPoint(Iterator verticesBegin, Iterator verticesEnd, QgsPoint &point)
A Z or M dimension is added to point if one of the points in the list points contains Z or M value.
A geometry is the spatial representation of a feature.
Definition: qgsgeometry.h:124
QgsAbstractGeometry::vertex_iterator vertices_begin() const
Returns STL-style iterator pointing to the first vertex of the geometry.
QgsAbstractGeometry::vertex_iterator vertices_end() const
Returns STL-style iterator pointing to the imaginary vertex after the last vertex of the geometry.
Line string geometry type, with support for z-dimension and m-values.
Definition: qgslinestring.h:44
A class to represent a 2D point.
Definition: qgspointxy.h:59
double y
Definition: qgspointxy.h:63
Q_GADGET double x
Definition: qgspointxy.h:62
Point geometry type, with support for z-dimension and m-values.
Definition: qgspoint.h:49
bool convertTo(QgsWkbTypes::Type type) override
Converts the geometry to a specified type.
Definition: qgspoint.cpp:620
void setZ(double z) SIP_HOLDGIL
Sets the point's z-coordinate.
Definition: qgspoint.h:304
void setM(double m) SIP_HOLDGIL
Sets the point's m-value.
Definition: qgspoint.h:319
A class to represent a vector.
Definition: qgsvector.h:30
WKB pointer handler.
Definition: qgswkbptr.h:44
Handles storage of information regarding WKB types and their properties.
Definition: qgswkbtypes.h:42
static Type addZ(Type type) SIP_HOLDGIL
Adds the z dimension to a WKB type and returns the new type.
Definition: qgswkbtypes.h:1146
static Type addM(Type type) SIP_HOLDGIL
Adds the m dimension to a WKB type and returns the new type.
Definition: qgswkbtypes.h:1171
double ANALYSIS_EXPORT angle(QgsPoint *p1, QgsPoint *p2, QgsPoint *p3, QgsPoint *p4)
Calculates the angle between two segments (in 2 dimension, z-values are ignored)
Definition: MathUtils.cpp:786
bool ANALYSIS_EXPORT lineIntersection(QgsPoint *p1, QgsPoint *p2, QgsPoint *p3, QgsPoint *p4)
Returns true, if line1 (p1 to p2) and line2 (p3 to p4) intersect. If the lines have an endpoint in co...
Definition: MathUtils.cpp:311
double ANALYSIS_EXPORT leftOf(const QgsPoint &thepoint, const QgsPoint *p1, const QgsPoint *p2)
Returns whether 'thepoint' is left or right of the line from 'p1' to 'p2'. Negative values mean left ...
Definition: MathUtils.cpp:292
As part of the API refactoring and improvements which landed in the Processing API was substantially reworked from the x version This was done in order to allow much of the underlying Processing framework to be ported into c
#define SIP_PYALTERNATIVETYPE(type)
Definition: qgis_sip.h:136
#define SIP_DEPRECATED
Definition: qgis_sip.h:106
#define SIP_SKIP
Definition: qgis_sip.h:126
#define SIP_OUT
Definition: qgis_sip.h:58
#define SIP_HOLDGIL
Definition: qgis_sip.h:157
#define SIP_FACTORY
Definition: qgis_sip.h:76
#define SIP_INOUT
Definition: qgis_sip.h:71
QVector< QgsPoint > QgsPointSequence
int precision
Utility class for identifying a unique vertex within a geometry.
int vertex
Vertex number.
int part
Part number.
int ring
Ring number.