QGIS API Documentation  2.8.2-Wien
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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 
26 
27 #include <spatialindex/SpatialIndex.h>
28 
29 
32 
37 
48 class CORE_EXPORT QgsPointLocator : public QObject
49 {
50  Q_OBJECT
51  public:
56  explicit QgsPointLocator( QgsVectorLayer* layer, const QgsCoordinateReferenceSystem* destCRS = 0, const QgsRectangle* extent = 0 );
57 
58  ~QgsPointLocator();
59 
60  enum Type { Invalid = 0, Vertex = 1, Edge = 2, Area = 4, All = Vertex | Edge | Area };
61 
66  bool init( int maxFeaturesToIndex = -1 );
67 
69  bool hasIndex() const;
70 
71  struct Match
72  {
74  Match() : mType( Invalid ), mDist( 0 ), mPoint(), mLayer( 0 ), mFid( 0 ), mVertexIndex( 0 ) {}
75 
76  Match( Type t, QgsVectorLayer* vl, QgsFeatureId fid, double dist, const QgsPoint& pt, int vertexIndex = 0, QgsPoint* edgePoints = 0 )
77  : mType( t ), mDist( dist ), mPoint( pt ), mLayer( vl ), mFid( fid ), mVertexIndex( vertexIndex )
78  {
79  if ( edgePoints )
80  {
81  mEdgePoints[0] = edgePoints[0];
82  mEdgePoints[1] = edgePoints[1];
83  }
84  }
85 
86  Type type() const { return mType; }
87 
88  bool isValid() const { return mType != Invalid; }
89  bool hasVertex() const { return mType == Vertex; }
90  bool hasEdge() const { return mType == Edge; }
91  bool hasArea() const { return mType == Area; }
92 
95  double distance() const { return mDist; }
96 
99  QgsPoint point() const { return mPoint; }
100 
102  int vertexIndex() const { return mVertexIndex; }
103 
105  QgsVectorLayer* layer() const { return mLayer; }
106 
107  QgsFeatureId featureId() const { return mFid; }
108 
110  void edgePoints( QgsPoint& pt1, QgsPoint& pt2 ) const
111  {
112  pt1 = mEdgePoints[0];
113  pt2 = mEdgePoints[1];
114  }
115 
116  protected:
118  double mDist;
122  int mVertexIndex; // e.g. vertex index
123  QgsPoint mEdgePoints[2];
124  };
125 
126  typedef class QList<Match> MatchList;
127 
131  struct MatchFilter
132  {
133  virtual bool acceptMatch( const Match& match ) = 0;
134  };
135 
136  // intersection queries
137 
140  Match nearestVertex( const QgsPoint& point, double tolerance, MatchFilter* filter = 0 );
143  Match nearestEdge( const QgsPoint& point, double tolerance, MatchFilter* filter = 0 );
146  MatchList edgesInRect( const QgsRectangle& rect, MatchFilter* filter = 0 );
148  MatchList edgesInRect( const QgsPoint& point, double tolerance, MatchFilter* filter = 0 );
149 
150  // point-in-polygon query
151 
152  // TODO: function to return just the first match?
154  MatchList pointInPolygon( const QgsPoint& point );
155 
156 
157  protected:
158  bool rebuildIndex( int maxFeaturesToIndex = -1 );
159  void destroyIndex();
160 
161  private slots:
162  void onFeatureAdded( QgsFeatureId fid );
163  void onFeatureDeleted( QgsFeatureId fid );
164  void onGeometryChanged( QgsFeatureId fid, QgsGeometry& geom );
165 
166  private:
168  SpatialIndex::IStorageManager* mStorage;
169 
170  QHash<QgsFeatureId, QgsGeometry*> mGeoms;
171  SpatialIndex::ISpatialIndex* mRTree;
172 
174  bool mIsEmptyLayer;
175 
177  QgsCoordinateTransform* mTransform;
178  QgsVectorLayer* mLayer;
179  QgsRectangle* mExtent;
180 
185 };
186 
187 
188 #endif // QGSPOINTLOCATOR_H