QGIS API Documentation 3.99.0-Master (2fe06baccd8)
Loading...
Searching...
No Matches
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
19class QgsPointXY;
22class QgsRectangle;
24
25#include <memory>
26
27#include "qgis_core.h"
30#include "qgsfeatureid.h"
31#include "qgsgeometry.h"
32#include "qgsgeometryutils.h"
33#include "qgslinestring.h"
35#include "qgspointxy.h"
36#include "qgsvectorlayer.h"
37
38#include <QPointer>
39
46
54
62
69
76
83
85{
86 class IStorageManager;
87 class ISpatialIndex;
88}
89
103class CORE_EXPORT QgsPointLocator : public QObject
104{
105 Q_OBJECT
106 public:
107
120 const QgsRectangle *extent = nullptr );
121
122 ~QgsPointLocator() override;
123
127 QgsVectorLayer *layer() const { return mLayer; }
128
132 QgsCoordinateReferenceSystem destinationCrs() const;
133
137 const QgsRectangle *extent() const { return mExtent.get(); }
138
142 void setExtent( const QgsRectangle *extent );
143
148 void setRenderContext( const QgsRenderContext *context );
149
153 enum Type SIP_ENUM_BASETYPE( IntFlag )
154 {
156 Vertex = 1 << 0,
157 Edge = 1 << 1,
158 Area = 1 << 2,
159 Centroid = 1 << 3,
161 LineEndpoint = 1 << 5,
163 };
164
165 Q_DECLARE_FLAGS( Types, Type )
166
167
180 bool init( int maxFeaturesToIndex = -1, bool relaxed = false );
181
183 bool hasIndex() const;
184
185 struct Match
186 {
188 Match() = default;
189
190 Match( QgsPointLocator::Type t, QgsVectorLayer *vl, QgsFeatureId fid, double dist, const QgsPointXY &pt, int vertexIndex = 0, QgsPointXY *edgePoints = nullptr )
191 : mType( t )
192 , mDist( dist )
193 , mPoint( pt )
194 , mLayer( vl )
195 , mFid( fid )
197 {
198 if ( edgePoints )
199 {
200 mEdgePoints[0] = edgePoints[0];
201 mEdgePoints[1] = edgePoints[1];
202 }
203 }
204
205 QgsPointLocator::Type type() const { return mType; }
206
207 bool isValid() const { return mType != Invalid; }
209 bool hasVertex() const { return mType == Vertex; }
211 bool hasEdge() const { return mType == Edge; }
213 bool hasCentroid() const { return mType == Centroid; }
215 bool hasArea() const { return mType == Area; }
217 bool hasMiddleSegment() const { return mType == MiddleOfSegment; }
218
224 bool hasLineEndpoint() const { return mType == LineEndpoint; }
225
230 double distance() const { return mDist; }
231
236 QgsPointXY point() const { return mPoint; }
237
239 int vertexIndex() const { return mVertexIndex; }
240
245 QgsVectorLayer *layer() const { return mLayer; }
246
250 QgsFeatureId featureId() const { return mFid; }
251
254 {
255 pt1 = mEdgePoints[0];
256 pt2 = mEdgePoints[1];
257 }
258
266 {
268
269 if ( mLayer )
270 {
271 QgsPointXY snappedPoint( mPoint );
272 const QgsGeometry geom = mLayer->getGeometry( mFid );
273 QgsCoordinateTransform transform( destinationCrs, mLayer->crs(), mLayer->transformContext() );
274 if ( transform.isValid() )
275 {
276 try
277 {
278 snappedPoint = transform.transform( snappedPoint );
279 }
280 catch ( QgsCsException & )
281 {
282 QgsDebugError( QStringLiteral( "transformation to layer coordinate failed" ) );
283 }
284 }
285
286 if ( !( geom.isNull() || geom.isEmpty() ) )
287 {
288 // when snapping to a curve we need to use real geometry in order to have correct location
289 // of the snapped point, see https://github.com/qgis/QGIS/issues/53197.
290 // In other cases it is ok to use only a segment to speedup calculations.
291 if ( QgsWkbTypes::isCurvedType( geom.wkbType() ) )
292 {
293 point = QgsGeometryUtils::closestPoint( *geom.constGet(), QgsPoint( snappedPoint ) );
294 }
295 else
296 {
297 const QgsLineString line( geom.vertexAt( mVertexIndex ), geom.vertexAt( mVertexIndex + 1 ) );
298 point = QgsGeometryUtils::closestPoint( line, QgsPoint( snappedPoint ) );
299 }
300 }
301
302 if ( transform.isValid() )
303 {
304 try
305 {
306 point.transform( transform, Qgis::TransformDirection::Reverse );
307 }
308 catch ( QgsCsException & )
309 {
310 QgsDebugError( QStringLiteral( "transformation to destination coordinate failed" ) );
311 }
312 }
313 }
314
315 return point;
316 }
317
318 // TODO c++20 - replace with = default
319 bool operator==( const QgsPointLocator::Match &other ) const
320 {
321 return mType == other.mType &&
322 mDist == other.mDist &&
323 mPoint == other.mPoint &&
324 mLayer == other.mLayer &&
325 mFid == other.mFid &&
326 mVertexIndex == other.mVertexIndex &&
327 mEdgePoints[0] == other.mEdgePoints[0] &&
328 mEdgePoints[1] == other.mEdgePoints[1] &&
329 mCentroid == other.mCentroid &&
331 }
332
333 protected:
335 double mDist = 0;
339 int mVertexIndex = 0; // e.g. vertex index
343 };
344
345#ifndef SIP_RUN
346 typedef class QList<QgsPointLocator::Match> MatchList;
347#else
348 typedef QList<QgsPointLocator::Match> MatchList;
349#endif
350
357 {
358 virtual ~MatchFilter() = default;
359 virtual bool acceptMatch( const QgsPointLocator::Match &match ) = 0;
360 };
361
362 // intersection queries
363
369 Match nearestVertex( const QgsPointXY &point, double tolerance, QgsPointLocator::MatchFilter *filter = nullptr, bool relaxed = false );
370
377 Match nearestCentroid( const QgsPointXY &point, double tolerance, QgsPointLocator::MatchFilter *filter = nullptr, bool relaxed = false );
378
385 Match nearestMiddleOfSegment( const QgsPointXY &point, double tolerance, QgsPointLocator::MatchFilter *filter = nullptr, bool relaxed = false );
386
393 Match nearestLineEndpoints( const QgsPointXY &point, double tolerance, QgsPointLocator::MatchFilter *filter = nullptr, bool relaxed = false );
394
400 Match nearestEdge( const QgsPointXY &point, double tolerance, QgsPointLocator::MatchFilter *filter = nullptr, bool relaxed = false );
401
409 Match nearestArea( const QgsPointXY &point, double tolerance, QgsPointLocator::MatchFilter *filter = nullptr, bool relaxed = false );
410
416 MatchList edgesInRect( const QgsRectangle &rect, QgsPointLocator::MatchFilter *filter = nullptr, bool relaxed = false );
417
422 MatchList edgesInRect( const QgsPointXY &point, double tolerance, QgsPointLocator::MatchFilter *filter = nullptr, bool relaxed = false );
423
430 MatchList verticesInRect( const QgsRectangle &rect, QgsPointLocator::MatchFilter *filter = nullptr, bool relaxed = false );
431
437 MatchList verticesInRect( const QgsPointXY &point, double tolerance, QgsPointLocator::MatchFilter *filter = nullptr, bool relaxed = false );
438
439 // point-in-polygon query
440
441 // TODO: function to return just the first match?
442
450 MatchList pointInPolygon( const QgsPointXY &point, bool relaxed = false, QgsPointLocator::MatchFilter *filter = nullptr );
451
455 int cachedGeometryCount() const { return mGeoms.count(); }
456
463 bool isIndexing() const { return mIsIndexing; }
464
469 void waitForIndexingFinished();
470
471 signals:
472
478 void initFinished( bool ok );
479
480 protected:
481 bool rebuildIndex( int maxFeaturesToIndex = -1 );
482
483 protected slots:
484 void destroyIndex();
485 private slots:
486 void onInitTaskFinished();
487 void onFeatureAdded( QgsFeatureId fid );
488 void onFeatureDeleted( QgsFeatureId fid );
489 void onGeometryChanged( QgsFeatureId fid, const QgsGeometry &geom );
490 void onAttributeValueChanged( QgsFeatureId fid, int idx, const QVariant &value );
491
492 private:
493
498 bool prepare( bool relaxed );
499
501 std::unique_ptr< SpatialIndex::IStorageManager > mStorage;
502
503 QHash<QgsFeatureId, QgsGeometry *> mGeoms;
504 std::unique_ptr< SpatialIndex::ISpatialIndex > mRTree;
505
507 bool mIsEmptyLayer = false;
508
509
511 QgsCoordinateTransform mTransform;
512 QgsVectorLayer *mLayer = nullptr;
513 std::unique_ptr< QgsRectangle > mExtent;
514
515 std::unique_ptr<QgsRenderContext> mContext;
516 std::unique_ptr<QgsFeatureRenderer> mRenderer;
517 std::unique_ptr<QgsVectorLayerFeatureSource> mSource;
518 int mMaxFeaturesToIndex = -1;
519 bool mIsIndexing = false;
520 bool mIsDestroying = false;
521 QgsFeatureIds mAddedFeatures;
522 QgsFeatureIds mDeletedFeatures;
523 QPointer<QgsPointLocatorInitTask> mInitTask;
524
537};
538
539
540#endif // QGSPOINTLOCATOR_H
@ Reverse
Reverse/inverse transform (from destination to source).
Definition qgis.h:2673
Represents a coordinate reference system (CRS).
Contains information about the context in which a coordinate transform is executed.
Handles coordinate transforms between two coordinate systems.
QgsPointXY transform(const QgsPointXY &point, Qgis::TransformDirection direction=Qgis::TransformDirection::Forward) const
Transform the point from the source CRS to the destination CRS.
bool isValid() const
Returns true if the coordinate transform is valid, ie both the source and destination CRS have been s...
Custom exception class for Coordinate Reference System related exceptions.
Abstract base class for all 2D vector feature renderers.
static QgsPoint closestPoint(const QgsAbstractGeometry &geometry, const QgsPoint &point)
Returns the nearest point on a segment of a geometry for the specified point.
A geometry is the spatial representation of a feature.
QgsPoint vertexAt(int atVertex) const
Returns coordinates of a vertex.
const QgsAbstractGeometry * constGet() const
Returns a non-modifiable (const) reference to the underlying abstract geometry primitive.
bool isEmpty() const
Returns true if the geometry is empty (eg a linestring with no vertices, or a collection with no geom...
Qgis::WkbType wkbType() const
Returns type of the geometry as a WKB type (point / linestring / polygon etc.).
Line string geometry type, with support for z-dimension and m-values.
Helper class used when traversing the index with areas - builds a list of matches.
Helper class used when traversing the index looking for edges - builds a list of matches.
Helper class used when traversing the index looking for centroid - builds a list of matches.
Helper class used when traversing the index looking for edges - builds a list of matches.
Helper class used when traversing the index looking for middle segment - builds a list of matches.
Helper class used when traversing the index looking for vertices - builds a list of matches.
friend class QgsPointLocator_VisitorMiddlesInRect
friend class QgsPointLocator_VisitorNearestLineEndpoint
friend class QgsPointLocatorInitTask
friend class QgsPointLocator_VisitorEdgesInRect
Match nearestEdge(const QgsPointXY &point, double tolerance, QgsPointLocator::MatchFilter *filter=nullptr, bool relaxed=false)
Find nearest edge to the specified point - up to distance specified by tolerance Optional filter may ...
friend class TestQgsPointLocator
friend class QgsPointLocator_VisitorNearestVertex
QgsCoordinateReferenceSystem destinationCrs() const
Gets destination CRS - may be an invalid QgsCoordinateReferenceSystem if not doing OTF reprojection.
friend class QgsPointLocator_VisitorNearestCentroid
int cachedGeometryCount() const
Returns how many geometries are cached in the index.
MatchList pointInPolygon(const QgsPointXY &point, bool relaxed=false, QgsPointLocator::MatchFilter *filter=nullptr)
Find out if the point is in any polygons.
bool init(int maxFeaturesToIndex=-1, bool relaxed=false)
Prepare the index for queries.
QFlags< Type > Types
QgsVectorLayer * layer() const
Gets associated layer.
friend class QgsPointLocator_VisitorNearestMiddleOfSegment
MatchList verticesInRect(const QgsRectangle &rect, QgsPointLocator::MatchFilter *filter=nullptr, bool relaxed=false)
Find vertices within a specified rectangle This method is either blocking or non blocking according t...
class QList< QgsPointLocator::Match > MatchList
Match nearestVertex(const QgsPointXY &point, double tolerance, QgsPointLocator::MatchFilter *filter=nullptr, bool relaxed=false)
Find nearest vertex to the specified point - up to distance specified by tolerance Optional filter ma...
bool isIndexing() const
Returns true if the point locator is currently indexing the data.
friend class QgsPointLocator_VisitorNearestEdge
const QgsRectangle * extent() const
Gets extent of the area point locator covers - if nullptr then it caches the whole layer.
MatchList edgesInRect(const QgsRectangle &rect, QgsPointLocator::MatchFilter *filter=nullptr, bool relaxed=false)
Find edges within a specified rectangle Optional filter may discard unwanted matches.
bool hasIndex() const
Indicate whether the data have been already indexed.
friend class QgsPointLocator_VisitorVerticesInRect
Match nearestMiddleOfSegment(const QgsPointXY &point, double tolerance, QgsPointLocator::MatchFilter *filter=nullptr, bool relaxed=false)
Find nearest middle of segment to the specified point - up to distance specified by tolerance Optiona...
Match nearestCentroid(const QgsPointXY &point, double tolerance, QgsPointLocator::MatchFilter *filter=nullptr, bool relaxed=false)
Find nearest centroid to the specified point - up to distance specified by tolerance Optional filter ...
void initFinished(bool ok)
Emitted whenever index has been built and initialization is finished.
bool rebuildIndex(int maxFeaturesToIndex=-1)
friend class QgsPointLocator_VisitorCentroidsInRect
Match nearestArea(const QgsPointXY &point, double tolerance, QgsPointLocator::MatchFilter *filter=nullptr, bool relaxed=false)
Find nearest area to the specified point - up to distance specified by tolerance Optional filter may ...
Type
The type of a snap result or the filter type for a snap request.
@ Area
Snapped to an area.
@ MiddleOfSegment
Snapped to the middle of a segment.
@ Vertex
Snapped to a vertex. Can be a vertex of the geometry or an intersection.
@ All
Combination of all types. Note LineEndpoint is not included as endpoints made redundant by the presen...
@ Centroid
Snapped to a centroid.
@ Edge
Snapped to an edge.
@ LineEndpoint
Start or end points of lines only.
QgsPointLocator(QgsVectorLayer *layer, const QgsCoordinateReferenceSystem &destinationCrs=QgsCoordinateReferenceSystem(), const QgsCoordinateTransformContext &transformContext=QgsCoordinateTransformContext(), const QgsRectangle *extent=nullptr)
Construct point locator for a layer.
friend class QgsPointLocator_VisitorArea
Match nearestLineEndpoints(const QgsPointXY &point, double tolerance, QgsPointLocator::MatchFilter *filter=nullptr, bool relaxed=false)
Find nearest line endpoint (start or end vertex) to the specified point - up to distance specified by...
Represents a 2D point.
Definition qgspointxy.h:60
Point geometry type, with support for z-dimension and m-values.
Definition qgspoint.h:49
A rectangle specified with double values.
Contains information about the context of a rendering operation.
Partial snapshot of vector layer's state (only the members necessary for access to features).
Represents a vector layer which manages a vector based dataset.
static Q_INVOKABLE bool isCurvedType(Qgis::WkbType type)
Returns true if the WKB type is a curved type or can contain curved geometries.
#define SIP_ENUM_BASETYPE(type)
Definition qgis_sip.h:275
#define SIP_SKIP
Definition qgis_sip.h:134
#define SIP_OUT
Definition qgis_sip.h:58
QSet< QgsFeatureId > QgsFeatureIds
qint64 QgsFeatureId
64 bit feature ids negative numbers are used for uncommitted/newly added features
#define QgsDebugError(str)
Definition qgslogger.h:57
Interface that allows rejection of some matches in intersection queries (e.g.
virtual bool acceptMatch(const QgsPointLocator::Match &match)=0
virtual ~MatchFilter()=default
QgsFeatureId featureId() const
The id of the feature to which the snapped geometry belongs.
bool hasCentroid() const
Returns true if the Match is a centroid.
QgsVectorLayer * layer() const
The vector layer where the snap occurred.
QgsPoint interpolatedPoint(const QgsCoordinateReferenceSystem &destinationCrs=QgsCoordinateReferenceSystem()) const
Convenient method to return a point on an edge with linear interpolation of the Z value.
double distance() const
for vertex / edge match units depending on what class returns it (geom.cache: layer units,...
QgsPointXY point() const
for vertex / edge match coords depending on what class returns it (geom.cache: layer coords,...
bool hasEdge() const
Returns true if the Match is an edge.
void edgePoints(QgsPointXY &pt1, QgsPointXY &pt2) const
Only for a valid edge match - obtain endpoints of the edge.
bool hasArea() const
Returns true if the Match is an area.
QgsVectorLayer * mLayer
Match()=default
construct invalid match
Match(QgsPointLocator::Type t, QgsVectorLayer *vl, QgsFeatureId fid, double dist, const QgsPointXY &pt, int vertexIndex=0, QgsPointXY *edgePoints=nullptr)
QgsPointLocator::Type type() const
bool hasLineEndpoint() const
Returns true if the Match is a line endpoint (start or end vertex).
bool hasMiddleSegment() const
Returns true if the Match is the middle of a segment.
int vertexIndex() const
for vertex / edge match (first vertex of the edge)
bool hasVertex() const
Returns true if the Match is a vertex.
bool operator==(const QgsPointLocator::Match &other) const