QGIS API Documentation  3.12.1-BucureČ™ti (121cc00ff0)
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 QgsPointXY;
20 class QgsFeatureRenderer;
21 class QgsRenderContext;
22 class QgsRectangle;
24 
25 #include "qgis_core.h"
26 #include "qgspointxy.h"
28 #include "qgscoordinatetransform.h"
29 #include "qgsfeatureid.h"
30 #include "qgsgeometry.h"
31 #include "qgsgeometryutils.h"
32 #include "qgsvectorlayer.h"
33 #include "qgslinestring.h"
35 #include <memory>
36 
41 
43 {
44  class IStorageManager;
45  class ISpatialIndex;
46 }
47 
59 class CORE_EXPORT QgsPointLocator : public QObject
60 {
61  Q_OBJECT
62  public:
63 
76  const QgsRectangle *extent = nullptr );
77 
78  ~QgsPointLocator() override;
79 
84  QgsVectorLayer *layer() const { return mLayer; }
85 
90  QgsCoordinateReferenceSystem destinationCrs() const;
91 
96  const QgsRectangle *extent() const { return mExtent.get(); }
97 
102  void setExtent( const QgsRectangle *extent );
103 
108  void setRenderContext( const QgsRenderContext *context );
109 
113  enum Type
114  {
115  Invalid = 0,
116  Vertex = 1,
117  Edge = 2,
118  Area = 4,
119  All = Vertex | Edge | Area
120  };
121 
122  Q_DECLARE_FLAGS( Types, Type )
123 
124 
137  bool init( int maxFeaturesToIndex = -1, bool relaxed = false );
138 
140  bool hasIndex() const;
141 
142  struct Match
143  {
145  Match() = default;
146 
147  Match( QgsPointLocator::Type t, QgsVectorLayer *vl, QgsFeatureId fid, double dist, const QgsPointXY &pt, int vertexIndex = 0, QgsPointXY *edgePoints = nullptr )
148  : mType( t )
149  , mDist( dist )
150  , mPoint( pt )
151  , mLayer( vl )
152  , mFid( fid )
153  , mVertexIndex( vertexIndex )
154  {
155  if ( edgePoints )
156  {
157  mEdgePoints[0] = edgePoints[0];
158  mEdgePoints[1] = edgePoints[1];
159  }
160  }
161 
162  QgsPointLocator::Type type() const { return mType; }
163 
164  bool isValid() const { return mType != Invalid; }
165  bool hasVertex() const { return mType == Vertex; }
166  bool hasEdge() const { return mType == Edge; }
167  bool hasArea() const { return mType == Area; }
168 
173  double distance() const { return mDist; }
174 
179  QgsPointXY point() const { return mPoint; }
180 
182  int vertexIndex() const { return mVertexIndex; }
183 
188  QgsVectorLayer *layer() const { return mLayer; }
189 
193  QgsFeatureId featureId() const { return mFid; }
194 
196  void edgePoints( QgsPointXY &pt1 SIP_OUT, QgsPointXY &pt2 SIP_OUT ) const
197  {
198  pt1 = mEdgePoints[0];
199  pt2 = mEdgePoints[1];
200  }
201 
208  {
209  QgsPoint point;
210  const QgsGeometry geom = mLayer->getGeometry( mFid );
211  if ( !( geom.isNull() || geom.isEmpty() ) )
212  {
213  QgsLineString line( geom.vertexAt( mVertexIndex ), geom.vertexAt( mVertexIndex + 1 ) );
214 
215  point = QgsGeometryUtils::closestPoint( line, QgsPoint( mPoint ) );
216  }
217  return point;
218  }
219 
220  bool operator==( const QgsPointLocator::Match &other ) const
221  {
222  return mType == other.mType &&
223  mDist == other.mDist &&
224  mPoint == other.mPoint &&
225  mLayer == other.mLayer &&
226  mFid == other.mFid &&
227  mVertexIndex == other.mVertexIndex &&
228  mEdgePoints == other.mEdgePoints;
229  }
230 
231  protected:
232  Type mType = Invalid;
233  double mDist = 0;
235  QgsVectorLayer *mLayer = nullptr;
236  QgsFeatureId mFid = 0;
237  int mVertexIndex = 0; // e.g. vertex index
238  QgsPointXY mEdgePoints[2];
239  };
240 
241 #ifndef SIP_RUN
242  typedef class QList<QgsPointLocator::Match> MatchList;
243 #else
244  typedef QList<QgsPointLocator::Match> MatchList;
245 #endif
246 
252  struct MatchFilter
253  {
254  virtual ~MatchFilter() = default;
255  virtual bool acceptMatch( const QgsPointLocator::Match &match ) = 0;
256  };
257 
258  // intersection queries
259 
265  Match nearestVertex( const QgsPointXY &point, double tolerance, QgsPointLocator::MatchFilter *filter = nullptr, bool relaxed = false );
266 
272  Match nearestEdge( const QgsPointXY &point, double tolerance, QgsPointLocator::MatchFilter *filter = nullptr, bool relaxed = false );
273 
282  Match nearestArea( const QgsPointXY &point, double tolerance, QgsPointLocator::MatchFilter *filter = nullptr, bool relaxed = false );
283 
289  MatchList edgesInRect( const QgsRectangle &rect, QgsPointLocator::MatchFilter *filter = nullptr, bool relaxed = false );
290 
295  MatchList edgesInRect( const QgsPointXY &point, double tolerance, QgsPointLocator::MatchFilter *filter = nullptr, bool relaxed = false );
296 
303  MatchList verticesInRect( const QgsRectangle &rect, QgsPointLocator::MatchFilter *filter = nullptr, bool relaxed = false );
304 
310  MatchList verticesInRect( const QgsPointXY &point, double tolerance, QgsPointLocator::MatchFilter *filter = nullptr, bool relaxed = false );
311 
312  // point-in-polygon query
313 
314  // TODO: function to return just the first match?
315 
320  MatchList pointInPolygon( const QgsPointXY &point, bool relaxed = false );
322 
327  int cachedGeometryCount() const { return mGeoms.count(); }
328 
335  bool isIndexing() const { return mIsIndexing; }
336 
341  void waitForIndexingFinished();
342 
343  signals:
344 
350  void initFinished( bool ok );
351 
352  protected:
353  bool rebuildIndex( int maxFeaturesToIndex = -1 );
354 
355  protected slots:
356  void destroyIndex();
357  private slots:
358  void onInitTaskFinished();
359  void onFeatureAdded( QgsFeatureId fid );
360  void onFeatureDeleted( QgsFeatureId fid );
361  void onGeometryChanged( QgsFeatureId fid, const QgsGeometry &geom );
362  void onAttributeValueChanged( QgsFeatureId fid, int idx, const QVariant &value );
363 
364  private:
365 
370  bool prepare( bool relaxed );
371 
373  std::unique_ptr< SpatialIndex::IStorageManager > mStorage;
374 
375  QHash<QgsFeatureId, QgsGeometry *> mGeoms;
376  std::unique_ptr< SpatialIndex::ISpatialIndex > mRTree;
377 
379  bool mIsEmptyLayer = false;
380 
381 
383  QgsCoordinateTransform mTransform;
384  QgsVectorLayer *mLayer = nullptr;
385  std::unique_ptr< QgsRectangle > mExtent;
386 
387  std::unique_ptr<QgsRenderContext> mContext;
388  std::unique_ptr<QgsFeatureRenderer> mRenderer;
389  std::unique_ptr<QgsVectorLayerFeatureSource> mSource;
390  int mMaxFeaturesToIndex = -1;
391  bool mIsIndexing = false;
392  bool mIsDestroying = false;
393  QgsFeatureIds mAddedFeatures;
394  QgsFeatureIds mDeletedFeatures;
395  QPointer<QgsPointLocatorInitTask> mInitTask;
396 
402  friend class QgsPointLocatorInitTask;
403  friend class TestQgsPointLocator;
404 };
405 
406 
407 #endif // QGSPOINTLOCATOR_H
The class defines interface for querying point location:
A rectangle specified with double values.
Definition: qgsrectangle.h:41
QSet< QgsFeatureId > QgsFeatureIds
Definition: qgsfeatureid.h:34
QgsVectorLayer * layer() const
The vector layer where the snap occurred.
QgsPointLocator::Type type() const
A class to represent a 2D point.
Definition: qgspointxy.h:43
qint64 QgsFeatureId
Definition: qgsfeatureid.h:25
class QList< QgsPointLocator::Match > MatchList
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:122
Interface that allows rejection of some matches in intersection queries (e.g.
bool operator==(const QgsPointLocator::Match &other) const
QgsPoint interpolatedPoint() const
Convenient method to return a point on an edge with linear interpolation of the Z value...
#define SIP_SKIP
Definition: qgis_sip.h:126
void edgePoints(QgsPointXY &pt1, QgsPointXY &pt2) const
Only for a valid edge match - obtain endpoints of the edge.
Match(QgsPointLocator::Type t, QgsVectorLayer *vl, QgsFeatureId fid, double dist, const QgsPointXY &pt, int vertexIndex=0, QgsPointXY *edgePoints=nullptr)
bool isEmpty() const
Returns true if the geometry is empty (eg a linestring with no vertices, or a collection with no geom...
Helper class used when traversing the index with areas - builds a list of matches.
QgsVectorLayer * mLayer
bool isIndexing() const
Returns true if the point locator is currently indexing the data.
Contains information about the context in which a coordinate transform is executed.
Helper class used when traversing the index looking for vertices - builds a list of matches...
Point geometry type, with support for z-dimension and m-values.
Definition: qgspoint.h:37
const QgsRectangle * extent() const
Gets extent of the area point locator covers - if nullptr then it caches the whole layer...
Partial snapshot of vector layer&#39;s state (only the members necessary for access to features) ...
int cachedGeometryCount() const
Returns how many geometries are cached in the index.
QgsPointXY point() const
for vertex / edge match coords depending on what class returns it (geom.cache: layer coords...
Contains information about the context of a rendering operation.
QgsPoint vertexAt(int atVertex) const
Returns coordinates of a vertex.
int vertexIndex() const
for vertex / edge match (first vertex of the edge)
#define SIP_OUT
Definition: qgis_sip.h:58
Line string geometry type, with support for z-dimension and m-values.
Definition: qgslinestring.h:43
This class represents a coordinate reference system (CRS).
Class for doing transforms between two map coordinate systems.
Helper class used when traversing the index looking for vertices - builds a list of matches...
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.
static QgsPoint closestPoint(const QgsAbstractGeometry &geometry, const QgsPoint &point)
Returns the nearest point on a segment of a geometry for the specified point.
QgsFeatureId featureId() const
The id of the feature to which the snapped geometry belongs.
Represents a vector layer which manages a vector based data sets.
QgsVectorLayer * layer() const
Gets associated layer.
Type
The type of a snap result or the filter type for a snap request.