QGIS API Documentation  3.12.1-BucureČ™ti (121cc00ff0)
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 
47 class CORE_EXPORT QgsSnappingUtils : public QObject
48 {
49  Q_OBJECT
50 
51  Q_PROPERTY( QgsSnappingConfig config READ config WRITE setConfig NOTIFY configChanged )
52 
53  public:
54 
60  QgsSnappingUtils( QObject *parent SIP_TRANSFERTHIS = nullptr, bool enableSnappingForInvisibleFeature = true );
61  ~QgsSnappingUtils() override;
62 
63  // main actions
64 
69  QgsPointLocator *locatorForLayer( QgsVectorLayer *vl );
70 
77  QgsPointLocator::Match snapToMap( QPoint point, QgsPointLocator::MatchFilter *filter = nullptr, bool relaxed = false );
78 
85  QgsPointLocator::Match snapToMap( const QgsPointXY &pointMap, QgsPointLocator::MatchFilter *filter = nullptr, bool relaxed = false );
86 
88  QgsPointLocator::Match snapToCurrentLayer( QPoint point, QgsPointLocator::Types type, QgsPointLocator::MatchFilter *filter = nullptr );
89 
90  // environment setup
91 
93  void setMapSettings( const QgsMapSettings &settings );
94  QgsMapSettings mapSettings() const { return mMapSettings; }
95 
97  void setCurrentLayer( QgsVectorLayer *layer );
99  QgsVectorLayer *currentLayer() const { return mCurrentLayer; }
100 
101  // configuration
102 
104  {
108  IndexExtent
109  };
110 
112  void setIndexingStrategy( IndexingStrategy strategy ) { mStrategy = strategy; }
114  IndexingStrategy indexingStrategy() const { return mStrategy; }
115 
119  struct LayerConfig
120  {
121 
139  LayerConfig( QgsVectorLayer *l, QgsPointLocator::Types t, double tol, QgsTolerance::UnitType u )
140  : layer( l )
141  , type( t )
142  , tolerance( tol )
143  , unit( u )
144  {}
145 
146  bool operator==( const QgsSnappingUtils::LayerConfig &other ) const
147  {
148  return layer == other.layer && type == other.type && tolerance == other.tolerance && unit == other.unit;
149  }
150  bool operator!=( const QgsSnappingUtils::LayerConfig &other ) const
151  {
152  return !operator==( other );
153  }
154 
156  QgsVectorLayer *layer = nullptr;
158  QgsPointLocator::Types type;
160  double tolerance;
163  };
164 
166  QList<QgsSnappingUtils::LayerConfig> layers() const { return mLayers; }
167 
172  QString dump();
173 
177  QgsSnappingConfig config() const;
178 
186  void setEnableSnappingForInvisibleFeature( bool enable );
187 
188  public slots:
189 
193  void setConfig( const QgsSnappingConfig &snappingConfig );
194 
200  void toggleEnabled();
201 
202  signals:
203 
207  void configChanged( const QgsSnappingConfig &snappingConfig );
208 
209  protected:
210 
212  virtual void prepareIndexStarting( int count ) { Q_UNUSED( count ); }
214  virtual void prepareIndexProgress( int index ) { Q_UNUSED( index ); }
215 
217  void clearAllLocators();
218 
219  private slots:
220 
222  void onInitFinished( bool ok );
223 
224  private:
225  void onIndividualLayerSettingsChanged( const QHash<QgsVectorLayer *, QgsSnappingConfig::IndividualLayerSettings> &layerSettings );
227  QgsCoordinateReferenceSystem destinationCrs() const;
228 
230  QgsPointLocator *locatorForLayerUsingStrategy( QgsVectorLayer *vl, const QgsPointXY &pointMap, double tolerance );
232  QgsPointLocator *temporaryLocatorForLayer( QgsVectorLayer *vl, const QgsPointXY &pointMap, double tolerance );
233 
234  typedef QPair< QgsVectorLayer *, QgsRectangle > LayerAndAreaOfInterest;
235 
237  bool isIndexPrepared( QgsPointLocator *loc, const QgsRectangle &areaOfInterest );
239  void prepareIndex( const QList<LayerAndAreaOfInterest> &layers, bool relaxed );
240 
241  private:
242  // environment
243  QgsMapSettings mMapSettings;
244  QgsVectorLayer *mCurrentLayer = nullptr;
245 
246  QgsSnappingConfig mSnappingConfig;
247 
248  // configuration
249  IndexingStrategy mStrategy = IndexHybrid;
250  QList<LayerConfig> mLayers;
251 
252  // internal data
253  typedef QMap<QgsVectorLayer *, QgsPointLocator *> LocatorsMap;
255  LocatorsMap mLocators;
257  LocatorsMap mTemporaryLocators;
259  QSet<QString> mHybridNonindexableLayers;
260 
271  QHash<QString, double> mHybridMaxAreaPerLayer;
273  int mHybridPerLayerFeatureLimit = 50000;
274 
276  bool mEnableSnappingForInvisibleFeature = true;
277 };
278 
279 
280 #endif // QGSSNAPPINGUTILS_H
The class defines interface for querying point location:
A rectangle specified with double values.
Definition: qgsrectangle.h:41
bool operator!=(const QgsSnappingUtils::LayerConfig &other) const
#define SIP_TRANSFERTHIS
Definition: qgis_sip.h:53
QgsMapSettings mapSettings() const
virtual void prepareIndexProgress(int index)
Called when finished indexing a layer with snapToMap. When index == count the indexing is complete...
bool operator==(const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2)
QgsVectorLayer * currentLayer() const
The current layer used if mode is SnapCurrentLayer.
A class to represent a 2D point.
Definition: qgspointxy.h:43
UnitType
Type of unit of tolerance value from settings.
Definition: qgstolerance.h:40
Interface that allows rejection of some matches in intersection queries (e.g.
void setIndexingStrategy(IndexingStrategy strategy)
Sets a strategy for indexing geometry data - determines how fast and memory consuming the data struct...
The QgsMapSettings class contains configuration for rendering of the map.
LayerConfig(QgsVectorLayer *l, QgsPointLocator::Types t, double tol, QgsTolerance::UnitType u)
Create a new configuration for a snapping layer.
For all layers build index of full extent. Uses more memory, but queries are faster.
For "big" layers using IndexNeverFull, for the rest IndexAlwaysFull. Compromise between speed and mem...
QList< QgsSnappingUtils::LayerConfig > layers() const
Query layers used for snapping.
QgsTolerance::UnitType unit
The units in which the tolerance is specified.
IndexingStrategy indexingStrategy() const
Find out which strategy is used for indexing - by default hybrid indexing is used.
double tolerance
The range around snapping targets in which snapping should occur.
For all layers only create temporary indexes of small extent. Low memory usage, slower queries...
virtual void prepareIndexStarting(int count)
Called when starting to index with snapToMap - can be overridden and e.g. progress dialog can be prov...
bool operator==(const QgsSnappingUtils::LayerConfig &other) const
QgsVectorLayer * layer
The layer to configure.
Configures how a certain layer should be handled in a snapping operation.
This class represents a coordinate reference system (CRS).
This class has all the configuration of snapping and can return answers to snapping queries...
QgsPointLocator::Types type
To which geometry properties of this layers a snapping should happen.
This is a container for configuration of the snapping of the project.
Represents a vector layer which manages a vector based data sets.