Class: QgsRectangle

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

List of all members, including inherited members

Class Hierarchy

Inheritance diagram of qgis.core.QgsRectangle

Subclasses

QgsReferencedRectangle

A QgsRectangle with associated coordinate reference system.

Constructor

__init__

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.

bottom

Returns the bottom side of the rectangle.

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.

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 has no area.

isFinite

Returns True if the rectangle has finite boundaries.

isMaximal

Test if the rectangle is the maximal possible rectangle.

isNormalized

Returns True if the rectangle is normalized.

isNull

Test if the rectangle is null (holding no spatial information).

isPoint

Returns True if the rectangle represents a point.

isValid

Test if the rectangle is valid

left

Returns the left side of the rectangle.

normalize

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

perimeter

Returns the perimeter of the rectangle.

right

Returns the right side 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.

setNull

Mark a rectangle as being null (holding no spatial information).

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.

splitAtAntimeridian

Splits the rectangle at the antimeridian (longitude 180 degrees).

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 rounded to the specified precision.

top

Returns the top side of the rectangle.

width

Returns the width of the rectangle.

xMaximum

Returns the x maximum value.

xMinimum

Returns the x minimum value.

yMaximum

Returns the y maximum value.

yMinimum

Returns the y minimum value.

Static Methods

fromCenterAndSize

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

fromEdges

Creates a rectangle from the left, top, right and bottom edges.

fromWkt

Creates a new rectangle from a wkt string.

class qgis.core.QgsRectangle[source]

Bases: object

__init__()
__init__(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.

If the the rectangle represents a portion of the globe in geographic coordinates where the extent wraps around the antimeridian then normalize must be set to False.

Parameters:
  • xMin (float)

  • yMin (float = 0)

  • xMax (float = 0)

  • yMax (float = 0)

  • normalize (bool = True)

__init__(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.

Parameters:
__init__(qRectF: QRectF)

Construct a rectangle from a QRectF.

The rectangle is NOT normalized after construction.

Parameters:

qRectF (QRectF)

__init__(other: QgsRectangle)
Parameters:

other (QgsRectangle)

area(self) → float[source]

Returns the area of the rectangle.

See also

width()

See also

height()

See also

perimeter()

Return type:

float

asPolygon(self) → str[source]

Returns the rectangle as a polygon.

Return type:

str

asWktCoordinates(self) → str[source]

Returns a string representation of the rectangle in WKT format.

Return type:

str

asWktPolygon(self) → str[source]

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

Return type:

str

bottom(self) → float[source]

Returns the bottom side of the rectangle.

This usually corresponds to the minimum y value, unless the rectangle was created using non-normalized coordinates.

See also

yMinimum()

Added in version 4.4.

Return type:

float

buffered(self, width: float) → QgsRectangle[source]

Gets rectangle enlarged by buffer.

Note

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

See also

grow()

Parameters:

width (float)

Return type:

QgsRectangle

center(self) → QgsPointXY[source]

Returns the center point of the rectangle.

Return type:

QgsPointXY

combineExtentWith(self, rect: QgsRectangle)[source]

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.

Added in version 3.2.

Parameters:

rect (QgsRectangle)

contains(self, rect: QgsRectangle) → bool[source]

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).

Added in version 3.20.

Parameters:

rect (QgsRectangle)

Return type:

bool

distance(self, point: QgsPointXY) → float[source]

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

Added in version 3.14.

Parameters:

point (QgsPointXY)

Return type:

float

static fromCenterAndSize(center: QgsPointXY, width: float, height: float) → QgsRectangle[source]

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

Parameters:
  • center (QgsPointXY)

  • width (float)

  • height (float)

Return type:

QgsRectangle

static fromEdges(left: float, top: float, right: float, bottom: float) → QgsRectangle[source]

Creates a rectangle from the left, top, right and bottom edges.

The rectangle will not be normalized, permitting situations where the left edge > right or top < bottom. E.g. for use when the rectangle represents a portion of the globe in geographic coordinates where the extent wraps around the antimeridian.

Added in version 4.4.

Parameters:
  • left (float)

  • top (float)

  • right (float)

  • bottom (float)

Return type:

QgsRectangle

static fromWkt(wkt: str | None) → QgsRectangle[source]

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

Parameters:

wkt (str|None)

Return type:

QgsRectangle

grow(self, delta: float)[source]

Grows the rectangle in place by the specified amount.

See also

buffered()

Parameters:

delta (float)

height(self) → float[source]

Returns the height of the rectangle.

See also

width()

See also

area()

Return type:

float

include(self, p: QgsPointXY)[source]

Updates the rectangle to include the specified point.

Parameters:

p (QgsPointXY)

intersect(self, rect: QgsRectangle) → QgsRectangle[source]

Returns the intersection with the given rectangle.

Parameters:

rect (QgsRectangle)

Return type:

QgsRectangle

intersects(self, rect: QgsRectangle) → bool[source]

Returns True when rectangle intersects with other rectangle.

Parameters:

rect (QgsRectangle)

Return type:

bool

invert(self)[source]

Swap x/y coordinates in the rectangle.

isEmpty(self) → bool[source]

Returns True if the rectangle has no area.

While a null rectangle is always considered empty, an empty rectangle may still be non-null if it contains valid spatial information (e.g. the bounding box of a point or of a vertical or horizontal segment).

Warning

Do not use this method to determine if the rectangle represents the bounding box of a point. The isPoint() method should be used instead.

See also

isNull()

See also

isPoint()

Return type:

bool

isFinite(self) → bool[source]

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

See also

isMaximal()

See also

isNull()

See also

isEmpty()

Return type:

bool

isMaximal(self) → bool[source]

Test if the rectangle is the maximal possible rectangle.

A rectangle is considered maximal if all boundaries are either at their maximum possible values or are infinite values.

See also

isFinite()

See also

isNull()

See also

isEmpty()

Added in version 3.44.

Return type:

bool

isNormalized(self) → bool[source]

Returns True if the rectangle is normalized.

A rectangle is normalized if the xMinimum() <= xMaximum() and yMinimum() <= yMaximum().

Added in version 4.4.

Return type:

bool

isNull(self) → bool[source]

Test if the rectangle is null (holding no spatial information).

A rectangle is considered null if all its coordinates are NaN, if it has not been defined yet, or if it has been explicitly initialized as null. A null rectangle is also an empty rectangle and an invalid rectangle.

See also

setNull()

Return type:

bool

isPoint(self) → bool[source]

Returns True if the rectangle represents a point.

A rectangle represents a point if both the x coordinates are an exact match and the y coordinates are an exact match.

See also

isNull()

Added in version 4.4.

Return type:

bool

isValid(self) → bool[source]

Test if the rectangle is valid

A rectangle is considered invalid if at least one of its coordinates is NaN or infinite, or if a maximum coordinate is less than or equal to the corresponding minimum coordinate.

See also

isNull()

Added in version 4.0.

Return type:

bool

left(self) → float[source]

Returns the left side of the rectangle.

This usually corresponds to the minimum x value, unless the rectangle represents a portion of the globe in geographic coordinates where the extent wraps around the antimeridian.

See also

xMinimum()

Added in version 4.4.

Return type:

float

normalize(self)[source]

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

perimeter(self) → float[source]

Returns the perimeter of the rectangle.

See also

area()

Return type:

float

right(self) → float[source]

Returns the right side of the rectangle.

This usually corresponds to the maximum x value, unless the rectangle represents a portion of the globe in geographic coordinates where the extent wraps around the antimeridian.

See also

xMaximum()

Added in version 4.4.

Return type:

float

scale(self, scaleFactor: float, c: QgsPointXY | None = None)[source]

Scale the rectangle around its center point.

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

Parameters:
scaled(self, scaleFactor: float, center: QgsPointXY | None = None) → QgsRectangle[source]

Scale the rectangle around its center point.

Added in version 3.4.

Parameters:
  • scaleFactor (float)

  • center (QgsPointXY|None = None)

Return type:

QgsRectangle

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

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)[source]

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

Deprecated since version 3.34: Will be removed in QGIS 5.0. Use setNull().

setNull(self)[source]

Mark a rectangle as being null (holding no spatial information).

A null rectangle is also empty by definition.

See also

isNull()

See also

isEmpty()

Added in version 3.34.

setXMaximum(self, x: float)[source]

Set the maximum x value.

Parameters:

x (float)

setXMinimum(self, x: float)[source]

Set the minimum x value.

Parameters:

x (float)

setYMaximum(self, y: float)[source]

Set the maximum y value.

Parameters:

y (float)

setYMinimum(self, y: float)[source]

Set the minimum y value.

Parameters:

y (float)

snappedToGrid(self, spacing: float) → QgsRectangle[source]

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

Added in version 3.4.

Parameters:

spacing (float)

Return type:

QgsRectangle

splitAtAntimeridian(self) → tuple[QgsRectangle, QgsRectangle]

Splits the rectangle at the antimeridian (longitude 180 degrees).

Returns two normalized rectangles, one representing the part to the west of the antimeridian and one which represents the part to the east.

If the rectangle does not cross the antimeridian then the second returned rectangle will be a null rectangle.

Warning

This method is only valid for calling on rectangles which represent geographic extents (i.e. contain latitude/longitude coordinates). If the extent is in a projected CRS, then the results will be incorrect, even if that projected rectangle crosses the antimeridian.

Added in version 4.4.

Return type:

tuple[QgsRectangle, QgsRectangle]

toBox3d(self, zMin: float, zMax: float) → QgsBox3D[source]

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

Parameters:
  • zMin (float)

  • zMax (float)

Return type:

QgsBox3D

toRectF(self) → QRectF[source]

Returns a QRectF with same coordinates as the rectangle.

Return type:

QRectF

toString(self, precision: int = 16) → str[source]

Returns a string representation of form xmin,ymin : xmax,ymax Coordinates will be rounded 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

top(self) → float[source]

Returns the top side of the rectangle.

This usually corresponds to the maximum y value, unless the rectangle was created using non-normalized coordinates.

See also

yMaximum()

Added in version 4.4.

Return type:

float

width(self) → float[source]

Returns the width of the rectangle.

See also

height()

See also

area()

Return type:

float

xMaximum(self) → float[source]

Returns the x maximum value.

This usually corresponds to the right side of the rectangle, unless the rectangle was created using non-normalized coordinates. E.g. when the rectangle represents a portion of the globe in geographic coordinates where the extent wraps around the antimeridian.

See also

right()

Return type:

float

xMinimum(self) → float[source]

Returns the x minimum value.

This usually corresponds to the left side of the rectangle, unless the rectangle was created using non-normalized coordinates. E.g. when the rectangle represents a portion of the globe in geographic coordinates where the extent wraps around the antimeridian.

See also

left()

Return type:

float

yMaximum(self) → float[source]

Returns the y maximum value.

This usually corresponds to the top of the rectangle, unless the rectangle was created using non-normalized coordinates.

See also

top()

Return type:

float

yMinimum(self) → float[source]

Returns the y minimum value.

This usually corresponds to the bottom of the rectangle, unless the rectangle was created using non-normalized coordinates.

See also

top()

Return type:

float