QGIS API Documentation 3.41.0-Master (cea29feecf2)
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 "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
41class ANALYSIS_EXPORT QgsGeometrySnapper : public QObject
42{
43 Q_OBJECT
44
45 public:
57
63 QgsGeometrySnapper( QgsFeatureSource *referenceSource );
64
70 QgsGeometry snapGeometry( const QgsGeometry &geometry, double snapTolerance, SnapMode mode = PreferNodes ) const;
71
77 QgsFeatureList snapFeatures( const QgsFeatureList &features, double snapTolerance, SnapMode mode = PreferNodes );
78
82 static QgsGeometry snapGeometry( const QgsGeometry &geometry, double snapTolerance, const QList<QgsGeometry> &referenceGeometries, SnapMode mode = PreferNodes );
83
84 signals:
85
88
89 private:
90 struct ProcessFeatureWrapper
91 {
92 QgsGeometrySnapper *instance = nullptr;
93 double snapTolerance;
94 SnapMode mode;
95 explicit ProcessFeatureWrapper( QgsGeometrySnapper *_instance, double snapTolerance, SnapMode mode )
96 : instance( _instance )
97 , snapTolerance( snapTolerance )
98 , mode( mode )
99 {}
100 void operator()( QgsFeature &feature ) { instance->processFeature( feature, snapTolerance, mode ); }
101 };
102
103 enum PointFlag
104 {
105 SnappedToRefNode,
106 SnappedToRefSegment,
107 Unsnapped
108 };
109
110 QgsFeatureSource *mReferenceSource = nullptr;
111 QHash<QgsFeatureId, QgsGeometry> mCachedReferenceGeometries;
112
113 QgsSpatialIndex mIndex;
114 mutable QMutex mIndexMutex;
115 mutable QMutex mReferenceLayerMutex;
116
117 void processFeature( QgsFeature &feature, double snapTolerance, SnapMode mode );
118
119 static int polyLineSize( const QgsAbstractGeometry *geom, int iPart, int iRing );
120};
121
122
137class ANALYSIS_EXPORT QgsInternalGeometrySnapper
138{
139 public:
145
150 QgsGeometry snapFeature( const QgsFeature &feature );
151
155 QgsGeometryMap snappedGeometries() const { return mProcessedGeometries; }
156
157 private:
158 bool mFirstFeature = true;
159 double mSnapTolerance = 0;
161 QgsSpatialIndex mProcessedIndex;
162 QgsGeometryMap mProcessedGeometries;
163};
164
165#ifndef SIP_RUN
166
168class QgsSnapIndex
169{
170 public:
171 struct CoordIdx
172 {
173 CoordIdx( const QgsAbstractGeometry *_geom, QgsVertexId _vidx )
174 : geom( _geom )
175 , vidx( _vidx )
176 {}
177 QgsPoint point() const { return geom->vertexAt( vidx ); }
178
179 const QgsAbstractGeometry *geom = nullptr;
180 QgsVertexId vidx;
181 };
182
183 enum SnapType
184 {
185 SnapPoint,
186 SnapEndPoint,
187 SnapSegment
188 };
189
190 class SnapItem
191 {
192 public:
193 virtual ~SnapItem() = default;
194 SnapType type;
195 virtual QgsPoint getSnapPoint( const QgsPoint &p ) const = 0;
196
197 protected:
198 explicit SnapItem( SnapType _type )
199 : type( _type ) {}
200 };
201
202 class PointSnapItem : public QgsSnapIndex::SnapItem
203 {
204 public:
205 explicit PointSnapItem( const CoordIdx *_idx, bool isEndPoint );
206 QgsPoint getSnapPoint( const QgsPoint & /*p*/ ) const override;
207 const CoordIdx *idx = nullptr;
208 };
209
210 class SegmentSnapItem : public QgsSnapIndex::SnapItem
211 {
212 public:
213 SegmentSnapItem( const CoordIdx *_idxFrom, const CoordIdx *_idxTo );
214 QgsPoint getSnapPoint( const QgsPoint &p ) const override;
215 bool getIntersection( const QgsPoint &p1, const QgsPoint &p2, QgsPoint &inter ) const;
216 bool getProjection( const QgsPoint &p, QgsPoint &pProj ) const;
217 bool withinSquaredDistance( const QgsPoint &p, const double squaredDistance );
218 const CoordIdx *idxFrom = nullptr;
219 const CoordIdx *idxTo = nullptr;
220 };
221
222 QgsSnapIndex();
223 ~QgsSnapIndex();
224
225 QgsSnapIndex( const QgsSnapIndex &rh ) = delete;
226 QgsSnapIndex &operator=( const QgsSnapIndex &rh ) = delete;
227
228 void addGeometry( const QgsAbstractGeometry *geom );
229 QgsPoint getClosestSnapToPoint( const QgsPoint &startPoint, const QgsPoint &midPoint );
230 SnapItem *getSnapItem( const QgsPoint &pos, const double tolerance, PointSnapItem **pSnapPoint = nullptr, SegmentSnapItem **pSnapSegment = nullptr, bool endPointOnly = false ) const;
231
232 private:
233 QList<CoordIdx *> mCoordIdxs;
234 QList<SnapItem *> mSnapItems;
235
236 void addPoint( const CoordIdx *idx, bool isEndPoint );
237 void addSegment( const CoordIdx *idxFrom, const CoordIdx *idxTo );
238
239 GEOSSTRtree *mSTRTree = nullptr;
240 std::vector<geos::unique_ptr> mSTRTreeItems;
241};
242
244
245#endif
246
247#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
QgsGeometrySnapper allows a geometry to be snapped to the geometries within a different reference lay...
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.
QgsInternalGeometrySnapper 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 data sets.
QMap< QgsFeatureId, QgsGeometry > QgsGeometryMap
QList< QgsFeature > QgsFeatureList
Utility class for identifying a unique vertex within a geometry.
Definition qgsvertexid.h:30