QGIS API Documentation  3.0.2-Girona (307d082)
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 <limits>
20 
21 #include "qgis_core.h"
22 #include "qgis.h"
23 #include "qgspoint.h"
24 
25 
26 class QgsLineString;
27 
34 class CORE_EXPORT QgsGeometryUtils
35 {
36  public:
37 
42  static QVector<QgsLineString *> extractLineStrings( const QgsAbstractGeometry *geom ) SIP_FACTORY;
43 
48  static QgsPoint closestVertex( const QgsAbstractGeometry &geom, const QgsPoint &pt, QgsVertexId &id SIP_OUT );
49 
55  static QgsPoint closestPoint( const QgsAbstractGeometry &geometry, const QgsPoint &point );
56 
64  static double distanceToVertex( const QgsAbstractGeometry &geom, QgsVertexId id );
65 
77  static bool verticesAtDistance( const QgsAbstractGeometry &geometry,
78  double distance,
79  QgsVertexId &previousVertex SIP_OUT,
80  QgsVertexId &nextVertex SIP_OUT );
81 
85  static double sqrDistance2D( const QgsPoint &pt1, const QgsPoint &pt2 );
86 
90  static double sqrDistToLine( double ptX, double ptY, double x1, double y1, double x2, double y2, double &minDistX SIP_OUT, double &minDistY SIP_OUT, double epsilon );
91 
103  static bool lineIntersection( const QgsPoint &p1, QgsVector v1, const QgsPoint &p2, QgsVector v2, QgsPoint &intersection SIP_OUT );
104 
139  static bool segmentIntersection( const QgsPoint &p1, const QgsPoint &p2, const QgsPoint &q1, const QgsPoint &q2, QgsPoint &intersectionPoint SIP_OUT, bool &isIntersection SIP_OUT, const double tolerance = 1e-8, bool acceptImproperIntersection = false );
140 
152  static bool lineCircleIntersection( const QgsPointXY &center, const double radius,
153  const QgsPointXY &linePoint1, const QgsPointXY &linePoint2,
154  QgsPointXY &intersection SIP_INOUT );
155 
163  static QgsPoint projectPointOnSegment( const QgsPoint &p, const QgsPoint &s1, const QgsPoint &s2 )
164  {
165  double nx = s2.y() - s1.y();
166  double ny = -( s2.x() - s1.x() );
167  double t = ( p.x() * ny - p.y() * nx - s1.x() * ny + s1.y() * nx ) / ( ( s2.x() - s1.x() ) * ny - ( s2.y() - s1.y() ) * nx );
168  return t < 0. ? s1 : t > 1. ? s2 : QgsPoint( s1.x() + ( s2.x() - s1.x() ) * t, s1.y() + ( s2.y() - s1.y() ) * t );
169  }
170 
173  {
174  int segment1;
175  int segment2;
177  };
178 
189  static QVector<SelfIntersection> selfIntersections( const QgsAbstractGeometry *geom, int part, int ring, double tolerance ) SIP_SKIP;
190 
198  static int leftOfLine( double x, double y, double x1, double y1, double x2, double y2 );
199 
203  static QgsPoint pointOnLineWithDistance( const QgsPoint &startPoint, const QgsPoint &directionPoint, double distance );
204 
206  static double ccwAngle( double dy, double dx );
207 
209  static void circleCenterRadius( const QgsPoint &pt1, const QgsPoint &pt2, const QgsPoint &pt3, double &radius SIP_OUT,
210  double &centerX SIP_OUT, double &centerY SIP_OUT );
211 
213  static bool circleClockwise( double angle1, double angle2, double angle3 );
214 
216  static bool circleAngleBetween( double angle, double angle1, double angle2, bool clockwise );
217 
222  static bool angleOnCircle( double angle, double angle1, double angle2, double angle3 );
223 
225  static double circleLength( double x1, double y1, double x2, double y2, double x3, double y3 );
226 
228  static double sweepAngle( double centerX, double centerY, double x1, double y1, double x2, double y2, double x3, double y3 );
229 
235  static bool segmentMidPoint( const QgsPoint &p1, const QgsPoint &p2, QgsPoint &result SIP_OUT, double radius, const QgsPoint &mousePos );
236 
238  static double circleTangentDirection( const QgsPoint &tangentPoint, const QgsPoint &cp1, const QgsPoint &cp2, const QgsPoint &cp3 );
239 
244  static void segmentizeArc( const QgsPoint &p1, const QgsPoint &p2, const QgsPoint &p3,
245  QgsPointSequence SIP_PYALTERNATIVETYPE( QVector<QgsPoint> ) &points SIP_OUT, double tolerance = M_PI_2 / 90,
247  bool hasZ = false, bool hasM = false );
248 
254  static int segmentSide( const QgsPoint &pt1, const QgsPoint &pt3, const QgsPoint &pt2 );
255 
260  static double interpolateArcValue( double angle, double a1, double a2, double a3, double zm1, double zm2, double zm3 );
261 
266  static QgsPointSequence pointsFromWKT( const QString &wktCoordinateList, bool is3D, bool isMeasure ) SIP_SKIP;
267 
272  static void pointsToWKB( QgsWkbPtr &wkb, const QgsPointSequence &points, bool is3D, bool isMeasure ) SIP_SKIP;
273 
278  static QString pointsToWKT( const QgsPointSequence &points, int precision, bool is3D, bool isMeasure ) SIP_SKIP;
279 
284  static QDomElement pointsToGML2( const QgsPointSequence &points, QDomDocument &doc, int precision, const QString &ns ) SIP_SKIP;
285 
290  static QDomElement pointsToGML3( const QgsPointSequence &points, QDomDocument &doc, int precision, const QString &ns, bool is3D ) SIP_SKIP;
291 
296  static QString pointsToJSON( const QgsPointSequence &points, int precision ) SIP_SKIP;
297 
303  static double normalizedAngle( double angle );
304 
313  static double lineAngle( double x1, double y1, double x2, double y2 );
314 
326  static double angleBetweenThreePoints( double x1, double y1, double x2, double y2,
327  double x3, double y3 );
328 
338  static double linePerpendicularAngle( double x1, double y1, double x2, double y2 );
339 
341  static double averageAngle( double x1, double y1, double x2, double y2, double x3, double y3 );
342 
349  static double averageAngle( double a1, double a2 );
350 
355  static QPair<QgsWkbTypes::Type, QString> wktReadBlock( const QString &wkt ) SIP_SKIP;
356 
364  static QStringList wktGetChildBlocks( const QString &wkt, const QString &defaultType = QString() ) SIP_SKIP;
365 
387  static QgsPoint midpoint( const QgsPoint &pt1, const QgsPoint &pt2 );
388 
402  static QgsPointXY interpolatePointOnLine( double x1, double y1, double x2, double y2, double fraction );
403 
420  static QgsPoint interpolatePointOnLine( const QgsPoint &p1, const QgsPoint &p2, double fraction );
421 
434  static QgsPointXY interpolatePointOnLineByValue( double x1, double y1, double v1, double x2, double y2, double v2, double value );
435 
443  static double gradient( const QgsPoint &pt1, const QgsPoint &pt2 );
444 
454  static void coefficients( const QgsPoint &pt1, const QgsPoint &pt2,
455  double &a SIP_OUT, double &b SIP_OUT, double &c SIP_OUT );
456 
464  static QgsLineString perpendicularSegment( const QgsPoint &p, const QgsPoint &s1, const QgsPoint &s2 );
465 
476  static bool setZValueFromPoints( const QgsPointSequence &points, QgsPoint &point );
477 
479  enum ComponentType SIP_SKIP
480  {
483  Part
484  };
485 
487  template<class T> static double closestSegmentFromComponents( T &container, ComponentType ctype, const QgsPoint &pt, QgsPoint &segmentPt, QgsVertexId &vertexAfter, int *leftOf, double epsilon ) SIP_SKIP
488  {
489  double minDist = std::numeric_limits<double>::max();
490  double minDistSegmentX = 0.0, minDistSegmentY = 0.0;
491  QgsVertexId minDistVertexAfter;
492  int minDistLeftOf = 0;
493  double sqrDist = 0.0;
494  int vertexOffset = 0;
495  int ringOffset = 0;
496  int partOffset = 0;
497 
498  for ( int i = 0; i < container.size(); ++i )
499  {
500  sqrDist = container.at( i )->closestSegment( pt, segmentPt, vertexAfter, leftOf, epsilon );
501  if ( sqrDist >= 0 && sqrDist < minDist )
502  {
503  minDist = sqrDist;
504  minDistSegmentX = segmentPt.x();
505  minDistSegmentY = segmentPt.y();
506  minDistVertexAfter = vertexAfter;
507  minDistVertexAfter.vertex = vertexAfter.vertex + vertexOffset;
508  minDistVertexAfter.part = vertexAfter.part + partOffset;
509  minDistVertexAfter.ring = vertexAfter.ring + ringOffset;
510  if ( leftOf )
511  {
512  minDistLeftOf = *leftOf;
513  }
514  }
515 
516  if ( ctype == Vertex )
517  {
518  //-1 because compoundcurve counts duplicated vertices of neighbour curves as one node
519  vertexOffset += container.at( i )->nCoordinates() - 1;
520  }
521  else if ( ctype == Ring )
522  {
523  ringOffset += 1;
524  }
525  else if ( ctype == Part )
526  {
527  partOffset += 1;
528  }
529  }
530 
531  if ( minDist == std::numeric_limits<double>::max() )
532  return -1; // error: no segments
533 
534  segmentPt.setX( minDistSegmentX );
535  segmentPt.setY( minDistSegmentY );
536  vertexAfter = minDistVertexAfter;
537  if ( leftOf )
538  {
539  *leftOf = minDistLeftOf;
540  }
541  return minDist;
542  }
543 };
544 
545 #endif // QGSGEOMETRYUTILS_H
double y
Definition: qgspoint.h:42
Maximum angle between generating radii (lines from arc center to output vertices) ...
A class to represent a 2D point.
Definition: qgspointxy.h:43
bool ANALYSIS_EXPORT lineIntersection(QgsPoint *p1, QgsPoint *p2, QgsPoint *p3, QgsPoint *p4)
Returns true, if line1 (p1 to p2) and line2 (p3 to p4) intersect. If the lines have an endpoint in co...
Definition: MathUtils.cpp:311
#define SIP_INOUT
Definition: qgis_sip.h:64
SegmentationToleranceType
Segmentation tolerance as maximum angle or maximum difference between approximation and circle...
double ANALYSIS_EXPORT angle(QgsPoint *p1, QgsPoint *p2, QgsPoint *p3, QgsPoint *p4)
Calculates the angle between two segments (in 2 dimension, z-values are ignored)
Definition: MathUtils.cpp:786
Contains various geometry utility functions.
Utility class for identifying a unique vertex within a geometry.
#define SIP_SKIP
Definition: qgis_sip.h:119
#define SIP_FACTORY
Definition: qgis_sip.h:69
Abstract base class for all geometries.
Point geometry type, with support for z-dimension and m-values.
Definition: qgspoint.h:37
A class to represent a vector.
Definition: qgsvector.h:27
QVector< QgsPoint > QgsPointSequence
#define SIP_OUT
Definition: qgis_sip.h:51
Line string geometry type, with support for z-dimension and m-values.
Definition: qgslinestring.h:41
static QgsPoint projectPointOnSegment(const QgsPoint &p, const QgsPoint &s1, const QgsPoint &s2)
Project the point on a segment.
double ANALYSIS_EXPORT leftOf(const QgsPoint &thepoint, const QgsPoint *p1, const QgsPoint *p2)
Returns whether &#39;thepoint&#39; is left or right of the line from &#39;p1&#39; to &#39;p2&#39;. Negativ values mean left a...
Definition: MathUtils.cpp:292
static double closestSegmentFromComponents(T &container, ComponentType ctype, const QgsPoint &pt, QgsPoint &segmentPt, QgsVertexId &vertexAfter, int *leftOf, double epsilon)
#define SIP_PYALTERNATIVETYPE(type)
Definition: qgis_sip.h:129
double x
Definition: qgspoint.h:41