QGIS API Documentation 3.43.0-Master (b60ef06885e)
qgsgeometrysnapper.h
Go to the documentation of this file.
1/***************************************************************************
2 * qgsgeometrysnapper.h *
3 * ------------------- *
4 * copyright : (C) 2014 by Sandro Mani / Sourcepole AG *
5 * email : smani@sourcepole.ch *
6 ***************************************************************************/
7
8/***************************************************************************
9 * *
10 * This program is free software; you can redistribute it and/or modify *
11 * it under the terms of the GNU General Public License as published by *
12 * the Free Software Foundation; either version 2 of the License, or *
13 * (at your option) any later version. *
14 * *
15 ***************************************************************************/
16
17#ifndef QGS_GEOMETRY_SNAPPER_H
18#define QGS_GEOMETRY_SNAPPER_H
19
20#include "qgsspatialindex.h"
21#include "qgsabstractgeometry.h"
22#include "qgspoint.h"
23#include "qgsgeometry.h"
24#include "qgsgeos.h"
25#include "qgis_analysis.h"
26
27#include <QMutex>
28#include <QFuture>
29#include <QStringList>
30#include <geos_c.h>
31
32class QgsVectorLayer;
33
43class ANALYSIS_EXPORT QgsGeometrySnapper : public QObject
44{
45 Q_OBJECT
46
47 public:
59
65 QgsGeometrySnapper( QgsFeatureSource *referenceSource );
66
72 QgsGeometry snapGeometry( const QgsGeometry &geometry, double snapTolerance, SnapMode mode = PreferNodes ) const;
73
79 QgsFeatureList snapFeatures( const QgsFeatureList &features, double snapTolerance, SnapMode mode = PreferNodes );
80
84 static QgsGeometry snapGeometry( const QgsGeometry &geometry, double snapTolerance, const QList<QgsGeometry> &referenceGeometries, SnapMode mode = PreferNodes );
85
86 signals:
87
90
91 private:
92 struct ProcessFeatureWrapper
93 {
94 QgsGeometrySnapper *instance = nullptr;
95 double snapTolerance;
96 SnapMode mode;
97 explicit ProcessFeatureWrapper( QgsGeometrySnapper *_instance, double snapTolerance, SnapMode mode )
98 : instance( _instance )
99 , snapTolerance( snapTolerance )
100 , mode( mode )
101 {}
102 void operator()( QgsFeature &feature ) { instance->processFeature( feature, snapTolerance, mode ); }
103 };
104
105 enum PointFlag
106 {
107 SnappedToRefNode,
108 SnappedToRefSegment,
109 Unsnapped
110 };
111
112 QgsFeatureSource *mReferenceSource = nullptr;
113 QHash<QgsFeatureId, QgsGeometry> mCachedReferenceGeometries;
114
115 QgsSpatialIndex mIndex;
116 mutable QMutex mIndexMutex;
117 mutable QMutex mReferenceLayerMutex;
118
119 void processFeature( QgsFeature &feature, double snapTolerance, SnapMode mode );
120
121 static int polyLineSize( const QgsAbstractGeometry *geom, int iPart, int iRing );
122};
123
124
141class ANALYSIS_EXPORT QgsInternalGeometrySnapper
142{
143 public:
149
154 QgsGeometry snapFeature( const QgsFeature &feature );
155
159 QgsGeometryMap snappedGeometries() const { return mProcessedGeometries; }
160
161 private:
162 bool mFirstFeature = true;
163 double mSnapTolerance = 0;
165 QgsSpatialIndex mProcessedIndex;
166 QgsGeometryMap mProcessedGeometries;
167};
168
169#ifndef SIP_RUN
170
172class QgsSnapIndex
173{
174 public:
175 struct CoordIdx
176 {
177 CoordIdx( const QgsAbstractGeometry *_geom, QgsVertexId _vidx )
178 : geom( _geom )
179 , vidx( _vidx )
180 {}
181 QgsPoint point() const { return geom->vertexAt( vidx ); }
182
183 const QgsAbstractGeometry *geom = nullptr;
184 QgsVertexId vidx;
185 };
186
187 enum SnapType
188 {
189 SnapPoint,
190 SnapEndPoint,
191 SnapSegment
192 };
193
194 class SnapItem
195 {
196 public:
197 virtual ~SnapItem() = default;
198 SnapType type;
199 virtual QgsPoint getSnapPoint( const QgsPoint &p ) const = 0;
200
201 protected:
202 explicit SnapItem( SnapType _type )
203 : type( _type ) {}
204 };
205
206 class PointSnapItem : public QgsSnapIndex::SnapItem
207 {
208 public:
209 explicit PointSnapItem( const CoordIdx *_idx, bool isEndPoint );
210 QgsPoint getSnapPoint( const QgsPoint & /*p*/ ) const override;
211 const CoordIdx *idx = nullptr;
212 };
213
214 class SegmentSnapItem : public QgsSnapIndex::SnapItem
215 {
216 public:
217 SegmentSnapItem( const CoordIdx *_idxFrom, const CoordIdx *_idxTo );
218 QgsPoint getSnapPoint( const QgsPoint &p ) const override;
219 bool getIntersection( const QgsPoint &p1, const QgsPoint &p2, QgsPoint &inter ) const;
220 bool getProjection( const QgsPoint &p, QgsPoint &pProj ) const;
221 bool withinSquaredDistance( const QgsPoint &p, const double squaredDistance );
222 const CoordIdx *idxFrom = nullptr;
223 const CoordIdx *idxTo = nullptr;
224 };
225
226 QgsSnapIndex();
227 ~QgsSnapIndex();
228
229 QgsSnapIndex( const QgsSnapIndex &rh ) = delete;
230 QgsSnapIndex &operator=( const QgsSnapIndex &rh ) = delete;
231
232 void addGeometry( const QgsAbstractGeometry *geom );
233 QgsPoint getClosestSnapToPoint( const QgsPoint &startPoint, const QgsPoint &midPoint );
234 SnapItem *getSnapItem( const QgsPoint &pos, const double tolerance, PointSnapItem **pSnapPoint = nullptr, SegmentSnapItem **pSnapSegment = nullptr, bool endPointOnly = false ) const;
235
236 private:
237 QList<CoordIdx *> mCoordIdxs;
238 QList<SnapItem *> mSnapItems;
239
240 void addPoint( const CoordIdx *idx, bool isEndPoint );
241 void addSegment( const CoordIdx *idxFrom, const CoordIdx *idxTo );
242
243 GEOSSTRtree *mSTRTree = nullptr;
244 std::vector<geos::unique_ptr> mSTRTreeItems;
245};
246
248
249#endif
250
251#endif // QGS_GEOMETRY_SNAPPER_H
Abstract base class for all geometries.
An interface for objects which provide features via a getFeatures method.
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Definition qgsfeature.h:58
Allows a geometry to be snapped to the geometries within a different reference layer.
void featureSnapped()
Emitted each time a feature has been processed when calling snapFeatures()
SnapMode
Snapping modes.
@ EndPointPreferClosest
Only snap start/end points of lines (point features will also be snapped, polygon features will not b...
@ PreferClosestNoExtraVertices
Snap to closest point, regardless of it is a node or a segment. No new nodes will be inserted.
@ EndPointPreferNodes
Only snap start/end points of lines (point features will also be snapped, polygon features will not b...
@ PreferNodes
Prefer to snap to nodes, even when a segment may be closer than a node. New nodes will be inserted to...
@ PreferClosest
Snap to closest point, regardless of it is a node or a segment. New nodes will be inserted to make ge...
@ EndPointToEndPoint
Only snap the start/end points of lines to other start/end points of lines.
@ PreferNodesNoExtraVertices
Prefer to snap to nodes, even when a segment may be closer than a node. No new nodes will be inserted...
A geometry is the spatial representation of a feature.
Allows a set of geometries to be snapped to each other.
QgsGeometryMap snappedGeometries() const
Returns a QgsGeometryMap of all feature geometries snapped by this object.
Point geometry type, with support for z-dimension and m-values.
Definition qgspoint.h:49
QgsPoint vertexAt(QgsVertexId) const override
Returns the point corresponding to a specified vertex id.
Definition qgspoint.cpp:527
A spatial index for QgsFeature objects.
Represents a vector layer which manages a vector based dataset.
QMap< QgsFeatureId, QgsGeometry > QgsGeometryMap
QList< QgsFeature > QgsFeatureList
Utility class for identifying a unique vertex within a geometry.
Definition qgsvertexid.h:30