QGIS API Documentation  3.8.0-Zanzibar (11aff65)
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 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 <memory>
32 
37 
39 {
40  class IStorageManager;
41  class ISpatialIndex;
42 }
43 
55 class CORE_EXPORT QgsPointLocator : public QObject
56 {
57  Q_OBJECT
58  public:
59 
72  const QgsRectangle *extent = nullptr );
73 
74  ~QgsPointLocator() override;
75 
80  QgsVectorLayer *layer() const { return mLayer; }
81 
86  QgsCoordinateReferenceSystem destinationCrs() const;
87 
92  const QgsRectangle *extent() const { return mExtent.get(); }
93 
98  void setExtent( const QgsRectangle *extent );
99 
104  void setRenderContext( const QgsRenderContext *context );
105 
109  enum Type
110  {
111  Invalid = 0,
112  Vertex = 1,
113  Edge = 2,
114  Area = 4,
115  All = Vertex | Edge | Area
116  };
117 
118  Q_DECLARE_FLAGS( Types, Type )
119 
120 
126  bool init( int maxFeaturesToIndex = -1 );
127 
129  bool hasIndex() const;
130 
131  struct Match
132  {
134  Match() = default;
135 
136  Match( QgsPointLocator::Type t, QgsVectorLayer *vl, QgsFeatureId fid, double dist, const QgsPointXY &pt, int vertexIndex = 0, QgsPointXY *edgePoints = nullptr )
137  : mType( t )
138  , mDist( dist )
139  , mPoint( pt )
140  , mLayer( vl )
141  , mFid( fid )
142  , mVertexIndex( vertexIndex )
143  {
144  if ( edgePoints )
145  {
146  mEdgePoints[0] = edgePoints[0];
147  mEdgePoints[1] = edgePoints[1];
148  }
149  }
150 
151  QgsPointLocator::Type type() const { return mType; }
152 
153  bool isValid() const { return mType != Invalid; }
154  bool hasVertex() const { return mType == Vertex; }
155  bool hasEdge() const { return mType == Edge; }
156  bool hasArea() const { return mType == Area; }
157 
162  double distance() const { return mDist; }
163 
168  QgsPointXY point() const { return mPoint; }
169 
171  int vertexIndex() const { return mVertexIndex; }
172 
177  QgsVectorLayer *layer() const { return mLayer; }
178 
182  QgsFeatureId featureId() const { return mFid; }
183 
185  void edgePoints( QgsPointXY &pt1 SIP_OUT, QgsPointXY &pt2 SIP_OUT ) const
186  {
187  pt1 = mEdgePoints[0];
188  pt2 = mEdgePoints[1];
189  }
190 
191  bool operator==( const QgsPointLocator::Match &other ) const
192  {
193  return mType == other.mType &&
194  mDist == other.mDist &&
195  mPoint == other.mPoint &&
196  mLayer == other.mLayer &&
197  mFid == other.mFid &&
198  mVertexIndex == other.mVertexIndex &&
199  mEdgePoints == other.mEdgePoints;
200  }
201 
202  protected:
203  Type mType = Invalid;
204  double mDist = 0;
206  QgsVectorLayer *mLayer = nullptr;
207  QgsFeatureId mFid = 0;
208  int mVertexIndex = 0; // e.g. vertex index
209  QgsPointXY mEdgePoints[2];
210  };
211 
212 #ifndef SIP_RUN
213  typedef class QList<QgsPointLocator::Match> MatchList;
214 #else
215  typedef QList<QgsPointLocator::Match> MatchList;
216 #endif
217 
223  struct MatchFilter
224  {
225  virtual ~MatchFilter() = default;
226  virtual bool acceptMatch( const QgsPointLocator::Match &match ) = 0;
227  };
228 
229  // intersection queries
230 
235  Match nearestVertex( const QgsPointXY &point, double tolerance, QgsPointLocator::MatchFilter *filter = nullptr );
236 
241  Match nearestEdge( const QgsPointXY &point, double tolerance, QgsPointLocator::MatchFilter *filter = nullptr );
242 
250  Match nearestArea( const QgsPointXY &point, double tolerance, QgsPointLocator::MatchFilter *filter = nullptr );
251 
256  MatchList edgesInRect( const QgsRectangle &rect, QgsPointLocator::MatchFilter *filter = nullptr );
258  MatchList edgesInRect( const QgsPointXY &point, double tolerance, QgsPointLocator::MatchFilter *filter = nullptr );
259 
265  MatchList verticesInRect( const QgsRectangle &rect, QgsPointLocator::MatchFilter *filter = nullptr );
266 
271  MatchList verticesInRect( const QgsPointXY &point, double tolerance, QgsPointLocator::MatchFilter *filter = nullptr );
272 
273  // point-in-polygon query
274 
275  // TODO: function to return just the first match?
277  MatchList pointInPolygon( const QgsPointXY &point );
278 
283  int cachedGeometryCount() const { return mGeoms.count(); }
284 
285  protected:
286  bool rebuildIndex( int maxFeaturesToIndex = -1 );
287  protected slots:
288  void destroyIndex();
289  private slots:
290  void onFeatureAdded( QgsFeatureId fid );
291  void onFeatureDeleted( QgsFeatureId fid );
292  void onGeometryChanged( QgsFeatureId fid, const QgsGeometry &geom );
293  void onAttributeValueChanged( QgsFeatureId fid, int idx, const QVariant &value );
294 
295  private:
297  std::unique_ptr< SpatialIndex::IStorageManager > mStorage;
298 
299  QHash<QgsFeatureId, QgsGeometry *> mGeoms;
300  std::unique_ptr< SpatialIndex::ISpatialIndex > mRTree;
301 
303  bool mIsEmptyLayer = false;
304 
305 
307  QgsCoordinateTransform mTransform;
308  QgsVectorLayer *mLayer = nullptr;
309  std::unique_ptr< QgsRectangle > mExtent;
310 
311  std::unique_ptr<QgsRenderContext> mContext;
312 
318 };
319 
320 
321 #endif // QGSPOINTLOCATOR_H
The class defines interface for querying point location:
A rectangle specified with double values.
Definition: qgsrectangle.h:41
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:111
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 nullptr 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.
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.
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.