QGIS API Documentation  2.14.0-Essen
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 
75  enum Type
76  {
77  Invalid = 0,
78  Vertex = 1,
79  Edge = 2,
80  Area = 4,
81  All = Vertex | Edge | Area
82  };
83 
84  Q_DECLARE_FLAGS( Types, Type )
85 
86 
90  bool init( int maxFeaturesToIndex = -1 );
91 
93  bool hasIndex() const;
94 
95  struct Match
96  {
98  Match() : mType( Invalid ), mDist( 0 ), mPoint(), mLayer( nullptr ), mFid( 0 ), mVertexIndex( 0 ) {}
99 
100  Match( Type t, QgsVectorLayer* vl, QgsFeatureId fid, double dist, const QgsPoint& pt, int vertexIndex = 0, QgsPoint* edgePoints = nullptr )
101  : mType( t ), mDist( dist ), mPoint( pt ), mLayer( vl ), mFid( fid ), mVertexIndex( vertexIndex )
102  {
103  if ( edgePoints )
104  {
105  mEdgePoints[0] = edgePoints[0];
106  mEdgePoints[1] = edgePoints[1];
107  }
108  }
109 
110  Type type() const { return mType; }
111 
112  bool isValid() const { return mType != Invalid; }
113  bool hasVertex() const { return mType == Vertex; }
114  bool hasEdge() const { return mType == Edge; }
115  bool hasArea() const { return mType == Area; }
116 
119  double distance() const { return mDist; }
120 
123  QgsPoint point() const { return mPoint; }
124 
126  int vertexIndex() const { return mVertexIndex; }
127 
129  QgsVectorLayer* layer() const { return mLayer; }
130 
131  QgsFeatureId featureId() const { return mFid; }
132 
134  void edgePoints( QgsPoint& pt1, QgsPoint& pt2 ) const
135  {
136  pt1 = mEdgePoints[0];
137  pt2 = mEdgePoints[1];
138  }
139 
140  protected:
142  double mDist;
146  int mVertexIndex; // e.g. vertex index
147  QgsPoint mEdgePoints[2];
148  };
149 
150  typedef class QList<Match> MatchList;
151 
155  struct MatchFilter
156  {
157  virtual ~MatchFilter() {}
158  virtual bool acceptMatch( const Match& match ) = 0;
159  };
160 
161  // intersection queries
162 
165  Match nearestVertex( const QgsPoint& point, double tolerance, MatchFilter* filter = nullptr );
168  Match nearestEdge( const QgsPoint& point, double tolerance, MatchFilter* filter = nullptr );
171  MatchList edgesInRect( const QgsRectangle& rect, MatchFilter* filter = nullptr );
173  MatchList edgesInRect( const QgsPoint& point, double tolerance, MatchFilter* filter = nullptr );
174 
175  // point-in-polygon query
176 
177  // TODO: function to return just the first match?
179  MatchList pointInPolygon( const QgsPoint& point );
180 
181  //
182 
185  int cachedGeometryCount() const { return mGeoms.count(); }
186 
187  protected:
188  bool rebuildIndex( int maxFeaturesToIndex = -1 );
189  void destroyIndex();
190 
191  private slots:
192  void onFeatureAdded( QgsFeatureId fid );
193  void onFeatureDeleted( QgsFeatureId fid );
194  void onGeometryChanged( QgsFeatureId fid, QgsGeometry& geom );
195 
196  private:
198  SpatialIndex::IStorageManager* mStorage;
199 
201  SpatialIndex::ISpatialIndex* mRTree;
202 
204  bool mIsEmptyLayer;
205 
207  QgsCoordinateTransform* mTransform;
208  QgsVectorLayer* mLayer;
209  QgsRectangle* mExtent;
210 
215 };
216 
217 
218 #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)
Match()
consruct 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
QgsFeatureId featureId() const
Interface that allows rejection of some matches in intersection queries (e.g.
const QgsRectangle * extent() const
Get extent of the area point locator covers - if null then it caches the whole layer.
int vertexIndex() const
for vertex / edge match (first vertex of the edge)
Helper class used when traversing the index with areas - builds a list of matches.
QgsVectorLayer * mLayer
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...
class QList< Match > MatchList
A class to represent a point.
Definition: qgspoint.h:65
QgsVectorLayer * layer() const
reference vector layer
Class for storing a coordinate reference system (CRS)
Class for doing transforms between two map coordinate systems.
void edgePoints(QgsPoint &pt1, QgsPoint &pt2) const
Only for a valid edge match - obtain endpoints of the edge.
qint64 QgsFeatureId
Definition: qgsfeature.h:31
QgsPoint point() const
for vertex / edge match coords depending on what class returns it (geom.cache: layer coords...
Helper class used when traversing the index looking for edges - builds a list of matches.
int cachedGeometryCount() const
Return how many geometries are cached in the index.
Represents a vector layer which manages a vector based data sets.
QgsVectorLayer * layer() const
Get associated layer.