QGIS API Documentation 3.99.0-Master (d270888f95f)
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 "qgsrectangle.h"
19
20#include <algorithm>
21#include <cmath>
22#include <limits>
23
24#include "qgsbox3d.h"
25#include "qgsgeometry.h"
26#include "qgslinestring.h"
27#include "qgslogger.h"
28#include "qgspointxy.h"
29#include "qgspolygon.h"
30
31#include <QString>
32#include <QTextStream>
33#include <QTransform>
34
35#include "moc_qgsrectangle.cpp"
36
37using namespace Qt::StringLiterals;
38
40{
41 const QgsGeometry geom = QgsGeometry::fromWkt( wkt );
42 if ( geom.isEmpty() )
43 return QgsRectangle();
44
46 {
47 if ( polygon->numInteriorRings() > 0 )
48 return QgsRectangle();
49
50 if ( const QgsLineString *exterior = qgsgeometry_cast< const QgsLineString * >( polygon->exteriorRing() ) )
51 {
52 if ( exterior->numPoints() == 5
53 && qgsDoubleNear( exterior->xAt( 0 ), exterior->xAt( 4 ) )
54 && qgsDoubleNear( exterior->yAt( 0 ), exterior->yAt( 4 ) )
55 && geom.isGeosValid() )
56 return QgsRectangle( exterior->xAt( 0 ), exterior->yAt( 0 ), exterior->xAt( 2 ), exterior->yAt( 2 ) );
57 }
58 }
59 return QgsRectangle();
60}
61
63{
64 const double xMin = center.x() - width / 2.0;
65 const double xMax = xMin + width;
66 const double yMin = center.y() - height / 2.0;
67 const double yMax = yMin + height;
68 return QgsRectangle( xMin, yMin, xMax, yMax );
69}
70
71QgsRectangle QgsRectangle::scaled( double scaleFactor, const QgsPointXY *center ) const
72{
73 QgsRectangle scaledRect = QgsRectangle( *this );
74 scaledRect.scale( scaleFactor, center );
75 return scaledRect;
76}
77
79{
80 const double xmin = mXmin - v.x();
81 const double xmax = mXmax - v.x();
82 const double ymin = mYmin - v.y();
83 const double ymax = mYmax - v.y();
84 return QgsRectangle( xmin, ymin, xmax, ymax );
85}
86
88{
89 const double xmin = mXmin + v.x();
90 const double xmax = mXmax + v.x();
91 const double ymin = mYmin + v.y();
92 const double ymax = mYmax + v.y();
93 return QgsRectangle( xmin, ymin, xmax, ymax );
94}
95
97{
98 mXmin -= v.x();
99 mXmax -= v.x();
100 mYmin -= v.y();
101 mYmax -= v.y();
102 return *this;
103}
104
106{
107 mXmin += v.x();
108 mXmax += v.x();
109 mYmin += v.y();
110 mYmax += v.y();
111 return *this;
112}
113
115{
116 QString rep =
117 qgsDoubleToString( mXmin ) + ' ' + qgsDoubleToString( mYmin ) + ", "_L1 +
118 qgsDoubleToString( mXmax ) + ' ' + qgsDoubleToString( mYmax );
119
120 return rep;
121}
122
124{
125 if ( isNull() )
126 {
127 return u"Polygon EMPTY"_s;
128 }
129
130 return u"Polygon ((%1 %2, %3 %2, %3 %4, %1 %4, %1 %2))"_s.arg(
131 qgsDoubleToString( mXmin ),
132 qgsDoubleToString( mYmin ),
133 qgsDoubleToString( mXmax ),
134 qgsDoubleToString( mYmax )
135 );
136}
137
138QString QgsRectangle::toString( int precision ) const
139{
140 QString rep;
141
142 if ( precision < 0 )
143 {
144 precision = 0;
145 if ( ( width() < 10 || height() < 10 ) && ( width() > 0 && height() > 0 ) )
146 {
147 precision = static_cast<int>( std::ceil( -1.0 * std::log10( std::min( width(), height() ) ) ) ) + 1;
148 // sanity check
149 if ( precision > 20 )
150 precision = 20;
151 }
152 }
153
154 if ( isNull() )
155 rep = u"Null"_s;
156 else
157 rep = u"%1,%2 : %3,%4"_s
158 .arg( mXmin, 0, 'f', precision )
159 .arg( mYmin, 0, 'f', precision )
160 .arg( mXmax, 0, 'f', precision )
161 .arg( mYmax, 0, 'f', precision );
162
163 QgsDebugMsgLevel( u"Extents : %1"_s.arg( rep ), 4 );
164
165 return rep;
166}
167
169{
170 if ( isNull() )
171 {
172 return u"EMPTY"_s;
173 }
174
175 QString rep;
176
177 QTextStream foo( &rep );
178
179 foo.setRealNumberPrecision( 8 );
180 foo.setRealNumberNotation( QTextStream::FixedNotation );
181 // NOTE: a polygon isn't a polygon unless its closed. In the case of
182 // a rectangle, that means 5 points (last == first)
183 foo
184 << mXmin << ' ' << mYmin << ", "
185 << mXmin << ' ' << mYmax << ", "
186 << mXmax << ' ' << mYmax << ", "
187 << mXmax << ' ' << mYmin << ", "
188 << mXmin << ' ' << mYmin;
189
190 return rep;
191
192}
193
194QgsBox3D QgsRectangle::toBox3d( double zMin, double zMax ) const
195{
196 return QgsBox3D( mXmin, mYmin, zMin, mXmax, mYmax, zMax );
197}
198
200{
201 if ( isNull() ) return *this;
202
203 // helper function
204 auto gridifyValue = []( double value, double spacing ) -> double
205 {
206 if ( spacing > 0 )
207 return std::round( value / spacing ) * spacing;
208 else
209 return value;
210 };
211
212 return QgsRectangle(
213 gridifyValue( mXmin, spacing ),
214 gridifyValue( mYmin, spacing ),
215 gridifyValue( mXmax, spacing ),
216 gridifyValue( mYmax, spacing )
217 );
218}
219
220QDataStream &operator<<( QDataStream &out, const QgsRectangle &rectangle )
221{
222 out << rectangle.xMinimum() << rectangle.yMinimum() << rectangle.xMaximum() << rectangle.yMaximum();
223 return out;
224}
225
226QDataStream &operator>>( QDataStream &in, QgsRectangle &rectangle )
227{
228 double xmin, ymin, xmax, ymax;
229 in >> xmin >> ymin >> xmax >> ymax;
230 rectangle.setXMinimum( xmin );
231 rectangle.setYMinimum( ymin );
232 rectangle.setXMaximum( xmax );
233 rectangle.setYMaximum( ymax );
234 return in;
235}
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:45
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.
Represents a 2D point.
Definition qgspointxy.h:62
Polygon geometry type.
Definition qgspolygon.h:37
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.
Represent a 2-dimensional vector.
Definition qgsvector.h:34
double y() const
Returns the vector's y-component.
Definition qgsvector.h:156
double x() const
Returns the vector's x-component.
Definition qgsvector.h:147
QString qgsDoubleToString(double a, int precision=17)
Returns a string representation of a double.
Definition qgis.h:6817
bool qgsDoubleNear(double a, double b, double epsilon=4 *std::numeric_limits< double >::epsilon())
Compare two doubles (but allow some difference).
Definition qgis.h:6900
T qgsgeometry_cast(QgsAbstractGeometry *geom)
#define QgsDebugMsgLevel(str, level)
Definition qgslogger.h:63
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.