QGIS API Documentation  3.6.0-Noosa (5873452)
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 
56  QgsSnappingUtils( QObject *parent SIP_TRANSFERTHIS = nullptr, bool enableSnappingForInvisibleFeature = true );
57  ~QgsSnappingUtils() override;
58 
59  // main actions
60 
62  QgsPointLocator *locatorForLayer( QgsVectorLayer *vl );
63 
65  QgsPointLocator::Match snapToMap( QPoint point, QgsPointLocator::MatchFilter *filter = nullptr );
66  QgsPointLocator::Match snapToMap( const QgsPointXY &pointMap, QgsPointLocator::MatchFilter *filter = nullptr );
67 
69 
71  QgsPointLocator::Match snapToCurrentLayer( QPoint point, QgsPointLocator::Types type, QgsPointLocator::MatchFilter *filter = nullptr );
72 
73  // environment setup
74 
76  void setMapSettings( const QgsMapSettings &settings );
77  QgsMapSettings mapSettings() const { return mMapSettings; }
78 
80  void setCurrentLayer( QgsVectorLayer *layer );
82  QgsVectorLayer *currentLayer() const { return mCurrentLayer; }
83 
84  // configuration
85 
87  {
91  IndexExtent
92  };
93 
95  void setIndexingStrategy( IndexingStrategy strategy ) { mStrategy = strategy; }
97  IndexingStrategy indexingStrategy() const { return mStrategy; }
98 
102  struct LayerConfig
103  {
104 
122  LayerConfig( QgsVectorLayer *l, QgsPointLocator::Types t, double tol, QgsTolerance::UnitType u )
123  : layer( l )
124  , type( t )
125  , tolerance( tol )
126  , unit( u )
127  {}
128 
129  bool operator==( const QgsSnappingUtils::LayerConfig &other ) const
130  {
131  return layer == other.layer && type == other.type && tolerance == other.tolerance && unit == other.unit;
132  }
133  bool operator!=( const QgsSnappingUtils::LayerConfig &other ) const
134  {
135  return !operator==( other );
136  }
137 
139  QgsVectorLayer *layer = nullptr;
141  QgsPointLocator::Types type;
143  double tolerance;
146  };
147 
149  QList<QgsSnappingUtils::LayerConfig> layers() const { return mLayers; }
150 
155  QString dump();
156 
160  QgsSnappingConfig config() const;
161 
169  void setEnableSnappingForInvisibleFeature( bool enable );
170 
171  public slots:
172 
176  void setConfig( const QgsSnappingConfig &snappingConfig );
177 
183  void toggleEnabled();
184 
185  signals:
186 
190  void configChanged( const QgsSnappingConfig &snappingConfig );
191 
192  protected:
194  virtual void prepareIndexStarting( int count ) { Q_UNUSED( count ); }
196  virtual void prepareIndexProgress( int index ) { Q_UNUSED( index ); }
197 
199  void clearAllLocators();
200 
201  private:
202  void onIndividualLayerSettingsChanged( const QHash<QgsVectorLayer *, QgsSnappingConfig::IndividualLayerSettings> &layerSettings );
204  QgsCoordinateReferenceSystem destinationCrs() const;
205 
207  QgsPointLocator *locatorForLayerUsingStrategy( QgsVectorLayer *vl, const QgsPointXY &pointMap, double tolerance );
209  QgsPointLocator *temporaryLocatorForLayer( QgsVectorLayer *vl, const QgsPointXY &pointMap, double tolerance );
210 
211  typedef QPair< QgsVectorLayer *, QgsRectangle > LayerAndAreaOfInterest;
212 
214  bool isIndexPrepared( QgsVectorLayer *vl, const QgsRectangle &areaOfInterest );
216  void prepareIndex( const QList<LayerAndAreaOfInterest> &layers );
217 
218  private:
219  // environment
220  QgsMapSettings mMapSettings;
221  QgsVectorLayer *mCurrentLayer = nullptr;
222 
223  QgsSnappingConfig mSnappingConfig;
224 
225  // configuration
226  IndexingStrategy mStrategy = IndexHybrid;
227  QList<LayerConfig> mLayers;
228 
229  // internal data
230  typedef QMap<QgsVectorLayer *, QgsPointLocator *> LocatorsMap;
232  LocatorsMap mLocators;
234  LocatorsMap mTemporaryLocators;
236  QSet<QString> mHybridNonindexableLayers;
237 
248  QHash<QString, double> mHybridMaxAreaPerLayer;
250  int mHybridPerLayerFeatureLimit = 50000;
251 
253  bool mIsIndexing = false;
254 
256  bool mEnableSnappingForInvisibleFeature = true;
257 
258 };
259 
260 
261 #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:46
QgsMapSettings mapSettings() const
virtual void prepareIndexProgress(int index)
Called when finished indexing a layer. 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 - can be overridden and e.g. progress dialog can be provided...
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.