Class: QgsMapToPixel

Perform transforms between map coordinates and device coordinates.

This class can convert device coordinates to map coordinates and vice versa.

Methods

isValid

Returns True if the object is valid (i.e. it has parameters set), or False if the object is default constructed with no parameters set.

mapHeight

Returns current map height in pixels

mapRotation

Returns the current map rotation in degrees (clockwise).

mapUnitsPerPixel

Returns the current map units per pixel.

mapWidth

Returns the current map width in pixels.

setMapRotation

Sets map rotation in degrees (clockwise).

setMapUnitsPerPixel

Sets the map units per pixel.

setParameters

Sets parameters for use in transforming coordinates.

showParameters

Returns a string representation of the parameters used in the transform.

toMapCoordinates

Transforms device coordinates to map (world) coordinates.

toMapCoordinatesF

Transforms device coordinates to map (world) coordinates.

toMapPoint

Transforms device coordinates to map (world) coordinates.

transform

Transforms a point p from map (world) coordinates to device coordinates.

transformBounds

Transforms a bounding box from map coordinates to device coordinates.

transformInPlace

Transforms map coordinates to device coordinates.

xCenter

Returns the center x-coordinate for the transform.

yCenter

Returns the center y-coordinate for the transform.

Static Methods

fromScale

Returns a new QgsMapToPixel created using a specified scale and distance unit.

class qgis.core.QgsMapToPixel[source]

Bases: object

__init__()

Constructor for an invalid QgsMapToPixel.

A manual call to setParameters() is required to initialize the object.

__init__(mapUnitsPerPixel: float, centerX: float, centerY: float, widthPixels: int, heightPixels: int, rotation: float)

Constructor

Parameters:
  • mapUnitsPerPixel (float) – Map units per pixel

  • centerX (float) – X coordinate of map center, in geographical units

  • centerY (float) – Y coordinate of map center, in geographical units

  • widthPixels (int) – Output width, in pixels

  • heightPixels (int) – Output height, in pixels

  • rotation (float) – clockwise rotation in degrees

__init__(mapUnitsPerPixel: float)

Constructor

Parameters:

mapUnitsPerPixel (float) – Map units per pixel

__init__(a0: QgsMapToPixel)
Parameters:

a0 (QgsMapToPixel)

static fromScale(scale: float, mapUnits: Qgis.DistanceUnit, dpi: float = 96) QgsMapToPixel[source]

Returns a new QgsMapToPixel created using a specified scale and distance unit.

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

  • dpi (float = 96) – screen DPI

  • mapUnits (Qgis.DistanceUnit) – map units

Return type:

QgsMapToPixel

Returns:

matching QgsMapToPixel

isValid(self) bool[source]

Returns True if the object is valid (i.e. it has parameters set), or False if the object is default constructed with no parameters set.

Added in version 3.22.

Return type:

bool

mapHeight(self) int[source]

Returns current map height in pixels

See also

mapWidth()

Return type:

int

mapRotation(self) float[source]

Returns the current map rotation in degrees (clockwise).

See also

setMapRotation()

Return type:

float

mapUnitsPerPixel(self) float[source]

Returns the current map units per pixel.

Return type:

float

mapWidth(self) int[source]

Returns the current map width in pixels.

The information is only known if setRotation was used.

See also

mapHeight()

Return type:

int

setMapRotation(self, degrees: float, cx: float, cy: float)[source]

Sets map rotation in degrees (clockwise).

Calling this method will automatically set the object as valid.

Parameters:
  • degrees (float) – clockwise rotation in degrees

  • cx (float) – X ordinate of map center in geographical units

  • cy (float) – Y ordinate of map center in geographical units

See also

mapRotation()

setMapUnitsPerPixel(self, mapUnitsPerPixel: float)[source]

Sets the map units per pixel.

Calling this method will automatically set the object as valid.

Parameters:

mapUnitsPerPixel (float) – Map units per pixel

setParameters(self, mapUnitsPerPixel: float, centerX: float, centerY: float, widthPixels: int, heightPixels: int, rotation: float)[source]

Sets parameters for use in transforming coordinates.

Calling this method will automatically set the object as valid.

Parameters:
  • mapUnitsPerPixel (float) – Map units per pixel

  • centerX (float) – X coordinate of map center, in geographical units

  • centerY (float) – Y coordinate of map center, in geographical units

  • widthPixels (int) – Output width, in pixels

  • heightPixels (int) – Output height, in pixels

  • rotation (float) – clockwise rotation in degrees

Note

if the specified parameters result in an invalid transform then no changes will be applied to the object

showParameters(self) str[source]

Returns a string representation of the parameters used in the transform.

Return type:

str

toMapCoordinates(self, x: int, y: int) QgsPointXY[source]

Transforms device coordinates to map (world) coordinates.

Parameters:
  • x (int)

  • y (int)

Return type:

QgsPointXY

toMapCoordinates(self, p: QPoint) QgsPointXY[source]

Transforms device coordinates to map (world) coordinates.

Parameters:

p (QPoint) – Point to be converted to map cooordinates

Return type:

QgsPointXY

Returns:

QgsPointXY in map coorndiates

toMapCoordinatesF(self, x: float, y: float) QgsPointXY

Transforms device coordinates to map (world) coordinates.

Parameters:
  • x (float)

  • y (float)

Return type:

QgsPointXY

toMapPoint(self, x: float, y: float) QgsPointXY[source]

Transforms device coordinates to map (world) coordinates.

Deprecated since version 3.4: Use toMapCoordinates() instead.

Parameters:
  • x (float)

  • y (float)

Return type:

QgsPointXY

transform(self, p: QgsPointXY) QgsPointXY[source]

Transforms a point p from map (world) coordinates to device coordinates.

Parameters:

p (QgsPointXY) – Point to transform

Return type:

QgsPointXY

Returns:

QgsPointXY in device coordinates

transform(self, p: QgsPointXY | None)[source]

Transforms a point p from map (world) coordinates to device coordinates in place.

Parameters:

p (Optional[QgsPointXY])

transform(self, x: float, y: float) QgsPointXY[source]

Transforms the point specified by x,y from map (world) coordinates to device coordinates.

Parameters:
  • x (float) – x coordinate of point to transform

  • y (float) – y coordinate of point to transform

Return type:

QgsPointXY

Returns:

QgsPointXY in device coordinates

transform(self) QTransform[source]

Returns a QTransform encapsulating the map to pixel conversion.

Return type:

QTransform

transformBounds(self, bounds: QRectF) QRectF[source]

Transforms a bounding box from map coordinates to device coordinates.

The returns bounding box will always completely enclose the transformed input bounding box (i.e. this method will grow the bounds wherever required).

Added in version 3.40.

Parameters:

bounds (QRectF)

Return type:

QRectF

transformInPlace(self)[source]

Transforms map coordinates to device coordinates.

This method modifies the given coordinates in place. It is intended as a fast way to do the transform.

xCenter(self) float[source]

Returns the center x-coordinate for the transform.

See also

yCenter()

Return type:

float

yCenter(self) float[source]

Returns the center y-coordinate for the transform.

See also

xCenter()

Return type:

float