QGIS API Documentation  3.10.0-A Coruña (6c816b4204)
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 "qgsvector3d.h"
26 
27 #include <QJsonArray>
28 
29 class QgsLineString;
30 
37 class CORE_EXPORT QgsGeometryUtils
38 {
39  public:
40 
45  static QVector<QgsLineString *> extractLineStrings( const QgsAbstractGeometry *geom ) SIP_FACTORY;
46 
51  static QgsPoint closestVertex( const QgsAbstractGeometry &geom, const QgsPoint &pt, QgsVertexId &id SIP_OUT );
52 
58  static QgsPoint closestPoint( const QgsAbstractGeometry &geometry, const QgsPoint &point );
59 
67  static double distanceToVertex( const QgsAbstractGeometry &geom, QgsVertexId id );
68 
80  static bool verticesAtDistance( const QgsAbstractGeometry &geometry,
81  double distance,
82  QgsVertexId &previousVertex SIP_OUT,
83  QgsVertexId &nextVertex SIP_OUT );
84 
88  static double sqrDistance2D( const QgsPoint &pt1, const QgsPoint &pt2 );
89 
93  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 );
94 
106  static bool lineIntersection( const QgsPoint &p1, QgsVector v1, const QgsPoint &p2, QgsVector v2, QgsPoint &intersection SIP_OUT );
107 
142  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 );
143 
155  static bool lineCircleIntersection( const QgsPointXY &center, double radius,
156  const QgsPointXY &linePoint1, const QgsPointXY &linePoint2,
157  QgsPointXY &intersection SIP_INOUT );
158 
169  static int circleCircleIntersections( QgsPointXY center1, double radius1,
170  QgsPointXY center2, double radius2,
171  QgsPointXY &intersection1 SIP_OUT, QgsPointXY &intersection2 SIP_OUT );
172 
181  static bool tangentPointAndCircle( const QgsPointXY &center, double radius,
182  const QgsPointXY &p, QgsPointXY &pt1 SIP_OUT, QgsPointXY &pt2 SIP_OUT );
183 
201  static int circleCircleOuterTangents(
202  const QgsPointXY &center1, double radius1, const QgsPointXY &center2, double radius2,
203  QgsPointXY &line1P1 SIP_OUT, QgsPointXY &line1P2 SIP_OUT,
204  QgsPointXY &line2P1 SIP_OUT, QgsPointXY &line2P2 SIP_OUT );
205 
224  static int circleCircleInnerTangents(
225  const QgsPointXY &center1, double radius1, const QgsPointXY &center2, double radius2,
226  QgsPointXY &line1P1 SIP_OUT, QgsPointXY &line1P2 SIP_OUT,
227  QgsPointXY &line2P1 SIP_OUT, QgsPointXY &line2P2 SIP_OUT );
228 
236  static QgsPoint projectPointOnSegment( const QgsPoint &p, const QgsPoint &s1, const QgsPoint &s2 )
237  {
238  double nx = s2.y() - s1.y();
239  double ny = -( s2.x() - s1.x() );
240  double t = ( p.x() * ny - p.y() * nx - s1.x() * ny + s1.y() * nx ) / ( ( s2.x() - s1.x() ) * ny - ( s2.y() - s1.y() ) * nx );
241  return t < 0. ? s1 : t > 1. ? s2 : QgsPoint( s1.x() + ( s2.x() - s1.x() ) * t, s1.y() + ( s2.y() - s1.y() ) * t );
242  }
243 
246  {
247  int segment1;
248  int segment2;
250  };
251 
262  static QVector<SelfIntersection> selfIntersections( const QgsAbstractGeometry *geom, int part, int ring, double tolerance ) SIP_SKIP;
263 
271  static int leftOfLine( const double x, const double y, const double x1, const double y1, const double x2, const double y2 );
272 
282  static int leftOfLine( const QgsPoint &point, const QgsPoint &p1, const QgsPoint &p2 );
283 
287  static QgsPoint pointOnLineWithDistance( const QgsPoint &startPoint, const QgsPoint &directionPoint, double distance );
288 
298  static void pointOnLineWithDistance( double x1, double y1, double x2, double y2, double distance, double &x, double &y,
299  double *z1 = nullptr, double *z2 = nullptr, double *z = nullptr,
300  double *m1 = nullptr, double *m2 = nullptr, double *m = nullptr ) SIP_SKIP;
301 
310  static QgsPoint interpolatePointOnArc( const QgsPoint &pt1, const QgsPoint &pt2, const QgsPoint &pt3, double distance );
311 
313  static double ccwAngle( double dy, double dx );
314 
316  static void circleCenterRadius( const QgsPoint &pt1, const QgsPoint &pt2, const QgsPoint &pt3, double &radius SIP_OUT,
317  double &centerX SIP_OUT, double &centerY SIP_OUT );
318 
325  static bool circleClockwise( double angle1, double angle2, double angle3 );
326 
328  static bool circleAngleBetween( double angle, double angle1, double angle2, bool clockwise );
329 
334  static bool angleOnCircle( double angle, double angle1, double angle2, double angle3 );
335 
337  static double circleLength( double x1, double y1, double x2, double y2, double x3, double y3 );
338 
340  static double sweepAngle( double centerX, double centerY, double x1, double y1, double x2, double y2, double x3, double y3 );
341 
348  static bool segmentMidPoint( const QgsPoint &p1, const QgsPoint &p2, QgsPoint &result SIP_OUT, double radius, const QgsPoint &mousePos );
349 
361  static QgsPoint segmentMidPointFromCenter( const QgsPoint &p1, const QgsPoint &p2, const QgsPoint &center, bool useShortestArc = true );
362 
364  static double circleTangentDirection( const QgsPoint &tangentPoint, const QgsPoint &cp1, const QgsPoint &cp2, const QgsPoint &cp3 );
365 
370  static void segmentizeArc( const QgsPoint &p1, const QgsPoint &p2, const QgsPoint &p3,
371  QgsPointSequence SIP_PYALTERNATIVETYPE( QVector<QgsPoint> ) &points SIP_OUT, double tolerance = M_PI_2 / 90,
372  QgsAbstractGeometry::SegmentationToleranceType toleranceType = QgsAbstractGeometry::MaximumAngle,
373  bool hasZ = false, bool hasM = false );
374 
380  static int segmentSide( const QgsPoint &pt1, const QgsPoint &pt3, const QgsPoint &pt2 );
381 
386  static double interpolateArcValue( double angle, double a1, double a2, double a3, double zm1, double zm2, double zm3 );
387 
392  static QgsPointSequence pointsFromWKT( const QString &wktCoordinateList, bool is3D, bool isMeasure ) SIP_SKIP;
393 
398  static void pointsToWKB( QgsWkbPtr &wkb, const QgsPointSequence &points, bool is3D, bool isMeasure ) SIP_SKIP;
399 
404  static QString pointsToWKT( const QgsPointSequence &points, int precision, bool is3D, bool isMeasure ) SIP_SKIP;
405 
410  static QDomElement pointsToGML2( const QgsPointSequence &points, QDomDocument &doc, int precision, const QString &ns, QgsAbstractGeometry::AxisOrder axisOrder = QgsAbstractGeometry::AxisOrder::XY ) SIP_SKIP;
411 
416  static QDomElement pointsToGML3( const QgsPointSequence &points, QDomDocument &doc, int precision, const QString &ns, bool is3D, QgsAbstractGeometry::AxisOrder axisOrder = QgsAbstractGeometry::AxisOrder::XY ) SIP_SKIP;
417 
422  static QString pointsToJSON( const QgsPointSequence &points, int precision ) SIP_SKIP;
423 
428  static json pointsToJson( const QgsPointSequence &points, int precision ) SIP_SKIP;
429 
435  static double normalizedAngle( double angle );
436 
445  static double lineAngle( double x1, double y1, double x2, double y2 );
446 
458  static double angleBetweenThreePoints( double x1, double y1, double x2, double y2,
459  double x3, double y3 );
460 
470  static double linePerpendicularAngle( double x1, double y1, double x2, double y2 );
471 
476  static double averageAngle( double x1, double y1, double x2, double y2, double x3, double y3 );
477 
484  static double averageAngle( double a1, double a2 );
485 
490  static QPair<QgsWkbTypes::Type, QString> wktReadBlock( const QString &wkt ) SIP_SKIP;
491 
499  static QStringList wktGetChildBlocks( const QString &wkt, const QString &defaultType = QString() ) SIP_SKIP;
500 
522  static QgsPoint midpoint( const QgsPoint &pt1, const QgsPoint &pt2 );
523 
537  static QgsPointXY interpolatePointOnLine( double x1, double y1, double x2, double y2, double fraction );
538 
555  static QgsPoint interpolatePointOnLine( const QgsPoint &p1, const QgsPoint &p2, double fraction );
556 
569  static QgsPointXY interpolatePointOnLineByValue( double x1, double y1, double v1, double x2, double y2, double v2, double value );
570 
578  static double gradient( const QgsPoint &pt1, const QgsPoint &pt2 );
579 
589  static void coefficients( const QgsPoint &pt1, const QgsPoint &pt2,
590  double &a SIP_OUT, double &b SIP_OUT, double &c SIP_OUT );
591 
599  static QgsLineString perpendicularSegment( const QgsPoint &p, const QgsPoint &s1, const QgsPoint &s2 );
600 
601 
610  static double skewLinesDistance( const QgsVector3D &P1, const QgsVector3D &P12,
611  const QgsVector3D &P2, const QgsVector3D &P22 );
612 
623  static bool skewLinesProjection( const QgsVector3D &P1, const QgsVector3D &P12,
624  const QgsVector3D &P2, const QgsVector3D &P22,
625  QgsVector3D &X1 SIP_OUT,
626  double epsilon = 0.0001 );
627 
662  static bool linesIntersection3D( const QgsVector3D &La1, const QgsVector3D &La2,
663  const QgsVector3D &Lb1, const QgsVector3D &Lb2,
664  QgsVector3D &intersection SIP_OUT );
665 
672  static double triangleArea( double aX, double aY, double bX, double bY, double cX, double cY );
673 
691  static void weightedPointInTriangle( double aX, double aY, double bX, double bY, double cX, double cY,
692  double weightB, double weightC, double &pointX SIP_OUT, double &pointY SIP_OUT );
693 
704  static bool setZValueFromPoints( const QgsPointSequence &points, QgsPoint &point );
705 
707  enum ComponentType SIP_SKIP
708  {
711  Part
712  };
713 
715  template<class T> static double closestSegmentFromComponents( T &container, ComponentType ctype, const QgsPoint &pt, QgsPoint &segmentPt, QgsVertexId &vertexAfter, int *leftOf, double epsilon ) SIP_SKIP
716  {
717  double minDist = std::numeric_limits<double>::max();
718  double minDistSegmentX = 0.0, minDistSegmentY = 0.0;
719  QgsVertexId minDistVertexAfter;
720  int minDistLeftOf = 0;
721  double sqrDist = 0.0;
722  int vertexOffset = 0;
723  int ringOffset = 0;
724  int partOffset = 0;
725 
726  for ( int i = 0; i < container.size(); ++i )
727  {
728  sqrDist = container.at( i )->closestSegment( pt, segmentPt, vertexAfter, leftOf, epsilon );
729  if ( sqrDist >= 0 && sqrDist < minDist )
730  {
731  minDist = sqrDist;
732  minDistSegmentX = segmentPt.x();
733  minDistSegmentY = segmentPt.y();
734  minDistVertexAfter = vertexAfter;
735  minDistVertexAfter.vertex = vertexAfter.vertex + vertexOffset;
736  minDistVertexAfter.part = vertexAfter.part + partOffset;
737  minDistVertexAfter.ring = vertexAfter.ring + ringOffset;
738  if ( leftOf )
739  {
740  minDistLeftOf = *leftOf;
741  }
742  }
743 
744  if ( ctype == Vertex )
745  {
746  //-1 because compoundcurve counts duplicated vertices of neighbour curves as one node
747  vertexOffset += container.at( i )->nCoordinates() - 1;
748  }
749  else if ( ctype == Ring )
750  {
751  ringOffset += 1;
752  }
753  else if ( ctype == Part )
754  {
755  partOffset += 1;
756  }
757  }
758 
759  if ( minDist == std::numeric_limits<double>::max() )
760  return -1; // error: no segments
761 
762  segmentPt.setX( minDistSegmentX );
763  segmentPt.setY( minDistSegmentY );
764  vertexAfter = minDistVertexAfter;
765  if ( leftOf )
766  {
767  *leftOf = minDistLeftOf;
768  }
769  return minDist;
770  }
771 };
772 
773 #endif // QGSGEOMETRYUTILS_H
3 Class for storage of 3D vectors similar to QVector3D, with the difference that it uses double preci...
Definition: qgsvector3d.h:31
int precision
double y
Definition: qgspoint.h:42
Handles storage of information regarding WKB types and their properties.
Definition: qgswkbtypes.h:40
A class to represent a 2D point.
Definition: qgspointxy.h:43
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
#define SIP_INOUT
Definition: qgis_sip.h:71
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
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
Contains various geometry utility functions.
Utility class for identifying a unique vertex within a geometry.
#define SIP_SKIP
Definition: qgis_sip.h:126
#define SIP_FACTORY
Definition: qgis_sip.h:76
Abstract base class for all geometries.
Point geometry type, with support for z-dimension and m-values.
Definition: qgspoint.h:37
A class to represent a vector.
Definition: qgsvector.h:29
QVector< QgsPoint > QgsPointSequence
#define SIP_OUT
Definition: qgis_sip.h:58
Line string geometry type, with support for z-dimension and m-values.
Definition: qgslinestring.h:43
static QgsPoint projectPointOnSegment(const QgsPoint &p, const QgsPoint &s1, const QgsPoint &s2)
Project the point on a segment.
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
static double closestSegmentFromComponents(T &container, ComponentType ctype, const QgsPoint &pt, QgsPoint &segmentPt, QgsVertexId &vertexAfter, int *leftOf, double epsilon)
#define SIP_PYALTERNATIVETYPE(type)
Definition: qgis_sip.h:136
double x
Definition: qgspoint.h:41