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
Class Hierarchy¶
Subclasses¶
A |
Methods
Returns the area of the rectangle. |
|
Returns the rectangle as a polygon. |
|
Returns a string representation of the rectangle in WKT format. |
|
Returns a string representation of the rectangle as a WKT Polygon. |
|
Gets rectangle enlarged by buffer. |
|
Returns the center point of the rectangle. |
|
Expands the rectangle so that it covers both the original rectangle and the given rectangle. |
|
Returns |
|
Returns the distance from point to the nearest point on the boundary of the rectangle. |
|
Grows the rectangle in place by the specified amount. |
|
Returns the height of the rectangle. |
|
Updates the rectangle to include the specified point. |
|
Returns the intersection with the given rectangle. |
|
Returns |
|
Swap x/y coordinates in the rectangle. |
|
Returns |
|
Returns |
|
Test if the rectangle is the maximal possible rectangle. |
|
Test if the rectangle is null (holding no spatial information). |
|
Normalize the rectangle so it has non-negative width/height. |
|
Returns the perimeter of the rectangle. |
|
Scale the rectangle around its center point. |
|
Scale the rectangle around its center point. |
|
Sets the rectangle from two |
|
Set a rectangle so that min corner is at max and max corner is at min. |
|
Mark a rectangle as being null (holding no spatial information). |
|
Set the maximum x value. |
|
Set the minimum x value. |
|
Set the maximum y value. |
|
Set the minimum y value. |
|
Returns a copy of this rectangle that is snapped to a grid with the specified spacing between the grid lines. |
|
Converts the rectangle to a 3D box, with the specified zMin and zMax z values. |
|
Returns a QRectF with same coordinates as the rectangle. |
|
Returns a string representation of form xmin,ymin : xmax,ymax Coordinates will be truncated to the specified precision. |
|
Returns the width of the rectangle. |
|
Returns the x maximum value (right side of rectangle). |
|
Returns the x minimum value (left side of rectangle). |
|
Returns the y maximum value (top side of rectangle). |
|
Returns the y minimum value (bottom side of rectangle). |
Static Methods
Creates a new rectangle, given the specified center point and width and height. |
|
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
isFalse
then the normalization step will not be applied automatically.- 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
isFalse
then the normalization step will not be applied automatically.- Parameters:
p1 (QgsPointXY)
p2 (QgsPointXY)
normalize (bool = True)
- __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
See also
See also
- Return type:
float
- 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
- buffered(self, width: float) QgsRectangle [source]¶
Gets rectangle enlarged by buffer.
Note
In earlier QGIS releases this method was named
buffer()
.See also
- Parameters:
width (float)
- Return type:
- center(self) QgsPointXY [source]¶
Returns the center point of the rectangle.
- Return type:
- combineExtentWith(self, rect: QgsRectangle)[source]¶
Expands the rectangle so that it covers both the original rectangle and the given rectangle.
- Parameters:
rect (
QgsRectangle
)
- combineExtentWith(self, x: float, y: float)[source]
Expands the rectangle so that it covers both the original rectangle and the given point.
- Parameters:
x (float)
y (float)
- combineExtentWith(self, point: QgsPointXY)[source]
Expands the rectangle so that it covers both the original rectangle and the given point.
Added in version 3.2.
- Parameters:
point (
QgsPointXY
)
- contains(self, rect: QgsRectangle) bool [source]¶
Returns
True
when rectangle contains other rectangle.- Parameters:
rect (
QgsRectangle
)- Return type:
bool
- contains(self, p: QgsPointXY) bool [source]
Returns
True
when rectangle contains a point.- Parameters:
p (
QgsPointXY
)- Return type:
bool
- contains(self, x: float, y: float) bool [source]
Returns
True
when rectangle contains the point at (x
,y
).Added in version 3.20.
- Parameters:
x (float)
y (float)
- 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 andwidth
andheight
.- Parameters:
center (QgsPointXY)
width (float)
height (float)
- Return type:
- 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 (Optional[str])
- Return type:
- grow(self, delta: float)[source]¶
Grows the rectangle in place by the specified amount.
See also
- Parameters:
delta (float)
- height(self) float [source]¶
Returns the height of the rectangle.
See also
See also
- 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:
- intersects(self, rect: QgsRectangle) bool [source]¶
Returns
True
when rectangle intersects with other rectangle.- Parameters:
rect (QgsRectangle)
- Return type:
bool
- isEmpty(self) bool [source]¶
Returns
True
if the rectangle has no area.An empty rectangle may still be non-null if it contains valid spatial information (e.g. bounding box of a point or of a vertical or horizontal segment).
See also
- Return type:
bool
- isFinite(self) bool [source]¶
Returns
True
if the rectangle has finite boundaries. Will returnFalse
if any of the rectangle boundaries are NaN or Inf.See also
See also
See also
- 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
See also
See also
Added in version 3.44.
- Return type:
bool
- isNull(self) bool [source]¶
Test if the rectangle is null (holding no spatial information).
A null rectangle is also an empty rectangle.
See also
- Return type:
bool
- scale(self, scaleFactor: float, c: QgsPointXY | None = None)[source]¶
Scale the rectangle around its center point.
- Parameters:
scaleFactor (float)
c (Optional[QgsPointXY] = None)
- scale(self, scaleFactor: float, centerX: float, centerY: float)[source]
Scale the rectangle around its center point.
- Parameters:
scaleFactor (float)
centerX (float)
centerY (float)
- 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 (Optional[QgsPointXY] = None)
- Return type:
- 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
isFalse
then the normalization step will not be applied automatically.- Parameters:
p1 (
QgsPointXY
)p2 (
QgsPointXY
)normalize (bool = True)
- set(self, xMin: float, yMin: float, xMax: float, yMax: float, normalize: bool = True)[source]
Sets the rectangle from four points.
The rectangle is normalised after construction. Since QGIS 3.20, if
normalize
isFalse
then the normalization step will not be applied automatically.- Parameters:
xMin (float)
yMin (float)
xMax (float)
yMax (float)
normalize (bool = True)
- 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 4.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
See also
Added in version 3.34.
- 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:
- toBox3d(self, zMin: float, zMax: float) QgsBox3D [source]¶
Converts the rectangle to a 3D box, with the specified
zMin
andzMax
z values.- Parameters:
zMin (float)
zMax (float)
- Return type:
- 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 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 [source]¶
Returns the width of the rectangle.
See also
See also
- Return type:
float
- xMaximum(self) float [source]¶
Returns the x maximum value (right side of rectangle).
- Return type:
float
- xMinimum(self) float [source]¶
Returns the x minimum value (left side of rectangle).
- Return type:
float