QGIS API Documentation 3.99.0-Master (21b3aa880ba)
Loading...
Searching...
No Matches
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 "qgsscalecalculator.h"
20
21#include <cmath>
22
23#include "qgslogger.h"
24#include "qgsrectangle.h"
25#include "qgsunittypes.h"
26
27#include <QSizeF>
28
33
38
40{
41 mDpi = dpi;
42}
44{
45 return mDpi;
46}
47
49{
50 QgsDebugMsgLevel( QStringLiteral( "Map units set to %1" ).arg( qgsEnumValueToKey( mapUnits ) ), 3 );
51 mMapUnits = mapUnits;
52}
53
55{
56 QgsDebugMsgLevel( QStringLiteral( "Map units returned as %1" ).arg( qgsEnumValueToKey( mMapUnits ) ), 4 );
57 return mMapUnits;
58}
59
60double QgsScaleCalculator::calculate( const QgsRectangle &mapExtent, double canvasWidth ) const
61{
62 if ( qgsDoubleNear( canvasWidth, 0. ) || qgsDoubleNear( mDpi, 0.0 ) )
63 {
64 QgsDebugError( QStringLiteral( "Can't calculate scale from the input values" ) );
65 return 0;
66 }
67
68 double conversionFactor = 0;
69 double delta = 0;
70 calculateMetrics( mapExtent, delta, conversionFactor );
71
72 const double scale = ( delta * conversionFactor ) / ( static_cast< double >( canvasWidth ) / mDpi );
73 QgsDebugMsgLevel( QStringLiteral( "scale = %1 conversionFactor = %2" ).arg( scale ).arg( conversionFactor ), 4 );
74 return scale;
75}
76
77QSizeF QgsScaleCalculator::calculateImageSize( const QgsRectangle &mapExtent, double scale ) const
78{
79 if ( qgsDoubleNear( scale, 0.0 ) || qgsDoubleNear( mDpi, 0.0 ) )
80 {
81 QgsDebugError( QStringLiteral( "Can't calculate image size from the input values" ) );
82 return QSizeF();
83 }
84 double conversionFactor = 0;
85 double delta = 0;
86
87 calculateMetrics( mapExtent, delta, conversionFactor );
88 const double imageWidth = ( delta * conversionFactor ) / ( static_cast< double >( scale ) ) * mDpi;
89 const double deltaHeight = ( mapExtent.yMaximum() - mapExtent.yMinimum() ) * delta / ( mapExtent.xMaximum() - mapExtent.xMinimum() );
90 const double imageHeight = ( deltaHeight * conversionFactor ) / ( static_cast< double >( scale ) ) * mDpi;
91
92 QgsDebugMsgLevel( QStringLiteral( "imageWidth = %1 imageHeight = %2 conversionFactor = %3" )
93 .arg( imageWidth ).arg( imageHeight ).arg( conversionFactor ), 4 );
94
95 return QSizeF( imageWidth, imageHeight );
96}
97
98void QgsScaleCalculator::calculateMetrics( const QgsRectangle &mapExtent, double &delta, double &conversionFactor ) const
99{
100 delta = mapExtent.xMaximum() - mapExtent.xMinimum();
101
102 switch ( mMapUnits )
103 {
105 conversionFactor = 1;
106 break;
107
155 // convert to inches
156 conversionFactor = QgsUnitTypes::fromUnitToUnitFactor( mMapUnits, Qgis::DistanceUnit::Inches );
157 break;
158
160 // assume degrees to maintain old API
161 [[fallthrough]];
162
164 // degrees require conversion to meters first
165 conversionFactor = 39.3700787;
166 delta = calculateGeographicDistance( mapExtent );
167 break;
168 }
169}
170
172{
173 switch ( mMethod )
174 {
177 mapExtent.xMinimum(), mapExtent.xMaximum() );
178
180 return calculateGeographicDistanceAtLatitude( ( mapExtent.yMaximum() + mapExtent.yMinimum() ) * 0.5,
181 mapExtent.xMinimum(), mapExtent.xMaximum() );
182
185 mapExtent.xMinimum(), mapExtent.xMaximum() );
186
188 {
189 const double dTop = calculateGeographicDistanceAtLatitude( mapExtent.yMaximum(),
190 mapExtent.xMinimum(), mapExtent.xMaximum() );
191 const double dMiddle = calculateGeographicDistanceAtLatitude( ( mapExtent.yMaximum() + mapExtent.yMinimum() ) * 0.5,
192 mapExtent.xMinimum(), mapExtent.xMaximum() );
193 const double dBottom = calculateGeographicDistanceAtLatitude( mapExtent.yMinimum(),
194 mapExtent.xMinimum(), mapExtent.xMaximum() );
195 return ( dTop + dMiddle + dBottom ) / 3.0;
196 }
197
200 mapExtent.xMinimum(), mapExtent.xMaximum() );
201 }
202 // unreachable!
203 return 0;
204}
205
206double QgsScaleCalculator::calculateGeographicDistanceAtLatitude( double lat, double longitude1, double longitude2 ) const
207{
208 // need to calculate the x distance in meters
209
210 // Note this is an approximation (although very close) but calculating scale
211 // for geographic data over large extents is quasi-meaningless
212
213 // The distance between two points on a sphere can be estimated
214 // using the Haversine formula. This gives the shortest distance
215 // between two points on the sphere. However, what we're after is
216 // the distance from the left of the given extent and the right of
217 // it. This is not necessarily the shortest distance between two
218 // points on a sphere.
219 //
220 // The code below uses the Haversine formula, but with some changes
221 // to cope with the above problem, and also to deal with the extent
222 // possibly extending beyond +/-180 degrees:
223 //
224 // - Use the Halversine formula to calculate the distance from -90 to
225 // +90 degrees at desired latitude.
226 // - Scale this distance by the number of degrees between
227 // the two longitudes
228
229
230 // - TODO: respect the actual ellipsoid parameters!!
231
232 // For a longitude change of 180 degrees
233 static const double RADS = ( 4.0 * std::atan( 1.0 ) ) / 180.0;
234 const double a = std::pow( std::cos( lat * RADS ), 2 );
235 const double c = 2.0 * std::atan2( std::sqrt( a ), std::sqrt( 1.0 - a ) );
236 static const double RA = 6378000; // [m]
237 // The eccentricity. This comes from sqrt(1.0 - rb*rb/(ra*ra)) with rb set
238 // to 6357000 m.
239 static const double E = 0.0810820288;
240 const double radius = RA * ( 1.0 - E * E ) /
241 std::pow( 1.0 - E * E * std::sin( lat * RADS ) * std::sin( lat * RADS ), 1.5 );
242 const double meters = ( longitude2 - longitude1 ) / 180.0 * radius * c;
243
244 QgsDebugMsgLevel( "Distance across map extent (m): " + QString::number( meters ), 4 );
245
246 return meters;
247}
DistanceUnit
Units of distance.
Definition qgis.h:5013
@ YardsBritishSears1922Truncated
British yards (Sears 1922 truncated).
Definition qgis.h:5053
@ Feet
Imperial feet.
Definition qgis.h:5016
@ MilesUSSurvey
US Survey miles.
Definition qgis.h:5060
@ LinksBritishSears1922
British links (Sears 1922).
Definition qgis.h:5048
@ YardsBritishBenoit1895A
British yards (Benoit 1895 A).
Definition qgis.h:5051
@ LinksBritishBenoit1895A
British links (Benoit 1895 A).
Definition qgis.h:5045
@ Centimeters
Centimeters.
Definition qgis.h:5021
@ YardsIndian1975
Indian yards (1975).
Definition qgis.h:5059
@ FeetUSSurvey
US Survey feet.
Definition qgis.h:5043
@ Millimeters
Millimeters.
Definition qgis.h:5022
@ FeetBritishSears1922
British feet (Sears 1922).
Definition qgis.h:5036
@ YardsClarkes
Clarke's yards.
Definition qgis.h:5055
@ YardsIndian
Indian yards.
Definition qgis.h:5056
@ FeetBritishBenoit1895B
British feet (Benoit 1895 B).
Definition qgis.h:5034
@ Miles
Terrestrial miles.
Definition qgis.h:5019
@ LinksUSSurvey
US Survey links.
Definition qgis.h:5050
@ Meters
Meters.
Definition qgis.h:5014
@ ChainsUSSurvey
US Survey chains.
Definition qgis.h:5030
@ FeetClarkes
Clarke's feet.
Definition qgis.h:5037
@ Unknown
Unknown distance unit.
Definition qgis.h:5063
@ Yards
Imperial yards.
Definition qgis.h:5018
@ FeetBritish1936
British feet (1936).
Definition qgis.h:5032
@ FeetIndian1962
Indian feet (1962).
Definition qgis.h:5041
@ YardsBritishSears1922
British yards (Sears 1922).
Definition qgis.h:5054
@ FeetIndian1937
Indian feet (1937).
Definition qgis.h:5040
@ YardsIndian1937
Indian yards (1937).
Definition qgis.h:5057
@ Degrees
Degrees, for planar geographic CRS distance measurements.
Definition qgis.h:5020
@ ChainsBritishBenoit1895B
British chains (Benoit 1895 B).
Definition qgis.h:5026
@ LinksBritishSears1922Truncated
British links (Sears 1922 truncated).
Definition qgis.h:5047
@ ChainsBritishBenoit1895A
British chains (Benoit 1895 A).
Definition qgis.h:5025
@ YardsBritishBenoit1895B
British yards (Benoit 1895 B).
Definition qgis.h:5052
@ FeetBritish1865
British feet (1865).
Definition qgis.h:5031
@ YardsIndian1962
Indian yards (1962).
Definition qgis.h:5058
@ FeetBritishSears1922Truncated
British feet (Sears 1922 truncated).
Definition qgis.h:5035
@ MetersGermanLegal
German legal meter.
Definition qgis.h:5062
@ LinksBritishBenoit1895B
British links (Benoit 1895 B).
Definition qgis.h:5046
@ ChainsInternational
International chains.
Definition qgis.h:5024
@ Inches
Inches.
Definition qgis.h:5023
@ Fathoms
Fathoms.
Definition qgis.h:5061
@ LinksInternational
International links.
Definition qgis.h:5044
@ ChainsBritishSears1922Truncated
British chains (Sears 1922 truncated).
Definition qgis.h:5027
@ FeetIndian
Indian (geodetic) feet.
Definition qgis.h:5039
@ NauticalMiles
Nautical miles.
Definition qgis.h:5017
@ ChainsClarkes
Clarke's chains.
Definition qgis.h:5029
@ LinksClarkes
Clarke's links.
Definition qgis.h:5049
@ ChainsBritishSears1922
British chains (Sears 1922).
Definition qgis.h:5028
@ Kilometers
Kilometers.
Definition qgis.h:5015
@ FeetIndian1975
Indian feet (1975).
Definition qgis.h:5042
@ FeetGoldCoast
Gold Coast feet.
Definition qgis.h:5038
@ FeetBritishBenoit1895A
British feet (Benoit 1895 A).
Definition qgis.h:5033
ScaleCalculationMethod
Scale calculation logic.
Definition qgis.h:5285
@ HorizontalTop
Calculate horizontally, across top of map.
Definition qgis.h:5286
@ HorizontalMiddle
Calculate horizontally, across midle of map.
Definition qgis.h:5287
@ AtEquator
Always calculate the scale at the equator, regardless of the actual visible map extent....
Definition qgis.h:5290
@ HorizontalAverage
Calculate horizontally, using the average of the top, middle and bottom scales.
Definition qgis.h:5289
@ HorizontalBottom
Calculate horizontally, across bottom of map.
Definition qgis.h:5288
A rectangle specified with double values.
double xMinimum
double yMinimum
double xMaximum
double yMaximum
double calculate(const QgsRectangle &mapExtent, double canvasWidth) const
Calculate the scale denominator.
void setDpi(double dpi)
Sets the dpi (dots per inch) for the output resolution, to be used in scale calculations.
void setMapUnits(Qgis::DistanceUnit mapUnits)
Set the map units.
double calculateGeographicDistance(const QgsRectangle &mapExtent) const
Calculate the distance in meters, horizontally across the specified map extent (in geographic coordin...
QSizeF calculateImageSize(const QgsRectangle &mapExtent, double scale) const
Calculate the image size in pixel (physical) units.
Qgis::ScaleCalculationMethod method() const
Returns the method to use for map scale calculations.
double dpi() const
Returns the DPI (dots per inch) used in scale calculations.
Qgis::DistanceUnit mapUnits() const
Returns current map units.
void setMethod(Qgis::ScaleCalculationMethod method)
Sets the method to use for map scale calculations.
QgsScaleCalculator(double dpi=0, Qgis::DistanceUnit mapUnits=Qgis::DistanceUnit::Meters)
Constructor.
double calculateGeographicDistanceAtLatitude(double latitude, double longitude1, double longitude2) const
Calculate the distance in meters, horizontally between two longitudes at a specified latitude.
static Q_INVOKABLE double fromUnitToUnitFactor(Qgis::DistanceUnit fromUnit, Qgis::DistanceUnit toUnit)
Returns the conversion factor between the specified distance units.
As part of the API refactoring and improvements which landed in the Processing API was substantially reworked from the x version This was done in order to allow much of the underlying Processing framework to be ported into c
QString qgsEnumValueToKey(const T &value, bool *returnOk=nullptr)
Returns the value for the given key of an enum.
Definition qgis.h:6798
bool qgsDoubleNear(double a, double b, double epsilon=4 *std::numeric_limits< double >::epsilon())
Compare two doubles (but allow some difference).
Definition qgis.h:6607
#define QgsDebugMsgLevel(str, level)
Definition qgslogger.h:61
#define QgsDebugError(str)
Definition qgslogger.h:57