QGIS API Documentation 3.99.0-Master (8e76e220402)
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#include <QString>
40
41using namespace Qt::StringLiterals;
42
49
57
65
72
79
86
88{
89 class IStorageManager;
90 class ISpatialIndex;
91}
92
106class CORE_EXPORT QgsPointLocator : public QObject
107{
108 Q_OBJECT
109 public:
110
123 const QgsRectangle *extent = nullptr );
124
125 ~QgsPointLocator() override;
126
130 QgsVectorLayer *layer() const { return mLayer; }
131
135 QgsCoordinateReferenceSystem destinationCrs() const;
136
140 const QgsRectangle *extent() const { return mExtent.get(); }
141
145 void setExtent( const QgsRectangle *extent );
146
151 void setRenderContext( const QgsRenderContext *context );
152
156 enum Type SIP_ENUM_BASETYPE( IntFlag )
157 {
159 Vertex = 1 << 0,
160 Edge = 1 << 1,
161 Area = 1 << 2,
162 Centroid = 1 << 3,
164 LineEndpoint = 1 << 5,
166 };
167
168 Q_DECLARE_FLAGS( Types, Type )
169
170
183 bool init( int maxFeaturesToIndex = -1, bool relaxed = false );
184
186 bool hasIndex() const;
187
188 struct Match
189 {
191 Match() = default;
192
193 Match( QgsPointLocator::Type t, QgsVectorLayer *vl, QgsFeatureId fid, double dist, const QgsPointXY &pt, int vertexIndex = 0, QgsPointXY *edgePoints = nullptr )
194 : mType( t )
195 , mDist( dist )
196 , mPoint( pt )
197 , mLayer( vl )
198 , mFid( fid )
200 {
201 if ( edgePoints )
202 {
203 mEdgePoints[0] = edgePoints[0];
204 mEdgePoints[1] = edgePoints[1];
205 }
206 }
207
208 QgsPointLocator::Type type() const { return mType; }
209
210 bool isValid() const { return mType != Invalid; }
212 bool hasVertex() const { return mType == Vertex; }
214 bool hasEdge() const { return mType == Edge; }
216 bool hasCentroid() const { return mType == Centroid; }
218 bool hasArea() const { return mType == Area; }
220 bool hasMiddleSegment() const { return mType == MiddleOfSegment; }
221
227 bool hasLineEndpoint() const { return mType == LineEndpoint; }
228
233 double distance() const { return mDist; }
234
239 QgsPointXY point() const { return mPoint; }
240
242 int vertexIndex() const { return mVertexIndex; }
243
248 QgsVectorLayer *layer() const { return mLayer; }
249
253 QgsFeatureId featureId() const { return mFid; }
254
257 {
258 pt1 = mEdgePoints[0];
259 pt2 = mEdgePoints[1];
260 }
261
269 {
271
272 if ( mLayer )
273 {
274 QgsPointXY snappedPoint( mPoint );
275 const QgsGeometry geom = mLayer->getGeometry( mFid );
276 QgsCoordinateTransform transform( destinationCrs, mLayer->crs(), mLayer->transformContext() );
277 if ( transform.isValid() )
278 {
279 try
280 {
281 snappedPoint = transform.transform( snappedPoint );
282 }
283 catch ( QgsCsException & )
284 {
285 QgsDebugError( u"transformation to layer coordinate failed"_s );
286 }
287 }
288
289 if ( !( geom.isNull() || geom.isEmpty() ) )
290 {
291 // when snapping to a curve we need to use real geometry in order to have correct location
292 // of the snapped point, see https://github.com/qgis/QGIS/issues/53197.
293 // In other cases it is ok to use only a segment to speedup calculations.
294 if ( QgsWkbTypes::isCurvedType( geom.wkbType() ) )
295 {
296 point = QgsGeometryUtils::closestPoint( *geom.constGet(), QgsPoint( snappedPoint ) );
297 }
298 else
299 {
300 const QgsLineString line( geom.vertexAt( mVertexIndex ), geom.vertexAt( mVertexIndex + 1 ) );
301 point = QgsGeometryUtils::closestPoint( line, QgsPoint( snappedPoint ) );
302 }
303 }
304
305 if ( transform.isValid() )
306 {
307 try
308 {
309 point.transform( transform, Qgis::TransformDirection::Reverse );
310 }
311 catch ( QgsCsException & )
312 {
313 QgsDebugError( u"transformation to destination coordinate failed"_s );
314 }
315 }
316 }
317
318 return point;
319 }
320
321 // TODO c++20 - replace with = default
322 bool operator==( const QgsPointLocator::Match &other ) const
323 {
324 return mType == other.mType &&
325 mDist == other.mDist &&
326 mPoint == other.mPoint &&
327 mLayer == other.mLayer &&
328 mFid == other.mFid &&
329 mVertexIndex == other.mVertexIndex &&
330 mEdgePoints[0] == other.mEdgePoints[0] &&
331 mEdgePoints[1] == other.mEdgePoints[1] &&
332 mCentroid == other.mCentroid &&
334 }
335
336 protected:
338 double mDist = 0;
342 int mVertexIndex = 0; // e.g. vertex index
346 };
347
348#ifndef SIP_RUN
349 typedef class QList<QgsPointLocator::Match> MatchList;
350#else
351 typedef QList<QgsPointLocator::Match> MatchList;
352#endif
353
360 {
361 virtual ~MatchFilter() = default;
362 virtual bool acceptMatch( const QgsPointLocator::Match &match ) = 0;
363 };
364
365 // intersection queries
366
372 Match nearestVertex( const QgsPointXY &point, double tolerance, QgsPointLocator::MatchFilter *filter = nullptr, bool relaxed = false );
373
380 Match nearestCentroid( const QgsPointXY &point, double tolerance, QgsPointLocator::MatchFilter *filter = nullptr, bool relaxed = false );
381
388 Match nearestMiddleOfSegment( const QgsPointXY &point, double tolerance, QgsPointLocator::MatchFilter *filter = nullptr, bool relaxed = false );
389
396 Match nearestLineEndpoints( const QgsPointXY &point, double tolerance, QgsPointLocator::MatchFilter *filter = nullptr, bool relaxed = false );
397
403 Match nearestEdge( const QgsPointXY &point, double tolerance, QgsPointLocator::MatchFilter *filter = nullptr, bool relaxed = false );
404
412 Match nearestArea( const QgsPointXY &point, double tolerance, QgsPointLocator::MatchFilter *filter = nullptr, bool relaxed = false );
413
419 MatchList edgesInRect( const QgsRectangle &rect, QgsPointLocator::MatchFilter *filter = nullptr, bool relaxed = false );
420
425 MatchList edgesInRect( const QgsPointXY &point, double tolerance, QgsPointLocator::MatchFilter *filter = nullptr, bool relaxed = false );
426
433 MatchList verticesInRect( const QgsRectangle &rect, QgsPointLocator::MatchFilter *filter = nullptr, bool relaxed = false );
434
440 MatchList verticesInRect( const QgsPointXY &point, double tolerance, QgsPointLocator::MatchFilter *filter = nullptr, bool relaxed = false );
441
442 // point-in-polygon query
443
444 // TODO: function to return just the first match?
445
453 MatchList pointInPolygon( const QgsPointXY &point, bool relaxed = false, QgsPointLocator::MatchFilter *filter = nullptr );
454
458 int cachedGeometryCount() const { return mGeoms.count(); }
459
466 bool isIndexing() const { return mIsIndexing; }
467
472 void waitForIndexingFinished();
473
474 signals:
475
481 void initFinished( bool ok );
482
483 protected:
484 bool rebuildIndex( int maxFeaturesToIndex = -1 );
485
486 protected slots:
487 void destroyIndex();
488 private slots:
489 void onInitTaskFinished();
490 void onFeatureAdded( QgsFeatureId fid );
491 void onFeatureDeleted( QgsFeatureId fid );
492 void onGeometryChanged( QgsFeatureId fid, const QgsGeometry &geom );
493 void onAttributeValueChanged( QgsFeatureId fid, int idx, const QVariant &value );
494
495 private:
496
501 bool prepare( bool relaxed );
502
504 std::unique_ptr< SpatialIndex::IStorageManager > mStorage;
505
506 QHash<QgsFeatureId, QgsGeometry *> mGeoms;
507 std::unique_ptr< SpatialIndex::ISpatialIndex > mRTree;
508
510 bool mIsEmptyLayer = false;
511
512
514 QgsCoordinateTransform mTransform;
515 QgsVectorLayer *mLayer = nullptr;
516 std::unique_ptr< QgsRectangle > mExtent;
517
518 std::unique_ptr<QgsRenderContext> mContext;
519 std::unique_ptr<QgsFeatureRenderer> mRenderer;
520 std::unique_ptr<QgsVectorLayerFeatureSource> mSource;
521 int mMaxFeaturesToIndex = -1;
522 bool mIsIndexing = false;
523 bool mIsDestroying = false;
524 QgsFeatureIds mAddedFeatures;
525 QgsFeatureIds mDeletedFeatures;
526 QPointer<QgsPointLocatorInitTask> mInitTask;
527
540};
541
542
543#endif // QGSPOINTLOCATOR_H
@ Reverse
Reverse/inverse transform (from destination to source).
Definition qgis.h:2731
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:62
Point geometry type, with support for z-dimension and m-values.
Definition qgspoint.h:53
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:59
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