QGIS API Documentation  2.18.21-Las Palmas (9fba24a)
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 QgsPoint;
20 class QgsVectorLayer;
21 
22 #include "qgsfeature.h"
23 #include "qgspoint.h"
24 #include "qgsrectangle.h"
25 
28 
33 
34 namespace SpatialIndex
35 {
36  class IStorageManager;
37  class ISpatialIndex;
38 }
39 
50 class CORE_EXPORT QgsPointLocator : public QObject
51 {
52  Q_OBJECT
53  public:
58  explicit QgsPointLocator( QgsVectorLayer* layer, const QgsCoordinateReferenceSystem* destCRS = nullptr, const QgsRectangle* extent = nullptr );
59 
60  ~QgsPointLocator();
61 
64  QgsVectorLayer* layer() const { return mLayer; }
67  const QgsCoordinateReferenceSystem* destCRS() const;
70  const QgsRectangle* extent() const { return mExtent; }
73  void setExtent( const QgsRectangle* extent );
74 
78  enum Type
79  {
80  Invalid = 0,
81  Vertex = 1,
82  Edge = 2,
83  Area = 4,
84  All = Vertex | Edge | Area
85  };
86 
87  Q_DECLARE_FLAGS( Types, Type )
88 
89 
93  bool init( int maxFeaturesToIndex = -1 );
94 
96  bool hasIndex() const;
97 
98  struct Match
99  {
102  : mType( Invalid )
103  , mDist( 0 )
104  , mPoint()
105  , mLayer( nullptr )
106  , mFid( 0 )
107  , mVertexIndex( 0 )
108  {}
109 
110  Match( Type t, QgsVectorLayer* vl, QgsFeatureId fid, double dist, const QgsPoint& pt, int vertexIndex = 0, QgsPoint* edgePoints = nullptr )
111  : mType( t )
112  , mDist( dist )
113  , mPoint( pt )
114  , mLayer( vl )
115  , mFid( fid )
116  , mVertexIndex( vertexIndex )
117  {
118  if ( edgePoints )
119  {
120  mEdgePoints[0] = edgePoints[0];
121  mEdgePoints[1] = edgePoints[1];
122  }
123  }
124 
125  Type type() const { return mType; }
126 
127  bool isValid() const { return mType != Invalid; }
128  bool hasVertex() const { return mType == Vertex; }
129  bool hasEdge() const { return mType == Edge; }
130  bool hasArea() const { return mType == Area; }
131 
134  double distance() const { return mDist; }
135 
138  QgsPoint point() const { return mPoint; }
139 
141  int vertexIndex() const { return mVertexIndex; }
142 
147  QgsVectorLayer* layer() const { return mLayer; }
148 
152  QgsFeatureId featureId() const { return mFid; }
153 
155  void edgePoints( QgsPoint& pt1, QgsPoint& pt2 ) const
156  {
157  pt1 = mEdgePoints[0];
158  pt2 = mEdgePoints[1];
159  }
160 
161  protected:
163  double mDist;
167  int mVertexIndex; // e.g. vertex index
168  QgsPoint mEdgePoints[2];
169  };
170 
171  typedef class QList<Match> MatchList;
172 
176  struct MatchFilter
177  {
178  virtual ~MatchFilter() {}
179  virtual bool acceptMatch( const Match& match ) = 0;
180  };
181 
182  // intersection queries
183 
186  Match nearestVertex( const QgsPoint& point, double tolerance, MatchFilter* filter = nullptr );
189  Match nearestEdge( const QgsPoint& point, double tolerance, MatchFilter* filter = nullptr );
192  MatchList edgesInRect( const QgsRectangle& rect, MatchFilter* filter = nullptr );
194  MatchList edgesInRect( const QgsPoint& point, double tolerance, MatchFilter* filter = nullptr );
195 
196  // point-in-polygon query
197 
198  // TODO: function to return just the first match?
200  MatchList pointInPolygon( const QgsPoint& point );
201 
202  //
203 
206  int cachedGeometryCount() const { return mGeoms.count(); }
207 
208  protected:
209  bool rebuildIndex( int maxFeaturesToIndex = -1 );
210  protected slots:
211  void destroyIndex();
212  private slots:
213  void onFeatureAdded( QgsFeatureId fid );
214  void onFeatureDeleted( QgsFeatureId fid );
215  void onGeometryChanged( QgsFeatureId fid, QgsGeometry& geom );
216 
217  private:
219  SpatialIndex::IStorageManager* mStorage;
220 
222  SpatialIndex::ISpatialIndex* mRTree;
223 
225  bool mIsEmptyLayer;
226 
228  QgsCoordinateTransform* mTransform;
229  QgsVectorLayer* mLayer;
230  QgsRectangle* mExtent;
231 
236 };
237 
238 
239 #endif // QGSPOINTLOCATOR_H
The class defines interface for querying point location:
A rectangle specified with double values.
Definition: qgsrectangle.h:35
Match(Type t, QgsVectorLayer *vl, QgsFeatureId fid, double dist, const QgsPoint &pt, int vertexIndex=0, QgsPoint *edgePoints=nullptr)
QgsVectorLayer * layer() const
The vector layer where the snap occurred.
Match()
construct invalid match
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:76
Interface that allows rejection of some matches in intersection queries (e.g.
Helper class used when traversing the index with areas - builds a list of matches.
QgsVectorLayer * mLayer
Helper class used when traversing the index looking for vertices - builds a list of matches...
class QList< Match > MatchList
A class to represent a point.
Definition: qgspoint.h:117
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.
int vertexIndex() const
for vertex / edge match (first vertex of the edge)
Class for storing a coordinate reference system (CRS)
Class for doing transforms between two map coordinate systems.
qint64 QgsFeatureId
Definition: qgsfeature.h:31
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.
void edgePoints(QgsPoint &pt1, QgsPoint &pt2) const
Only for a valid edge match - obtain endpoints of the edge.
QgsFeatureId featureId() const
The id of the feature to which the snapped geometry belongs.
QgsPoint point() const
for vertex / edge match coords depending on what class returns it (geom.cache: layer coords...
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.