Class: QgsRectangle

class qgis.core.QgsRectangle

Bases: sip.wrapper

A rectangle specified with double values.

QgsRectangle is used to store a rectangle when double values are required. Examples are storing a layer extent or the current view extent of a map

See also

QgsBox3d

QgsRectangle()

QgsRectangle(xMin: float, yMin: float = 0, xMax: float = 0, yMax: float = 0, normalize: bool = True) Constructs a QgsRectangle from a set of x and y minimum and maximum coordinates.

The rectangle will be normalized after creation. Since QGIS 3.20, if normalize is False then the normalization step will not be applied automatically.

QgsRectangle(p1: QgsPointXY, p2: QgsPointXY, normalize: bool = True) Construct a rectangle from two points.

The rectangle is normalized after construction. Since QGIS 3.20, if normalize is False then the normalization step will not be applied automatically.

QgsRectangle(qRectF: QRectF) Construct a rectangle from a QRectF.

The rectangle is NOT normalized after construction.

QgsRectangle(other: QgsRectangle) Copy constructor

Methods

area

Returns the area of the rectangle.

asPolygon

Returns the rectangle as a polygon.

asWktCoordinates

Returns a string representation of the rectangle in WKT format.

asWktPolygon

Returns a string representation of the rectangle as a WKT Polygon.

buffered

Gets rectangle enlarged by buffer.

center

Returns the center point of the rectangle.

combineExtentWith

Expands the rectangle so that it covers both the original rectangle and the given rectangle.

contains

Returns True when rectangle contains other rectangle.

distance

Returns the distance from point to the nearest point on the boundary of the rectangle.

fromCenterAndSize

Creates a new rectangle, given the specified center point and width and height.

fromWkt

Creates a new rectangle from a wkt string.

grow

Grows the rectangle in place by the specified amount.

height

Returns the height of the rectangle.

include

Updates the rectangle to include the specified point.

intersect

Returns the intersection with the given rectangle.

intersects

Returns True when rectangle intersects with other rectangle.

invert

Swap x/y coordinates in the rectangle.

isEmpty

Returns True if the rectangle is empty.

isFinite

Returns True if the rectangle has finite boundaries.

isNull

Test if the rectangle is null (all coordinates zero or after call to setMinimal()).

normalize

Normalize the rectangle so it has non-negative width/height.

perimeter

Returns the perimeter of the rectangle.

scale

Scale the rectangle around its center point.

scaled

Scale the rectangle around its center point.

set

Sets the rectangle from two QgsPoints.

setMinimal

Set a rectangle so that min corner is at max and max corner is at min.

setXMaximum

Set the maximum x value.

setXMinimum

Set the minimum x value.

setYMaximum

Set the maximum y value.

setYMinimum

Set the minimum y value.

snappedToGrid

Returns a copy of this rectangle that is snapped to a grid with the specified spacing between the grid lines.

toBox3d

Converts the rectangle to a 3D box, with the specified zMin and zMax z values.

toRectF

Returns a QRectF with same coordinates as the rectangle.

toString

Returns a string representation of form xmin,ymin : xmax,ymax Coordinates will be truncated to the specified precision.

width

Returns the width of the rectangle.

xMaximum

Returns the x maximum value (right side of rectangle).

xMinimum

Returns the x minimum value (left side of rectangle).

yMaximum

Returns the y maximum value (top side of rectangle).

yMinimum

Returns the y minimum value (bottom side of rectangle).

area(self) float

Returns the area of the rectangle.

See also

width()

See also

height()

See also

perimeter()

New in version 3.0.

Return type

float

asPolygon(self) str

Returns the rectangle as a polygon.

Return type

str

asWktCoordinates(self) str

Returns a string representation of the rectangle in WKT format.

Return type

str

asWktPolygon(self) str

Returns a string representation of the rectangle as a WKT Polygon.

Return type

str

buffered(self, width: float) QgsRectangle

Gets rectangle enlarged by buffer.

Note

In earlier QGIS releases this method was named buffer().

See also

grow()

New in version 3.0.

Parameters

width (float) –

Return type

QgsRectangle

center(self) QgsPointXY

Returns the center point of the rectangle.

Return type

QgsPointXY

combineExtentWith(self, rect: QgsRectangle)

Expands the rectangle so that it covers both the original rectangle and the given rectangle.

combineExtentWith(self, x: float, y: float) Expands the rectangle so that it covers both the original rectangle and the given point.

combineExtentWith(self, point: QgsPointXY) Expands the rectangle so that it covers both the original rectangle and the given point.

New in version 3.2.

Parameters

rect (QgsRectangle) –

contains(self, rect: QgsRectangle) bool

Returns True when rectangle contains other rectangle.

contains(self, p: QgsPointXY) -> bool Returns True when rectangle contains a point.

contains(self, x: float, y: float) -> bool Returns True when rectangle contains the point at (x, y).

New in version 3.20.

Parameters

rect (QgsRectangle) –

Return type

bool

distance(self, point: QgsPointXY) float

Returns the distance from point to the nearest point on the boundary of the rectangle.

New in version 3.14.

Parameters

point (QgsPointXY) –

Return type

float

fromCenterAndSize(center: QgsPointXY, width: float, height: float) QgsRectangle

Creates a new rectangle, given the specified center point and width and height.

New in version 3.0.

Parameters
  • center (QgsPointXY) –

  • width (float) –

  • height (float) –

Return type

QgsRectangle

fromWkt(wkt: str) QgsRectangle

Creates a new rectangle from a wkt string. The WKT must contain only 5 vertices, representing a rectangle aligned with X and Y axes.

New in version 3.0.

Parameters

wkt (str) –

Return type

QgsRectangle

grow(self, delta: float)

Grows the rectangle in place by the specified amount.

See also

buffered()

Parameters

delta (float) –

height(self) float

Returns the height of the rectangle.

See also

width()

See also

area()

Return type

float

include(self, p: QgsPointXY)

Updates the rectangle to include the specified point.

Parameters

p (QgsPointXY) –

intersect(self, rect: QgsRectangle) QgsRectangle

Returns the intersection with the given rectangle.

Parameters

rect (QgsRectangle) –

Return type

QgsRectangle

intersects(self, rect: QgsRectangle) bool

Returns True when rectangle intersects with other rectangle.

Parameters

rect (QgsRectangle) –

Return type

bool

invert(self)

Swap x/y coordinates in the rectangle.

isEmpty(self) bool

Returns True if the rectangle is empty. An empty rectangle may still be non-null if it contains valid information (e.g. bounding box of a point).

Return type

bool

isFinite(self) bool

Returns True if the rectangle has finite boundaries. Will return False if any of the rectangle boundaries are NaN or Inf.

Return type

bool

isNull(self) bool

Test if the rectangle is null (all coordinates zero or after call to setMinimal()). A null rectangle is also an empty rectangle.

New in version 2.4.

Return type

bool

normalize(self)

Normalize the rectangle so it has non-negative width/height.

perimeter(self) float

Returns the perimeter of the rectangle.

See also

area()

New in version 3.0.

Return type

float

scale(self, scaleFactor: float, c: QgsPointXY = None)

Scale the rectangle around its center point.

scale(self, scaleFactor: float, centerX: float, centerY: float) Scale the rectangle around its center point.

Parameters
  • scaleFactor (float) –

  • c (QgsPointXY = None) –

scaled(self, scaleFactor: float, center: QgsPointXY = None) QgsRectangle

Scale the rectangle around its center point.

New in version 3.4.

Parameters
  • scaleFactor (float) –

  • center (QgsPointXY = None) –

Return type

QgsRectangle

set(self, p1: QgsPointXY, p2: QgsPointXY, normalize: bool = True)

Sets the rectangle from two QgsPoints.

The rectangle is normalised after construction. Since QGIS 3.20, if normalize is False then the normalization step will not be applied automatically.

set(self, xMin: float, yMin: float, xMax: float, yMax: float, normalize: bool = True) Sets the rectangle from four points.

The rectangle is normalised after construction. Since QGIS 3.20, if normalize is False then the normalization step will not be applied automatically.

Parameters
setMinimal(self)

Set a rectangle so that min corner is at max and max corner is at min. It is NOT normalized.

setXMaximum(self, x: float)

Set the maximum x value.

Parameters

x (float) –

setXMinimum(self, x: float)

Set the minimum x value.

Parameters

x (float) –

setYMaximum(self, y: float)

Set the maximum y value.

Parameters

y (float) –

setYMinimum(self, y: float)

Set the minimum y value.

Parameters

y (float) –

snappedToGrid(self, spacing: float) QgsRectangle

Returns a copy of this rectangle that is snapped to a grid with the specified spacing between the grid lines.

New in version 3.4.

Parameters

spacing (float) –

Return type

QgsRectangle

toBox3d(self, zMin: float, zMax: float) QgsBox3d

Converts the rectangle to a 3D box, with the specified zMin and zMax z values.

New in version 3.0.

Parameters
  • zMin (float) –

  • zMax (float) –

Return type

QgsBox3d

toRectF(self) QRectF

Returns a QRectF with same coordinates as the rectangle.

Return type

QRectF

toString(self, precision: int = 16) str

Returns a string representation of form xmin,ymin : xmax,ymax Coordinates will be truncated to the specified precision. If the specified precision is less than 0, a suitable minimum precision is used.

Parameters

precision (int = 16) –

Return type

str

width(self) float

Returns the width of the rectangle.

See also

height()

See also

area()

Return type

float

xMaximum(self) float

Returns the x maximum value (right side of rectangle).

Return type

float

xMinimum(self) float

Returns the x minimum value (left side of rectangle).

Return type

float

yMaximum(self) float

Returns the y maximum value (top side of rectangle).

Return type

float

yMinimum(self) float

Returns the y minimum value (bottom side of rectangle).

Return type

float