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