QGIS API Documentation  2.18.21-Las Palmas (9fba24a)
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 "qgspointv2.h"
20 #include <limits>
21 
22 class QgsLineStringV2;
23 
31 class CORE_EXPORT QgsGeometryUtils
32 {
33  public:
34 
38  static QList<QgsLineStringV2*> extractLineStrings( const QgsAbstractGeometryV2* geom );
39 
43  static QgsPointV2 closestVertex( const QgsAbstractGeometryV2& geom, const QgsPointV2& pt, QgsVertexId& id );
44 
51  static double distanceToVertex( const QgsAbstractGeometryV2& geom, const QgsVertexId& id );
52 
63  static bool verticesAtDistance( const QgsAbstractGeometryV2& geometry,
64  double distance,
65  QgsVertexId& previousVertex,
66  QgsVertexId& nextVertex );
67 
70  static void adjacentVertices( const QgsAbstractGeometryV2& geom, QgsVertexId atVertex, QgsVertexId& beforeVertex, QgsVertexId& afterVertex );
71 
74  static double sqrDistance2D( const QgsPointV2& pt1, const QgsPointV2& pt2 );
75 
78  static double sqrDistToLine( double ptX, double ptY, double x1, double y1, double x2, double y2, double& minDistX, double& minDistY, double epsilon );
79 
89  static bool lineIntersection( const QgsPointV2& p1, QgsVector v, const QgsPointV2& q1, QgsVector w, QgsPointV2& inter );
90 
101  static bool segmentIntersection( const QgsPointV2 &p1, const QgsPointV2 &p2, const QgsPointV2 &q1, const QgsPointV2 &q2, QgsPointV2& inter, double tolerance );
102 
110  static QgsPointV2 projPointOnSegment( const QgsPointV2& p, const QgsPointV2& s1, const QgsPointV2& s2 )
111  {
112  double nx = s2.y() - s1.y();
113  double ny = -( s2.x() - s1.x() );
114  double t = ( p.x() * ny - p.y() * nx - s1.x() * ny + s1.y() * nx ) / (( s2.x() - s1.x() ) * ny - ( s2.y() - s1.y() ) * nx );
115  return t < 0. ? s1 : t > 1. ? s2 : QgsPointV2( s1.x() + ( s2.x() - s1.x() ) * t, s1.y() + ( s2.y() - s1.y() ) * t );
116  }
117 
119  {
120  int segment1;
121  int segment2;
123  };
124 
134  static QList<SelfIntersection> getSelfIntersections( const QgsAbstractGeometryV2* geom, int part, int ring, double tolerance );
135 
137  static double leftOfLine( double x, double y, double x1, double y1, double x2, double y2 );
138 
141  static QgsPointV2 pointOnLineWithDistance( const QgsPointV2& startPoint, const QgsPointV2& directionPoint, double distance );
142 
144  static double ccwAngle( double dy, double dx );
145 
147  static void circleCenterRadius( const QgsPointV2& pt1, const QgsPointV2& pt2, const QgsPointV2& pt3, double& radius,
148  double& centerX, double& centerY );
149 
151  static bool circleClockwise( double angle1, double angle2, double angle3 );
152 
154  static bool circleAngleBetween( double angle, double angle1, double angle2, bool clockwise );
155 
159  static bool angleOnCircle( double angle, double angle1, double angle2, double angle3 );
160 
162  static double circleLength( double x1, double y1, double x2, double y2, double x3, double y3 );
163 
165  static double sweepAngle( double centerX, double centerY, double x1, double y1, double x2, double y2, double x3, double y3 );
166 
168  static bool segmentMidPoint( const QgsPointV2& p1, const QgsPointV2& p2, QgsPointV2& result, double radius, const QgsPointV2& mousePos );
169 
171  static double circleTangentDirection( const QgsPointV2& tangentPoint, const QgsPointV2& cp1, const QgsPointV2& cp2, const QgsPointV2& cp3 );
172 
175  static QgsPointSequenceV2 pointsFromWKT( const QString& wktCoordinateList, bool is3D, bool isMeasure );
177  static void pointsToWKB( QgsWkbPtr &wkb, const QgsPointSequenceV2 &points, bool is3D, bool isMeasure );
179  static QString pointsToWKT( const QgsPointSequenceV2 &points, int precision, bool is3D, bool isMeasure );
181  static QDomElement pointsToGML2( const QgsPointSequenceV2 &points, QDomDocument &doc, int precision, const QString& ns );
183  static QDomElement pointsToGML3( const QgsPointSequenceV2 &points, QDomDocument &doc, int precision, const QString& ns, bool is3D );
185  static QString pointsToJSON( const QgsPointSequenceV2 &points, int precision );
186 
191  static double normalizedAngle( double angle );
192 
200  static double lineAngle( double x1, double y1, double x2, double y2 );
201 
210  static double linePerpendicularAngle( double x1, double y1, double x2, double y2 );
211 
213  static double averageAngle( double x1, double y1, double x2, double y2, double x3, double y3 );
214 
220  static double averageAngle( double a1, double a2 );
221 
224  static QPair<QgsWKBTypes::Type, QString> wktReadBlock( const QString& wkt );
225 
231  static QStringList wktGetChildBlocks( const QString& wkt , const QString &defaultType = "" );
232 
234  {
237  PART
238  };
239 
240  template<class T> static double closestSegmentFromComponents( T& container, componentType ctype, const QgsPointV2& pt, QgsPointV2& segmentPt, QgsVertexId& vertexAfter, bool* leftOf, double epsilon )
241  {
242  double minDist = std::numeric_limits<double>::max();
243  double minDistSegmentX = 0.0, minDistSegmentY = 0.0;
244  QgsVertexId minDistVertexAfter;
245  bool minDistLeftOf = false;
246  double sqrDist = 0.0;
247  int vertexOffset = 0;
248  int ringOffset = 0;
249  int partOffset = 0;
250 
251  for ( int i = 0; i < container.size(); ++i )
252  {
253  sqrDist = container.at( i )->closestSegment( pt, segmentPt, vertexAfter, leftOf, epsilon );
254  if ( sqrDist >= 0 && sqrDist < minDist )
255  {
256  minDist = sqrDist;
257  minDistSegmentX = segmentPt.x();
258  minDistSegmentY = segmentPt.y();
259  minDistVertexAfter = vertexAfter;
260  minDistVertexAfter.vertex = vertexAfter.vertex + vertexOffset;
261  minDistVertexAfter.part = vertexAfter.part + partOffset;
262  minDistVertexAfter.ring = vertexAfter.ring + ringOffset;
263  if ( leftOf )
264  {
265  minDistLeftOf = *leftOf;
266  }
267  }
268 
269  if ( ctype == VERTEX )
270  {
271  //-1 because compoundcurve counts duplicated vertices of neighbour curves as one node
272  vertexOffset += container.at( i )->nCoordinates() - 1;
273  }
274  else if ( ctype == RING )
275  {
276  ringOffset += 1;
277  }
278  else if ( ctype == PART )
279  {
280  partOffset += 1;
281  }
282  }
283 
284  if ( minDist == std::numeric_limits<double>::max() )
285  return -1; // error: no segments
286 
287  segmentPt.setX( minDistSegmentX );
288  segmentPt.setY( minDistSegmentY );
289  vertexAfter = minDistVertexAfter;
290  if ( leftOf )
291  {
292  *leftOf = minDistLeftOf;
293  }
294  return minDist;
295  }
296 };
297 
298 #endif // QGSGEOMETRYUTILS_H
Abstract base class for all geometries.
void setX(double x)
Sets the point&#39;s x-coordinate.
Definition: qgspointv2.h:124
static double closestSegmentFromComponents(T &container, componentType ctype, const QgsPointV2 &pt, QgsPointV2 &segmentPt, QgsVertexId &vertexAfter, bool *leftOf, double epsilon)
static QgsPointV2 projPointOnSegment(const QgsPointV2 &p, const QgsPointV2 &s1, const QgsPointV2 &s2)
Project the point on a segment.
double y() const
Returns the point&#39;s y-coordinate.
Definition: qgspointv2.h:74
void setY(double y)
Sets the point&#39;s y-coordinate.
Definition: qgspointv2.h:130
double ANALYSIS_EXPORT max(double x, double y)
Returns the maximum of two doubles or the first argument if both are equal.
Contains various geometry utility functions.
Utility class for identifying a unique vertex within a geometry.
Line string geometry type, with support for z-dimension and m-values.
Point geometry type, with support for z-dimension and m-values.
Definition: qgspointv2.h:34
double x() const
Returns the point&#39;s x-coordinate.
Definition: qgspointv2.h:68
bool ANALYSIS_EXPORT lineIntersection(Point3D *p1, Point3D *p2, Point3D *p3, Point3D *p4)
Returns true, if line1 (p1 to p2) and line2 (p3 to p4) intersect.
double ANALYSIS_EXPORT angle(Point3D *p1, Point3D *p2, Point3D *p3, Point3D *p4)
Calculates the angle between two segments (in 2 dimension, z-values are ignored)
A class to represent a vector.
Definition: qgspoint.h:32
double ANALYSIS_EXPORT leftOf(Point3D *thepoint, Point3D *p1, Point3D *p2)
Returns whether &#39;thepoint&#39; is left or right of the line from &#39;p1&#39; to &#39;p2&#39;.