QGIS API Documentation  2.12.0-Lyon
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 
28 class CORE_EXPORT QgsGeometryUtils
29 {
30  public:
31 
34  static QgsPointV2 closestVertex( const QgsAbstractGeometryV2& geom, const QgsPointV2& pt, QgsVertexId& id );
35 
38  static void adjacentVertices( const QgsAbstractGeometryV2& geom, const QgsVertexId& atVertex, QgsVertexId& beforeVertex, QgsVertexId& afterVertex );
39 
42  static double sqrDistance2D( const QgsPointV2& pt1, const QgsPointV2& pt2 );
43 
46  static double sqrDistToLine( double ptX, double ptY, double x1, double y1, double x2, double y2, double& minDistX, double& minDistY, double epsilon );
47 
57  static bool lineIntersection( const QgsPointV2& p1, const QgsVector& v, const QgsPointV2& q1, const QgsVector& w, QgsPointV2& inter );
58 
69  static bool segmentIntersection( const QgsPointV2 &p1, const QgsPointV2 &p2, const QgsPointV2 &q1, const QgsPointV2 &q2, QgsPointV2& inter, double tolerance );
70 
78  static QgsPointV2 projPointOnSegment( const QgsPointV2& p, const QgsPointV2& s1, const QgsPointV2& s2 )
79  {
80  double nx = s2.y() - s1.y();
81  double ny = -( s2.x() - s1.x() );
82  double t = ( p.x() * ny - p.y() * nx - s1.x() * ny + s1.y() * nx ) / (( s2.x() - s1.x() ) * ny - ( s2.y() - s1.y() ) * nx );
83  return t < 0. ? s1 : t > 1. ? s2 : QgsPointV2( s1.x() + ( s2.x() - s1.x() ) * t, s1.y() + ( s2.y() - s1.y() ) * t );
84  }
85 
87  {
88  int segment1;
89  int segment2;
91  };
92 
102  static QList<SelfIntersection> getSelfIntersections( const QgsAbstractGeometryV2* geom, int part, int ring, double tolerance );
103 
105  static double leftOfLine( double x, double y, double x1, double y1, double x2, double y2 );
106 
109  static QgsPointV2 pointOnLineWithDistance( const QgsPointV2& startPoint, const QgsPointV2& directionPoint, double distance );
110 
112  static double ccwAngle( double dy, double dx );
113 
115  static void circleCenterRadius( const QgsPointV2& pt1, const QgsPointV2& pt2, const QgsPointV2& pt3, double& radius,
116  double& centerX, double& centerY );
117 
119  static bool circleClockwise( double angle1, double angle2, double angle3 );
120 
122  static bool circleAngleBetween( double angle, double angle1, double angle2, bool clockwise );
123 
127  static bool angleOnCircle( double angle, double angle1, double angle2, double angle3 );
128 
130  static double circleLength( double x1, double y1, double x2, double y2, double x3, double y3 );
131 
133  static double sweepAngle( double centerX, double centerY, double x1, double y1, double x2, double y2, double x3, double y3 );
134 
136  static bool segmentMidPoint( const QgsPointV2& p1, const QgsPointV2& p2, QgsPointV2& result, double radius, const QgsPointV2& mousePos );
137 
139  static double circleTangentDirection( const QgsPointV2& tangentPoint, const QgsPointV2& cp1, const QgsPointV2& cp2, const QgsPointV2& cp3 );
140 
143  static QList<QgsPointV2> pointsFromWKT( const QString& wktCoordinateList, bool is3D, bool isMeasure );
145  static void pointsToWKB( QgsWkbPtr &wkb, const QList<QgsPointV2>& points, bool is3D, bool isMeasure );
147  static QString pointsToWKT( const QList<QgsPointV2>& points, int precision, bool is3D, bool isMeasure );
149  static QDomElement pointsToGML2( const QList<QgsPointV2>& points, QDomDocument &doc, int precision, const QString& ns );
151  static QDomElement pointsToGML3( const QList<QgsPointV2>& points, QDomDocument &doc, int precision, const QString& ns, bool is3D );
153  static QString pointsToJSON( const QList<QgsPointV2>& points, int precision );
155  static double lineAngle( double x1, double y1, double x2, double y2 );
157  static double linePerpendicularAngle( double x1, double y1, double x2, double y2 );
159  static double averageAngle( double x1, double y1, double x2, double y2, double x3, double y3 );
161  static double averageAngle( double a1, double a2 );
162 
163 
166  static QPair<QgsWKBTypes::Type, QString> wktReadBlock( const QString& wkt );
167 
173  static QStringList wktGetChildBlocks( const QString& wkt , const QString &defaultType = "" );
174 
176  {
179  PART
180  };
181 
182  template<class T> static double closestSegmentFromComponents( T& container, componentType ctype, const QgsPointV2& pt, QgsPointV2& segmentPt, QgsVertexId& vertexAfter, bool* leftOf, double epsilon )
183  {
184  double minDist = std::numeric_limits<double>::max();
185  double minDistSegmentX = 0.0, minDistSegmentY = 0.0;
186  QgsVertexId minDistVertexAfter;
187  bool minDistLeftOf = false;
188  double sqrDist = 0.0;
189  int vertexOffset = 0;
190  int ringOffset = 0;
191  int partOffset = 0;
192 
193  for ( int i = 0; i < container.size(); ++i )
194  {
195  sqrDist = container.at( i )->closestSegment( pt, segmentPt, vertexAfter, leftOf, epsilon );
196  if ( sqrDist < minDist )
197  {
198  minDist = sqrDist;
199  minDistSegmentX = segmentPt.x();
200  minDistSegmentY = segmentPt.y();
201  minDistVertexAfter = vertexAfter;
202  minDistVertexAfter.vertex = vertexAfter.vertex + vertexOffset;
203  minDistVertexAfter.part = vertexAfter.part + partOffset;
204  minDistVertexAfter.ring = vertexAfter.ring + ringOffset;
205  if ( leftOf )
206  {
207  minDistLeftOf = *leftOf;
208  }
209  }
210 
211  if ( ctype == VERTEX )
212  {
213  //-1 because compoundcurve counts duplicated vertices of neighbour curves as one node
214  vertexOffset += container.at( i )->nCoordinates() - 1;
215  }
216  else if ( ctype == RING )
217  {
218  ringOffset += 1;
219  }
220  else if ( ctype == PART )
221  {
222  partOffset += 1;
223  }
224  }
225 
226  segmentPt.setX( minDistSegmentX );
227  segmentPt.setY( minDistSegmentY );
228  vertexAfter = minDistVertexAfter;
229  if ( leftOf )
230  {
231  *leftOf = minDistLeftOf;
232  }
233  return minDist;
234  }
235 };
236 
237 #endif // QGSGEOMETRYUTILS_H
double x() const
Definition: qgspointv2.h:42
Abstract base class for all geometries.
void setX(double x)
Definition: qgspointv2.h:47
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.
void setY(double y)
Definition: qgspointv2.h:48
double y() const
Definition: qgspointv2.h:43
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.
Point geometry type.
Definition: qgspointv2.h:29
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 'thepoint' is left or right of the line from 'p1' to 'p2'.