QGIS API Documentation  3.4.15-Madeira (e83d02e274)
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 
264  MatchList verticesInRect( const QgsRectangle &rect, QgsPointLocator::MatchFilter *filter = nullptr );
265 
270  MatchList verticesInRect( const QgsPointXY &point, double tolerance, QgsPointLocator::MatchFilter *filter = nullptr );
271 
272  // point-in-polygon query
273 
274  // TODO: function to return just the first match?
276  MatchList pointInPolygon( const QgsPointXY &point );
277 
282  int cachedGeometryCount() const { return mGeoms.count(); }
283 
284  protected:
285  bool rebuildIndex( int maxFeaturesToIndex = -1 );
286  protected slots:
287  void destroyIndex();
288  private slots:
289  void onFeatureAdded( QgsFeatureId fid );
290  void onFeatureDeleted( QgsFeatureId fid );
291  void onGeometryChanged( QgsFeatureId fid, const QgsGeometry &geom );
292  void onAttributeValueChanged( QgsFeatureId fid, int idx, const QVariant &value );
293 
294  private:
296  std::unique_ptr< SpatialIndex::IStorageManager > mStorage;
297 
298  QHash<QgsFeatureId, QgsGeometry *> mGeoms;
299  std::unique_ptr< SpatialIndex::ISpatialIndex > mRTree;
300 
302  bool mIsEmptyLayer = false;
303 
304 
306  QgsCoordinateTransform mTransform;
307  QgsVectorLayer *mLayer = nullptr;
308  std::unique_ptr< QgsRectangle > mExtent;
309 
310  std::unique_ptr<QgsRenderContext> mContext;
311 
317 };
318 
319 
320 #endif // QGSPOINTLOCATOR_H
The class defines interface for querying point location:
A rectangle specified with double values.
Definition: qgsrectangle.h:40
bool operator==(const QgsPointLocator::Match &other) const
QgsPointXY point() const
for vertex / edge match coords depending on what class returns it (geom.cache: layer coords...
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
QgsFeatureId featureId() const
The id of the feature to which the snapped geometry belongs.
Interface that allows rejection of some matches in intersection queries (e.g.
const QgsRectangle * extent() const
Gets extent of the area point locator covers - if null then it caches the whole layer.
#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.
int vertexIndex() const
for vertex / edge match (first vertex 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.
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 vertices - builds a list of matches...
QgsVectorLayer * layer() const
The vector layer where the snap occurred.
Contains information about the context of a rendering operation.
QgsPointLocator::Type type() const
#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...
Helper class used when traversing the index looking for edges - builds a list of matches.
int cachedGeometryCount() const
Returns how many geometries are cached in the index.
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.