QGIS API Documentation  3.18.1-Zürich (202f1bf7e5)
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  bool operator==( const QgsSnappingUtils::LayerConfig &other ) const
150  {
151  return layer == other.layer && type == other.type && tolerance == other.tolerance && unit == other.unit;
152  }
153  bool operator!=( const QgsSnappingUtils::LayerConfig &other ) const
154  {
155  return !operator==( other );
156  }
157 
159  QgsVectorLayer *layer = nullptr;
161  QgsPointLocator::Types type;
163  double tolerance;
166  };
167 
169  QList<QgsSnappingUtils::LayerConfig> layers() const { return mLayers; }
170 
175  QString dump();
176 
180  QgsSnappingConfig config() const;
181 
189  void setEnableSnappingForInvisibleFeature( bool enable );
190 
202  {
203  mExtraSnapLayers.insert( vl );
204  }
205 
215  {
216  mExtraSnapLayers.remove( vl );
217  }
218 
227  QSet<QgsVectorLayer *> getExtraSnapLayers()
228  {
229  return mExtraSnapLayers;
230  }
231 
232 
233  public slots:
234 
238  void setConfig( const QgsSnappingConfig &snappingConfig );
239 
245  void toggleEnabled();
246 
247  signals:
248 
252  void configChanged( const QgsSnappingConfig &snappingConfig );
253 
254  protected:
255 
257  virtual void prepareIndexStarting( int count ) { Q_UNUSED( count ); }
259  virtual void prepareIndexProgress( int index ) { Q_UNUSED( index ); }
260 
262  void clearAllLocators();
263 
264  private slots:
265 
267  void onInitFinished( bool ok );
268 
269  private:
270  void onIndividualLayerSettingsChanged( const QHash<QgsVectorLayer *, QgsSnappingConfig::IndividualLayerSettings> &layerSettings );
272  QgsCoordinateReferenceSystem destinationCrs() const;
273 
275  QgsPointLocator *locatorForLayerUsingStrategy( QgsVectorLayer *vl, const QgsPointXY &pointMap, double tolerance );
277  QgsPointLocator *temporaryLocatorForLayer( QgsVectorLayer *vl, const QgsPointXY &pointMap, double tolerance );
278 
279  typedef QPair< QgsVectorLayer *, QgsRectangle > LayerAndAreaOfInterest;
280 
282  bool isIndexPrepared( QgsPointLocator *loc, const QgsRectangle &areaOfInterest );
284  void prepareIndex( const QList<LayerAndAreaOfInterest> &layers, bool relaxed );
285 
286  private:
287  // environment
288  QgsMapSettings mMapSettings;
289  QgsVectorLayer *mCurrentLayer = nullptr;
290 
291  QgsSnappingConfig mSnappingConfig;
292 
293  // configuration
294  IndexingStrategy mStrategy = IndexHybrid;
295  QList<LayerConfig> mLayers;
296 
297  // internal data
298  typedef QMap<QgsVectorLayer *, QgsPointLocator *> LocatorsMap;
300  LocatorsMap mLocators;
302  LocatorsMap mTemporaryLocators;
304  QSet<QString> mHybridNonindexableLayers;
306  QSet<QgsVectorLayer *> mExtraSnapLayers;
307 
320  QHash<QString, double> mHybridMaxAreaPerLayer;
322  int mHybridPerLayerFeatureLimit = 50000;
323 
325  bool mEnableSnappingForInvisibleFeature = true;
326 };
327 
328 
329 #endif // QGSSNAPPINGUTILS_H
This class represents a coordinate reference system (CRS).
The QgsMapSettings class contains configuration for rendering of the map.
The class defines interface for querying point location:
A class to represent a 2D point.
Definition: qgspointxy.h:44
A rectangle specified with double values.
Definition: qgsrectangle.h:42
This is a container for configuration of the snapping of the project.
This class has all the configuration of snapping and can return answers to snapping queries.
@ IndexAlwaysFull
For all layers build index of full extent. Uses more memory, but queries are faster.
@ IndexHybrid
For "big" layers using IndexNeverFull, for the rest IndexAlwaysFull. Compromise between speed and mem...
@ IndexNeverFull
For all layers only create temporary indexes of small extent. Low memory usage, slower queries.
QgsMapSettings mapSettings() const
QList< QgsSnappingUtils::LayerConfig > layers() const
Query layers used for snapping.
void addExtraSnapLayer(QgsVectorLayer *vl)
Supply an extra snapping layer (typically a memory layer).
QSet< QgsVectorLayer * > getExtraSnapLayers()
Returns the list of extra snapping layers.
virtual void prepareIndexProgress(int index)
Called when finished indexing a layer with snapToMap. When index == count the indexing is complete.
IndexingStrategy indexingStrategy() const
Find out which strategy is used for indexing - by default hybrid indexing is used.
void configChanged(const QgsSnappingConfig &snappingConfig)
Emitted when the snapping settings object changes.
void removeExtraSnapLayer(QgsVectorLayer *vl)
Removes an extra snapping layer.
QgsVectorLayer * currentLayer() const
The current layer used if mode is SnapCurrentLayer.
void setIndexingStrategy(IndexingStrategy strategy)
Sets a strategy for indexing geometry data - determines how fast and memory consuming the data struct...
virtual void prepareIndexStarting(int count)
Called when starting to index with snapToMap - can be overridden and e.g. progress dialog can be prov...
UnitType
Type of unit of tolerance value from settings.
Definition: qgstolerance.h:42
Represents a vector layer which manages a vector based data sets.
#define SIP_TRANSFERTHIS
Definition: qgis_sip.h:53
bool operator==(const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2)
Interface that allows rejection of some matches in intersection queries (e.g.
Configures how a certain layer should be handled in a snapping operation.
bool operator!=(const QgsSnappingUtils::LayerConfig &other) const
double tolerance
The range around snapping targets in which snapping should occur.
QgsTolerance::UnitType unit
The units in which the tolerance is specified.
LayerConfig(QgsVectorLayer *l, QgsPointLocator::Types t, double tol, QgsTolerance::UnitType u)
Create a new configuration for a snapping layer.
QgsPointLocator::Types type
To which geometry properties of this layers a snapping should happen.
QgsVectorLayer * layer
The layer to configure.
bool operator==(const QgsSnappingUtils::LayerConfig &other) const