QGIS API Documentation  3.0.2-Girona (307d082)
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 
22 #include "qgis_core.h"
23 #include "qgsfeature.h"
24 #include "qgspointxy.h"
26 #include "qgscoordinatetransform.h"
27 
32 
34 {
35  class IStorageManager;
36  class ISpatialIndex;
37 }
38 
50 class CORE_EXPORT QgsPointLocator : public QObject
51 {
52  Q_OBJECT
53  public:
54 
67  const QgsRectangle *extent = nullptr );
68 
69  ~QgsPointLocator() override;
70 
75  QgsVectorLayer *layer() const { return mLayer; }
76 
81  QgsCoordinateReferenceSystem destinationCrs() const;
82 
87  const QgsRectangle *extent() const { return mExtent; }
88 
93  void setExtent( const QgsRectangle *extent );
94 
98  enum Type
99  {
100  Invalid = 0,
101  Vertex = 1,
102  Edge = 2,
103  Area = 4,
104  All = Vertex | Edge | Area
105  };
106 
107  Q_DECLARE_FLAGS( Types, Type )
108 
109 
114  bool init( int maxFeaturesToIndex = -1 );
115 
117  bool hasIndex() const;
118 
119  struct Match
120  {
122  Match() = default;
123 
124  Match( QgsPointLocator::Type t, QgsVectorLayer *vl, QgsFeatureId fid, double dist, const QgsPointXY &pt, int vertexIndex = 0, QgsPointXY *edgePoints = nullptr )
125  : mType( t )
126  , mDist( dist )
127  , mPoint( pt )
128  , mLayer( vl )
129  , mFid( fid )
130  , mVertexIndex( vertexIndex )
131  {
132  if ( edgePoints )
133  {
134  mEdgePoints[0] = edgePoints[0];
135  mEdgePoints[1] = edgePoints[1];
136  }
137  }
138 
139  QgsPointLocator::Type type() const { return mType; }
140 
141  bool isValid() const { return mType != Invalid; }
142  bool hasVertex() const { return mType == Vertex; }
143  bool hasEdge() const { return mType == Edge; }
144  bool hasArea() const { return mType == Area; }
145 
150  double distance() const { return mDist; }
151 
156  QgsPointXY point() const { return mPoint; }
157 
159  int vertexIndex() const { return mVertexIndex; }
160 
165  QgsVectorLayer *layer() const { return mLayer; }
166 
170  QgsFeatureId featureId() const { return mFid; }
171 
173  void edgePoints( QgsPointXY &pt1 SIP_OUT, QgsPointXY &pt2 SIP_OUT ) const
174  {
175  pt1 = mEdgePoints[0];
176  pt2 = mEdgePoints[1];
177  }
178 
179  bool operator==( const QgsPointLocator::Match &other ) const
180  {
181  return mType == other.mType &&
182  mDist == other.mDist &&
183  mPoint == other.mPoint &&
184  mLayer == other.mLayer &&
185  mFid == other.mFid &&
186  mVertexIndex == other.mVertexIndex &&
187  mEdgePoints == other.mEdgePoints;
188  }
189 
190  protected:
191  Type mType = Invalid;
192  double mDist = 0;
194  QgsVectorLayer *mLayer = nullptr;
195  QgsFeatureId mFid = 0;
196  int mVertexIndex = 0; // e.g. vertex index
197  QgsPointXY mEdgePoints[2];
198  };
199 
200 #ifndef SIP_RUN
201  typedef class QList<QgsPointLocator::Match> MatchList;
202 #else
203  typedef QList<QgsPointLocator::Match> MatchList;
204 #endif
205 
211  struct MatchFilter
212  {
213  virtual ~MatchFilter() = default;
214  virtual bool acceptMatch( const QgsPointLocator::Match &match ) = 0;
215  };
216 
217  // intersection queries
218 
223  Match nearestVertex( const QgsPointXY &point, double tolerance, QgsPointLocator::MatchFilter *filter = nullptr );
224 
229  Match nearestEdge( const QgsPointXY &point, double tolerance, QgsPointLocator::MatchFilter *filter = nullptr );
230 
238  Match nearestArea( const QgsPointXY &point, double tolerance, QgsPointLocator::MatchFilter *filter = nullptr );
239 
244  MatchList edgesInRect( const QgsRectangle &rect, QgsPointLocator::MatchFilter *filter = nullptr );
246  MatchList edgesInRect( const QgsPointXY &point, double tolerance, QgsPointLocator::MatchFilter *filter = nullptr );
247 
248  // point-in-polygon query
249 
250  // TODO: function to return just the first match?
252  MatchList pointInPolygon( const QgsPointXY &point );
253 
254  //
255 
260  int cachedGeometryCount() const { return mGeoms.count(); }
261 
262  protected:
263  bool rebuildIndex( int maxFeaturesToIndex = -1 );
264  protected slots:
265  void destroyIndex();
266  private slots:
267  void onFeatureAdded( QgsFeatureId fid );
268  void onFeatureDeleted( QgsFeatureId fid );
269  void onGeometryChanged( QgsFeatureId fid, const QgsGeometry &geom );
270 
271  private:
273  SpatialIndex::IStorageManager *mStorage = nullptr;
274 
275  QHash<QgsFeatureId, QgsGeometry *> mGeoms;
276  SpatialIndex::ISpatialIndex *mRTree = nullptr;
277 
279  bool mIsEmptyLayer;
280 
282  QgsCoordinateTransform mTransform;
283  QgsVectorLayer *mLayer = nullptr;
284  QgsRectangle *mExtent = nullptr;
285 
290 };
291 
292 
293 #endif // QGSPOINTLOCATOR_H
The class defines interface for querying point location:
A rectangle specified with double values.
Definition: qgsrectangle.h:39
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: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
Get extent of the area point locator covers - if null then it caches the whole layer.
int cachedGeometryCount() const
Return 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...
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
Get associated layer.
Type
The type of a snap result or the filter type for a snap request.