QGIS API Documentation  3.2.0-Bonn (bc43194)
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 QgsVectorLayer;
21 class QgsFeatureRenderer;
22 class QgsRenderContext;
23 
24 #include "qgis_core.h"
25 #include "qgsfeature.h"
26 #include "qgspointxy.h"
28 #include "qgscoordinatetransform.h"
29 #include <memory>
30 
35 
37 {
38  class IStorageManager;
39  class ISpatialIndex;
40 }
41 
53 class CORE_EXPORT QgsPointLocator : public QObject
54 {
55  Q_OBJECT
56  public:
57 
70  const QgsRectangle *extent = nullptr );
71 
72  ~QgsPointLocator() override;
73 
78  QgsVectorLayer *layer() const { return mLayer; }
79 
84  QgsCoordinateReferenceSystem destinationCrs() const;
85 
90  const QgsRectangle *extent() const { return mExtent; }
91 
96  void setExtent( const QgsRectangle *extent );
97 
102  void setRenderContext( const QgsRenderContext *context );
103 
107  enum Type
108  {
109  Invalid = 0,
110  Vertex = 1,
111  Edge = 2,
112  Area = 4,
113  All = Vertex | Edge | Area
114  };
115 
116  Q_DECLARE_FLAGS( Types, Type )
117 
118 
123  bool init( int maxFeaturesToIndex = -1 );
124 
126  bool hasIndex() const;
127 
128  struct Match
129  {
131  Match() = default;
132 
133  Match( QgsPointLocator::Type t, QgsVectorLayer *vl, QgsFeatureId fid, double dist, const QgsPointXY &pt, int vertexIndex = 0, QgsPointXY *edgePoints = nullptr )
134  : mType( t )
135  , mDist( dist )
136  , mPoint( pt )
137  , mLayer( vl )
138  , mFid( fid )
139  , mVertexIndex( vertexIndex )
140  {
141  if ( edgePoints )
142  {
143  mEdgePoints[0] = edgePoints[0];
144  mEdgePoints[1] = edgePoints[1];
145  }
146  }
147 
148  QgsPointLocator::Type type() const { return mType; }
149 
150  bool isValid() const { return mType != Invalid; }
151  bool hasVertex() const { return mType == Vertex; }
152  bool hasEdge() const { return mType == Edge; }
153  bool hasArea() const { return mType == Area; }
154 
159  double distance() const { return mDist; }
160 
165  QgsPointXY point() const { return mPoint; }
166 
168  int vertexIndex() const { return mVertexIndex; }
169 
174  QgsVectorLayer *layer() const { return mLayer; }
175 
179  QgsFeatureId featureId() const { return mFid; }
180 
182  void edgePoints( QgsPointXY &pt1 SIP_OUT, QgsPointXY &pt2 SIP_OUT ) const
183  {
184  pt1 = mEdgePoints[0];
185  pt2 = mEdgePoints[1];
186  }
187 
188  bool operator==( const QgsPointLocator::Match &other ) const
189  {
190  return mType == other.mType &&
191  mDist == other.mDist &&
192  mPoint == other.mPoint &&
193  mLayer == other.mLayer &&
194  mFid == other.mFid &&
195  mVertexIndex == other.mVertexIndex &&
196  mEdgePoints == other.mEdgePoints;
197  }
198 
199  protected:
200  Type mType = Invalid;
201  double mDist = 0;
203  QgsVectorLayer *mLayer = nullptr;
204  QgsFeatureId mFid = 0;
205  int mVertexIndex = 0; // e.g. vertex index
206  QgsPointXY mEdgePoints[2];
207  };
208 
209 #ifndef SIP_RUN
210  typedef class QList<QgsPointLocator::Match> MatchList;
211 #else
212  typedef QList<QgsPointLocator::Match> MatchList;
213 #endif
214 
220  struct MatchFilter
221  {
222  virtual ~MatchFilter() = default;
223  virtual bool acceptMatch( const QgsPointLocator::Match &match ) = 0;
224  };
225 
226  // intersection queries
227 
232  Match nearestVertex( const QgsPointXY &point, double tolerance, QgsPointLocator::MatchFilter *filter = nullptr );
233 
238  Match nearestEdge( const QgsPointXY &point, double tolerance, QgsPointLocator::MatchFilter *filter = nullptr );
239 
247  Match nearestArea( const QgsPointXY &point, double tolerance, QgsPointLocator::MatchFilter *filter = nullptr );
248 
253  MatchList edgesInRect( const QgsRectangle &rect, QgsPointLocator::MatchFilter *filter = nullptr );
255  MatchList edgesInRect( const QgsPointXY &point, double tolerance, QgsPointLocator::MatchFilter *filter = nullptr );
256 
257  // point-in-polygon query
258 
259  // TODO: function to return just the first match?
261  MatchList pointInPolygon( const QgsPointXY &point );
262 
267  int cachedGeometryCount() const { return mGeoms.count(); }
268 
269  protected:
270  bool rebuildIndex( int maxFeaturesToIndex = -1 );
271  protected slots:
272  void destroyIndex();
273  private slots:
274  void onFeatureAdded( QgsFeatureId fid );
275  void onFeatureDeleted( QgsFeatureId fid );
276  void onGeometryChanged( QgsFeatureId fid, const QgsGeometry &geom );
277  void onAttributeValueChanged( QgsFeatureId fid, int idx, const QVariant &value );
278 
279  private:
281  SpatialIndex::IStorageManager *mStorage = nullptr;
282 
283  QHash<QgsFeatureId, QgsGeometry *> mGeoms;
284  SpatialIndex::ISpatialIndex *mRTree = nullptr;
285 
287  bool mIsEmptyLayer;
288 
289 
291  QgsCoordinateTransform mTransform;
292  QgsVectorLayer *mLayer = nullptr;
293  QgsRectangle *mExtent = nullptr;
294 
295  std::unique_ptr<QgsRenderContext> mContext;
296 
301 };
302 
303 
304 #endif // QGSPOINTLOCATOR_H
The class defines interface for querying point location:
A rectangle specified with double values.
Definition: qgsrectangle.h:40
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
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:104
Interface that allows rejection of some matches in intersection queries (e.g.
bool operator==(const QgsPointLocator::Match &other) const
#define SIP_SKIP
Definition: qgis_sip.h:119
void edgePoints(QgsPointXY &pt1, QgsPointXY &pt2) const
Only for a valid edge match - obtain endpoints of the edge.
Match(QgsPointLocator::Type t, QgsVectorLayer *vl, QgsFeatureId fid, double dist, const QgsPointXY &pt, int vertexIndex=0, QgsPointXY *edgePoints=nullptr)
Helper class used when traversing the index with areas - builds a list of matches.
QgsVectorLayer * mLayer
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...
const QgsRectangle * extent() const
Gets extent of the area point locator covers - if null then it caches the whole layer.
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.
int vertexIndex() const
for vertex / edge match (first vertex of the edge)
#define SIP_OUT
Definition: qgis_sip.h:51
This class represents a coordinate reference system (CRS).
Class for doing transforms between two map coordinate systems.
qint64 QgsFeatureId
Definition: qgsfeature.h:37
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.
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.