QGIS API Documentation  3.24.2-Tisler (13c1a02865)
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 "qgsvertexid.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 
700  static void perpendicularCenterSegment( double centerPointX, double centerPointY,
701  double segmentPoint1x, double segmentPoint1y,
702  double segmentPoint2x, double segmentPoint2y,
703  double &perpendicularSegmentPoint1x SIP_OUT, double &perpendicularSegmentPoint1y SIP_OUT,
704  double &perpendicularSegmentPoint2x SIP_OUT, double &perpendicularSegmentPoint2y SIP_OUT,
705  double segmentLength = 0
706  ) SIP_HOLDGIL;
707 
716  static double skewLinesDistance( const QgsVector3D &P1, const QgsVector3D &P12,
717  const QgsVector3D &P2, const QgsVector3D &P22 ) SIP_HOLDGIL;
718 
729  static bool skewLinesProjection( const QgsVector3D &P1, const QgsVector3D &P12,
730  const QgsVector3D &P2, const QgsVector3D &P22,
731  QgsVector3D &X1 SIP_OUT,
732  double epsilon = 0.0001 ) SIP_HOLDGIL;
733 
770  static bool linesIntersection3D( const QgsVector3D &La1, const QgsVector3D &La2,
771  const QgsVector3D &Lb1, const QgsVector3D &Lb2,
772  QgsVector3D &intersection SIP_OUT ) SIP_HOLDGIL;
773 
780  static double triangleArea( double aX, double aY, double bX, double bY, double cX, double cY ) SIP_HOLDGIL;
781 
799  static void weightedPointInTriangle( double aX, double aY, double bX, double bY, double cX, double cY,
800  double weightB, double weightC, double &pointX SIP_OUT, double &pointY SIP_OUT ) SIP_HOLDGIL;
801 
818  Q_DECL_DEPRECATED static bool setZValueFromPoints( const QgsPointSequence &points, QgsPoint &point ) SIP_DEPRECATED
819  {
820  return transferFirstZValueToPoint( points, point );
821  }
822 
838  static bool transferFirstZValueToPoint( const QgsPointSequence &points, QgsPoint &point );
839 
855  static bool transferFirstMValueToPoint( const QgsPointSequence &points, QgsPoint &point );
856 
875  template <class Iterator> static bool transferFirstZOrMValueToPoint( Iterator verticesBegin, Iterator verticesEnd, QgsPoint &point ) SIP_SKIP
876  {
877  bool zFound = false;
878  bool mFound = false;
879 
880  for ( auto it = verticesBegin ; it != verticesEnd ; ++it )
881  {
882  if ( !mFound && ( *it ).isMeasure() )
883  {
884  point.convertTo( QgsWkbTypes::addM( point.wkbType() ) );
885  point.setM( ( *it ).m() );
886  mFound = true;
887  }
888  if ( !zFound && ( *it ).is3D() )
889  {
890  point.convertTo( QgsWkbTypes::addZ( point.wkbType() ) );
891  point.setZ( ( *it ).z() );
892  zFound = true;
893  }
894  if ( zFound && mFound )
895  break;
896  }
897 
898  return zFound || mFound;
899  }
900 
917  static bool transferFirstZOrMValueToPoint( const QgsPointSequence &points, QgsPoint &point )
918  {
919  return QgsGeometryUtils::transferFirstZOrMValueToPoint( points.constBegin(), points.constEnd(), point );
920  }
921 
938  static bool transferFirstZOrMValueToPoint( const QgsGeometry &geom, QgsPoint &point )
939  {
941  }
942 
964  static bool angleBisector( double aX, double aY, double bX, double bY, double cX, double cY, double dX, double dY,
965  double &pointX SIP_OUT, double &pointY SIP_OUT, double &angle SIP_OUT ) SIP_HOLDGIL;
966 
983  static bool bisector( double aX, double aY, double bX, double bY, double cX, double cY,
984  double &pointX SIP_OUT, double &pointY SIP_OUT ) SIP_HOLDGIL;
985 
988  {
991  Part
992  };
993 
995  template<class T> static double closestSegmentFromComponents( T &container, ComponentType ctype, const QgsPoint &pt, QgsPoint &segmentPt, QgsVertexId &vertexAfter, int *leftOf, double epsilon ) SIP_SKIP
996  {
997  double minDist = std::numeric_limits<double>::max();
998  double minDistSegmentX = 0.0, minDistSegmentY = 0.0;
999  QgsVertexId minDistVertexAfter;
1000  int minDistLeftOf = 0;
1001  double sqrDist = 0.0;
1002  int vertexOffset = 0;
1003  int ringOffset = 0;
1004  int partOffset = 0;
1005 
1006  for ( int i = 0; i < container.size(); ++i )
1007  {
1008  sqrDist = container.at( i )->closestSegment( pt, segmentPt, vertexAfter, leftOf, epsilon );
1009  if ( sqrDist >= 0 && sqrDist < minDist )
1010  {
1011  minDist = sqrDist;
1012  minDistSegmentX = segmentPt.x();
1013  minDistSegmentY = segmentPt.y();
1014  minDistVertexAfter = vertexAfter;
1015  minDistVertexAfter.vertex = vertexAfter.vertex + vertexOffset;
1016  minDistVertexAfter.part = vertexAfter.part + partOffset;
1017  minDistVertexAfter.ring = vertexAfter.ring + ringOffset;
1018  if ( leftOf )
1019  {
1020  minDistLeftOf = *leftOf;
1021  }
1022  }
1023 
1024  if ( ctype == Vertex )
1025  {
1026  //-1 because compoundcurve counts duplicated vertices of neighbour curves as one node
1027  vertexOffset += container.at( i )->nCoordinates() - 1;
1028  }
1029  else if ( ctype == Ring )
1030  {
1031  ringOffset += 1;
1032  }
1033  else if ( ctype == Part )
1034  {
1035  partOffset += 1;
1036  }
1037  }
1038 
1039  if ( minDist == std::numeric_limits<double>::max() )
1040  return -1; // error: no segments
1041 
1042  segmentPt.setX( minDistSegmentX );
1043  segmentPt.setY( minDistSegmentY );
1044  vertexAfter = minDistVertexAfter;
1045  if ( leftOf )
1046  {
1047  *leftOf = minDistLeftOf;
1048  }
1049  return minDist;
1050  }
1051 };
1052 
1053 #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:125
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:1176
static Type addM(Type type) SIP_HOLDGIL
Adds the m dimension to a WKB type and returns the new type.
Definition: qgswkbtypes.h:1201
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.
Definition: qgsvertexid.h:31
int vertex
Vertex number.
Definition: qgsvertexid.h:95
int part
Part number.
Definition: qgsvertexid.h:89
int ring
Ring number.
Definition: qgsvertexid.h:92