Skip to content

Commit 767e7a5

Browse files
committed
Expose QgsGeometryUtils to python bindings
Has some handy functions and there's no strong reason anymore why these shouldn't be part of stable API
1 parent 9ceb6c6 commit 767e7a5

File tree

3 files changed

+105
-9
lines changed

3 files changed

+105
-9
lines changed

python/core/core.sip

+1
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,7 @@
356356
%Include geometry/qgsgeometry.sip
357357
%Include geometry/qgsgeometrycollection.sip
358358
%Include geometry/qgsgeometryengine.sip
359+
%Include geometry/qgsgeometryutils.sip
359360
%Include geometry/qgslinestring.sip
360361
%Include geometry/qgsmulticurve.sip
361362
%Include geometry/qgsmultilinestring.sip
+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
class QgsGeometryUtils
2+
{
3+
%TypeHeaderCode
4+
#include <qgsgeometryutils.h>
5+
%End
6+
7+
public:
8+
9+
static QList<QgsLineString*> extractLineStrings( const QgsAbstractGeometry* geom );
10+
11+
static QgsPointV2 closestVertex( const QgsAbstractGeometry& geom, const QgsPointV2& pt, QgsVertexId& id /Out/ );
12+
13+
static double distanceToVertex( const QgsAbstractGeometry& geom, QgsVertexId id );
14+
15+
static bool verticesAtDistance( const QgsAbstractGeometry& geometry,
16+
double distance,
17+
QgsVertexId& previousVertex /Out/,
18+
QgsVertexId& nextVertex /Out/ );
19+
20+
static void adjacentVertices( const QgsAbstractGeometry& geom, QgsVertexId atVertex, QgsVertexId& beforeVertex /Out/, QgsVertexId& afterVertex /Out/ );
21+
22+
static double sqrDistance2D( const QgsPointV2& pt1, const QgsPointV2& pt2 );
23+
24+
static double sqrDistToLine( double ptX, double ptY, double x1, double y1, double x2, double y2, double& minDistX /Out/, double& minDistY /Out/, double epsilon );
25+
26+
static bool lineIntersection( const QgsPointV2& p1, QgsVector v, const QgsPointV2& q1, QgsVector w, QgsPointV2& inter /Out/ );
27+
28+
static bool segmentIntersection( const QgsPointV2 &p1, const QgsPointV2 &p2, const QgsPointV2 &q1, const QgsPointV2 &q2, QgsPointV2& inter /Out/, double tolerance );
29+
30+
static QgsPointV2 projPointOnSegment( const QgsPointV2& p, const QgsPointV2& s1, const QgsPointV2& s2 );
31+
32+
static double leftOfLine( double x, double y, double x1, double y1, double x2, double y2 );
33+
34+
static QgsPointV2 pointOnLineWithDistance( const QgsPointV2& startPoint, const QgsPointV2& directionPoint, double distance );
35+
36+
static double ccwAngle( double dy, double dx );
37+
38+
static void circleCenterRadius( const QgsPointV2& pt1, const QgsPointV2& pt2, const QgsPointV2& pt3, double& radius /Out/,
39+
double& centerX /Out/, double& centerY /Out/ );
40+
41+
static bool circleClockwise( double angle1, double angle2, double angle3 );
42+
43+
static bool circleAngleBetween( double angle, double angle1, double angle2, bool clockwise );
44+
45+
static bool angleOnCircle( double angle, double angle1, double angle2, double angle3 );
46+
47+
static double circleLength( double x1, double y1, double x2, double y2, double x3, double y3 );
48+
49+
static double sweepAngle( double centerX, double centerY, double x1, double y1, double x2, double y2, double x3, double y3 );
50+
51+
static bool segmentMidPoint( const QgsPointV2& p1, const QgsPointV2& p2, QgsPointV2& result /Out/, double radius, const QgsPointV2& mousePos );
52+
53+
static double circleTangentDirection( const QgsPointV2& tangentPoint, const QgsPointV2& cp1, const QgsPointV2& cp2, const QgsPointV2& cp3 );
54+
55+
static double normalizedAngle( double angle );
56+
57+
static double lineAngle( double x1, double y1, double x2, double y2 );
58+
59+
static double angleBetweenThreePoints( double x1, double y1, double x2, double y2,
60+
double x3, double y3 );
61+
62+
static double linePerpendicularAngle( double x1, double y1, double x2, double y2 );
63+
64+
static double averageAngle( double x1, double y1, double x2, double y2, double x3, double y3 );
65+
66+
static double averageAngle( double a1, double a2 );
67+
68+
};

src/core/geometry/qgsgeometryutils.h

+36-9
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,6 @@ class QgsLineString;
2626
* \class QgsGeometryUtils
2727
* \brief Contains various geometry utility functions.
2828
* \note added in QGIS 2.10
29-
* \note this API is not considered stable and may change for 2.12
30-
* \note not available in Python bindings
3129
*/
3230
class CORE_EXPORT QgsGeometryUtils
3331
{
@@ -116,6 +114,7 @@ class CORE_EXPORT QgsGeometryUtils
116114
return t < 0. ? s1 : t > 1. ? s2 : QgsPointV2( s1.x() + ( s2.x() - s1.x() ) * t, s1.y() + ( s2.y() - s1.y() ) * t );
117115
}
118116

117+
//! @note not available in Python bindings
119118
struct SelfIntersection
120119
{
121120
int segment1;
@@ -130,6 +129,7 @@ class CORE_EXPORT QgsGeometryUtils
130129
* @param ring The ring of the geometry part to check
131130
* @param tolerance The tolerance to use
132131
* @return The list of self intersections
132+
* @note not available in Python bindings
133133
* @note added in QGIS 2.12
134134
*/
135135
static QList<SelfIntersection> getSelfIntersections( const QgsAbstractGeometry* geom, int part, int ring, double tolerance );
@@ -172,17 +172,38 @@ class CORE_EXPORT QgsGeometryUtils
172172
static double circleTangentDirection( const QgsPointV2& tangentPoint, const QgsPointV2& cp1, const QgsPointV2& cp2, const QgsPointV2& cp3 );
173173

174174
/** Returns a list of points contained in a WKT string.
175+
* @note not available in Python bindings
175176
*/
176177
static QgsPointSequence pointsFromWKT( const QString& wktCoordinateList, bool is3D, bool isMeasure );
177-
//! Returns a LinearRing { uint32 numPoints; Point points[numPoints]; }
178+
179+
/**
180+
* Returns a LinearRing { uint32 numPoints; Point points[numPoints]; }
181+
* @note not available in Python bindings
182+
*/
178183
static void pointsToWKB( QgsWkbPtr &wkb, const QgsPointSequence &points, bool is3D, bool isMeasure );
179-
//! Returns a WKT coordinate list
184+
185+
/**
186+
* Returns a WKT coordinate list
187+
* @note not available in Python bindings
188+
*/
180189
static QString pointsToWKT( const QgsPointSequence &points, int precision, bool is3D, bool isMeasure );
181-
//! Returns a gml::coordinates DOM element
190+
191+
/**
192+
* Returns a gml::coordinates DOM element.
193+
* @note not available in Python bindings
194+
*/
182195
static QDomElement pointsToGML2( const QgsPointSequence &points, QDomDocument &doc, int precision, const QString& ns );
183-
//! Returns a gml::posList DOM element
196+
197+
/**
198+
* Returns a gml::posList DOM element.
199+
* @note not available in Python bindings
200+
*/
184201
static QDomElement pointsToGML3( const QgsPointSequence &points, QDomDocument &doc, int precision, const QString& ns, bool is3D );
185-
//! Returns a geoJSON coordinates string
202+
203+
/**
204+
* Returns a geoJSON coordinates string.
205+
* @note not available in Python bindings
206+
*/
186207
static QString pointsToJSON( const QgsPointSequence &points, int precision );
187208

188209
/** Ensures that an angle is in the range 0 <= angle < 2 pi.
@@ -233,24 +254,30 @@ class CORE_EXPORT QgsGeometryUtils
233254
*/
234255
static double averageAngle( double a1, double a2 );
235256

236-
/** Parses a WKT block of the format "TYPE( contents )" and returns a pair of geometry type to contents ("Pair(wkbType, "contents")")
257+
/**
258+
* Parses a WKT block of the format "TYPE( contents )" and returns a pair of geometry type to contents ("Pair(wkbType, "contents")")
259+
* @note not available in Python bindings
237260
*/
238261
static QPair<QgsWkbTypes::Type, QString> wktReadBlock( const QString& wkt );
239262

240-
/** Parses a WKT string and returns of list of blocks contained in the WKT.
263+
/**
264+
* Parses a WKT string and returns of list of blocks contained in the WKT.
241265
* @param wkt WKT string in the format "TYPE1 (contents1), TYPE2 (TYPE3 (contents3), TYPE4 (contents4))"
242266
* @param defaultType default geometry type for children
243267
* @returns list of WKT child block strings, e.g., List("TYPE1 (contents1)", "TYPE2 (TYPE3 (contents3), TYPE4 (contents4))")
268+
* @note not available in Python bindings
244269
*/
245270
static QStringList wktGetChildBlocks( const QString& wkt , const QString &defaultType = "" );
246271

272+
//! @note not available in Python bindings
247273
enum ComponentType
248274
{
249275
Vertex,
250276
Ring,
251277
Part
252278
};
253279

280+
//! @note not available in Python bindings
254281
template<class T> static double closestSegmentFromComponents( T& container, ComponentType ctype, const QgsPointV2& pt, QgsPointV2& segmentPt, QgsVertexId& vertexAfter, bool* leftOf, double epsilon )
255282
{
256283
double minDist = std::numeric_limits<double>::max();

0 commit comments

Comments
 (0)