QGIS API Documentation 3.99.0-Master (21b3aa880ba)
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:
145
151
156 QgsGeometry snapFeature( const QgsFeature &feature );
157
161 QgsGeometryMap snappedGeometries() const { return mProcessedGeometries; }
162
163 private:
164 bool mFirstFeature = true;
165 double mSnapTolerance = 0;
167 QgsSpatialIndex mProcessedIndex;
168 QgsGeometryMap mProcessedGeometries;
169};
170
171#ifndef SIP_RUN
172
174class QgsSnapIndex
175{
176 public:
177 struct CoordIdx
178 {
179 CoordIdx( const QgsAbstractGeometry *_geom, QgsVertexId _vidx )
180 : geom( _geom )
181 , vidx( _vidx )
182 {}
183 QgsPoint point() const { return geom->vertexAt( vidx ); }
184
185 const QgsAbstractGeometry *geom = nullptr;
186 QgsVertexId vidx;
187 };
188
189 enum SnapType
190 {
191 SnapPoint,
192 SnapEndPoint,
193 SnapSegment
194 };
195
196 class SnapItem
197 {
198 public:
199 virtual ~SnapItem() = default;
200 SnapType type;
201 virtual QgsPoint getSnapPoint( const QgsPoint &p ) const = 0;
202
203 protected:
204 explicit SnapItem( SnapType _type )
205 : type( _type ) {}
206 };
207
208 class PointSnapItem : public QgsSnapIndex::SnapItem
209 {
210 public:
211 explicit PointSnapItem( const CoordIdx *_idx, bool isEndPoint );
212 QgsPoint getSnapPoint( const QgsPoint & /*p*/ ) const override;
213 const CoordIdx *idx = nullptr;
214 };
215
216 class SegmentSnapItem : public QgsSnapIndex::SnapItem
217 {
218 public:
219 SegmentSnapItem( const CoordIdx *_idxFrom, const CoordIdx *_idxTo );
220 QgsPoint getSnapPoint( const QgsPoint &p ) const override;
221 bool getIntersection( const QgsPoint &p1, const QgsPoint &p2, QgsPoint &inter ) const;
222 bool getProjection( const QgsPoint &p, QgsPoint &pProj ) const;
223 bool withinSquaredDistance( const QgsPoint &p, const double squaredDistance );
224 const CoordIdx *idxFrom = nullptr;
225 const CoordIdx *idxTo = nullptr;
226 };
227
228 QgsSnapIndex();
229 ~QgsSnapIndex();
230
231 QgsSnapIndex( const QgsSnapIndex &rh ) = delete;
232 QgsSnapIndex &operator=( const QgsSnapIndex &rh ) = delete;
233
234 void addGeometry( const QgsAbstractGeometry *geom );
235 QgsPoint getClosestSnapToPoint( const QgsPoint &startPoint, const QgsPoint &midPoint );
236 SnapItem *getSnapItem( const QgsPoint &pos, const double tolerance, PointSnapItem **pSnapPoint = nullptr, SegmentSnapItem **pSnapSegment = nullptr, bool endPointOnly = false ) const;
237
238 private:
239 QList<CoordIdx *> mCoordIdxs;
240 QList<SnapItem *> mSnapItems;
241
242 void addPoint( const CoordIdx *idx, bool isEndPoint );
243 void addSegment( const CoordIdx *idxFrom, const CoordIdx *idxTo );
244
245 GEOSSTRtree *mSTRTree = nullptr;
246 std::vector<geos::unique_ptr> mSTRTreeItems;
247};
248
250
251#endif
252
253#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