QGIS API Documentation 4.3.0-Master (4ecdf0f3f59)
Loading...
Searching...
No Matches
QgsRectangle Class Reference

A rectangle specified with double values. More...

#include <qgsrectangle.h>

Inheritance diagram for QgsRectangle:

Public Member Functions

 QgsRectangle ()=default
 Constructor for a null rectangle.
 QgsRectangle (const QgsPointXY &p1, const QgsPointXY &p2, bool normalize=true)
 Construct a rectangle from two points.
 QgsRectangle (const QgsRectangle &other)
 QgsRectangle (const QRectF &qRectF)
 Construct a rectangle from a QRectF.
 QgsRectangle (double xMin, double yMin=0, double xMax=0, double yMax=0, bool normalize=true)
 Constructs a QgsRectangle from a set of x and y minimum and maximum coordinates.
 ~QgsRectangle ()=default
double area () const
 Returns the area of the rectangle.
QString asPolygon () const
 Returns the rectangle as a polygon.
Q_INVOKABLE QString asWktCoordinates () const
 Returns a string representation of the rectangle in WKT format.
Q_INVOKABLE QString asWktPolygon () const
 Returns a string representation of the rectangle as a WKT Polygon.
double bottom () const
 Returns the bottom side of the rectangle.
QgsRectangle buffered (double width) const
 Gets rectangle enlarged by buffer.
QgsPointXY center () const
 Returns the center point of the rectangle.
void combineExtentWith (const QgsPointXY &point)
 Expands the rectangle so that it covers both the original rectangle and the given point.
void combineExtentWith (const QgsRectangle &rect)
 Expands the rectangle so that it covers both the original rectangle and the given rectangle.
void combineExtentWith (double x, double y)
 Expands the rectangle so that it covers both the original rectangle and the given point.
bool contains (const QgsPointXY &p) const
 Returns true when rectangle contains a point.
bool contains (const QgsRectangle &rect) const
 Returns true when rectangle contains other rectangle.
bool contains (double x, double y) const
 Returns true when rectangle contains the point at (x, y).
double distance (const QgsPointXY &point) const
 Returns the distance from point to the nearest point on the boundary of the rectangle.
void grow (double delta)
 Grows the rectangle in place by the specified amount.
double height () const
 Returns the height of the rectangle.
void include (const QgsPointXY &p)
 Updates the rectangle to include the specified point.
QgsRectangle intersect (const QgsRectangle &rect) const
 Returns the intersection with the given rectangle.
bool intersects (const QgsRectangle &rect) const
 Returns true when rectangle intersects with other rectangle.
void invert ()
 Swap x/y coordinates in the rectangle.
bool isEmpty () const
 Returns true if the rectangle has no area.
bool isFinite () const
 Returns true if the rectangle has finite boundaries.
bool isMaximal () const
 Test if the rectangle is the maximal possible rectangle.
bool isNull () const
 Test if the rectangle is null (holding no spatial information).
bool isPoint () const
 Returns true if the rectangle represents a point.
bool isValid () const
 Test if the rectangle is valid.
double left () const
 Returns the left side of the rectangle.
void normalize ()
 Normalize the rectangle so it has non-negative width/height.
 operator QVariant () const
 Allows direct construction of QVariants from rectangles.
bool operator!= (const QgsRectangle &r1) const
QgsRectangle operator+ (QgsVector v) const
 Returns a rectangle offset from this one in the direction of the vector.
QgsRectangle & operator+= (QgsVector v)
 Moves this rectangle in the direction of the vector.
QgsRectangle operator- (QgsVector v) const
 Returns a rectangle offset from this one in the direction of the reversed vector.
QgsRectangle & operator-= (QgsVector v)
 Moves this rectangle in the direction of the reversed vector.
QgsRectangle & operator= (const QgsRectangle &r1)
bool operator== (const QgsRectangle &r1) const
double perimeter () const
 Returns the perimeter of the rectangle.
double right () const
 Returns the right side of the rectangle.
void scale (double scaleFactor, const QgsPointXY *c=nullptr)
 Scale the rectangle around its center point.
void scale (double scaleFactor, double centerX, double centerY)
 Scale the rectangle around its center point.
QgsRectangle scaled (double scaleFactor, const QgsPointXY *center=nullptr) const
 Scale the rectangle around its center point.
void set (const QgsPointXY &p1, const QgsPointXY &p2, bool normalize=true)
 Sets the rectangle from two QgsPoints.
void set (double xMin, double yMin, double xMax, double yMax, bool normalize=true)
 Sets the rectangle from four points.
Q_DECL_DEPRECATED void setMinimal ()
 Set a rectangle so that min corner is at max and max corner is at min.
void setNull ()
 Mark a rectangle as being null (holding no spatial information).
void setXMaximum (double x)
 Set the maximum x value.
void setXMinimum (double x)
 Set the minimum x value.
void setYMaximum (double y)
 Set the maximum y value.
void setYMinimum (double y)
 Set the minimum y value.
QgsRectangle snappedToGrid (double spacing) const
 Returns a copy of this rectangle that is snapped to a grid with the specified spacing between the grid lines.
QgsBox3D toBox3d (double zMin, double zMax) const
 Converts the rectangle to a 3D box, with the specified zMin and zMax z values.
double top () const
 Returns the top side of the rectangle.
QRectF toRectF () const
 Returns a QRectF with same coordinates as the rectangle.
Q_INVOKABLE QString toString (int precision=16) const
 Returns a string representation of form xmin,ymin : xmax,ymax Coordinates will be rounded to the specified precision.
double width () const
 Returns the width of the rectangle.
double xMaximum () const
 Returns the x maximum value.
double xMinimum () const
 Returns the x minimum value.
double yMaximum () const
 Returns the y maximum value.
double yMinimum () const
 Returns the y minimum value.

Static Public Member Functions

static QgsRectangle fromCenterAndSize (const QgsPointXY &center, double width, double height)
 Creates a new rectangle, given the specified center point and width and height.
static QgsRectangle fromEdges (double left, double top, double right, double bottom)
 Creates a rectangle from the left, top, right and bottom edges.
static QgsRectangle fromWkt (const QString &wkt)
 Creates a new rectangle from a wkt string.

Properties

double area
QgsPointXY center
double height
bool isEmpty
bool isNull
bool isValid
double perimeter
double width
double xMaximum
double xMinimum
double yMaximum
double yMinimum

Detailed Description

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

Definition at line 46 of file qgsrectangle.h.

Constructor & Destructor Documentation

◆ QgsRectangle() [1/5]

QgsRectangle::QgsRectangle ( )
default

Constructor for a null rectangle.

◆ QgsRectangle() [2/5]

QgsRectangle::QgsRectangle ( double xMin,
double yMin = 0,
double xMax = 0,
double yMax = 0,
bool normalize = true )
inlineexplicit

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.

Definition at line 77 of file qgsrectangle.h.

◆ QgsRectangle() [3/5]

QgsRectangle::QgsRectangle ( const QgsPointXY & p1,
const QgsPointXY & p2,
bool normalize = true )
inline

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.

Definition at line 94 of file qgsrectangle.h.

◆ QgsRectangle() [4/5]

QgsRectangle::QgsRectangle ( const QRectF & qRectF)
inline

Construct a rectangle from a QRectF.

The rectangle is NOT normalized after construction.

Definition at line 104 of file qgsrectangle.h.

◆ QgsRectangle() [5/5]

QgsRectangle::QgsRectangle ( const QgsRectangle & other)
inline

Definition at line 112 of file qgsrectangle.h.

◆ ~QgsRectangle()

QgsRectangle::~QgsRectangle ( )
default

Member Function Documentation

◆ area()

double QgsRectangle::area ( ) const
inline

Returns the area of the rectangle.

See also
width()
height()
perimeter()

Definition at line 356 of file qgsrectangle.h.

◆ asPolygon()

QString QgsRectangle::asPolygon ( ) const

Returns the rectangle as a polygon.

Definition at line 159 of file qgsrectangle.cpp.

◆ asWktCoordinates()

QString QgsRectangle::asWktCoordinates ( ) const

Returns a string representation of the rectangle in WKT format.

Definition at line 116 of file qgsrectangle.cpp.

◆ asWktPolygon()

QString QgsRectangle::asWktPolygon ( ) const

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

Definition at line 123 of file qgsrectangle.cpp.

◆ bottom()

double QgsRectangle::bottom ( ) const
inline

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()
Since
QGIS 4.4

Definition at line 316 of file qgsrectangle.h.

◆ buffered()

QgsRectangle QgsRectangle::buffered ( double width) const
inline

Gets rectangle enlarged by buffer.

Note
In earlier QGIS releases this method was named buffer().
See also
grow()

Definition at line 450 of file qgsrectangle.h.

◆ center()

QgsPointXY QgsRectangle::center ( ) const
inline

Returns the center point of the rectangle.

Definition at line 367 of file qgsrectangle.h.

◆ combineExtentWith() [1/3]

void QgsRectangle::combineExtentWith ( const QgsPointXY & point)
inline

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

Since
QGIS 3.2

Definition at line 557 of file qgsrectangle.h.

◆ combineExtentWith() [2/3]

void QgsRectangle::combineExtentWith ( const QgsRectangle & rect)
inline

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

Definition at line 523 of file qgsrectangle.h.

◆ combineExtentWith() [3/3]

void QgsRectangle::combineExtentWith ( double x,
double y )
inline

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

Definition at line 539 of file qgsrectangle.h.

◆ contains() [1/3]

bool QgsRectangle::contains ( const QgsPointXY & p) const
inline

Returns true when rectangle contains a point.

Definition at line 503 of file qgsrectangle.h.

◆ contains() [2/3]

bool QgsRectangle::contains ( const QgsRectangle & rect) const
inline

Returns true when rectangle contains other rectangle.

Definition at line 495 of file qgsrectangle.h.

◆ contains() [3/3]

bool QgsRectangle::contains ( double x,
double y ) const
inline

Returns true when rectangle contains the point at (x, y).

Since
QGIS 3.20

Definition at line 514 of file qgsrectangle.h.

◆ distance()

double QgsRectangle::distance ( const QgsPointXY & point) const
inline

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

Since
QGIS 3.14

Definition at line 566 of file qgsrectangle.h.

◆ fromCenterAndSize()

QgsRectangle QgsRectangle::fromCenterAndSize ( const QgsPointXY & center,
double width,
double height )
static

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

Definition at line 59 of file qgsrectangle.cpp.

◆ fromEdges()

QgsRectangle QgsRectangle::fromEdges ( double left,
double top,
double right,
double bottom )
static

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.

Since
QGIS 4.4

Definition at line 68 of file qgsrectangle.cpp.

◆ fromWkt()

QgsRectangle QgsRectangle::fromWkt ( const QString & wkt)
static

Creates a new rectangle from a wkt string.

The WKT must contain only 5 vertices, representing a rectangle aligned with X and Y axes.

Definition at line 39 of file qgsrectangle.cpp.

◆ grow()

void QgsRectangle::grow ( double delta)
inline

Grows the rectangle in place by the specified amount.

See also
buffered()

Definition at line 412 of file qgsrectangle.h.

◆ height()

double QgsRectangle::height ( ) const
inline

Returns the height of the rectangle.

See also
width()
area()

Definition at line 348 of file qgsrectangle.h.

◆ include()

void QgsRectangle::include ( const QgsPointXY & p)
inline

Updates the rectangle to include the specified point.

Definition at line 425 of file qgsrectangle.h.

◆ intersect()

QgsRectangle QgsRectangle::intersect ( const QgsRectangle & rect) const
inline

Returns the intersection with the given rectangle.

Definition at line 460 of file qgsrectangle.h.

◆ intersects()

bool QgsRectangle::intersects ( const QgsRectangle & rect) const
inline

Returns true when rectangle intersects with other rectangle.

Definition at line 476 of file qgsrectangle.h.

◆ invert()

void QgsRectangle::invert ( )
inline

Swap x/y coordinates in the rectangle.

Definition at line 765 of file qgsrectangle.h.

◆ isEmpty()

bool QgsRectangle::isEmpty ( ) const
inline

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

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()
isPoint()

Definition at line 607 of file qgsrectangle.h.

◆ isFinite()

bool QgsRectangle::isFinite ( ) const
inline

Returns true if the rectangle has finite boundaries.

Will return false if any of the rectangle boundaries are NaN or Inf.

See also
isMaximal()
isNull()
isEmpty()

Definition at line 749 of file qgsrectangle.h.

◆ isMaximal()

bool QgsRectangle::isMaximal ( ) const
inline

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()
isNull()
isEmpty()
Since
QGIS 3.44

Definition at line 675 of file qgsrectangle.h.

◆ isNull()

bool QgsRectangle::isNull ( ) const
inline

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

Definition at line 636 of file qgsrectangle.h.

◆ isPoint()

bool QgsRectangle::isPoint ( ) const
inline

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()
Since
QGIS 4.4

Definition at line 621 of file qgsrectangle.h.

◆ isValid()

bool QgsRectangle::isValid ( ) const
inline

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()
Since
QGIS 4.0

Definition at line 654 of file qgsrectangle.h.

◆ left()

double QgsRectangle::left ( ) const
inline

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()
Since
QGIS 4.4

Definition at line 282 of file qgsrectangle.h.

◆ normalize()

void QgsRectangle::normalize ( )
inline

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

Definition at line 321 of file qgsrectangle.h.

◆ operator QVariant()

QgsRectangle::operator QVariant ( ) const
inline

Allows direct construction of QVariants from rectangles.

Definition at line 778 of file qgsrectangle.h.

◆ operator!=()

bool QgsRectangle::operator!= ( const QgsRectangle & r1) const
inline

Definition at line 723 of file qgsrectangle.h.

◆ operator+()

QgsRectangle QgsRectangle::operator+ ( QgsVector v) const

Returns a rectangle offset from this one in the direction of the vector.

Definition at line 89 of file qgsrectangle.cpp.

◆ operator+=()

QgsRectangle & QgsRectangle::operator+= ( QgsVector v)

Moves this rectangle in the direction of the vector.

Definition at line 107 of file qgsrectangle.cpp.

◆ operator-()

QgsRectangle QgsRectangle::operator- ( QgsVector v) const

Returns a rectangle offset from this one in the direction of the reversed vector.

Definition at line 80 of file qgsrectangle.cpp.

◆ operator-=()

QgsRectangle & QgsRectangle::operator-= ( QgsVector v)

Moves this rectangle in the direction of the reversed vector.

Definition at line 98 of file qgsrectangle.cpp.

◆ operator=()

QgsRectangle & QgsRectangle::operator= ( const QgsRectangle & r1)
inline

Definition at line 728 of file qgsrectangle.h.

◆ operator==()

bool QgsRectangle::operator== ( const QgsRectangle & r1) const
inline

Definition at line 713 of file qgsrectangle.h.

◆ perimeter()

double QgsRectangle::perimeter ( ) const
inline

Returns the perimeter of the rectangle.

See also
area()

Definition at line 362 of file qgsrectangle.h.

◆ right()

double QgsRectangle::right ( ) const
inline

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()
Since
QGIS 4.4

Definition at line 294 of file qgsrectangle.h.

◆ scale() [1/2]

void QgsRectangle::scale ( double scaleFactor,
const QgsPointXY * c = nullptr )
inline

Scale the rectangle around its center point.

Definition at line 372 of file qgsrectangle.h.

◆ scale() [2/2]

void QgsRectangle::scale ( double scaleFactor,
double centerX,
double centerY )
inline

Scale the rectangle around its center point.

Definition at line 392 of file qgsrectangle.h.

◆ scaled()

QgsRectangle QgsRectangle::scaled ( double scaleFactor,
const QgsPointXY * center = nullptr ) const

Scale the rectangle around its center point.

Since
QGIS 3.4

Definition at line 73 of file qgsrectangle.cpp.

◆ set() [1/2]

void QgsRectangle::set ( const QgsPointXY & p1,
const QgsPointXY & p2,
bool normalize = true )
inline

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.

Definition at line 155 of file qgsrectangle.h.

◆ set() [2/2]

void QgsRectangle::set ( double xMin,
double yMin,
double xMax,
double yMax,
bool normalize = true )
inline

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.

Definition at line 171 of file qgsrectangle.h.

◆ setMinimal()

Q_DECL_DEPRECATED void QgsRectangle::setMinimal ( )
inline

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

It is NOT normalized.

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

Definition at line 223 of file qgsrectangle.h.

◆ setNull()

void QgsRectangle::setNull ( )
inline

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

A null rectangle is also empty by definition.

See also
isNull()
isEmpty()
Since
QGIS 3.34

Definition at line 211 of file qgsrectangle.h.

◆ setXMaximum()

void QgsRectangle::setXMaximum ( double x)
inline

Set the maximum x value.

Definition at line 189 of file qgsrectangle.h.

◆ setXMinimum()

void QgsRectangle::setXMinimum ( double x)
inline

Set the minimum x value.

Definition at line 184 of file qgsrectangle.h.

◆ setYMaximum()

void QgsRectangle::setYMaximum ( double y)
inline

Set the maximum y value.

Definition at line 199 of file qgsrectangle.h.

◆ setYMinimum()

void QgsRectangle::setYMinimum ( double y)
inline

Set the minimum y value.

Definition at line 194 of file qgsrectangle.h.

◆ snappedToGrid()

QgsRectangle QgsRectangle::snappedToGrid ( double spacing) const

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

Since
QGIS 3.4

Definition at line 184 of file qgsrectangle.cpp.

◆ toBox3d()

QgsBox3D QgsRectangle::toBox3d ( double zMin,
double zMax ) const

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

Definition at line 179 of file qgsrectangle.cpp.

◆ top()

double QgsRectangle::top ( ) const
inline

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()
Since
QGIS 4.4

Definition at line 305 of file qgsrectangle.h.

◆ toRectF()

QRectF QgsRectangle::toRectF ( ) const
inline

Returns a QRectF with same coordinates as the rectangle.

Definition at line 696 of file qgsrectangle.h.

◆ toString()

QString QgsRectangle::toString ( int precision = 16) const

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.

Definition at line 133 of file qgsrectangle.cpp.

◆ width()

double QgsRectangle::width ( ) const
inline

Returns the width of the rectangle.

See also
height()
area()

Definition at line 341 of file qgsrectangle.h.

◆ xMaximum()

double QgsRectangle::xMaximum ( ) const
inline

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

Definition at line 238 of file qgsrectangle.h.

◆ xMinimum()

double QgsRectangle::xMinimum ( ) const
inline

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

Definition at line 250 of file qgsrectangle.h.

◆ yMaximum()

double QgsRectangle::yMaximum ( ) const
inline

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

Definition at line 260 of file qgsrectangle.h.

◆ yMinimum()

double QgsRectangle::yMinimum ( ) const
inline

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

Definition at line 270 of file qgsrectangle.h.

Property Documentation

◆ area

double QgsRectangle::area
read

Definition at line 56 of file qgsrectangle.h.

◆ center

QgsPointXY QgsRectangle::center
read

Definition at line 58 of file qgsrectangle.h.

◆ height

double QgsRectangle::height
read

Definition at line 55 of file qgsrectangle.h.

◆ isEmpty

bool QgsRectangle::isEmpty
read

Definition at line 59 of file qgsrectangle.h.

◆ isNull

bool QgsRectangle::isNull
read

Definition at line 60 of file qgsrectangle.h.

◆ isValid

bool QgsRectangle::isValid
read

Definition at line 61 of file qgsrectangle.h.

◆ perimeter

double QgsRectangle::perimeter
read

Definition at line 57 of file qgsrectangle.h.

◆ width

double QgsRectangle::width
read

Definition at line 54 of file qgsrectangle.h.

◆ xMaximum

double QgsRectangle::xMaximum
readwrite

Definition at line 51 of file qgsrectangle.h.

◆ xMinimum

double QgsRectangle::xMinimum
readwrite

Definition at line 50 of file qgsrectangle.h.

◆ yMaximum

double QgsRectangle::yMaximum
readwrite

Definition at line 53 of file qgsrectangle.h.

◆ yMinimum

double QgsRectangle::yMinimum
readwrite

Definition at line 52 of file qgsrectangle.h.


The documentation for this class was generated from the following files: