QGIS API Documentation 3.34.0-Prizren (ffbdd678812)
Loading...
Searching...
No Matches
qgsgeometryutils.h
Go to the documentation of this file.
1/***************************************************************************
2 qgsgeometryutils.h
3 -------------------------------------------------------------------
4Date : 21 Nov 2014
5Copyright : (C) 2014 by Marco Hugentobler
6email : 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
30class QgsLineString;
31
38class 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
104 static double distToInfiniteLine( const QgsPoint &point, const QgsPoint &linePoint1, const QgsPoint &linePoint2, double epsilon = 1e-7 );
105
117 static bool lineIntersection( const QgsPoint &p1, QgsVector v1, const QgsPoint &p2, QgsVector v2, QgsPoint &intersection SIP_OUT ) SIP_HOLDGIL;
118
155 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;
156
168 static bool lineCircleIntersection( const QgsPointXY &center, double radius,
169 const QgsPointXY &linePoint1, const QgsPointXY &linePoint2,
170 QgsPointXY &intersection SIP_INOUT ) SIP_HOLDGIL;
171
182 static int circleCircleIntersections( const QgsPointXY &center1, double radius1,
183 const QgsPointXY &center2, double radius2,
184 QgsPointXY &intersection1 SIP_OUT, QgsPointXY &intersection2 SIP_OUT ) SIP_HOLDGIL;
185
194 static bool tangentPointAndCircle( const QgsPointXY &center, double radius,
196
214 static int circleCircleOuterTangents(
215 const QgsPointXY &center1, double radius1, const QgsPointXY &center2, double radius2,
216 QgsPointXY &line1P1 SIP_OUT, QgsPointXY &line1P2 SIP_OUT,
217 QgsPointXY &line2P1 SIP_OUT, QgsPointXY &line2P2 SIP_OUT ) SIP_HOLDGIL;
218
237 static int circleCircleInnerTangents(
238 const QgsPointXY &center1, double radius1, const QgsPointXY &center2, double radius2,
239 QgsPointXY &line1P1 SIP_OUT, QgsPointXY &line1P2 SIP_OUT,
240 QgsPointXY &line2P1 SIP_OUT, QgsPointXY &line2P2 SIP_OUT ) SIP_HOLDGIL;
241
249 static QgsPoint projectPointOnSegment( const QgsPoint &p, const QgsPoint &s1, const QgsPoint &s2 ) SIP_HOLDGIL
250 {
251 const double nx = s2.y() - s1.y();
252 const double ny = -( s2.x() - s1.x() );
253 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 );
254 return t < 0. ? s1 : t > 1. ? s2 : QgsPoint( s1.x() + ( s2.x() - s1.x() ) * t, s1.y() + ( s2.y() - s1.y() ) * t );
255 }
256
264
275 static QVector<SelfIntersection> selfIntersections( const QgsAbstractGeometry *geom, int part, int ring, double tolerance ) SIP_SKIP;
276
284 static int leftOfLine( const double x, const double y, const double x1, const double y1, const double x2, const double y2 ) SIP_HOLDGIL;
285
295 static int leftOfLine( const QgsPoint &point, const QgsPoint &p1, const QgsPoint &p2 ) SIP_HOLDGIL;
296
300 static QgsPoint pointOnLineWithDistance( const QgsPoint &startPoint, const QgsPoint &directionPoint, double distance ) SIP_HOLDGIL;
301
311 static void pointOnLineWithDistance( double x1, double y1, double x2, double y2, double distance, double &x, double &y,
312 double *z1 = nullptr, double *z2 = nullptr, double *z = nullptr,
313 double *m1 = nullptr, double *m2 = nullptr, double *m = nullptr ) SIP_SKIP;
314
342 static void perpendicularOffsetPointAlongSegment( double x1, double y1, double x2, double y2, double proportion, double offset, double *x SIP_OUT, double *y SIP_OUT );
343
352 static QgsPoint interpolatePointOnArc( const QgsPoint &pt1, const QgsPoint &pt2, const QgsPoint &pt3, double distance ) SIP_HOLDGIL;
353
355 static double ccwAngle( double dy, double dx ) SIP_HOLDGIL;
356
358 static void circleCenterRadius( const QgsPoint &pt1, const QgsPoint &pt2, const QgsPoint &pt3, double &radius SIP_OUT,
359 double &centerX SIP_OUT, double &centerY SIP_OUT ) SIP_HOLDGIL;
360
367 static bool circleClockwise( double angle1, double angle2, double angle3 ) SIP_HOLDGIL;
368
370 static bool circleAngleBetween( double angle, double angle1, double angle2, bool clockwise ) SIP_HOLDGIL;
371
376 static bool angleOnCircle( double angle, double angle1, double angle2, double angle3 ) SIP_HOLDGIL;
377
379 static double circleLength( double x1, double y1, double x2, double y2, double x3, double y3 ) SIP_HOLDGIL;
380
382 static double sweepAngle( double centerX, double centerY, double x1, double y1, double x2, double y2, double x3, double y3 ) SIP_HOLDGIL;
383
390 static bool segmentMidPoint( const QgsPoint &p1, const QgsPoint &p2, QgsPoint &result SIP_OUT, double radius, const QgsPoint &mousePos ) SIP_HOLDGIL;
391
403 static QgsPoint segmentMidPointFromCenter( const QgsPoint &p1, const QgsPoint &p2, const QgsPoint &center, bool useShortestArc = true ) SIP_HOLDGIL;
404
406 static double circleTangentDirection( const QgsPoint &tangentPoint, const QgsPoint &cp1, const QgsPoint &cp2, const QgsPoint &cp3 ) SIP_HOLDGIL;
407
412 static void segmentizeArc( const QgsPoint &p1, const QgsPoint &p2, const QgsPoint &p3,
413 QgsPointSequence SIP_PYALTERNATIVETYPE( QVector<QgsPoint> ) &points SIP_OUT, double tolerance = M_PI_2 / 90,
414 QgsAbstractGeometry::SegmentationToleranceType toleranceType = QgsAbstractGeometry::MaximumAngle,
415 bool hasZ = false, bool hasM = false );
416
432 static bool pointContinuesArc( const QgsPoint &a1, const QgsPoint &a2, const QgsPoint &a3, const QgsPoint &b, double distanceTolerance,
433 double pointSpacingAngleTolerance ) SIP_HOLDGIL;
434
440 static int segmentSide( const QgsPoint &pt1, const QgsPoint &pt3, const QgsPoint &pt2 ) SIP_HOLDGIL;
441
446 static double interpolateArcValue( double angle, double a1, double a2, double a3, double zm1, double zm2, double zm3 ) SIP_HOLDGIL;
447
452 static QgsPointSequence pointsFromWKT( const QString &wktCoordinateList, bool is3D, bool isMeasure ) SIP_SKIP;
453
458 static void pointsToWKB( QgsWkbPtr &wkb, const QgsPointSequence &points, bool is3D, bool isMeasure, QgsAbstractGeometry::WkbFlags flags ) SIP_SKIP;
459
464 static QString pointsToWKT( const QgsPointSequence &points, int precision, bool is3D, bool isMeasure ) SIP_SKIP;
465
470 static QDomElement pointsToGML2( const QgsPointSequence &points, QDomDocument &doc, int precision, const QString &ns, QgsAbstractGeometry::AxisOrder axisOrder = QgsAbstractGeometry::AxisOrder::XY ) SIP_SKIP;
471
476 static QDomElement pointsToGML3( const QgsPointSequence &points, QDomDocument &doc, int precision, const QString &ns, bool is3D, QgsAbstractGeometry::AxisOrder axisOrder = QgsAbstractGeometry::AxisOrder::XY ) SIP_SKIP;
477
482 static QString pointsToJSON( const QgsPointSequence &points, int precision ) SIP_SKIP;
483
488 static json pointsToJson( const QgsPointSequence &points, int precision ) SIP_SKIP;
489
495 static double normalizedAngle( double angle ) SIP_HOLDGIL;
496
505 static double lineAngle( double x1, double y1, double x2, double y2 ) SIP_HOLDGIL;
506
518 static double angleBetweenThreePoints( double x1, double y1, double x2, double y2,
519 double x3, double y3 ) SIP_HOLDGIL;
520
530 static double linePerpendicularAngle( double x1, double y1, double x2, double y2 ) SIP_HOLDGIL;
531
536 static double averageAngle( double x1, double y1, double x2, double y2, double x3, double y3 ) SIP_HOLDGIL;
537
544 static double averageAngle( double a1, double a2 ) SIP_HOLDGIL;
545
550 static QPair<Qgis::WkbType, QString> wktReadBlock( const QString &wkt ) SIP_SKIP;
551
559 static QStringList wktGetChildBlocks( const QString &wkt, const QString &defaultType = QString() ) SIP_SKIP;
560
583 static int closestSideOfRectangle( double right, double bottom, double left, double top, double x, double y );
584
608 static QgsPoint midpoint( const QgsPoint &pt1, const QgsPoint &pt2 ) SIP_HOLDGIL;
609
623 static QgsPointXY interpolatePointOnLine( double x1, double y1, double x2, double y2, double fraction ) SIP_HOLDGIL;
624
641 static QgsPoint interpolatePointOnLine( const QgsPoint &p1, const QgsPoint &p2, double fraction ) SIP_HOLDGIL;
642
655 static QgsPointXY interpolatePointOnLineByValue( double x1, double y1, double v1, double x2, double y2, double v2, double value ) SIP_HOLDGIL;
656
664 static double gradient( const QgsPoint &pt1, const QgsPoint &pt2 ) SIP_HOLDGIL;
665
675 static void coefficients( const QgsPoint &pt1, const QgsPoint &pt2,
676 double &a SIP_OUT, double &b SIP_OUT, double &c SIP_OUT ) SIP_HOLDGIL;
677
685 static QgsLineString perpendicularSegment( const QgsPoint &p, const QgsPoint &s1, const QgsPoint &s2 ) SIP_HOLDGIL;
686
710 static void perpendicularCenterSegment( double centerPointX, double centerPointY,
711 double segmentPoint1x, double segmentPoint1y,
712 double segmentPoint2x, double segmentPoint2y,
713 double &perpendicularSegmentPoint1x SIP_OUT, double &perpendicularSegmentPoint1y SIP_OUT,
714 double &perpendicularSegmentPoint2x SIP_OUT, double &perpendicularSegmentPoint2y SIP_OUT,
715 double segmentLength = 0
716 ) SIP_HOLDGIL;
717
726 static double skewLinesDistance( const QgsVector3D &P1, const QgsVector3D &P12,
727 const QgsVector3D &P2, const QgsVector3D &P22 ) SIP_HOLDGIL;
728
739 static bool skewLinesProjection( const QgsVector3D &P1, const QgsVector3D &P12,
740 const QgsVector3D &P2, const QgsVector3D &P22,
742 double epsilon = 0.0001 ) SIP_HOLDGIL;
743
780 static bool linesIntersection3D( const QgsVector3D &La1, const QgsVector3D &La2,
781 const QgsVector3D &Lb1, const QgsVector3D &Lb2,
782 QgsVector3D &intersection SIP_OUT ) SIP_HOLDGIL;
783
790 static double triangleArea( double aX, double aY, double bX, double bY, double cX, double cY ) SIP_HOLDGIL;
791
800 static double pointFractionAlongLine( double x1, double y1, double x2, double y2, double px, double py );
801
819 static void weightedPointInTriangle( double aX, double aY, double bX, double bY, double cX, double cY,
820 double weightB, double weightC, double &pointX SIP_OUT, double &pointY SIP_OUT ) SIP_HOLDGIL;
821
828 static bool pointsAreCollinear( double x1, double y1, double x2, double y2, double x3, double y3, double epsilon );
829
846 Q_DECL_DEPRECATED static bool setZValueFromPoints( const QgsPointSequence &points, QgsPoint &point ) SIP_DEPRECATED
847 {
848 return transferFirstZValueToPoint( points, point );
849 }
850
866 static bool transferFirstZValueToPoint( const QgsPointSequence &points, QgsPoint &point );
867
883 static bool transferFirstMValueToPoint( const QgsPointSequence &points, QgsPoint &point );
884
903 template <class Iterator> static bool transferFirstZOrMValueToPoint( Iterator verticesBegin, Iterator verticesEnd, QgsPoint &point ) SIP_SKIP
904 {
905 bool zFound = false;
906 bool mFound = false;
907
908 for ( auto it = verticesBegin ; it != verticesEnd ; ++it )
909 {
910 if ( !mFound && ( *it ).isMeasure() )
911 {
912 point.convertTo( QgsWkbTypes::addM( point.wkbType() ) );
913 point.setM( ( *it ).m() );
914 mFound = true;
915 }
916 if ( !zFound && ( *it ).is3D() )
917 {
918 point.convertTo( QgsWkbTypes::addZ( point.wkbType() ) );
919 point.setZ( ( *it ).z() );
920 zFound = true;
921 }
922 if ( zFound && mFound )
923 break;
924 }
925
926 return zFound || mFound;
927 }
928
945 static bool transferFirstZOrMValueToPoint( const QgsPointSequence &points, QgsPoint &point )
946 {
947 return QgsGeometryUtils::transferFirstZOrMValueToPoint( points.constBegin(), points.constEnd(), point );
948 }
949
966 static bool transferFirstZOrMValueToPoint( const QgsGeometry &geom, QgsPoint &point )
967 {
969 }
970
992 static bool angleBisector( double aX, double aY, double bX, double bY, double cX, double cY, double dX, double dY,
993 double &pointX SIP_OUT, double &pointY SIP_OUT, double &angle SIP_OUT ) SIP_HOLDGIL;
994
1011 static bool bisector( double aX, double aY, double bX, double bY, double cX, double cY,
1012 double &pointX SIP_OUT, double &pointY SIP_OUT ) SIP_HOLDGIL;
1013
1021
1023 template<class T> static double closestSegmentFromComponents( T &container, ComponentType ctype, const QgsPoint &pt, QgsPoint &segmentPt, QgsVertexId &vertexAfter, int *leftOf, double epsilon ) SIP_SKIP
1024 {
1025 double minDist = std::numeric_limits<double>::max();
1026 double minDistSegmentX = 0.0, minDistSegmentY = 0.0;
1027 QgsVertexId minDistVertexAfter;
1028 int minDistLeftOf = 0;
1029 double sqrDist = 0.0;
1030 int vertexOffset = 0;
1031 int ringOffset = 0;
1032 int partOffset = 0;
1033
1034 for ( int i = 0; i < container.size(); ++i )
1035 {
1036 sqrDist = container.at( i )->closestSegment( pt, segmentPt, vertexAfter, leftOf, epsilon );
1037 if ( sqrDist >= 0 && sqrDist < minDist )
1038 {
1039 minDist = sqrDist;
1040 minDistSegmentX = segmentPt.x();
1041 minDistSegmentY = segmentPt.y();
1042 minDistVertexAfter = vertexAfter;
1043 minDistVertexAfter.vertex = vertexAfter.vertex + vertexOffset;
1044 minDistVertexAfter.part = vertexAfter.part + partOffset;
1045 minDistVertexAfter.ring = vertexAfter.ring + ringOffset;
1046 if ( leftOf )
1047 {
1048 minDistLeftOf = *leftOf;
1049 }
1050 }
1051
1052 if ( ctype == Vertex )
1053 {
1054 //-1 because compoundcurve counts duplicated vertices of neighbour curves as one node
1055 vertexOffset += container.at( i )->nCoordinates() - 1;
1056 }
1057 else if ( ctype == Ring )
1058 {
1059 ringOffset += 1;
1060 }
1061 else if ( ctype == Part )
1062 {
1063 partOffset += 1;
1064 }
1065 }
1066
1067 if ( minDist == std::numeric_limits<double>::max() )
1068 return -1; // error: no segments
1069
1070 segmentPt.setX( minDistSegmentX );
1071 segmentPt.setY( minDistSegmentY );
1072 vertexAfter = minDistVertexAfter;
1073 if ( leftOf )
1074 {
1075 *leftOf = minDistLeftOf;
1076 }
1077 return minDist;
1078 }
1079};
1080
1081#endif // QGSGEOMETRYUTILS_H
The Qgis class provides global constants for use throughout the application.
Definition qgis.h:54
Abstract base class for all geometries.
Qgis::WkbType wkbType() const
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)
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.
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.
A class to represent a 2D point.
Definition qgspointxy.h:59
double y
Definition qgspointxy.h:63
double x
Definition qgspointxy.h:62
Point geometry type, with support for z-dimension and m-values.
Definition qgspoint.h:49
void setM(double m)
Sets the point's m-value.
Definition qgspoint.h:319
bool convertTo(Qgis::WkbType type) override
Converts the geometry to a specified type.
Definition qgspoint.cpp:626
void setZ(double z)
Sets the point's z-coordinate.
Definition qgspoint.h:304
double y
Definition qgspoint.h:53
Class for storage of 3D vectors similar to QVector3D, with the difference that it uses double precisi...
Definition qgsvector3d.h:32
A class to represent a vector.
Definition qgsvector.h:30
WKB pointer handler.
Definition qgswkbptr.h:44
static Qgis::WkbType addM(Qgis::WkbType type)
Adds the m dimension to a WKB type and returns the new type.
static Qgis::WkbType addZ(Qgis::WkbType type)
Adds the z dimension to a WKB type and returns the new type.
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:141
#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:166
#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