QGIS API Documentation  2.14.0-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
42  static QgsPointV2 closestVertex( const QgsAbstractGeometryV2& geom, const QgsPointV2& pt, QgsVertexId& id );
43
46  static void adjacentVertices( const QgsAbstractGeometryV2& geom, QgsVertexId atVertex, QgsVertexId& beforeVertex, QgsVertexId& afterVertex );
47
50  static double sqrDistance2D( const QgsPointV2& pt1, const QgsPointV2& pt2 );
51
54  static double sqrDistToLine( double ptX, double ptY, double x1, double y1, double x2, double y2, double& minDistX, double& minDistY, double epsilon );
55
65  static bool lineIntersection( const QgsPointV2& p1, QgsVector v, const QgsPointV2& q1, QgsVector w, QgsPointV2& inter );
66
77  static bool segmentIntersection( const QgsPointV2 &p1, const QgsPointV2 &p2, const QgsPointV2 &q1, const QgsPointV2 &q2, QgsPointV2& inter, double tolerance );
78
86  static QgsPointV2 projPointOnSegment( const QgsPointV2& p, const QgsPointV2& s1, const QgsPointV2& s2 )
87  {
88  double nx = s2.y() - s1.y();
89  double ny = -( s2.x() - s1.x() );
90  double t = ( p.x() * ny - p.y() * nx - s1.x() * ny + s1.y() * nx ) / (( s2.x() - s1.x() ) * ny - ( s2.y() - s1.y() ) * nx );
91  return t < 0. ? s1 : t > 1. ? s2 : QgsPointV2( s1.x() + ( s2.x() - s1.x() ) * t, s1.y() + ( s2.y() - s1.y() ) * t );
92  }
93
95  {
96  int segment1;
97  int segment2;
99  };
100
110  static QList<SelfIntersection> getSelfIntersections( const QgsAbstractGeometryV2* geom, int part, int ring, double tolerance );
111
113  static double leftOfLine( double x, double y, double x1, double y1, double x2, double y2 );
114
117  static QgsPointV2 pointOnLineWithDistance( const QgsPointV2& startPoint, const QgsPointV2& directionPoint, double distance );
118
120  static double ccwAngle( double dy, double dx );
121
123  static void circleCenterRadius( const QgsPointV2& pt1, const QgsPointV2& pt2, const QgsPointV2& pt3, double& radius,
124  double& centerX, double& centerY );
125
127  static bool circleClockwise( double angle1, double angle2, double angle3 );
128
130  static bool circleAngleBetween( double angle, double angle1, double angle2, bool clockwise );
131
135  static bool angleOnCircle( double angle, double angle1, double angle2, double angle3 );
136
138  static double circleLength( double x1, double y1, double x2, double y2, double x3, double y3 );
139
141  static double sweepAngle( double centerX, double centerY, double x1, double y1, double x2, double y2, double x3, double y3 );
142
144  static bool segmentMidPoint( const QgsPointV2& p1, const QgsPointV2& p2, QgsPointV2& result, double radius, const QgsPointV2& mousePos );
145
147  static double circleTangentDirection( const QgsPointV2& tangentPoint, const QgsPointV2& cp1, const QgsPointV2& cp2, const QgsPointV2& cp3 );
148
151  static QgsPointSequenceV2 pointsFromWKT( const QString& wktCoordinateList, bool is3D, bool isMeasure );
153  static void pointsToWKB( QgsWkbPtr &wkb, const QgsPointSequenceV2 &points, bool is3D, bool isMeasure );
155  static QString pointsToWKT( const QgsPointSequenceV2 &points, int precision, bool is3D, bool isMeasure );
157  static QDomElement pointsToGML2( const QgsPointSequenceV2 &points, QDomDocument &doc, int precision, const QString& ns );
159  static QDomElement pointsToGML3( const QgsPointSequenceV2 &points, QDomDocument &doc, int precision, const QString& ns, bool is3D );
161  static QString pointsToJSON( const QgsPointSequenceV2 &points, int precision );
162
167  static double normalizedAngle( double angle );
168
176  static double lineAngle( double x1, double y1, double x2, double y2 );
177
186  static double linePerpendicularAngle( double x1, double y1, double x2, double y2 );
187
189  static double averageAngle( double x1, double y1, double x2, double y2, double x3, double y3 );
190
196  static double averageAngle( double a1, double a2 );
197
200  static QPair<QgsWKBTypes::Type, QString> wktReadBlock( const QString& wkt );
201
207  static QStringList wktGetChildBlocks( const QString& wkt , const QString &defaultType = "" );
208
210  {
213  PART
214  };
215
216  template<class T> static double closestSegmentFromComponents( T& container, componentType ctype, const QgsPointV2& pt, QgsPointV2& segmentPt, QgsVertexId& vertexAfter, bool* leftOf, double epsilon )
217  {
218  double minDist = std::numeric_limits<double>::max();
219  double minDistSegmentX = 0.0, minDistSegmentY = 0.0;
220  QgsVertexId minDistVertexAfter;
221  bool minDistLeftOf = false;
222  double sqrDist = 0.0;
223  int vertexOffset = 0;
224  int ringOffset = 0;
225  int partOffset = 0;
226
227  for ( int i = 0; i < container.size(); ++i )
228  {
229  sqrDist = container.at( i )->closestSegment( pt, segmentPt, vertexAfter, leftOf, epsilon );
230  if ( sqrDist < minDist )
231  {
232  minDist = sqrDist;
233  minDistSegmentX = segmentPt.x();
234  minDistSegmentY = segmentPt.y();
235  minDistVertexAfter = vertexAfter;
236  minDistVertexAfter.vertex = vertexAfter.vertex + vertexOffset;
237  minDistVertexAfter.part = vertexAfter.part + partOffset;
238  minDistVertexAfter.ring = vertexAfter.ring + ringOffset;
239  if ( leftOf )
240  {
241  minDistLeftOf = *leftOf;
242  }
243  }
244
245  if ( ctype == VERTEX )
246  {
247  //-1 because compoundcurve counts duplicated vertices of neighbour curves as one node
248  vertexOffset += container.at( i )->nCoordinates() - 1;
249  }
250  else if ( ctype == RING )
251  {
252  ringOffset += 1;
253  }
254  else if ( ctype == PART )
255  {
256  partOffset += 1;
257  }
258  }
259
260  segmentPt.setX( minDistSegmentX );
261  segmentPt.setY( minDistSegmentY );
262  vertexAfter = minDistVertexAfter;
263  if ( leftOf )
264  {
265  *leftOf = minDistLeftOf;
266  }
267  return minDist;
268  }
269 };
270
271 #endif // QGSGEOMETRYUTILS_H
double x() const
Returns the point&#39;s x-coordinate.
Definition: qgspointv2.h:68
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.
void setY(double y)
Sets the point&#39;s y-coordinate.
Definition: qgspointv2.h:130
double y() const
Returns the point&#39;s y-coordinate.
Definition: qgspointv2.h:74
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
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;.