QGIS API Documentation  3.16.0-Hannover (43b64b13f3)
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 
49 class CORE_EXPORT QgsSnappingUtils : public QObject
50 {
51  Q_OBJECT
52 
53  Q_PROPERTY( QgsSnappingConfig config READ config WRITE setConfig NOTIFY configChanged )
54 
55  public:
56 
62  QgsSnappingUtils( QObject *parent SIP_TRANSFERTHIS = nullptr, bool enableSnappingForInvisibleFeature = true );
63  ~QgsSnappingUtils() override;
64 
65  // main actions
66 
71  QgsPointLocator *locatorForLayer( QgsVectorLayer *vl );
72 
79  QgsPointLocator::Match snapToMap( QPoint point, QgsPointLocator::MatchFilter *filter = nullptr, bool relaxed = false );
80 
87  QgsPointLocator::Match snapToMap( const QgsPointXY &pointMap, QgsPointLocator::MatchFilter *filter = nullptr, bool relaxed = false );
88 
90  QgsPointLocator::Match snapToCurrentLayer( QPoint point, QgsPointLocator::Types type, QgsPointLocator::MatchFilter *filter = nullptr );
91 
92  // environment setup
93 
95  void setMapSettings( const QgsMapSettings &settings );
96  QgsMapSettings mapSettings() const { return mMapSettings; }
97 
99  void setCurrentLayer( QgsVectorLayer *layer );
101  QgsVectorLayer *currentLayer() const { return mCurrentLayer; }
102 
103  // configuration
104 
106  {
110  IndexExtent
111  };
112 
114  void setIndexingStrategy( IndexingStrategy strategy ) { mStrategy = strategy; }
116  IndexingStrategy indexingStrategy() const { return mStrategy; }
117 
121  struct LayerConfig
122  {
123 
141  LayerConfig( QgsVectorLayer *l, QgsPointLocator::Types t, double tol, QgsTolerance::UnitType u )
142  : layer( l )
143  , type( t )
144  , tolerance( tol )
145  , unit( u )
146  {}
147 
148  bool operator==( const QgsSnappingUtils::LayerConfig &other ) const
149  {
150  return layer == other.layer && type == other.type && tolerance == other.tolerance && unit == other.unit;
151  }
152  bool operator!=( const QgsSnappingUtils::LayerConfig &other ) const
153  {
154  return !operator==( other );
155  }
156 
158  QgsVectorLayer *layer = nullptr;
160  QgsPointLocator::Types type;
162  double tolerance;
165  };
166 
168  QList<QgsSnappingUtils::LayerConfig> layers() const { return mLayers; }
169 
174  QString dump();
175 
179  QgsSnappingConfig config() const;
180 
188  void setEnableSnappingForInvisibleFeature( bool enable );
189 
201  {
202  mExtraSnapLayers.insert( vl );
203  }
204 
214  {
215  mExtraSnapLayers.remove( vl );
216  }
217 
226  QSet<QgsVectorLayer *> getExtraSnapLayers()
227  {
228  return mExtraSnapLayers;
229  }
230 
231 
232  public slots:
233 
237  void setConfig( const QgsSnappingConfig &snappingConfig );
238 
244  void toggleEnabled();
245 
246  signals:
247 
251  void configChanged( const QgsSnappingConfig &snappingConfig );
252 
253  protected:
254 
256  virtual void prepareIndexStarting( int count ) { Q_UNUSED( count ); }
258  virtual void prepareIndexProgress( int index ) { Q_UNUSED( index ); }
259 
261  void clearAllLocators();
262 
263  private slots:
264 
266  void onInitFinished( bool ok );
267 
268  private:
269  void onIndividualLayerSettingsChanged( const QHash<QgsVectorLayer *, QgsSnappingConfig::IndividualLayerSettings> &layerSettings );
271  QgsCoordinateReferenceSystem destinationCrs() const;
272 
274  QgsPointLocator *locatorForLayerUsingStrategy( QgsVectorLayer *vl, const QgsPointXY &pointMap, double tolerance );
276  QgsPointLocator *temporaryLocatorForLayer( QgsVectorLayer *vl, const QgsPointXY &pointMap, double tolerance );
277 
278  typedef QPair< QgsVectorLayer *, QgsRectangle > LayerAndAreaOfInterest;
279 
281  bool isIndexPrepared( QgsPointLocator *loc, const QgsRectangle &areaOfInterest );
283  void prepareIndex( const QList<LayerAndAreaOfInterest> &layers, bool relaxed );
284 
285  private:
286  // environment
287  QgsMapSettings mMapSettings;
288  QgsVectorLayer *mCurrentLayer = nullptr;
289 
290  QgsSnappingConfig mSnappingConfig;
291 
292  // configuration
293  IndexingStrategy mStrategy = IndexHybrid;
294  QList<LayerConfig> mLayers;
295 
296  // internal data
297  typedef QMap<QgsVectorLayer *, QgsPointLocator *> LocatorsMap;
299  LocatorsMap mLocators;
301  LocatorsMap mTemporaryLocators;
303  QSet<QString> mHybridNonindexableLayers;
305  QSet<QgsVectorLayer *> mExtraSnapLayers;
306 
319  QHash<QString, double> mHybridMaxAreaPerLayer;
321  int mHybridPerLayerFeatureLimit = 50000;
322 
324  bool mEnableSnappingForInvisibleFeature = true;
325 };
326 
327 
328 #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:114
QgsSnappingUtils::LayerConfig::tolerance
double tolerance
The range around snapping targets in which snapping should occur.
Definition: qgssnappingutils.h:162
operator==
bool operator==(const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2)
Definition: qgsfeatureiterator.h:400
QgsSnappingUtils::addExtraSnapLayer
void addExtraSnapLayer(QgsVectorLayer *vl)
Supply an extra snapping layer (typically a memory layer).
Definition: qgssnappingutils.h:200
QgsSnappingUtils::mapSettings
QgsMapSettings mapSettings() const
Definition: qgssnappingutils.h:96
qgstolerance.h
qgsmapsettings.h
QgsRectangle
A rectangle specified with double values.
Definition: qgsrectangle.h:42
QgsSnappingUtils::currentLayer
QgsVectorLayer * currentLayer() const
The current layer used if mode is SnapCurrentLayer.
Definition: qgssnappingutils.h:101
qgssnappingconfig.h
QgsSnappingUtils::IndexHybrid
@ IndexHybrid
For "big" layers using IndexNeverFull, for the rest IndexAlwaysFull. Compromise between speed and mem...
Definition: qgssnappingutils.h:109
QgsSnappingUtils::IndexingStrategy
IndexingStrategy
Definition: qgssnappingutils.h:106
QgsTolerance::UnitType
UnitType
Type of unit of tolerance value from settings.
Definition: qgstolerance.h:42
QgsSnappingUtils::configChanged
void configChanged(const QgsSnappingConfig &snappingConfig)
Emitted when the snapping settings object changes.
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:256
QgsSnappingUtils::IndexNeverFull
@ IndexNeverFull
For all layers only create temporary indexes of small extent. Low memory usage, slower queries.
Definition: qgssnappingutils.h:108
qgis_sip.h
QgsSnappingUtils::getExtraSnapLayers
QSet< QgsVectorLayer * > getExtraSnapLayers()
Returns the list of extra snapping layers.
Definition: qgssnappingutils.h:226
QgsSnappingConfig
This is a container for configuration of the snapping of the project.
Definition: qgssnappingconfig.h:34
QgsSnappingUtils::LayerConfig::operator==
bool operator==(const QgsSnappingUtils::LayerConfig &other) const
Definition: qgssnappingutils.h:148
QgsSnappingUtils::IndexAlwaysFull
@ IndexAlwaysFull
For all layers build index of full extent. Uses more memory, but queries are faster.
Definition: qgssnappingutils.h:107
QgsCoordinateReferenceSystem
This class represents a coordinate reference system (CRS).
Definition: qgscoordinatereferencesystem.h:206
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:164
QgsPointLocator::Match
Definition: qgspointlocator.h:185
QgsPointXY
A class to represent a 2D point.
Definition: qgspointxy.h:44
QgsSnappingUtils::layers
QList< QgsSnappingUtils::LayerConfig > layers() const
Query layers used for snapping.
Definition: qgssnappingutils.h:168
QgsSnappingUtils::indexingStrategy
IndexingStrategy indexingStrategy() const
Find out which strategy is used for indexing - by default hybrid indexing is used.
Definition: qgssnappingutils.h:116
QgsVectorLayer
Represents a vector layer which manages a vector based data sets.
Definition: qgsvectorlayer.h:387
QgsPointLocator
The class defines interface for querying point location:
Definition: qgspointlocator.h:100
QgsPointLocator::MatchFilter
Interface that allows rejection of some matches in intersection queries (e.g.
Definition: qgspointlocator.h:306
QgsSnappingUtils::LayerConfig::layer
QgsVectorLayer * layer
The layer to configure.
Definition: qgssnappingutils.h:158
qgspointlocator.h
QgsSnappingUtils::LayerConfig::operator!=
bool operator!=(const QgsSnappingUtils::LayerConfig &other) const
Definition: qgssnappingutils.h:152
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:141
QgsSnappingUtils::removeExtraSnapLayer
void removeExtraSnapLayer(QgsVectorLayer *vl)
Removes an extra snapping layer.
Definition: qgssnappingutils.h:213
QgsMapSettings
The QgsMapSettings class contains configuration for rendering of the map.
Definition: qgsmapsettings.h:88
QgsSnappingUtils::LayerConfig::type
QgsPointLocator::Types type
To which geometry properties of this layers a snapping should happen.
Definition: qgssnappingutils.h:160
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:258