QGIS API Documentation  2.10.1-Pisa
 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 
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
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:75
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.