QGIS API Documentation 3.99.0-Master (2fe06baccd8)
Loading...
Searching...
No Matches
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 : [email protected] *
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 <geos_c.h>
21
22#include "qgis_analysis.h"
23#include "qgsabstractgeometry.h"
24#include "qgsgeometry.h"
25#include "qgsgeos.h"
26#include "qgspoint.h"
27#include "qgsspatialindex.h"
28
29#include <QFuture>
30#include <QMutex>
31#include <QStringList>
32
33class QgsVectorLayer;
34
44class ANALYSIS_EXPORT QgsGeometrySnapper : public QObject
45{
46 Q_OBJECT
47
48 public:
60
66 QgsGeometrySnapper( QgsFeatureSource *referenceSource );
67
73 QgsGeometry snapGeometry( const QgsGeometry &geometry, double snapTolerance, SnapMode mode = PreferNodes ) const;
74
80 QgsFeatureList snapFeatures( const QgsFeatureList &features, double snapTolerance, SnapMode mode = PreferNodes );
81
85 static QgsGeometry snapGeometry( const QgsGeometry &geometry, double snapTolerance, const QList<QgsGeometry> &referenceGeometries, SnapMode mode = PreferNodes );
86
87 signals:
88
91
92 private:
93 struct ProcessFeatureWrapper
94 {
95 QgsGeometrySnapper *instance = nullptr;
96 double snapTolerance;
97 SnapMode mode;
98 explicit ProcessFeatureWrapper( QgsGeometrySnapper *_instance, double snapTolerance, SnapMode mode )
99 : instance( _instance )
100 , snapTolerance( snapTolerance )
101 , mode( mode )
102 {}
103 void operator()( QgsFeature &feature ) { instance->processFeature( feature, snapTolerance, mode ); }
104 };
105
106 enum PointFlag
107 {
108 SnappedToRefNode,
109 SnappedToRefSegment,
110 Unsnapped
111 };
112
113 QgsFeatureSource *mReferenceSource = nullptr;
114 QHash<QgsFeatureId, QgsGeometry> mCachedReferenceGeometries;
115
116 QgsSpatialIndex mIndex;
117 mutable QMutex mIndexMutex;
118 mutable QMutex mReferenceLayerMutex;
119
120 void processFeature( QgsFeature &feature, double snapTolerance, SnapMode mode );
121
122 static int polyLineSize( const QgsAbstractGeometry *geom, int iPart, int iRing );
123};
124
125
142class ANALYSIS_EXPORT QgsInternalGeometrySnapper
143{
144 public:
150
155 QgsGeometry snapFeature( const QgsFeature &feature );
156
160 QgsGeometryMap snappedGeometries() const { return mProcessedGeometries; }
161
162 private:
163 bool mFirstFeature = true;
164 double mSnapTolerance = 0;
166 QgsSpatialIndex mProcessedIndex;
167 QgsGeometryMap mProcessedGeometries;
168};
169
170#ifndef SIP_RUN
171
173class QgsSnapIndex
174{
175 public:
176 struct CoordIdx
177 {
178 CoordIdx( const QgsAbstractGeometry *_geom, QgsVertexId _vidx )
179 : geom( _geom )
180 , vidx( _vidx )
181 {}
182 QgsPoint point() const { return geom->vertexAt( vidx ); }
183
184 const QgsAbstractGeometry *geom = nullptr;
185 QgsVertexId vidx;
186 };
187
188 enum SnapType
189 {
190 SnapPoint,
191 SnapEndPoint,
192 SnapSegment
193 };
194
195 class SnapItem
196 {
197 public:
198 virtual ~SnapItem() = default;
199 SnapType type;
200 virtual QgsPoint getSnapPoint( const QgsPoint &p ) const = 0;
201
202 protected:
203 explicit SnapItem( SnapType _type )
204 : type( _type ) {}
205 };
206
207 class PointSnapItem : public QgsSnapIndex::SnapItem
208 {
209 public:
210 explicit PointSnapItem( const CoordIdx *_idx, bool isEndPoint );
211 QgsPoint getSnapPoint( const QgsPoint & /*p*/ ) const override;
212 const CoordIdx *idx = nullptr;
213 };
214
215 class SegmentSnapItem : public QgsSnapIndex::SnapItem
216 {
217 public:
218 SegmentSnapItem( const CoordIdx *_idxFrom, const CoordIdx *_idxTo );
219 QgsPoint getSnapPoint( const QgsPoint &p ) const override;
220 bool getIntersection( const QgsPoint &p1, const QgsPoint &p2, QgsPoint &inter ) const;
221 bool getProjection( const QgsPoint &p, QgsPoint &pProj ) const;
222 bool withinSquaredDistance( const QgsPoint &p, const double squaredDistance );
223 const CoordIdx *idxFrom = nullptr;
224 const CoordIdx *idxTo = nullptr;
225 };
226
227 QgsSnapIndex();
228 ~QgsSnapIndex();
229
230 QgsSnapIndex( const QgsSnapIndex &rh ) = delete;
231 QgsSnapIndex &operator=( const QgsSnapIndex &rh ) = delete;
232
233 void addGeometry( const QgsAbstractGeometry *geom );
234 QgsPoint getClosestSnapToPoint( const QgsPoint &startPoint, const QgsPoint &midPoint );
235 SnapItem *getSnapItem( const QgsPoint &pos, const double tolerance, PointSnapItem **pSnapPoint = nullptr, SegmentSnapItem **pSnapSegment = nullptr, bool endPointOnly = false ) const;
236
237 private:
238 QList<CoordIdx *> mCoordIdxs;
239 QList<SnapItem *> mSnapItems;
240
241 void addPoint( const CoordIdx *idx, bool isEndPoint );
242 void addSegment( const CoordIdx *idxFrom, const CoordIdx *idxTo );
243
244 GEOSSTRtree *mSTRTree = nullptr;
245 std::vector<geos::unique_ptr> mSTRTreeItems;
246};
247
249
250#endif
251
252#endif // QGS_GEOMETRY_SNAPPER_H
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...
QgsGeometrySnapper(QgsFeatureSource *referenceSource)
Constructor for QgsGeometrySnapper.
A geometry is the spatial representation of a feature.
QgsInternalGeometrySnapper(double snapTolerance, QgsGeometrySnapper::SnapMode mode=QgsGeometrySnapper::PreferNodes)
Constructor for QgsInternalGeometrySnapper.
QgsGeometry snapFeature(const QgsFeature &feature)
Snaps a single feature's geometry against all feature geometries already processed by calls to snapFe...
QgsGeometryMap snappedGeometries() const
Returns a QgsGeometryMap of all feature geometries snapped by this object.
A spatial index for QgsFeature objects.
Represents a vector layer which manages a vector based dataset.
QMap< QgsFeatureId, QgsGeometry > QgsGeometryMap
QList< QgsFeature > QgsFeatureList