QGIS API Documentation  2.10.1-Pisa
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 *
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
49  static double leftOfLine( double x, double y, double x1, double y1, double x2, double y2 );
50
53  static QgsPointV2 pointOnLineWithDistance( const QgsPointV2& startPoint, const QgsPointV2& directionPoint, double distance );
54
56  static double ccwAngle( double dy, double dx );
57
59  static void circleCenterRadius( const QgsPointV2& pt1, const QgsPointV2& pt2, const QgsPointV2& pt3, double& radius,
60  double& centerX, double& centerY );
61
63  static bool circleClockwise( double angle1, double angle2, double angle3 );
64
66  static bool circleAngleBetween( double angle, double angle1, double angle2, bool clockwise );
67
71  static bool angleOnCircle( double angle, double angle1, double angle2, double angle3 );
72
74  static double circleLength( double x1, double y1, double x2, double y2, double x3, double y3 );
75
77  static double sweepAngle( double centerX, double centerY, double x1, double y1, double x2, double y2, double x3, double y3 );
78
80  static bool segmentMidPoint( const QgsPointV2& p1, const QgsPointV2& p2, QgsPointV2& result, double radius, const QgsPointV2& mousePos );
81
84  static QList<QgsPointV2> pointsFromWKT( const QString& wktCoordinateList, bool is3D, bool isMeasure );
86  static void pointsToWKB( QgsWkbPtr &wkb, const QList<QgsPointV2>& points, bool is3D, bool isMeasure );
88  static QString pointsToWKT( const QList<QgsPointV2>& points, int precision, bool is3D, bool isMeasure );
90  static QDomElement pointsToGML2( const QList<QgsPointV2>& points, QDomDocument &doc, int precision, const QString& ns );
92  static QDomElement pointsToGML3( const QList<QgsPointV2>& points, QDomDocument &doc, int precision, const QString& ns, bool is3D );
94  static QString pointsToJSON( const QList<QgsPointV2>& points, int precision );
95
98  static QPair<QgsWKBTypes::Type, QString> wktReadBlock( const QString& wkt );
99
105  static QStringList wktGetChildBlocks( const QString& wkt , const QString &defaultType = "" );
106
108  {
111  PART
112  };
113
114  template<class T> static double closestSegmentFromComponents( T& container, componentType ctype, const QgsPointV2& pt, QgsPointV2& segmentPt, QgsVertexId& vertexAfter, bool* leftOf, double epsilon )
115  {
116  double minDist = std::numeric_limits<double>::max();
117  double minDistSegmentX = 0.0, minDistSegmentY = 0.0;
118  QgsVertexId minDistVertexAfter;
119  bool minDistLeftOf = false;
120  double sqrDist = 0.0;
121  int vertexOffset = 0;
122  int ringOffset = 0;
123  int partOffset = 0;
124
125  for ( int i = 0; i < container.size(); ++i )
126  {
127  sqrDist = container.at( i )->closestSegment( pt, segmentPt, vertexAfter, leftOf, epsilon );
128  if ( sqrDist < minDist )
129  {
130  minDist = sqrDist;
131  minDistSegmentX = segmentPt.x();
132  minDistSegmentY = segmentPt.y();
133  minDistVertexAfter = vertexAfter;
134  minDistVertexAfter.vertex = vertexAfter.vertex + vertexOffset;
135  minDistVertexAfter.part = vertexAfter.part + partOffset;
136  minDistVertexAfter.ring = vertexAfter.ring + ringOffset;
137  if ( leftOf )
138  {
139  minDistLeftOf = *leftOf;
140  }
141  }
142
143  if ( ctype == VERTEX )
144  {
145  //-1 because compoundcurve counts duplicated vertices of neighbour curves as one node
146  vertexOffset += container.at( i )->nCoordinates() - 1;
147  }
148  else if ( ctype == RING )
149  {
150  ringOffset += 1;
151  }
152  else if ( ctype == PART )
153  {
154  partOffset += 1;
155  }
156  }
157
158  segmentPt.setX( minDistSegmentX );
159  segmentPt.setY( minDistSegmentY );
160  vertexAfter = minDistVertexAfter;
161  if ( leftOf )
162  {
163  *leftOf = minDistLeftOf;
164  }
165  return minDist;
166  }
167 };
168
169 #endif // QGSGEOMETRYUTILS_H
double x() const
Definition: qgspointv2.h:41
Abstract base class for all geometries.
void setX(double x)
Definition: qgspointv2.h:46
static double closestSegmentFromComponents(T &container, componentType ctype, const QgsPointV2 &pt, QgsPointV2 &segmentPt, QgsVertexId &vertexAfter, bool *leftOf, double epsilon)
void setY(double y)
Definition: qgspointv2.h:47
double y() const
Definition: qgspointv2.h:42
Contains various geometry utility functions.
Utility class for identifying a unique vertex within a geometry.
Point geometry type.
Definition: qgspointv2.h:29
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)
double ANALYSIS_EXPORT leftOf(Point3D *thepoint, Point3D *p1, Point3D *p2)
Returns whether 'thepoint' is left or right of the line from 'p1' to 'p2'.
int max(int a, int b)
Definition: util.h:87