QGIS API Documentation  3.4.3-Madeira (2f64a3c)
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 
125  bool init( int maxFeaturesToIndex = -1 );
126 
128  bool hasIndex() const;
129 
130  struct Match
131  {
133  Match() = default;
134 
135  Match( QgsPointLocator::Type t, QgsVectorLayer *vl, QgsFeatureId fid, double dist, const QgsPointXY &pt, int vertexIndex = 0, QgsPointXY *edgePoints = nullptr )
136  : mType( t )
137  , mDist( dist )
138  , mPoint( pt )
139  , mLayer( vl )
140  , mFid( fid )
141  , mVertexIndex( vertexIndex )
142  {
143  if ( edgePoints )
144  {
145  mEdgePoints[0] = edgePoints[0];
146  mEdgePoints[1] = edgePoints[1];
147  }
148  }
149 
150  QgsPointLocator::Type type() const { return mType; }
151 
152  bool isValid() const { return mType != Invalid; }
153  bool hasVertex() const { return mType == Vertex; }
154  bool hasEdge() const { return mType == Edge; }
155  bool hasArea() const { return mType == Area; }
156 
161  double distance() const { return mDist; }
162 
167  QgsPointXY point() const { return mPoint; }
168 
170  int vertexIndex() const { return mVertexIndex; }
171 
176  QgsVectorLayer *layer() const { return mLayer; }
177 
181  QgsFeatureId featureId() const { return mFid; }
182 
184  void edgePoints( QgsPointXY &pt1 SIP_OUT, QgsPointXY &pt2 SIP_OUT ) const
185  {
186  pt1 = mEdgePoints[0];
187  pt2 = mEdgePoints[1];
188  }
189 
190  bool operator==( const QgsPointLocator::Match &other ) const
191  {
192  return mType == other.mType &&
193  mDist == other.mDist &&
194  mPoint == other.mPoint &&
195  mLayer == other.mLayer &&
196  mFid == other.mFid &&
197  mVertexIndex == other.mVertexIndex &&
198  mEdgePoints == other.mEdgePoints;
199  }
200 
201  protected:
202  Type mType = Invalid;
203  double mDist = 0;
205  QgsVectorLayer *mLayer = nullptr;
206  QgsFeatureId mFid = 0;
207  int mVertexIndex = 0; // e.g. vertex index
208  QgsPointXY mEdgePoints[2];
209  };
210 
211 #ifndef SIP_RUN
212  typedef class QList<QgsPointLocator::Match> MatchList;
213 #else
214  typedef QList<QgsPointLocator::Match> MatchList;
215 #endif
216 
222  struct MatchFilter
223  {
224  virtual ~MatchFilter() = default;
225  virtual bool acceptMatch( const QgsPointLocator::Match &match ) = 0;
226  };
227 
228  // intersection queries
229 
234  Match nearestVertex( const QgsPointXY &point, double tolerance, QgsPointLocator::MatchFilter *filter = nullptr );
235 
240  Match nearestEdge( const QgsPointXY &point, double tolerance, QgsPointLocator::MatchFilter *filter = nullptr );
241 
249  Match nearestArea( const QgsPointXY &point, double tolerance, QgsPointLocator::MatchFilter *filter = nullptr );
250 
255  MatchList edgesInRect( const QgsRectangle &rect, QgsPointLocator::MatchFilter *filter = nullptr );
257  MatchList edgesInRect( const QgsPointXY &point, double tolerance, QgsPointLocator::MatchFilter *filter = nullptr );
258 
259  // point-in-polygon query
260 
261  // TODO: function to return just the first match?
263  MatchList pointInPolygon( const QgsPointXY &point );
264 
269  int cachedGeometryCount() const { return mGeoms.count(); }
270 
271  protected:
272  bool rebuildIndex( int maxFeaturesToIndex = -1 );
273  protected slots:
274  void destroyIndex();
275  private slots:
276  void onFeatureAdded( QgsFeatureId fid );
277  void onFeatureDeleted( QgsFeatureId fid );
278  void onGeometryChanged( QgsFeatureId fid, const QgsGeometry &geom );
279  void onAttributeValueChanged( QgsFeatureId fid, int idx, const QVariant &value );
280 
281  private:
283  std::unique_ptr< SpatialIndex::IStorageManager > mStorage;
284 
285  QHash<QgsFeatureId, QgsGeometry *> mGeoms;
286  std::unique_ptr< SpatialIndex::ISpatialIndex > mRTree;
287 
289  bool mIsEmptyLayer = false;
290 
291 
293  QgsCoordinateTransform mTransform;
294  QgsVectorLayer *mLayer = nullptr;
295  std::unique_ptr< QgsRectangle > mExtent;
296 
297  std::unique_ptr<QgsRenderContext> mContext;
298 
303 };
304 
305 
306 #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
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:106
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.
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.