QGIS API Documentation 3.41.0-Master (3440c17df1d)
Loading...
Searching...
No Matches
qgsrectangle.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsrectangle.cpp - description
3 -------------------
4 begin : Sat Jun 22 2002
5 copyright : (C) 2002 by Gary E.Sherman
6 email : sherman at mrcc.com
7 ***************************************************************************/
8
9/***************************************************************************
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 ***************************************************************************/
17
18#include "qgsgeometry.h"
19#include "qgspointxy.h"
20#include "qgsrectangle.h"
21#include "qgslogger.h"
22#include "qgsbox3d.h"
23#include "qgspolygon.h"
24#include "qgslinestring.h"
25#include "moc_qgsrectangle.cpp"
26
27#include <QString>
28#include <QTextStream>
29#include <QTransform>
30
31#include <algorithm>
32#include <cmath>
33#include <limits>
34
36{
37 const QgsGeometry geom = QgsGeometry::fromWkt( wkt );
38 if ( geom.isEmpty() )
39 return QgsRectangle();
40
41 if ( const QgsPolygon *polygon = qgsgeometry_cast< const QgsPolygon * >( geom.constGet()->simplifiedTypeRef() ) )
42 {
43 if ( polygon->numInteriorRings() > 0 )
44 return QgsRectangle();
45
46 if ( const QgsLineString *exterior = qgsgeometry_cast< QgsLineString * >( polygon->exteriorRing() ) )
47 {
48 if ( exterior->numPoints() == 5
49 && qgsDoubleNear( exterior->xAt( 0 ), exterior->xAt( 4 ) )
50 && qgsDoubleNear( exterior->yAt( 0 ), exterior->yAt( 4 ) )
51 && geom.isGeosValid() )
52 return QgsRectangle( exterior->xAt( 0 ), exterior->yAt( 0 ), exterior->xAt( 2 ), exterior->yAt( 2 ) );
53 }
54 }
55 return QgsRectangle();
56}
57
58QgsRectangle QgsRectangle::fromCenterAndSize( const QgsPointXY &center, double width, double height )
59{
60 const double xMin = center.x() - width / 2.0;
61 const double xMax = xMin + width;
62 const double yMin = center.y() - height / 2.0;
63 const double yMax = yMin + height;
64 return QgsRectangle( xMin, yMin, xMax, yMax );
65}
66
67QgsRectangle QgsRectangle::scaled( double scaleFactor, const QgsPointXY *center ) const
68{
69 QgsRectangle scaledRect = QgsRectangle( *this );
70 scaledRect.scale( scaleFactor, center );
71 return scaledRect;
72}
73
75{
76 const double xmin = mXmin - v.x();
77 const double xmax = mXmax - v.x();
78 const double ymin = mYmin - v.y();
79 const double ymax = mYmax - v.y();
80 return QgsRectangle( xmin, ymin, xmax, ymax );
81}
82
84{
85 const double xmin = mXmin + v.x();
86 const double xmax = mXmax + v.x();
87 const double ymin = mYmin + v.y();
88 const double ymax = mYmax + v.y();
89 return QgsRectangle( xmin, ymin, xmax, ymax );
90}
91
93{
94 mXmin -= v.x();
95 mXmax -= v.x();
96 mYmin -= v.y();
97 mYmax -= v.y();
98 return *this;
99}
100
102{
103 mXmin += v.x();
104 mXmax += v.x();
105 mYmin += v.y();
106 mYmax += v.y();
107 return *this;
108}
109
111{
112 QString rep =
113 qgsDoubleToString( mXmin ) + ' ' + qgsDoubleToString( mYmin ) + QLatin1String( ", " ) +
114 qgsDoubleToString( mXmax ) + ' ' + qgsDoubleToString( mYmax );
115
116 return rep;
117}
118
120{
121 if ( isNull() )
122 {
123 return QStringLiteral( "Polygon EMPTY" );
124 }
125
126 return QStringLiteral( "Polygon ((%1 %2, %3 %2, %3 %4, %1 %4, %1 %2))" ).arg(
127 qgsDoubleToString( mXmin ),
128 qgsDoubleToString( mYmin ),
129 qgsDoubleToString( mXmax ),
130 qgsDoubleToString( mYmax )
131 );
132}
133
135{
136 QString rep;
137
138 if ( precision < 0 )
139 {
140 precision = 0;
141 if ( ( width() < 10 || height() < 10 ) && ( width() > 0 && height() > 0 ) )
142 {
143 precision = static_cast<int>( std::ceil( -1.0 * std::log10( std::min( width(), height() ) ) ) ) + 1;
144 // sanity check
145 if ( precision > 20 )
146 precision = 20;
147 }
148 }
149
150 if ( isNull() )
151 rep = QStringLiteral( "Null" );
152 else
153 rep = QStringLiteral( "%1,%2 : %3,%4" )
154 .arg( mXmin, 0, 'f', precision )
155 .arg( mYmin, 0, 'f', precision )
156 .arg( mXmax, 0, 'f', precision )
157 .arg( mYmax, 0, 'f', precision );
158
159 QgsDebugMsgLevel( QStringLiteral( "Extents : %1" ).arg( rep ), 4 );
160
161 return rep;
162}
163
165{
166 if ( isNull() )
167 {
168 return QStringLiteral( "EMPTY" );
169 }
170
171 QString rep;
172
173 QTextStream foo( &rep );
174
175 foo.setRealNumberPrecision( 8 );
176 foo.setRealNumberNotation( QTextStream::FixedNotation );
177 // NOTE: a polygon isn't a polygon unless its closed. In the case of
178 // a rectangle, that means 5 points (last == first)
179 foo
180 << mXmin << ' ' << mYmin << ", "
181 << mXmin << ' ' << mYmax << ", "
182 << mXmax << ' ' << mYmax << ", "
183 << mXmax << ' ' << mYmin << ", "
184 << mXmin << ' ' << mYmin;
185
186 return rep;
187
188}
189
190QgsBox3D QgsRectangle::toBox3d( double zMin, double zMax ) const
191{
192 return QgsBox3D( mXmin, mYmin, zMin, mXmax, mYmax, zMax );
193}
194
196{
197 if ( isNull() ) return *this;
198
199 // helper function
200 auto gridifyValue = []( double value, double spacing ) -> double
201 {
202 if ( spacing > 0 )
203 return std::round( value / spacing ) * spacing;
204 else
205 return value;
206 };
207
208 return QgsRectangle(
209 gridifyValue( mXmin, spacing ),
210 gridifyValue( mYmin, spacing ),
211 gridifyValue( mXmax, spacing ),
212 gridifyValue( mYmax, spacing )
213 );
214}
215
216QDataStream &operator<<( QDataStream &out, const QgsRectangle &rectangle )
217{
218 out << rectangle.xMinimum() << rectangle.yMinimum() << rectangle.xMaximum() << rectangle.yMaximum();
219 return out;
220}
221
222QDataStream &operator>>( QDataStream &in, QgsRectangle &rectangle )
223{
224 double xmin, ymin, xmax, ymax;
225 in >> xmin >> ymin >> xmax >> ymax;
226 rectangle.setXMinimum( xmin );
227 rectangle.setYMinimum( ymin );
228 rectangle.setXMaximum( xmax );
229 rectangle.setYMaximum( ymax );
230 return in;
231}
virtual const QgsAbstractGeometry * simplifiedTypeRef() const
Returns a reference to the simplest lossless representation of this geometry, e.g.
A 3-dimensional box composed of x, y, z coordinates.
Definition qgsbox3d.h:43
A geometry is the spatial representation of a feature.
const QgsAbstractGeometry * constGet() const
Returns a non-modifiable (const) reference to the underlying abstract geometry primitive.
static Q_INVOKABLE QgsGeometry fromWkt(const QString &wkt)
Creates a new geometry from a WKT string.
bool isGeosValid(Qgis::GeometryValidityFlags flags=Qgis::GeometryValidityFlags()) const
Checks validity of the geometry using GEOS.
bool isEmpty() const
Returns true if the geometry is empty (eg a linestring with no vertices, or a collection with no geom...
Line string geometry type, with support for z-dimension and m-values.
A class to represent a 2D point.
Definition qgspointxy.h:60
double y
Definition qgspointxy.h:64
double x
Definition qgspointxy.h:63
Polygon geometry type.
Definition qgspolygon.h:33
A rectangle specified with double values.
Q_INVOKABLE QString toString(int precision=16) const
Returns a string representation of form xmin,ymin : xmax,ymax Coordinates will be truncated to the sp...
void scale(double scaleFactor, const QgsPointXY *c=nullptr)
Scale the rectangle around its center point.
double xMinimum
QgsRectangle & operator+=(QgsVector v)
Moves this rectangle in the direction of the vector.
QgsRectangle()=default
Constructor for a null rectangle.
double yMinimum
double xMaximum
static QgsRectangle fromWkt(const QString &wkt)
Creates a new rectangle from a wkt string.
void setYMinimum(double y)
Set the minimum y value.
QgsRectangle & operator-=(QgsVector v)
Moves this rectangle in the direction of the reversed vector.
void setXMinimum(double x)
Set the minimum x value.
Q_INVOKABLE QString asWktPolygon() const
Returns a string representation of the rectangle as a WKT Polygon.
Q_INVOKABLE QString asWktCoordinates() const
Returns a string representation of the rectangle in WKT format.
QgsRectangle operator-(QgsVector v) const
Returns a rectangle offset from this one in the direction of the reversed vector.
QgsRectangle scaled(double scaleFactor, const QgsPointXY *center=nullptr) const
Scale the rectangle around its center point.
void setYMaximum(double y)
Set the maximum y value.
void setXMaximum(double x)
Set the maximum x value.
double yMaximum
static QgsRectangle fromCenterAndSize(const QgsPointXY &center, double width, double height)
Creates a new rectangle, given the specified center point and width and height.
QString asPolygon() const
Returns the rectangle as a polygon.
QgsPointXY center
QgsRectangle snappedToGrid(double spacing) const
Returns a copy of this rectangle that is snapped to a grid with the specified spacing between the gri...
QgsRectangle operator+(QgsVector v) const
Returns a rectangle offset from this one in the direction of the vector.
QgsBox3D toBox3d(double zMin, double zMax) const
Converts the rectangle to a 3D box, with the specified zMin and zMax z values.
A class to represent a vector.
Definition qgsvector.h:30
double y() const
Returns the vector's y-component.
Definition qgsvector.h:152
double x() const
Returns the vector's x-component.
Definition qgsvector.h:143
QString qgsDoubleToString(double a, int precision=17)
Returns a string representation of a double.
Definition qgis.h:5875
bool qgsDoubleNear(double a, double b, double epsilon=4 *std::numeric_limits< double >::epsilon())
Compare two doubles (but allow some difference)
Definition qgis.h:5958
#define QgsDebugMsgLevel(str, level)
Definition qgslogger.h:39
QDataStream & operator>>(QDataStream &in, QgsRectangle &rectangle)
Reads a rectangle from stream in into rectangle.
QDataStream & operator<<(QDataStream &out, const QgsRectangle &rectangle)
Writes the list rectangle to stream out.
int precision