QGIS API Documentation  3.26.3-Buenos Aires (65e4edfdad)
qgssnappingutils.h
Go to the documentation of this file.
1 /***************************************************************************
2  qgssnappingutils.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 QGSSNAPPINGUTILS_H
17 #define QGSSNAPPINGUTILS_H
18 
19 
20 #include "qgis_core.h"
21 #include "qgis_sip.h"
22 #include "qgsmapsettings.h"
23 #include "qgstolerance.h"
24 #include "qgspointlocator.h"
25 #include "qgssnappingconfig.h"
26 
27 class QgsSnappingConfig;
28 
50 class CORE_EXPORT QgsSnappingUtils : public QObject
51 {
52  Q_OBJECT
53 
54  Q_PROPERTY( QgsSnappingConfig config READ config WRITE setConfig NOTIFY configChanged )
55 
56  public:
57 
63  QgsSnappingUtils( QObject *parent SIP_TRANSFERTHIS = nullptr, bool enableSnappingForInvisibleFeature = true );
64  ~QgsSnappingUtils() override;
65 
66  // main actions
67 
72  QgsPointLocator *locatorForLayer( QgsVectorLayer *vl );
73 
80  QgsPointLocator::Match snapToMap( QPoint point, QgsPointLocator::MatchFilter *filter = nullptr, bool relaxed = false );
81 
88  QgsPointLocator::Match snapToMap( const QgsPointXY &pointMap, QgsPointLocator::MatchFilter *filter = nullptr, bool relaxed = false );
89 
91  QgsPointLocator::Match snapToCurrentLayer( QPoint point, QgsPointLocator::Types type, QgsPointLocator::MatchFilter *filter = nullptr );
92 
93  // environment setup
94 
96  void setMapSettings( const QgsMapSettings &settings );
97  QgsMapSettings mapSettings() const { return mMapSettings; }
98 
100  void setCurrentLayer( QgsVectorLayer *layer );
102  QgsVectorLayer *currentLayer() const { return mCurrentLayer; }
103 
104  // configuration
105 
107  {
111  IndexExtent
112  };
113 
115  void setIndexingStrategy( IndexingStrategy strategy ) { mStrategy = strategy; }
117  IndexingStrategy indexingStrategy() const { return mStrategy; }
118 
122  struct LayerConfig
123  {
124 
142  LayerConfig( QgsVectorLayer *l, QgsPointLocator::Types t, double tol, QgsTolerance::UnitType u )
143  : layer( l )
144  , type( t )
145  , tolerance( tol )
146  , unit( u )
147  {}
148 
149  // TODO c++20 - replace with = default
150  bool operator==( const QgsSnappingUtils::LayerConfig &other ) const
151  {
152  return layer == other.layer && type == other.type && tolerance == other.tolerance && unit == other.unit;
153  }
154  bool operator!=( const QgsSnappingUtils::LayerConfig &other ) const
155  {
156  return !operator==( other );
157  }
158 
160  QgsVectorLayer *layer = nullptr;
162  QgsPointLocator::Types type;
164  double tolerance;
167  };
168 
170  QList<QgsSnappingUtils::LayerConfig> layers() const { return mLayers; }
171 
176  QString dump();
177 
181  QgsSnappingConfig config() const;
182 
190  void setEnableSnappingForInvisibleFeature( bool enable );
191 
203  {
204  mExtraSnapLayers.insert( vl );
205  }
206 
216  {
217  mExtraSnapLayers.remove( vl );
218  }
219 
228  QSet<QgsVectorLayer *> getExtraSnapLayers()
229  {
230  return mExtraSnapLayers;
231  }
232 
233 
234  public slots:
235 
239  void setConfig( const QgsSnappingConfig &snappingConfig );
240 
246  void toggleEnabled();
247 
248  signals:
249 
253  void configChanged( const QgsSnappingConfig &snappingConfig );
254 
255  protected:
256 
258  virtual void prepareIndexStarting( int count ) { Q_UNUSED( count ); }
260  virtual void prepareIndexProgress( int index ) { Q_UNUSED( index ); }
261 
263  void clearAllLocators();
264 
265  private slots:
266 
268  void onInitFinished( bool ok );
269 
270  private:
271  void onIndividualLayerSettingsChanged( const QHash<QgsVectorLayer *, QgsSnappingConfig::IndividualLayerSettings> &layerSettings );
273  QgsCoordinateReferenceSystem destinationCrs() const;
274 
276  QgsPointLocator *locatorForLayerUsingStrategy( QgsVectorLayer *vl, const QgsPointXY &pointMap, double tolerance );
278  QgsPointLocator *temporaryLocatorForLayer( QgsVectorLayer *vl, const QgsPointXY &pointMap, double tolerance );
279 
280  typedef QPair< QgsVectorLayer *, QgsRectangle > LayerAndAreaOfInterest;
281 
283  bool isIndexPrepared( QgsPointLocator *loc, const QgsRectangle &areaOfInterest );
285  void prepareIndex( const QList<LayerAndAreaOfInterest> &layers, bool relaxed );
286 
287  private:
288  // environment
289  QgsMapSettings mMapSettings;
290  QgsVectorLayer *mCurrentLayer = nullptr;
291 
292  QgsSnappingConfig mSnappingConfig;
293 
294  // configuration
295  IndexingStrategy mStrategy = IndexHybrid;
296  QList<LayerConfig> mLayers;
297 
298  // internal data
299  typedef QMap<QgsVectorLayer *, QgsPointLocator *> LocatorsMap;
301  LocatorsMap mLocators;
303  LocatorsMap mTemporaryLocators;
305  QSet<QString> mHybridNonindexableLayers;
307  QSet<QgsVectorLayer *> mExtraSnapLayers;
308 
321  QHash<QString, double> mHybridMaxAreaPerLayer;
323  int mHybridPerLayerFeatureLimit = 50000;
324 
326  bool mEnableSnappingForInvisibleFeature = true;
327 };
328 
329 
330 #endif // QGSSNAPPINGUTILS_H
QgsSnappingUtils::setIndexingStrategy
void setIndexingStrategy(IndexingStrategy strategy)
Sets a strategy for indexing geometry data - determines how fast and memory consuming the data struct...
Definition: qgssnappingutils.h:115
QgsSnappingUtils::LayerConfig::tolerance
double tolerance
The range around snapping targets in which snapping should occur.
Definition: qgssnappingutils.h:164
operator==
bool operator==(const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2)
Definition: qgsfeatureiterator.h:425
QgsSnappingUtils::addExtraSnapLayer
void addExtraSnapLayer(QgsVectorLayer *vl)
Supply an extra snapping layer (typically a memory layer).
Definition: qgssnappingutils.h:202
QgsSnappingUtils::mapSettings
QgsMapSettings mapSettings() const
Definition: qgssnappingutils.h:97
qgstolerance.h
qgsmapsettings.h
QgsRectangle
A rectangle specified with double values.
Definition: qgsrectangle.h:41
QgsSnappingUtils::currentLayer
QgsVectorLayer * currentLayer() const
The current layer used if mode is SnapCurrentLayer.
Definition: qgssnappingutils.h:102
qgssnappingconfig.h
QgsSnappingUtils::IndexHybrid
@ IndexHybrid
For "big" layers using IndexNeverFull, for the rest IndexAlwaysFull. Compromise between speed and mem...
Definition: qgssnappingutils.h:110
QgsSnappingUtils::IndexingStrategy
IndexingStrategy
Definition: qgssnappingutils.h:106
QgsTolerance::UnitType
UnitType
Type of unit of tolerance value from settings.
Definition: qgstolerance.h:41
QgsSnappingUtils::prepareIndexStarting
virtual void prepareIndexStarting(int count)
Called when starting to index with snapToMap - can be overridden and e.g. progress dialog can be prov...
Definition: qgssnappingutils.h:258
QgsSnappingUtils::IndexNeverFull
@ IndexNeverFull
For all layers only create temporary indexes of small extent. Low memory usage, slower queries.
Definition: qgssnappingutils.h:109
qgis_sip.h
QgsSnappingUtils::getExtraSnapLayers
QSet< QgsVectorLayer * > getExtraSnapLayers()
Returns the list of extra snapping layers.
Definition: qgssnappingutils.h:228
QgsSnappingConfig
This is a container for configuration of the snapping of the project.
Definition: qgssnappingconfig.h:37
QgsSnappingUtils::LayerConfig::operator==
bool operator==(const QgsSnappingUtils::LayerConfig &other) const
Definition: qgssnappingutils.h:150
QgsSnappingUtils::IndexAlwaysFull
@ IndexAlwaysFull
For all layers build index of full extent. Uses more memory, but queries are faster.
Definition: qgssnappingutils.h:108
QgsCoordinateReferenceSystem
This class represents a coordinate reference system (CRS).
Definition: qgscoordinatereferencesystem.h:211
QgsSnappingUtils
This class has all the configuration of snapping and can return answers to snapping queries.
Definition: qgssnappingutils.h:50
QgsSnappingUtils::LayerConfig::unit
QgsTolerance::UnitType unit
The units in which the tolerance is specified.
Definition: qgssnappingutils.h:166
QgsPointLocator::Match
Definition: qgspointlocator.h:187
QgsPointXY
A class to represent a 2D point.
Definition: qgspointxy.h:58
QgsSnappingUtils::layers
QList< QgsSnappingUtils::LayerConfig > layers() const
Query layers used for snapping.
Definition: qgssnappingutils.h:170
QgsSnappingUtils::indexingStrategy
IndexingStrategy indexingStrategy() const
Find out which strategy is used for indexing - by default hybrid indexing is used.
Definition: qgssnappingutils.h:117
QgsVectorLayer
Represents a vector layer which manages a vector based data sets.
Definition: qgsvectorlayer.h:391
QgsPointLocator
The class defines interface for querying point location:
Definition: qgspointlocator.h:101
QgsPointLocator::MatchFilter
Interface that allows rejection of some matches in intersection queries (e.g.
Definition: qgspointlocator.h:349
QgsSnappingUtils::LayerConfig::layer
QgsVectorLayer * layer
The layer to configure.
Definition: qgssnappingutils.h:160
qgspointlocator.h
QgsSnappingUtils::LayerConfig::operator!=
bool operator!=(const QgsSnappingUtils::LayerConfig &other) const
Definition: qgssnappingutils.h:154
QgsSnappingUtils::LayerConfig::LayerConfig
LayerConfig(QgsVectorLayer *l, QgsPointLocator::Types t, double tol, QgsTolerance::UnitType u)
Create a new configuration for a snapping layer.
Definition: qgssnappingutils.h:142
QgsSnappingUtils::removeExtraSnapLayer
void removeExtraSnapLayer(QgsVectorLayer *vl)
Removes an extra snapping layer.
Definition: qgssnappingutils.h:215
QgsMapSettings
The QgsMapSettings class contains configuration for rendering of the map. The rendering itself is don...
Definition: qgsmapsettings.h:88
QgsSnappingUtils::LayerConfig::type
QgsPointLocator::Types type
To which geometry properties of this layers a snapping should happen.
Definition: qgssnappingutils.h:162
QgsSnappingUtils::LayerConfig
Configures how a certain layer should be handled in a snapping operation.
Definition: qgssnappingutils.h:122
SIP_TRANSFERTHIS
#define SIP_TRANSFERTHIS
Definition: qgis_sip.h:53
QgsSnappingUtils::prepareIndexProgress
virtual void prepareIndexProgress(int index)
Called when finished indexing a layer with snapToMap. When index == count the indexing is complete.
Definition: qgssnappingutils.h:260