QGIS API Documentation  3.13.0-Master (95f9187dac)
qgspointlocator.h
Go to the documentation of this file.
1 /***************************************************************************
2  qgspointlocator.h
3  --------------------------------------
4  Date : November 2014
5  Copyright : (C) 2014 by Martin Dobias
6  Email : wonder dot sk at gmail 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 QGSPOINTLOCATOR_H
17 #define QGSPOINTLOCATOR_H
18 
19 class QgsPointXY;
20 class QgsFeatureRenderer;
21 class QgsRenderContext;
22 class QgsRectangle;
24 
25 #include "qgis_core.h"
26 #include "qgspointxy.h"
28 #include "qgscoordinatetransform.h"
29 #include "qgsfeatureid.h"
30 #include "qgsgeometry.h"
31 #include "qgsgeometryutils.h"
32 #include "qgsvectorlayer.h"
33 #include "qgslinestring.h"
35 #include <memory>
36 
43 
51 
59 
66 
73 
80 
82 {
83  class IStorageManager;
84  class ISpatialIndex;
85 }
86 
98 class CORE_EXPORT QgsPointLocator : public QObject
99 {
100  Q_OBJECT
101  public:
102 
115  const QgsRectangle *extent = nullptr );
116 
117  ~QgsPointLocator() override;
118 
123  QgsVectorLayer *layer() const { return mLayer; }
124 
129  QgsCoordinateReferenceSystem destinationCrs() const;
130 
135  const QgsRectangle *extent() const { return mExtent.get(); }
136 
141  void setExtent( const QgsRectangle *extent );
142 
147  void setRenderContext( const QgsRenderContext *context );
148 
152  enum Type
153  {
154  Invalid = 0,
155  Vertex = 1,
156  Edge = 2,
157  Area = 4,
158  Centroid = 8,
159  MiddleOfSegment = 16,
160  All = Vertex | Edge | Area | Centroid | MiddleOfSegment
161  };
162 
163  Q_DECLARE_FLAGS( Types, Type )
164 
165 
178  bool init( int maxFeaturesToIndex = -1, bool relaxed = false );
179 
181  bool hasIndex() const;
182 
183  struct Match
184  {
186  Match() = default;
187 
188  Match( QgsPointLocator::Type t, QgsVectorLayer *vl, QgsFeatureId fid, double dist, const QgsPointXY &pt, int vertexIndex = 0, QgsPointXY *edgePoints = nullptr )
189  : mType( t )
190  , mDist( dist )
191  , mPoint( pt )
192  , mLayer( vl )
193  , mFid( fid )
194  , mVertexIndex( vertexIndex )
195  {
196  if ( edgePoints )
197  {
198  mEdgePoints[0] = edgePoints[0];
199  mEdgePoints[1] = edgePoints[1];
200  }
201  }
202 
203  QgsPointLocator::Type type() const { return mType; }
204 
205  bool isValid() const { return mType != Invalid; }
207  bool hasVertex() const { return mType == Vertex; }
209  bool hasEdge() const { return mType == Edge; }
211  bool hasCentroid() const { return mType == Centroid; }
213  bool hasArea() const { return mType == Area; }
215  bool hasMiddleSegment() const { return mType == MiddleOfSegment; }
216 
221  double distance() const { return mDist; }
222 
227  QgsPointXY point() const { return mPoint; }
228 
230  int vertexIndex() const { return mVertexIndex; }
231 
236  QgsVectorLayer *layer() const { return mLayer; }
237 
241  QgsFeatureId featureId() const { return mFid; }
242 
244  void edgePoints( QgsPointXY &pt1 SIP_OUT, QgsPointXY &pt2 SIP_OUT ) const
245  {
246  pt1 = mEdgePoints[0];
247  pt2 = mEdgePoints[1];
248  }
249 
256  {
257  QgsPoint point;
258  const QgsGeometry geom = mLayer->getGeometry( mFid );
259  if ( !( geom.isNull() || geom.isEmpty() ) )
260  {
261  QgsLineString line( geom.vertexAt( mVertexIndex ), geom.vertexAt( mVertexIndex + 1 ) );
262 
263  point = QgsGeometryUtils::closestPoint( line, QgsPoint( mPoint ) );
264  }
265  return point;
266  }
267 
268  bool operator==( const QgsPointLocator::Match &other ) const
269  {
270  return mType == other.mType &&
271  mDist == other.mDist &&
272  mPoint == other.mPoint &&
273  mLayer == other.mLayer &&
274  mFid == other.mFid &&
275  mVertexIndex == other.mVertexIndex &&
276  mEdgePoints == other.mEdgePoints &&
277  mCentroid == other.mCentroid &&
278  mMiddleOfSegment == other.mMiddleOfSegment;
279  }
280 
281  protected:
282  Type mType = Invalid;
283  double mDist = 0;
285  QgsVectorLayer *mLayer = nullptr;
286  QgsFeatureId mFid = 0;
287  int mVertexIndex = 0; // e.g. vertex index
288  QgsPointXY mEdgePoints[2];
291  };
292 
293 #ifndef SIP_RUN
294  typedef class QList<QgsPointLocator::Match> MatchList;
295 #else
296  typedef QList<QgsPointLocator::Match> MatchList;
297 #endif
298 
304  struct MatchFilter
305  {
306  virtual ~MatchFilter() = default;
307  virtual bool acceptMatch( const QgsPointLocator::Match &match ) = 0;
308  };
309 
310  // intersection queries
311 
317  Match nearestVertex( const QgsPointXY &point, double tolerance, QgsPointLocator::MatchFilter *filter = nullptr, bool relaxed = false );
318 
325  Match nearestCentroid( const QgsPointXY &point, double tolerance, QgsPointLocator::MatchFilter *filter = nullptr, bool relaxed = false );
326 
333  Match nearestMiddleOfSegment( const QgsPointXY &point, double tolerance, QgsPointLocator::MatchFilter *filter = nullptr, bool relaxed = false );
334 
340  Match nearestEdge( const QgsPointXY &point, double tolerance, QgsPointLocator::MatchFilter *filter = nullptr, bool relaxed = false );
341 
350  Match nearestArea( const QgsPointXY &point, double tolerance, QgsPointLocator::MatchFilter *filter = nullptr, bool relaxed = false );
351 
357  MatchList edgesInRect( const QgsRectangle &rect, QgsPointLocator::MatchFilter *filter = nullptr, bool relaxed = false );
358 
363  MatchList edgesInRect( const QgsPointXY &point, double tolerance, QgsPointLocator::MatchFilter *filter = nullptr, bool relaxed = false );
364 
371  MatchList verticesInRect( const QgsRectangle &rect, QgsPointLocator::MatchFilter *filter = nullptr, bool relaxed = false );
372 
378  MatchList verticesInRect( const QgsPointXY &point, double tolerance, QgsPointLocator::MatchFilter *filter = nullptr, bool relaxed = false );
379 
380  // point-in-polygon query
381 
382  // TODO: function to return just the first match?
383 
388  MatchList pointInPolygon( const QgsPointXY &point, bool relaxed = false );
390 
395  int cachedGeometryCount() const { return mGeoms.count(); }
396 
403  bool isIndexing() const { return mIsIndexing; }
404 
409  void waitForIndexingFinished();
410 
411  signals:
412 
418  void initFinished( bool ok );
419 
420  protected:
421  bool rebuildIndex( int maxFeaturesToIndex = -1 );
422 
423  protected slots:
424  void destroyIndex();
425  private slots:
426  void onInitTaskFinished();
427  void onFeatureAdded( QgsFeatureId fid );
428  void onFeatureDeleted( QgsFeatureId fid );
429  void onGeometryChanged( QgsFeatureId fid, const QgsGeometry &geom );
430  void onAttributeValueChanged( QgsFeatureId fid, int idx, const QVariant &value );
431 
432  private:
433 
438  bool prepare( bool relaxed );
439 
441  std::unique_ptr< SpatialIndex::IStorageManager > mStorage;
442 
443  QHash<QgsFeatureId, QgsGeometry *> mGeoms;
444  std::unique_ptr< SpatialIndex::ISpatialIndex > mRTree;
445 
447  bool mIsEmptyLayer = false;
448 
449 
451  QgsCoordinateTransform mTransform;
452  QgsVectorLayer *mLayer = nullptr;
453  std::unique_ptr< QgsRectangle > mExtent;
454 
455  std::unique_ptr<QgsRenderContext> mContext;
456  std::unique_ptr<QgsFeatureRenderer> mRenderer;
457  std::unique_ptr<QgsVectorLayerFeatureSource> mSource;
458  int mMaxFeaturesToIndex = -1;
459  bool mIsIndexing = false;
460  bool mIsDestroying = false;
461  QgsFeatureIds mAddedFeatures;
462  QgsFeatureIds mDeletedFeatures;
463  QPointer<QgsPointLocatorInitTask> mInitTask;
464 
472  friend class QgsPointLocatorInitTask;
473  friend class TestQgsPointLocator;
476 };
477 
478 
479 #endif // QGSPOINTLOCATOR_H
The class defines interface for querying point location:
A rectangle specified with double values.
Definition: qgsrectangle.h:41
QSet< QgsFeatureId > QgsFeatureIds
Definition: qgsfeatureid.h:34
QgsVectorLayer * layer() const
The vector layer where the snap occurred.
QgsPointLocator::Type type() const
A class to represent a 2D point.
Definition: qgspointxy.h:43
qint64 QgsFeatureId
Definition: qgsfeatureid.h:25
class QList< QgsPointLocator::Match > MatchList
Helper class used when traversing the index looking for edges - builds a list of matches.
A geometry is the spatial representation of a feature.
Definition: qgsgeometry.h:122
bool hasEdge() const
Returns true if the Match is an edge.
Interface that allows rejection of some matches in intersection queries (e.g.
bool operator==(const QgsPointLocator::Match &other) const
Helper class used when traversing the index looking for centroid - builds a list of matches...
Helper class used when traversing the index looking for centroid - builds a list of matches...
QgsPoint interpolatedPoint() const
Convenient method to return a point on an edge with linear interpolation of the Z value...
#define SIP_SKIP
Definition: qgis_sip.h:126
bool hasVertex() const
Returns true if the Match is a vertex.
void edgePoints(QgsPointXY &pt1, QgsPointXY &pt2) const
Only for a valid edge match - obtain endpoints of the edge.
Helper class used when traversing the index looking for middle segment - builds a list of matches...
Match(QgsPointLocator::Type t, QgsVectorLayer *vl, QgsFeatureId fid, double dist, const QgsPointXY &pt, int vertexIndex=0, QgsPointXY *edgePoints=nullptr)
bool isEmpty() const
Returns true if the geometry is empty (eg a linestring with no vertices, or a collection with no geom...
Helper class used when traversing the index with areas - builds a list of matches.
QgsVectorLayer * mLayer
bool isIndexing() const
Returns true if the point locator is currently indexing the data.
Contains information about the context in which a coordinate transform is executed.
Helper class used when traversing the index looking for vertices - builds a list of matches...
Point geometry type, with support for z-dimension and m-values.
Definition: qgspoint.h:37
const QgsRectangle * extent() const
Gets extent of the area point locator covers - if nullptr then it caches the whole layer...
Helper class used when traversing the index looking for middle segment - builds a list of matches...
Partial snapshot of vector layer&#39;s state (only the members necessary for access to features) ...
int cachedGeometryCount() const
Returns how many geometries are cached in the index.
QgsPointXY point() const
for vertex / edge match coords depending on what class returns it (geom.cache: layer coords...
Contains information about the context of a rendering operation.
QgsPoint vertexAt(int atVertex) const
Returns coordinates of a vertex.
int vertexIndex() const
for vertex / edge match (first vertex of the edge)
#define SIP_OUT
Definition: qgis_sip.h:58
bool hasMiddleSegment() const
Returns true if the Match is the middle of a segment.
Line string geometry type, with support for z-dimension and m-values.
Definition: qgslinestring.h:43
This class represents a coordinate reference system (CRS).
Class for doing transforms between two map coordinate systems.
bool hasCentroid() const
Returns true if the Match is a centroid.
Helper class used when traversing the index looking for vertices - builds a list of matches...
double distance() const
for vertex / edge match units depending on what class returns it (geom.cache: layer units...
Helper class used when traversing the index looking for edges - builds a list of matches.
bool hasArea() const
Returns true if the Match is an area.
static QgsPoint closestPoint(const QgsAbstractGeometry &geometry, const QgsPoint &point)
Returns the nearest point on a segment of a geometry for the specified point.
QgsFeatureId featureId() const
The id of the feature to which the snapped geometry belongs.
Represents a vector layer which manages a vector based data sets.
QgsVectorLayer * layer() const
Gets associated layer.
Type
The type of a snap result or the filter type for a snap request.