QGIS API Documentation  2.18.21-Las Palmas (9fba24a)
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 "qgsmapsettings.h"
21 #include "qgstolerance.h"
22 #include "qgspointlocator.h"
23 
41 class CORE_EXPORT QgsSnappingUtils : public QObject
42 {
43  Q_OBJECT
44  public:
45  QgsSnappingUtils( QObject* parent = nullptr );
47 
48  // main actions
49 
51  QgsPointLocator* locatorForLayer( QgsVectorLayer* vl );
52 
54  QgsPointLocator::Match snapToMap( QPoint point, QgsPointLocator::MatchFilter* filter = nullptr );
55  QgsPointLocator::Match snapToMap( const QgsPoint& pointMap, QgsPointLocator::MatchFilter* filter = nullptr );
56 
58  QgsPointLocator::Match snapToCurrentLayer( QPoint point, int type, QgsPointLocator::MatchFilter* filter = nullptr );
59 
60  // environment setup
61 
63  void setMapSettings( const QgsMapSettings& settings );
64  const QgsMapSettings& mapSettings() const { return mMapSettings; }
65 
67  void setCurrentLayer( QgsVectorLayer* layer );
69  QgsVectorLayer* currentLayer() const { return mCurrentLayer; }
70 
71 
72  // configuration
73 
76  {
80  };
81 
83  void setSnapToMapMode( SnapToMapMode mode );
85  SnapToMapMode snapToMapMode() const { return mSnapToMapMode; }
86 
88  {
92  IndexExtent
93  };
94 
96  void setIndexingStrategy( IndexingStrategy strategy ) { mStrategy = strategy; }
98  IndexingStrategy indexingStrategy() const { return mStrategy; }
99 
101  void setDefaultSettings( int type, double tolerance, QgsTolerance::UnitType unit );
103  void defaultSettings( int& type, double& tolerance, QgsTolerance::UnitType& unit );
104 
108  struct LayerConfig
109  {
128  LayerConfig( QgsVectorLayer* l, QgsPointLocator::Types t, double tol, QgsTolerance::UnitType u )
129  : layer( l )
130  , type( t )
131  , tolerance( tol )
132  , unit( u )
133  {}
134 
135  bool operator==( const LayerConfig& other ) const
136  {
137  return layer == other.layer && type == other.type && tolerance == other.tolerance && unit == other.unit;
138  }
139  bool operator!=( const LayerConfig& other ) const
140  {
141  return !operator==( other );
142  }
143 
147  QgsPointLocator::Types type;
149  double tolerance;
152  };
153 
155  void setLayers( const QList<LayerConfig>& layers );
157  QList<LayerConfig> layers() const { return mLayers; }
158 
160  void setSnapOnIntersections( bool enabled );
162  bool snapOnIntersections() const { return mSnapOnIntersection; }
163 
167  QString dump();
168 
169  public slots:
171  void readConfigFromProject();
172 
173  signals:
177  void configChanged();
178 
179  protected:
181  virtual void prepareIndexStarting( int count ) { Q_UNUSED( count ); }
183  virtual void prepareIndexProgress( int index ) { Q_UNUSED( index ); }
184 
185  private slots:
186  void onLayersWillBeRemoved( const QStringList& layerIds );
187 
188  private:
190  const QgsCoordinateReferenceSystem* destCRS();
191 
193  void clearAllLocators();
194 
196  QgsPointLocator* locatorForLayerUsingStrategy( QgsVectorLayer* vl, const QgsPoint& pointMap, double tolerance );
198  QgsPointLocator* temporaryLocatorForLayer( QgsVectorLayer* vl, const QgsPoint& pointMap, double tolerance );
199 
201 
203  bool isIndexPrepared( QgsVectorLayer* vl, const QgsRectangle& areaOfInterest );
205  void prepareIndex( const QList<LayerAndAreaOfInterest>& layers );
206 
207  private:
208  // environment
209  QgsMapSettings mMapSettings;
210  QgsVectorLayer* mCurrentLayer;
211 
212  // configuration
213  SnapToMapMode mSnapToMapMode;
214  IndexingStrategy mStrategy;
215  int mDefaultType;
216  double mDefaultTolerance;
217  QgsTolerance::UnitType mDefaultUnit;
218  QList<LayerConfig> mLayers;
219  bool mSnapOnIntersection;
220 
221  // internal data
224  LocatorsMap mLocators;
226  LocatorsMap mTemporaryLocators;
228  QSet<QString> mHybridNonindexableLayers;
237  QHash<QString, double> mHybridMaxAreaPerLayer;
239  int mHybridPerLayerFeatureLimit;
240 
242  bool mIsIndexing;
243 };
244 
245 
246 #endif // QGSSNAPPINGUTILS_H
The class defines interface for querying point location:
SnapToMapMode snapToMapMode() const
Find out how the snapping to map is done.
static unsigned index
A rectangle specified with double values.
Definition: qgsrectangle.h:35
bool operator!=(const LayerConfig &other) const
virtual void prepareIndexProgress(int index)
Called when finished indexing a layer. When index == count the indexing is complete.
snap to all rendered layers (tolerance and type from defaultSettings())
QList< LayerConfig > layers() const
Query layers used for snapping.
bool operator==(const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2)
QgsVectorLayer * currentLayer() const
The current layer used if mode is SnapCurrentLayer.
bool snapOnIntersections() const
Query whether to consider intersections of nearby segments for snapping.
UnitType
Type of unit of tolerance value from settings.
Definition: qgstolerance.h:33
Interface that allows rejection of some matches in intersection queries (e.g.
void setIndexingStrategy(IndexingStrategy strategy)
Set a strategy for indexing geometry data - determines how fast and memory consuming the data structu...
bool operator==(const LayerConfig &other) const
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...
QgsTolerance::UnitType unit
The units in which the tolerance is specified.
snap according to the configuration set in setLayers()
IndexingStrategy indexingStrategy() const
Find out which strategy is used for indexing - by default hybrid indexing is used.
A class to represent a point.
Definition: qgspoint.h:117
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...
QgsVectorLayer * layer
The layer to configure.
Configures how a certain layer should be handled in a snapping operation.
Class for storing 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.
const QgsMapSettings & mapSettings() const
snap just to current layer (tolerance and type from defaultSettings())
Represents a vector layer which manages a vector based data sets.
SnapToMapMode
modes for "snap to background"