QGIS API Documentation  2.14.11-Essen
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 
47  static void adjacentVertices( const QgsAbstractGeometryV2& geom, QgsVertexId atVertex, QgsVertexId& beforeVertex, QgsVertexId& afterVertex );
48 
51  static double sqrDistance2D( const QgsPointV2& pt1, const QgsPointV2& pt2 );
52 
55  static double sqrDistToLine( double ptX, double ptY, double x1, double y1, double x2, double y2, double& minDistX, double& minDistY, double epsilon );
56 
66  static bool lineIntersection( const QgsPointV2& p1, QgsVector v, const QgsPointV2& q1, QgsVector w, QgsPointV2& inter );
67 
78  static bool segmentIntersection( const QgsPointV2 &p1, const QgsPointV2 &p2, const QgsPointV2 &q1, const QgsPointV2 &q2, QgsPointV2& inter, double tolerance );
79 
87  static QgsPointV2 projPointOnSegment( const QgsPointV2& p, const QgsPointV2& s1, const QgsPointV2& s2 )
88  {
89  double nx = s2.y() - s1.y();
90  double ny = -( s2.x() - s1.x() );
91  double t = ( p.x() * ny - p.y() * nx - s1.x() * ny + s1.y() * nx ) / (( s2.x() - s1.x() ) * ny - ( s2.y() - s1.y() ) * nx );
92  return t < 0. ? s1 : t > 1. ? s2 : QgsPointV2( s1.x() + ( s2.x() - s1.x() ) * t, s1.y() + ( s2.y() - s1.y() ) * t );
93  }
94 
96  {
97  int segment1;
98  int segment2;
100  };
101 
111  static QList<SelfIntersection> getSelfIntersections( const QgsAbstractGeometryV2* geom, int part, int ring, double tolerance );
112 
114  static double leftOfLine( double x, double y, double x1, double y1, double x2, double y2 );
115 
118  static QgsPointV2 pointOnLineWithDistance( const QgsPointV2& startPoint, const QgsPointV2& directionPoint, double distance );
119 
121  static double ccwAngle( double dy, double dx );
122 
124  static void circleCenterRadius( const QgsPointV2& pt1, const QgsPointV2& pt2, const QgsPointV2& pt3, double& radius,
125  double& centerX, double& centerY );
126 
128  static bool circleClockwise( double angle1, double angle2, double angle3 );
129 
131  static bool circleAngleBetween( double angle, double angle1, double angle2, bool clockwise );
132 
136  static bool angleOnCircle( double angle, double angle1, double angle2, double angle3 );
137 
139  static double circleLength( double x1, double y1, double x2, double y2, double x3, double y3 );
140 
142  static double sweepAngle( double centerX, double centerY, double x1, double y1, double x2, double y2, double x3, double y3 );
143 
145  static bool segmentMidPoint( const QgsPointV2& p1, const QgsPointV2& p2, QgsPointV2& result, double radius, const QgsPointV2& mousePos );
146 
148  static double circleTangentDirection( const QgsPointV2& tangentPoint, const QgsPointV2& cp1, const QgsPointV2& cp2, const QgsPointV2& cp3 );
149 
152  static QgsPointSequenceV2 pointsFromWKT( const QString& wktCoordinateList, bool is3D, bool isMeasure );
154  static void pointsToWKB( QgsWkbPtr &wkb, const QgsPointSequenceV2 &points, bool is3D, bool isMeasure );
156  static QString pointsToWKT( const QgsPointSequenceV2 &points, int precision, bool is3D, bool isMeasure );
158  static QDomElement pointsToGML2( const QgsPointSequenceV2 &points, QDomDocument &doc, int precision, const QString& ns );
160  static QDomElement pointsToGML3( const QgsPointSequenceV2 &points, QDomDocument &doc, int precision, const QString& ns, bool is3D );
162  static QString pointsToJSON( const QgsPointSequenceV2 &points, int precision );
163 
168  static double normalizedAngle( double angle );
169 
177  static double lineAngle( double x1, double y1, double x2, double y2 );
178 
187  static double linePerpendicularAngle( double x1, double y1, double x2, double y2 );
188 
190  static double averageAngle( double x1, double y1, double x2, double y2, double x3, double y3 );
191 
197  static double averageAngle( double a1, double a2 );
198 
201  static QPair<QgsWKBTypes::Type, QString> wktReadBlock( const QString& wkt );
202 
208  static QStringList wktGetChildBlocks( const QString& wkt , const QString &defaultType = "" );
209 
211  {
214  PART
215  };
216 
217  template<class T> static double closestSegmentFromComponents( T& container, componentType ctype, const QgsPointV2& pt, QgsPointV2& segmentPt, QgsVertexId& vertexAfter, bool* leftOf, double epsilon )
218  {
219  double minDist = std::numeric_limits<double>::max();
220  double minDistSegmentX = 0.0, minDistSegmentY = 0.0;
221  QgsVertexId minDistVertexAfter;
222  bool minDistLeftOf = false;
223  double sqrDist = 0.0;
224  int vertexOffset = 0;
225  int ringOffset = 0;
226  int partOffset = 0;
227 
228  for ( int i = 0; i < container.size(); ++i )
229  {
230  sqrDist = container.at( i )->closestSegment( pt, segmentPt, vertexAfter, leftOf, epsilon );
231  if ( sqrDist >= 0 && sqrDist < minDist )
232  {
233  minDist = sqrDist;
234  minDistSegmentX = segmentPt.x();
235  minDistSegmentY = segmentPt.y();
236  minDistVertexAfter = vertexAfter;
237  minDistVertexAfter.vertex = vertexAfter.vertex + vertexOffset;
238  minDistVertexAfter.part = vertexAfter.part + partOffset;
239  minDistVertexAfter.ring = vertexAfter.ring + ringOffset;
240  if ( leftOf )
241  {
242  minDistLeftOf = *leftOf;
243  }
244  }
245 
246  if ( ctype == VERTEX )
247  {
248  //-1 because compoundcurve counts duplicated vertices of neighbour curves as one node
249  vertexOffset += container.at( i )->nCoordinates() - 1;
250  }
251  else if ( ctype == RING )
252  {
253  ringOffset += 1;
254  }
255  else if ( ctype == PART )
256  {
257  partOffset += 1;
258  }
259  }
260 
261  if ( minDist == std::numeric_limits<double>::max() )
262  return -1; // error: no segments
263 
264  segmentPt.setX( minDistSegmentX );
265  segmentPt.setY( minDistSegmentY );
266  vertexAfter = minDistVertexAfter;
267  if ( leftOf )
268  {
269  *leftOf = minDistLeftOf;
270  }
271  return minDist;
272  }
273 };
274 
275 #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;.