QGIS API Documentation  2.3.0-Master
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
qgstolerance.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgstolerance.cpp - wrapper for tolerance handling
3  ----------------------
4  begin : March 2009
5  copyright : (C) 2009 by Richard Kostecky
6  email : csf.kostej 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 #include "qgstolerance.h"
17 #include <QSettings>
18 #include <QPoint>
19 #include <cmath>
20 
21 double QgsTolerance::toleranceInMapUnits( double tolerance, QgsMapLayer *layer, const QgsMapSettings& mapSettings, QgsTolerance::UnitType units )
22 {
23  if ( units == MapUnits )
24  {
25  return tolerance;
26  }
27  double mapUnitsPerPixel = computeMapUnitPerPixel( layer, mapSettings );
28  return tolerance * mapUnitsPerPixel;
29 }
30 
31 double QgsTolerance::toleranceInMapUnits( double tolerance, QgsMapLayer* layer, QgsMapRenderer* renderer, UnitType units )
32 {
33  return toleranceInMapUnits( tolerance, layer, renderer->mapSettings(), units );
34 }
35 
36 double QgsTolerance::vertexSearchRadius( QgsMapLayer *layer, const QgsMapSettings &mapSettings )
37 {
38  QSettings settings;
39  double tolerance = settings.value( "/qgis/digitizing/search_radius_vertex_edit", 10 ).toDouble();
40  UnitType units = ( QgsTolerance::UnitType ) settings.value( "/qgis/digitizing/search_radius_vertex_edit_unit", QgsTolerance::Pixels ).toInt();
41  return toleranceInMapUnits( tolerance, layer, mapSettings, units );
42 }
43 
45 {
46  return vertexSearchRadius( layer, renderer->mapSettings() );
47 }
48 
49 double QgsTolerance::defaultTolerance( QgsMapLayer *layer, const QgsMapSettings& mapSettings )
50 {
51  QSettings settings;
52  double tolerance = settings.value( "/qgis/digitizing/default_snapping_tolerance", 0 ).toDouble();
53  UnitType units = ( QgsTolerance::UnitType ) settings.value( "/qgis/digitizing/default_snapping_tolerance_unit", 0 ).toInt();
54  return toleranceInMapUnits( tolerance, layer, mapSettings, units );
55 }
56 
57 
59 {
60  return defaultTolerance( layer, renderer->mapSettings() );
61 }
62 
63 
65 {
66  if ( ! mapSettings.hasCrsTransformEnabled() )
67  {
68  // if the on-the-fly projections are not enabled, layer units pre pixel are the same as map units per pixel
69  return mapSettings.mapUnitsPerPixel();
70  }
71 
72  // the layer is projected. Find out how many pixels are in one map unit - either horizontal and vertical direction
73  // this check might not work correctly in some cases
74  // (on a large area the pixels projected around "0,0" can have different properties from the actual point)
75  QgsPoint p1 = toLayerCoordinates( layer, mapSettings, QPoint( 0, 1 ) );
76  QgsPoint p2 = toLayerCoordinates( layer, mapSettings, QPoint( 0, 2 ) );
77  QgsPoint p3 = toLayerCoordinates( layer, mapSettings, QPoint( 1, 0 ) );
78  QgsPoint p4 = toLayerCoordinates( layer, mapSettings, QPoint( 2, 0 ) );
79  double x = p1.sqrDist( p2 );
80  double y = p3.sqrDist( p4 );
81  if ( x > y )
82  {
83  return sqrt( x );
84  }
85  else
86  {
87  return sqrt( y );
88  }
89 }
90 
91 
92 QgsPoint QgsTolerance::toLayerCoordinates( QgsMapLayer* layer, const QgsMapSettings& mapSettings, const QPoint& point )
93 {
94  QgsPoint pt = mapSettings.mapToPixel().toMapCoordinates( point );
95  return mapSettings.mapToLayerCoordinates( layer, pt );
96 }
const QgsMapSettings & mapSettings()
bridge to QgsMapSettings
Base class for all map layer types.
Definition: qgsmaplayer.h:46
static double toleranceInMapUnits(double tolerance, QgsMapLayer *layer, const QgsMapSettings &mapSettings, UnitType units=MapUnits)
Static function to translate tolerance value into current map unit value.
UnitType
Type of unit of tolerance value from settings.
Definition: qgstolerance.h:33
bool hasCrsTransformEnabled() const
returns true if projections are enabled for this layer set
const QgsMapToPixel & mapToPixel() const
A non GUI class for rendering a map layer set onto a QPainter.
QgsPoint mapToLayerCoordinates(QgsMapLayer *theLayer, QgsPoint point) const
transform point coordinates from output CRS to layer's CRS
double sqrDist(double x, double y) const
Returns the squared distance between this point and x,y.
Definition: qgspoint.cpp:186
Pixels unit of tolerance.
Definition: qgstolerance.h:38
Map unit value.
Definition: qgstolerance.h:36
double mapUnitsPerPixel() const
static double defaultTolerance(QgsMapLayer *layer, const QgsMapSettings &mapSettings)
Static function to get default tolerance value for a layer.
A class to represent a point geometry.
Definition: qgspoint.h:63
QgsPoint toMapCoordinates(int x, int y) const
static QgsPoint toLayerCoordinates(QgsMapLayer *layer, const QgsMapSettings &mapSettings, const QPoint &point)
static double computeMapUnitPerPixel(QgsMapLayer *layer, const QgsMapSettings &mapSettings)
static double vertexSearchRadius(QgsMapLayer *layer, const QgsMapSettings &mapSettings)
Static function to get vertex tolerance value for a layer.