QGIS API Documentation  2.0.1-Dufour
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
qgsscalecalculator.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsscalecalculator.h
3  Calculates scale based on map extent and units
4  -------------------
5  begin : May 18, 2004
6  copyright : (C) 2004 by Gary E.Sherman
7  email : sherman at mrcc.com
8  ***************************************************************************/
9 
10 /***************************************************************************
11  * *
12  * This program is free software; you can redistribute it and/or modify *
13  * it under the terms of the GNU General Public License as published by *
14  * the Free Software Foundation; either version 2 of the License, or *
15  * (at your option) any later version. *
16  * *
17  ***************************************************************************/
18 
19 #include <cmath>
20 #include "qgslogger.h"
21 #include "qgsrectangle.h"
22 #include "qgsscalecalculator.h"
23 
25  : mDpi( dpi ), mMapUnits( mapUnits )
26 {}
27 
29 {}
30 
31 void QgsScaleCalculator::setDpi( double dpi )
32 {
33  mDpi = dpi;
34 }
36 {
37  return mDpi;
38 }
39 
41 {
42  QgsDebugMsg( QString( "Map units set to %1" ).arg( QString::number( mapUnits ) ) );
44 }
45 
47 {
48  QgsDebugMsgLevel( QString( "Map units returned as %1" ).arg( QString::number( mMapUnits ) ), 4 );
49  return mMapUnits;
50 }
51 
52 double QgsScaleCalculator::calculate( const QgsRectangle &mapExtent, int canvasWidth )
53 {
54  double conversionFactor = 0;
55  double delta = 0;
56  // calculation is based on the map units and extent, the dpi of the
57  // users display, and the canvas width
58  switch ( mMapUnits )
59  {
60  case QGis::Meters:
61  // convert meters to inches
62  conversionFactor = 39.3700787;
63  delta = mapExtent.xMaximum() - mapExtent.xMinimum();
64  break;
65  case QGis::Feet:
66  conversionFactor = 12.0;
67  delta = mapExtent.xMaximum() - mapExtent.xMinimum();
68  break;
69 
70  default:
71  case QGis::Degrees:
72  // degrees require conversion to meters first
73  conversionFactor = 39.3700787;
74  delta = calculateGeographicDistance( mapExtent );
75  break;
76  }
77  if ( canvasWidth == 0 || mDpi == 0 )
78  {
79  QgsDebugMsg( "Can't calculate scale from the input values" );
80  return 0;
81  }
82  double scale = ( delta * conversionFactor ) / (( double )canvasWidth / mDpi );
83  QgsDebugMsg( QString( "scale = %1 conversionFactor = %2" ).arg( scale ).arg( conversionFactor ) );
84  return scale;
85 }
86 
87 
89 {
90  // need to calculate the x distance in meters
91  // We'll use the middle latitude for the calculation
92  // Note this is an approximation (although very close) but calculating scale
93  // for geographic data over large extents is quasi-meaningless
94 
95  // The distance between two points on a sphere can be estimated
96  // using the Haversine formula. This gives the shortest distance
97  // between two points on the sphere. However, what we're after is
98  // the distance from the left of the given extent and the right of
99  // it. This is not necessarily the shortest distance between two
100  // points on a sphere.
101  //
102  // The code below uses the Haversine formula, but with some changes
103  // to cope with the above problem, and also to deal with the extent
104  // possibly extending beyond +/-180 degrees:
105  //
106  // - Use the Halversine formula to calculate the distance from -90 to
107  // +90 degrees at the mean latitude.
108  // - Scale this distance by the number of degrees between
109  // mapExtent.xMinimum() and mapExtent.xMaximum();
110  // - For a slight improvemnt, allow for the ellipsoid shape of earth.
111 
112 
113  // For a longitude change of 180 degrees
114  double lat = ( mapExtent.yMaximum() + mapExtent.yMinimum() ) * 0.5;
115  const static double rads = ( 4.0 * atan( 1.0 ) ) / 180.0;
116  double a = pow( cos( lat * rads ), 2 );
117  double c = 2.0 * atan2( sqrt( a ), sqrt( 1.0 - a ) );
118  const static double ra = 6378000; // [m]
119  // The eccentricity. This comes from sqrt(1.0 - rb*rb/(ra*ra)) with rb set
120  // to 6357000 m.
121  const static double e = 0.0810820288;
122  double radius = ra * ( 1.0 - e * e ) /
123  pow( 1.0 - e * e * sin( lat * rads ) * sin( lat * rads ), 1.5 );
124  double meters = ( mapExtent.xMaximum() - mapExtent.xMinimum() ) / 180.0 * radius * c;
125 
126  QgsDebugMsg( "Distance across map extent (m): " + QString::number( meters ) );
127 
128  return meters;
129 }