Class: QgsScaleCalculator

Calculates scale for a given combination of canvas size, map extent, and monitor dpi.

The calculation defaults to using the scale calculated horizontally across the center of the map, however this may be changed by calling setMethod().

Methods

calculate

Calculate the scale denominator.

calculateGeographicDistance

Calculate the distance in meters, horizontally across the specified map extent (in geographic coordinates).

calculateGeographicDistanceAtLatitude

Calculate the distance in meters, horizontally between two longitudes at a specified latitude.

calculateImageSize

Calculate the image size in pixel (physical) units.

dpi

Returns the DPI (dots per inch) used in scale calculations.

mapUnits

Returns current map units.

method

Returns the method to use for map scale calculations.

setDpi

Sets the dpi (dots per inch) for the output resolution, to be used in scale calculations.

setMapUnits

Set the map units.

setMethod

Sets the method to use for map scale calculations.

class qgis.core.QgsScaleCalculator[source]

Bases: object

__init__(dpi: float = 0, mapUnits: Qgis.DistanceUnit = Qgis.DistanceUnit.Meters)

Constructor

Parameters:
  • dpi (float = 0) – Monitor resolution in dots per inch

  • mapUnits (Qgis.DistanceUnit = Qgis.DistanceUnit.Meters) – Units of the data on the map

__init__(a0: QgsScaleCalculator)
Parameters:

a0 (QgsScaleCalculator)

calculate(self, mapExtent: QgsRectangle, canvasWidth: float) float[source]

Calculate the scale denominator.

Parameters:
  • mapExtent (QgsRectangle) – QgsRectangle containing the current map extent. Units are specified by mapUnits().

  • canvasWidth (float) – Width of the map canvas in pixel (physical) units

Return type:

float

Returns:

scale denominator of current map view, e.g. 1000.0 for a 1:1000 map.

calculateGeographicDistance(self, mapExtent: QgsRectangle) float[source]

Calculate the distance in meters, horizontally across the specified map extent (in geographic coordinates).

Used to calculate scale for map views with geographic (decimal degree) data.

This method respects the scale method().

Parameters:

mapExtent (QgsRectangle) – the map extent, in geographic (degrees) coordinates

Return type:

float

Returns:

distance in meters across the map extent

calculateGeographicDistanceAtLatitude(self, latitude: float, longitude1: float, longitude2: float) float[source]

Calculate the distance in meters, horizontally between two longitudes at a specified latitude.

Used to calculate scale for map views with geographic (decimal degree) data.

Parameters:
  • latitude (float) – latitude (in degrees) to calculate distance at

  • longitude1 (float) – first longitude (left side of map, or x-minimum) to calculate distance between

  • longitude2 (float) – second longitude (right side of map, or x-maximum) to calculate distance between

Return type:

float

Returns:

distance in meters between the two longitudes

Added in version 3.44.

calculateImageSize(self, mapExtent: QgsRectangle, scale: float) QSizeF[source]

Calculate the image size in pixel (physical) units.

Parameters:
  • mapExtent (QgsRectangle) – QgsRectangle containing the current map extent. Units are specified by mapUnits()

  • scale (float) – Scale denominator, e.g. 1000.0 for a 1:1000 map

Return type:

QSizeF

Returns:

image size

Added in version 3.24.

dpi(self) float[source]

Returns the DPI (dots per inch) used in scale calculations.

See also

setDpi()

Return type:

float

mapUnits(self) Qgis.DistanceUnit[source]

Returns current map units.

See also

setMapUnits()

Return type:

Qgis.DistanceUnit

method(self) Qgis.ScaleCalculationMethod[source]

Returns the method to use for map scale calculations.

See also

setMethod()

Added in version 3.44.

Return type:

Qgis.ScaleCalculationMethod

setDpi(self, dpi: float)[source]

Sets the dpi (dots per inch) for the output resolution, to be used in scale calculations.

See also

dpi()

Parameters:

dpi (float)

setMapUnits(self, mapUnits: Qgis.DistanceUnit)[source]

Set the map units.

See also

mapUnits()

Parameters:

mapUnits (Qgis.DistanceUnit)

setMethod(self, method: Qgis.ScaleCalculationMethod)[source]

Sets the method to use for map scale calculations.

See also

method()

Added in version 3.44.

Parameters:

method (Qgis.ScaleCalculationMethod)