QgsGeometryUtils class

Contains various geometry utility functions.

Public types

struct SelfIntersection
enum ComponentType { Vertex, Ring, Part }

Public static functions

static auto angleBetweenThreePoints(double x1, double y1, double x2, double y2, double x3, double y3) -> double
Calculates the angle between the lines AB and BC, where AB and BC described by points a, b and b, c.
static auto angleOnCircle(double angle, double angle1, double angle2, double angle3) -> bool
Returns true if an angle is between angle1 and angle3 on a circle described by angle1, angle2 and angle3.
static auto averageAngle(double x1, double y1, double x2, double y2, double x3, double y3) -> double
Calculates the average angle (in radians) between the two linear segments from (x1, y1) to (x2, y2) and (x2, y2) to (x3, y3).
static auto averageAngle(double a1, double a2) -> double
Averages two angles, correctly handling negative angles and ensuring the result is between 0 and 2 pi.
static auto ccwAngle(double dy, double dx) -> double
Returns the counter clockwise angle between a line with components dx, dy and the line with dx > 0 and dy = 0.
static auto circleAngleBetween(double angle, double angle1, double angle2, bool clockwise) -> bool
Returns true if, in a circle, angle is between angle1 and angle2.
static void circleCenterRadius(const QgsPoint& pt1, const QgsPoint& pt2, const QgsPoint& pt3, double& radius, double& centerX, double& centerY)
Returns radius and center of the circle through pt1, pt2, pt3.
static auto circleCircleInnerTangents(const QgsPointXY& center1, double radius1, const QgsPointXY& center2, double radius2, QgsPointXY& line1P1, QgsPointXY& line1P2, QgsPointXY& line2P1, QgsPointXY& line2P2) -> int
Calculates the inner tangent points for two circles, centered at center1 and center2 and with radii of radius1 and radius2 respectively.
static auto circleCircleIntersections(QgsPointXY center1, double radius1, QgsPointXY center2, double radius2, QgsPointXY& intersection1, QgsPointXY& intersection2) -> int
Calculates the intersections points between the circle with center center1 and radius radius1 and the circle with center center2 and radius radius2.
static auto circleCircleOuterTangents(const QgsPointXY& center1, double radius1, const QgsPointXY& center2, double radius2, QgsPointXY& line1P1, QgsPointXY& line1P2, QgsPointXY& line2P1, QgsPointXY& line2P2) -> int
Calculates the outer tangent points for two circles, centered at center1 and center2 and with radii of radius1 and radius2 respectively.
static auto circleClockwise(double angle1, double angle2, double angle3) -> bool
Returns true if the circle defined by three angles is ordered clockwise.
static auto circleLength(double x1, double y1, double x2, double y2, double x3, double y3) -> double
Length of a circular string segment defined by pt1, pt2, pt3.
static auto circleTangentDirection(const QgsPoint& tangentPoint, const QgsPoint& cp1, const QgsPoint& cp2, const QgsPoint& cp3) -> double
Calculates the direction angle of a circle tangent (clockwise from north in radians)
static auto closestPoint(const QgsAbstractGeometry& geometry, const QgsPoint& point) -> QgsPoint
Returns the nearest point on a segment of a geometry for the specified point.
template<class T>
static auto closestSegmentFromComponents(T& container, ComponentType ctype, const QgsPoint& pt, QgsPoint& segmentPt, QgsVertexId& vertexAfter, int* leftOf, double epsilon) -> double
static auto closestVertex(const QgsAbstractGeometry& geom, const QgsPoint& pt, QgsVertexId& id) -> QgsPoint
Returns the closest vertex to a geometry for a specified point.
static void coefficients(const QgsPoint& pt1, const QgsPoint& pt2, double& a, double& b, double& c)
Returns the coefficients (a, b, c for equation "ax + by + c = 0") of a line defined by points pt1 and pt2.
static auto distanceToVertex(const QgsAbstractGeometry& geom, QgsVertexId id) -> double
Returns the distance along a geometry from its first vertex to the specified vertex.
static auto extractLineStrings(const QgsAbstractGeometry* geom) -> QVector<QgsLineString*>
Returns list of linestrings extracted from the passed geometry.
static auto gradient(const QgsPoint& pt1, const QgsPoint& pt2) -> double
Returns the gradient of a line defined by points pt1 and pt2.
static auto interpolateArcValue(double angle, double a1, double a2, double a3, double zm1, double zm2, double zm3) -> double
Interpolate a value at given angle on circular arc given values (zm1, zm2, zm3) at three different angles (a1, a2, a3).
static auto interpolatePointOnArc(const QgsPoint& pt1, const QgsPoint& pt2, const QgsPoint& pt3, double distance) -> QgsPoint
Interpolates a point on an arc defined by three points, pt1, pt2 and pt3.
static auto interpolatePointOnLine(double x1, double y1, double x2, double y2, double fraction) -> QgsPointXY
Interpolates the position of a point a fraction of the way along the line from (x1, y1) to (x2, y2).
static auto interpolatePointOnLine(const QgsPoint& p1, const QgsPoint& p2, double fraction) -> QgsPoint
Interpolates the position of a point a fraction of the way along the line from p1 to p2.
static auto interpolatePointOnLineByValue(double x1, double y1, double v1, double x2, double y2, double v2, double value) -> QgsPointXY
Interpolates the position of a point along the line from (x1, y1) to (x2, y2).
static auto leftOfLine(const double x, const double y, const double x1, const double y1, const double x2, const double y2) -> int
Returns a value < 0 if the point (x, y) is left of the line from (x1, y1) -> ( x2, y2).
static auto leftOfLine(const QgsPoint& point, const QgsPoint& p1, const QgsPoint& p2) -> int
Returns a value < 0 if the point point is left of the line from p1 -> p2.
static auto lineAngle(double x1, double y1, double x2, double y2) -> double
Calculates the direction of line joining two points in radians, clockwise from the north direction.
static auto lineCircleIntersection(const QgsPointXY& center, double radius, const QgsPointXY& linePoint1, const QgsPointXY& linePoint2, QgsPointXY& intersection) -> bool
Compute the intersection of a line and a circle.
static auto lineIntersection(const QgsPoint& p1, QgsVector v1, const QgsPoint& p2, QgsVector v2, QgsPoint& intersection) -> bool
Computes the intersection between two lines.
static auto linePerpendicularAngle(double x1, double y1, double x2, double y2) -> double
Calculates the perpendicular angle to a line joining two points.
static auto linesIntersection3D(const QgsVector3D& La1, const QgsVector3D& La2, const QgsVector3D& Lb1, const QgsVector3D& Lb2, QgsVector3D& intersection) -> bool
An algorithm to calculate an (approximate) intersection of two lines in 3D.
static auto midpoint(const QgsPoint& pt1, const QgsPoint& pt2) -> QgsPoint
Returns a middle point between points pt1 and pt2.
static auto normalizedAngle(double angle) -> double
Ensures that an angle is in the range 0 <= angle < 2 pi.
static auto perpendicularSegment(const QgsPoint& p, const QgsPoint& s1, const QgsPoint& s2) -> QgsLineString
Create a perpendicular line segment from p to segment [s1, s2].
static auto pointOnLineWithDistance(const QgsPoint& startPoint, const QgsPoint& directionPoint, double distance) -> QgsPoint
Returns a point a specified distance toward a second point.
static void pointOnLineWithDistance(double x1, double y1, double x2, double y2, double distance, double& x, double& y, double* z1 = nullptr, double* z2 = nullptr, double* z = nullptr, double* m1 = nullptr, double* m2 = nullptr, double* m = nullptr)
Calculates the point a specified distance from (x1, y1) toward a second point (x2, y2).
static auto pointsFromWKT(const QString& wktCoordinateList, bool is3D, bool isMeasure) -> QgsPointSequence
Returns a list of points contained in a WKT string.
static auto pointsToGML2(const QgsPointSequence& points, QDomDocument& doc, int precision, const QString& ns, QgsAbstractGeometry::AxisOrder axisOrder = QgsAbstractGeometry::AxisOrder::XY) -> QDomElement
Returns a gml::coordinates DOM element.
static auto pointsToGML3(const QgsPointSequence& points, QDomDocument& doc, int precision, const QString& ns, bool is3D, QgsAbstractGeometry::AxisOrder axisOrder = QgsAbstractGeometry::AxisOrder::XY) -> QDomElement
Returns a gml::posList DOM element.
static auto pointsToJSON(const QgsPointSequence& points, int precision) -> QString
Returns a geoJSON coordinates string.
static void pointsToWKB(QgsWkbPtr& wkb, const QgsPointSequence& points, bool is3D, bool isMeasure)
Returns a LinearRing { uint32 numPoints; Point points[numPoints]; }.
static auto pointsToWKT(const QgsPointSequence& points, int precision, bool is3D, bool isMeasure) -> QString
Returns a WKT coordinate list.
static auto projectPointOnSegment(const QgsPoint& p, const QgsPoint& s1, const QgsPoint& s2) -> QgsPoint
Project the point on a segment.
static auto segmentIntersection(const QgsPoint& p1, const QgsPoint& p2, const QgsPoint& q1, const QgsPoint& q2, QgsPoint& intersectionPoint, bool& isIntersection, double tolerance = 1e-8, bool acceptImproperIntersection = false) -> bool
Compute the intersection between two segments.
static void segmentizeArc(const QgsPoint& p1, const QgsPoint& p2, const QgsPoint& p3, QgsPointSequence& points, double tolerance = M_PI_2/90, QgsAbstractGeometry::SegmentationToleranceType toleranceType = QgsAbstractGeometry::MaximumAngle, bool hasZ = false, bool hasM = false)
Convert circular arc defined by p1, p2, p3 (p1/p3 being start resp.
static auto segmentMidPoint(const QgsPoint& p1, const QgsPoint& p2, QgsPoint& result, double radius, const QgsPoint& mousePos) -> bool
Calculates midpoint on circle passing through p1 and p2, closest to the given coordinate mousePos.
static auto segmentMidPointFromCenter(const QgsPoint& p1, const QgsPoint& p2, const QgsPoint& center, bool useShortestArc = true) -> QgsPoint
Calculates the midpoint on the circle passing through p1 and p2, with the specified center coordinate.
static auto segmentSide(const QgsPoint& pt1, const QgsPoint& pt3, const QgsPoint& pt2) -> int
For line defined by points pt1 and pt3, find out on which side of the line is point pt3.
static auto selfIntersections(const QgsAbstractGeometry* geom, int part, int ring, double tolerance) -> QVector<SelfIntersection>
Find self intersections in a polyline.
static auto setZValueFromPoints(const QgsPointSequence& points, QgsPoint& point) -> bool
A Z dimension is added to point if one of the point in the list points is in 3D.
static auto skewLinesDistance(const QgsVector3D& P1, const QgsVector3D& P12, const QgsVector3D& P2, const QgsVector3D& P22) -> double
An algorithm to calculate the shortest distance between two skew lines.
static auto skewLinesProjection(const QgsVector3D& P1, const QgsVector3D& P12, const QgsVector3D& P2, const QgsVector3D& P22, QgsVector3D& X1, double epsilon = 0.0001) -> bool
A method to project one skew line onto another.
static auto sqrDistance2D(const QgsPoint& pt1, const QgsPoint& pt2) -> double
Returns the squared 2D distance between two points.
static auto sqrDistToLine(double ptX, double ptY, double x1, double y1, double x2, double y2, double& minDistX, double& minDistY, double epsilon) -> double
Returns the squared distance between a point and a line.
static auto sweepAngle(double centerX, double centerY, double x1, double y1, double x2, double y2, double x3, double y3) -> double
Calculates angle of a circular string part defined by pt1, pt2, pt3.
static auto tangentPointAndCircle(const QgsPointXY& center, double radius, const QgsPointXY& p, QgsPointXY& pt1, QgsPointXY& pt2) -> bool
Calculates the tangent points between the circle with the specified center and radius and the point p.
static auto verticesAtDistance(const QgsAbstractGeometry& geometry, double distance, QgsVertexId& previousVertex, QgsVertexId& nextVertex) -> bool
Retrieves the vertices which are before and after the interpolated point at a specified distance along a linestring (or polygon boundary).
static auto wktGetChildBlocks(const QString& wkt, const QString& defaultType = QString()) -> QStringList
Parses a WKT string and returns of list of blocks contained in the WKT.
static auto wktReadBlock(const QString& wkt) -> QPair<QgsWkbTypes::Type, QString>
Parses a WKT block of the format "TYPE( contents )" and returns a pair of geometry type to contents ("Pair(wkbType, "contents")")

Enum documentation

enum QgsGeometryUtils::ComponentType

Function documentation

static double QgsGeometryUtils::angleBetweenThreePoints(double x1, double y1, double x2, double y2, double x3, double y3)

Calculates the angle between the lines AB and BC, where AB and BC described by points a, b and b, c.

Parameters
x1 x-coordinate of point a
y1 y-coordinate of point a
x2 x-coordinate of point b
y2 y-coordinate of point b
x3 x-coordinate of point c
y3 y-coordinate of point c
Returns angle between lines in radians. Returned value is undefined if two or more points are equal.

static double QgsGeometryUtils::averageAngle(double a1, double a2)

Averages two angles, correctly handling negative angles and ensuring the result is between 0 and 2 pi.

Parameters
a1 first angle (in radians)
a2 second angle (in radians)
Returns average angle (in radians)

static int QgsGeometryUtils::circleCircleInnerTangents(const QgsPointXY& center1, double radius1, const QgsPointXY& center2, double radius2, QgsPointXY& line1P1, QgsPointXY& line1P2, QgsPointXY& line2P1, QgsPointXY& line2P2)

Calculates the inner tangent points for two circles, centered at center1 and center2 and with radii of radius1 and radius2 respectively.

The inner tangent points correspond to the points at which the two lines which are drawn so that they are tangential to both circles and are crossing each other.

The first tangent line is described by the points stored in line1P1 and line1P2, and the second line is described by the points stored in line2P1 and line2P2.

Returns the number of tangents (either 0 or 2).

static int QgsGeometryUtils::circleCircleIntersections(QgsPointXY center1, double radius1, QgsPointXY center2, double radius2, QgsPointXY& intersection1, QgsPointXY& intersection2)

Calculates the intersections points between the circle with center center1 and radius radius1 and the circle with center center2 and radius radius2.

Returns number of intersection points found.

If found, the intersection points will be stored in intersection1 and intersection2.

static int QgsGeometryUtils::circleCircleOuterTangents(const QgsPointXY& center1, double radius1, const QgsPointXY& center2, double radius2, QgsPointXY& line1P1, QgsPointXY& line1P2, QgsPointXY& line2P1, QgsPointXY& line2P2)

Calculates the outer tangent points for two circles, centered at center1 and center2 and with radii of radius1 and radius2 respectively.

The outer tangent points correspond to the points at which the two lines which are drawn so that they are tangential to both circles touch the circles.

The first tangent line is described by the points stored in line1P1 and line1P2, and the second line is described by the points stored in line2P1 and line2P2.

Returns the number of tangents (either 0 or 2).

static bool QgsGeometryUtils::circleClockwise(double angle1, double angle2, double angle3)

Returns true if the circle defined by three angles is ordered clockwise.

The angles are defined counter-clockwise from the origin, i.e. using Euclidean angles as opposed to geographic "North up" angles.

static QgsPoint QgsGeometryUtils::closestPoint(const QgsAbstractGeometry& geometry, const QgsPoint& point)

Returns the nearest point on a segment of a geometry for the specified point.

The z and m values will be linearly interpolated between the two neighbouring vertices.

template<class T>
static double QgsGeometryUtils::closestSegmentFromComponents(T& container, ComponentType ctype, const QgsPoint& pt, QgsPoint& segmentPt, QgsVertexId& vertexAfter, int* leftOf, double epsilon)

static QgsPoint QgsGeometryUtils::closestVertex(const QgsAbstractGeometry& geom, const QgsPoint& pt, QgsVertexId& id)

Returns the closest vertex to a geometry for a specified point.

On error null point will be returned and "id" argument will be invalid.

static void QgsGeometryUtils::coefficients(const QgsPoint& pt1, const QgsPoint& pt2, double& a, double& b, double& c)

Returns the coefficients (a, b, c for equation "ax + by + c = 0") of a line defined by points pt1 and pt2.

Parameters
pt1 first point.
pt2 second point.
a Output parameter, a coefficient of the equation.
b Output parameter, b coefficient of the equation.
c Output parameter, c coefficient of the equation.

static double QgsGeometryUtils::distanceToVertex(const QgsAbstractGeometry& geom, QgsVertexId id)

Returns the distance along a geometry from its first vertex to the specified vertex.

Parameters
geom geometry
id vertex id to find distance to
Returns distance to vertex (following geometry)

static QVector<QgsLineString*> QgsGeometryUtils::extractLineStrings(const QgsAbstractGeometry* geom)

Returns list of linestrings extracted from the passed geometry.

The returned objects have to be deleted by the caller.

static double QgsGeometryUtils::gradient(const QgsPoint& pt1, const QgsPoint& pt2)

Returns the gradient of a line defined by points pt1 and pt2.

Parameters
pt1 first point.
pt2 second point.
Returns The gradient of this linear entity, or infinity if vertical

static double QgsGeometryUtils::interpolateArcValue(double angle, double a1, double a2, double a3, double zm1, double zm2, double zm3)

Interpolate a value at given angle on circular arc given values (zm1, zm2, zm3) at three different angles (a1, a2, a3).

static QgsPoint QgsGeometryUtils::interpolatePointOnArc(const QgsPoint& pt1, const QgsPoint& pt2, const QgsPoint& pt3, double distance)

Interpolates a point on an arc defined by three points, pt1, pt2 and pt3.

The arc will be interpolated by the specified distance from pt1.

Any z or m values present in the points will also be linearly interpolated in the output.

static QgsPointXY QgsGeometryUtils::interpolatePointOnLine(double x1, double y1, double x2, double y2, double fraction)

Interpolates the position of a point a fraction of the way along the line from (x1, y1) to (x2, y2).

Usually the fraction should be between 0 and 1, where 0 represents the point at the start of the line (x1, y1) and 1 represents the end of the line (x2, y2). However, it is possible to use a fraction < 0 or > 1, in which case the returned point is extrapolated from the supplied line.

static QgsPoint QgsGeometryUtils::interpolatePointOnLine(const QgsPoint& p1, const QgsPoint& p2, double fraction)

Interpolates the position of a point a fraction of the way along the line from p1 to p2.

Usually the fraction should be between 0 and 1, where 0 represents the point at the start of the line (p1) and 1 represents the end of the line (p2). However, it is possible to use a fraction < 0 or > 1, in which case the returned point is extrapolated from the supplied line.

Any Z or M values present in the input points will also be interpolated and present in the returned point.

static QgsPointXY QgsGeometryUtils::interpolatePointOnLineByValue(double x1, double y1, double v1, double x2, double y2, double v2, double value)

Interpolates the position of a point along the line from (x1, y1) to (x2, y2).

The position is interpolated using a supplied target value and the value at the start of the line (v1) and end of the line (v2). The returned point will be linearly interpolated to match position corresponding to the target value.

static int QgsGeometryUtils::leftOfLine(const double x, const double y, const double x1, const double y1, const double x2, const double y2)

Returns a value < 0 if the point (x, y) is left of the line from (x1, y1) -> ( x2, y2).

A positive return value indicates the point is to the right of the line.

If the return value is 0, then the test was unsuccessful (e.g. due to testing a point exactly on the line, or exactly in line with the segment) and the result is undefined.

static int QgsGeometryUtils::leftOfLine(const QgsPoint& point, const QgsPoint& p1, const QgsPoint& p2)

Returns a value < 0 if the point point is left of the line from p1 -> p2.

A positive return value indicates the point is to the right of the line.

If the return value is 0, then the test was unsuccessful (e.g. due to testing a point exactly on the line, or exactly in line with the segment) and the result is undefined.

static double QgsGeometryUtils::lineAngle(double x1, double y1, double x2, double y2)

Calculates the direction of line joining two points in radians, clockwise from the north direction.

Parameters
x1 x-coordinate of line start
y1 y-coordinate of line start
x2 x-coordinate of line end
y2 y-coordinate of line end
Returns angle in radians. Returned value is undefined if start and end point are the same.

static bool QgsGeometryUtils::lineCircleIntersection(const QgsPointXY& center, double radius, const QgsPointXY& linePoint1, const QgsPointXY& linePoint2, QgsPointXY& intersection)

Compute the intersection of a line and a circle.

Parameters
center the center of the circle
radius the radius of the circle
linePoint1 a first point on the line
linePoint2 a second point on the line
intersection the initial point and the returned intersection point
Returns true if an intersection has been found

If the intersection has two solutions (points), the closest point to the initial intersection point is returned.

static bool QgsGeometryUtils::lineIntersection(const QgsPoint& p1, QgsVector v1, const QgsPoint& p2, QgsVector v2, QgsPoint& intersection)

Computes the intersection between two lines.

Parameters
p1 Point on the first line
v1 Direction vector of the first line
p2 Point on the second line
v2 Direction vector of the second line
intersection Output parameter, the intersection point
Returns Whether the lines intersect

Z dimension is supported and is retrieved from the first 3D point amongst p1 and p2.

static double QgsGeometryUtils::linePerpendicularAngle(double x1, double y1, double x2, double y2)

Calculates the perpendicular angle to a line joining two points.

Parameters
x1 x-coordinate of line start
y1 y-coordinate of line start
x2 x-coordinate of line end
y2 y-coordinate of line end
Returns angle in radians. Returned value is undefined if start and end point are the same.

Returned angle is in radians, clockwise from the north direction.

static bool QgsGeometryUtils::linesIntersection3D(const QgsVector3D& La1, const QgsVector3D& La2, const QgsVector3D& Lb1, const QgsVector3D& Lb2, QgsVector3D& intersection)

An algorithm to calculate an (approximate) intersection of two lines in 3D.

Parameters
La1 is the first point on the first line,
La2 is the second point on the first line,
Lb1 is the first point on the second line,
Lb2 is the second point on the second line,
intersection is the result intersection, of it can be found.
Returns

true if the intersection can be found, false - otherwise. example:

QgsGeometryUtils.linesIntersection3D(QgsVector3D(0,0,0), QgsVector3D(5,0,0), QgsVector3D(2,1,0), QgsVector3D(2,3,0))
# (True, PyQt5.QtGui.QgsVector3D(2.0, 0.0, 0.0))
QgsGeometryUtils.linesIntersection3D(QgsVector3D(0,0,0), QgsVector3D(5,0,0), QgsVector3D(2,1,0), QgsVector3D(2,0,0))
# (True, PyQt5.QtGui.QgsVector3D(2.0, 0.0, 0.0))
QgsGeometryUtils.linesIntersection3D(QgsVector3D(0,0,0), QgsVector3D(5,0,0), QgsVector3D(0,1,0), QgsVector3D(0,3,0))
# (True, PyQt5.QtGui.QgsVector3D(0.0, 0.0, 0.0))
QgsGeometryUtils.linesIntersection3D(QgsVector3D(0,0,0), QgsVector3D(5,0,0), QgsVector3D(0,1,0), QgsVector3D(0,0,0))
# (True, PyQt5.QtGui.QgsVector3D(0.0, 0.0, 0.0))
QgsGeometryUtils.linesIntersection3D(QgsVector3D(0,0,0), QgsVector3D(5,0,0), QgsVector3D(5,1,0), QgsVector3D(5,3,0))
# (False, PyQt5.QtGui.QgsVector3D(0.0, 0.0, 0.0))
QgsGeometryUtils.linesIntersection3D(QgsVector3D(0,0,0), QgsVector3D(5,0,0), QgsVector3D(5,1,0), QgsVector3D(5,0,0))
# (False, PyQt5.QtGui.QgsVector3D(0.0, 0.0, 0.0))
QgsGeometryUtils.linesIntersection3D(QgsVector3D(1,1,0), QgsVector3D(2,2,0), QgsVector3D(3,1,0), QgsVector3D(3,2,0))
# (True, PyQt5.QtGui.QgsVector3D(3.0, 3.0, 0.0))
QgsGeometryUtils.linesIntersection3D(QgsVector3D(1,1,0), QgsVector3D(2,2,0), QgsVector3D(3,2,0), QgsVector3D(3,1,0))
# (True, PyQt5.QtGui.QgsVector3D(3.0, 3.0, 0.0))
QgsGeometryUtils.linesIntersection3D(QgsVector3D(5,5,5), QgsVector3D(0,0,0), QgsVector3D(0,5,5), QgsVector3D(5,0,0))
# (True, PyQt5.QtGui.QgsVector3D(2.5, 2.5, 2.5))
QgsGeometryUtils.linesIntersection3D(QgsVector3D(2.5,2.5,2.5), QgsVector3D(0,5,0), QgsVector3D(2.5,2.5,2.5), QgsVector3D(5,0,0))
# (True, PyQt5.QtGui.QgsVector3D(2.5, 2.5, 2.5))
QgsGeometryUtils.linesIntersection3D(QgsVector3D(2.5,2.5,2.5), QgsVector3D(5,0,0), QgsVector3D(0,5,5), QgsVector3D(5,5,5))
# (True, PyQt5.QtGui.QgsVector3D(0.0, 5.0, 5.0))

static QgsPoint QgsGeometryUtils::midpoint(const QgsPoint& pt1, const QgsPoint& pt2)

Returns a middle point between points pt1 and pt2.

Parameters
pt1 first point.
pt2 second point.
Returns

New point at middle between points pt1 and pt2.

  • Example:

    p = QgsPoint( 4, 6 ) # 2D point
    pr = midpoint ( p, QgsPoint( 2, 2 ) )
    # pr is a 2D point: 'Point (3 4)'
    pr = midpoint ( p, QgsPoint( QgsWkbTypes.PointZ, 2, 2, 2 ) )
    # pr is a 3D point: 'PointZ (3 4 1)'
    pr = midpoint ( p, QgsPoint( QgsWkbTypes.PointM, 2, 2, 0, 2 ) )
    # pr is a 3D point: 'PointM (3 4 1)'
    pr = midpoint ( p, QgsPoint( QgsWkbTypes.PointZM, 2, 2, 2, 2 ) )
    # pr is a 3D point: 'PointZM (3 4 1 1)'

Z value is computed if one of this point have Z. M value is computed if one of this point have M.

static double QgsGeometryUtils::normalizedAngle(double angle)

Ensures that an angle is in the range 0 <= angle < 2 pi.

Parameters
angle angle in radians
Returns equivalent angle within the range [0, 2 pi)

static QgsLineString QgsGeometryUtils::perpendicularSegment(const QgsPoint& p, const QgsPoint& s1, const QgsPoint& s2)

Create a perpendicular line segment from p to segment [s1, s2].

Parameters
p The point
s1 The segment start point
s2 The segment end point
Returns A line (segment) from p to perpendicular point on segment [s1, s2]

static void QgsGeometryUtils::pointOnLineWithDistance(double x1, double y1, double x2, double y2, double distance, double& x, double& y, double* z1 = nullptr, double* z2 = nullptr, double* z = nullptr, double* m1 = nullptr, double* m2 = nullptr, double* m = nullptr)

Calculates the point a specified distance from (x1, y1) toward a second point (x2, y2).

Optionally, interpolated z and m values can be obtained by specifying the z1, z2 and z arguments and/or the m1, m2, m arguments.

static QgsPointSequence QgsGeometryUtils::pointsFromWKT(const QString& wktCoordinateList, bool is3D, bool isMeasure)

Returns a list of points contained in a WKT string.

static QDomElement QgsGeometryUtils::pointsToGML2(const QgsPointSequence& points, QDomDocument& doc, int precision, const QString& ns, QgsAbstractGeometry::AxisOrder axisOrder = QgsAbstractGeometry::AxisOrder::XY)

Returns a gml::coordinates DOM element.

static QDomElement QgsGeometryUtils::pointsToGML3(const QgsPointSequence& points, QDomDocument& doc, int precision, const QString& ns, bool is3D, QgsAbstractGeometry::AxisOrder axisOrder = QgsAbstractGeometry::AxisOrder::XY)

Returns a gml::posList DOM element.

static QString QgsGeometryUtils::pointsToJSON(const QgsPointSequence& points, int precision)

Returns a geoJSON coordinates string.

static void QgsGeometryUtils::pointsToWKB(QgsWkbPtr& wkb, const QgsPointSequence& points, bool is3D, bool isMeasure)

Returns a LinearRing { uint32 numPoints; Point points[numPoints]; }.

static QString QgsGeometryUtils::pointsToWKT(const QgsPointSequence& points, int precision, bool is3D, bool isMeasure)

Returns a WKT coordinate list.

static QgsPoint QgsGeometryUtils::projectPointOnSegment(const QgsPoint& p, const QgsPoint& s1, const QgsPoint& s2)

Project the point on a segment.

Parameters
p The point
s1 The segment start point
s2 The segment end point
Returns The projection of the point on the segment

static bool QgsGeometryUtils::segmentIntersection(const QgsPoint& p1, const QgsPoint& p2, const QgsPoint& q1, const QgsPoint& q2, QgsPoint& intersectionPoint, bool& isIntersection, double tolerance = 1e-8, bool acceptImproperIntersection = false)

Compute the intersection between two segments.

Parameters
p1 First segment start point
p2 First segment end point
q1 Second segment start point
q2 Second segment end point
intersectionPoint Output parameter, the intersection point
isIntersection Output parameter, return true if an intersection is found
tolerance The tolerance to use
acceptImproperIntersection By default, this method returns true only if segments have proper intersection. If set true, returns also true if segments have improper intersection (end of one segment on other segment ; continuous segments).
Returns

Whether the segments intersect

  • Example:

    ret = QgsGeometryUtils.segmentIntersection( QgsPoint( 0, 0 ), QgsPoint( 0, 1 ), QgsPoint( 1, 1 ), QgsPoint( 1, 0 ) )
    ret[0], ret[1].asWkt(), ret[2]
    # Whether the segments intersect, the intersection point, is intersect
    # (False, 'Point (0 0)', False)
    ret = QgsGeometryUtils.segmentIntersection( QgsPoint( 0, 0 ), QgsPoint( 0, 5 ), QgsPoint( 0, 5 ), QgsPoint( 1, 5 ) )
    ret[0], ret[1].asWkt(), ret[2]
    # (False, 'Point (0 5)', True)
    ret = QgsGeometryUtils.segmentIntersection( QgsPoint( 0, 0 ), QgsPoint( 0, 5 ), QgsPoint( 0, 5 ), QgsPoint( 1, 5 ), acceptImproperIntersection=True )
    ret[0], ret[1].asWkt(), ret[2]
    # (True, 'Point (0 5)', True)
    ret = QgsGeometryUtils.segmentIntersection( QgsPoint( 0, 0 ), QgsPoint( 0, 5 ), QgsPoint( 0, 2 ), QgsPoint( 1, 5 ) )
    ret[0], ret[1].asWkt(), ret[2]
    # (False, 'Point (0 2)', True)
    ret = QgsGeometryUtils.segmentIntersection( QgsPoint( 0, 0 ), QgsPoint( 0, 5 ), QgsPoint( 0, 2 ), QgsPoint( 1, 5 ), acceptImproperIntersection=True )
    ret[0], ret[1].asWkt(), ret[2]
    # (True, 'Point (0 2)', True)
    ret = QgsGeometryUtils.segmentIntersection( QgsPoint( 0, -5 ), QgsPoint( 0, 5 ), QgsPoint( 2, 0 ), QgsPoint( -1, 0 ) )
    ret[0], ret[1].asWkt(), ret[2]
    # (True, 'Point (0 0)', True)

static void QgsGeometryUtils::segmentizeArc(const QgsPoint& p1, const QgsPoint& p2, const QgsPoint& p3, QgsPointSequence& points, double tolerance = M_PI_2/90, QgsAbstractGeometry::SegmentationToleranceType toleranceType = QgsAbstractGeometry::MaximumAngle, bool hasZ = false, bool hasM = false)

Convert circular arc defined by p1, p2, p3 (p1/p3 being start resp.

end point, p2 lies on the arc) into a sequence of points.

static bool QgsGeometryUtils::segmentMidPoint(const QgsPoint& p1, const QgsPoint& p2, QgsPoint& result, double radius, const QgsPoint& mousePos)

Calculates midpoint on circle passing through p1 and p2, closest to the given coordinate mousePos.

Z dimension is supported and is retrieved from the first 3D point amongst p1 and p2.

static QgsPoint QgsGeometryUtils::segmentMidPointFromCenter(const QgsPoint& p1, const QgsPoint& p2, const QgsPoint& center, bool useShortestArc = true)

Calculates the midpoint on the circle passing through p1 and p2, with the specified center coordinate.

If useShortestArc is true, then the midpoint returned will be that corresponding to the shorter arc from p1 to p2. If it is false, the longer arc from p1 to p2 will be used (i.e. winding the other way around the circle).

static int QgsGeometryUtils::segmentSide(const QgsPoint& pt1, const QgsPoint& pt3, const QgsPoint& pt2)

For line defined by points pt1 and pt3, find out on which side of the line is point pt3.

Returns -1 if pt3 on the left side, 1 if pt3 is on the right side or 0 if pt3 lies on the line.

static QVector<SelfIntersection> QgsGeometryUtils::selfIntersections(const QgsAbstractGeometry* geom, int part, int ring, double tolerance)

Find self intersections in a polyline.

Parameters
geom The geometry to check
part The part of the geometry to check
ring The ring of the geometry part to check
tolerance The tolerance to use
Returns The list of self intersections

static bool QgsGeometryUtils::setZValueFromPoints(const QgsPointSequence& points, QgsPoint& point)

A Z dimension is added to point if one of the point in the list points is in 3D.

Parameters
points List of points in which a 3D point is searched.
point The point to update with Z dimension and value.
Returns true if the point is updated, false otherwise

Moreover, the Z value of point is updated with.

static double QgsGeometryUtils::skewLinesDistance(const QgsVector3D& P1, const QgsVector3D& P12, const QgsVector3D& P2, const QgsVector3D& P22)

An algorithm to calculate the shortest distance between two skew lines.

Parameters
P1 is the first point of the first line,
P12 is the second point on the first line,
P2 is the first point on the second line,
P22 is the second point on the second line.
Returns the shortest distance

static bool QgsGeometryUtils::skewLinesProjection(const QgsVector3D& P1, const QgsVector3D& P12, const QgsVector3D& P2, const QgsVector3D& P22, QgsVector3D& X1, double epsilon = 0.0001)

A method to project one skew line onto another.

Parameters
P1 is a first point that belonds to first skew line,
P12 is the second point that belongs to first skew line,
P2 is the first point that belongs to second skew line,
P22 is the second point that belongs to second skew line,
X1 is the result projection point of line P2P22 onto line P1P12,
epsilon the tolerance to use.
Returns true if such point exists, false - otherwise.

static bool QgsGeometryUtils::tangentPointAndCircle(const QgsPointXY& center, double radius, const QgsPointXY& p, QgsPointXY& pt1, QgsPointXY& pt2)

Calculates the tangent points between the circle with the specified center and radius and the point p.

If found, the tangent points will be stored in pt1 and pt2.

static bool QgsGeometryUtils::verticesAtDistance(const QgsAbstractGeometry& geometry, double distance, QgsVertexId& previousVertex, QgsVertexId& nextVertex)

Retrieves the vertices which are before and after the interpolated point at a specified distance along a linestring (or polygon boundary).

Parameters
geometry line or polygon geometry
distance distance to traverse along geometry
previousVertex will be set to previous vertex ID
nextVertex will be set to next vertex ID
Returns true if vertices were successfully retrieved

static QStringList QgsGeometryUtils::wktGetChildBlocks(const QString& wkt, const QString& defaultType = QString())

Parses a WKT string and returns of list of blocks contained in the WKT.

Parameters
wkt WKT string in the format "TYPE1 (contents1), TYPE2 (TYPE3 (contents3), TYPE4 (contents4))"
defaultType default geometry type for children
Returns list of WKT child block strings, e.g., List("TYPE1 (contents1)", "TYPE2 (TYPE3 (contents3), TYPE4 (contents4))")

static QPair<QgsWkbTypes::Type, QString> QgsGeometryUtils::wktReadBlock(const QString& wkt)

Parses a WKT block of the format "TYPE( contents )" and returns a pair of geometry type to contents ("Pair(wkbType, "contents")")