QGIS API Documentation  2.12.0-Lyon
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
61  {
62  Invalid = 0,
63  Vertex = 1,
64  Edge = 2,
65  Area = 4,
66  All = Vertex | Edge | Area
67  };
68 
69  Q_DECLARE_FLAGS( Types, Type )
70 
71 
75  bool init( int maxFeaturesToIndex = -1 );
76 
78  bool hasIndex() const;
79 
80  struct Match
81  {
83  Match() : mType( Invalid ), mDist( 0 ), mPoint(), mLayer( 0 ), mFid( 0 ), mVertexIndex( 0 ) {}
84 
85  Match( Type t, QgsVectorLayer* vl, QgsFeatureId fid, double dist, const QgsPoint& pt, int vertexIndex = 0, QgsPoint* edgePoints = 0 )
86  : mType( t ), mDist( dist ), mPoint( pt ), mLayer( vl ), mFid( fid ), mVertexIndex( vertexIndex )
87  {
88  if ( edgePoints )
89  {
90  mEdgePoints[0] = edgePoints[0];
91  mEdgePoints[1] = edgePoints[1];
92  }
93  }
94 
95  Type type() const { return mType; }
96 
97  bool isValid() const { return mType != Invalid; }
98  bool hasVertex() const { return mType == Vertex; }
99  bool hasEdge() const { return mType == Edge; }
100  bool hasArea() const { return mType == Area; }
101 
104  double distance() const { return mDist; }
105 
108  QgsPoint point() const { return mPoint; }
109 
111  int vertexIndex() const { return mVertexIndex; }
112 
114  QgsVectorLayer* layer() const { return mLayer; }
115 
116  QgsFeatureId featureId() const { return mFid; }
117 
119  void edgePoints( QgsPoint& pt1, QgsPoint& pt2 ) const
120  {
121  pt1 = mEdgePoints[0];
122  pt2 = mEdgePoints[1];
123  }
124 
125  protected:
127  double mDist;
131  int mVertexIndex; // e.g. vertex index
132  QgsPoint mEdgePoints[2];
133  };
134 
135  typedef class QList<Match> MatchList;
136 
140  struct MatchFilter
141  {
142  virtual bool acceptMatch( const Match& match ) = 0;
143  };
144 
145  // intersection queries
146 
149  Match nearestVertex( const QgsPoint& point, double tolerance, MatchFilter* filter = 0 );
152  Match nearestEdge( const QgsPoint& point, double tolerance, MatchFilter* filter = 0 );
155  MatchList edgesInRect( const QgsRectangle& rect, MatchFilter* filter = 0 );
157  MatchList edgesInRect( const QgsPoint& point, double tolerance, MatchFilter* filter = 0 );
158 
159  // point-in-polygon query
160 
161  // TODO: function to return just the first match?
163  MatchList pointInPolygon( const QgsPoint& point );
164 
165 
166  protected:
167  bool rebuildIndex( int maxFeaturesToIndex = -1 );
168  void destroyIndex();
169 
170  private slots:
171  void onFeatureAdded( QgsFeatureId fid );
172  void onFeatureDeleted( QgsFeatureId fid );
173  void onGeometryChanged( QgsFeatureId fid, QgsGeometry& geom );
174 
175  private:
177  SpatialIndex::IStorageManager* mStorage;
178 
180  SpatialIndex::ISpatialIndex* mRTree;
181 
183  bool mIsEmptyLayer;
184 
186  QgsCoordinateTransform* mTransform;
187  QgsVectorLayer* mLayer;
188  QgsRectangle* mExtent;
189 
194 };
195 
196 
197 #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=0)
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.
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:63
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.
Represents a vector layer which manages a vector based data sets.