QGIS API Documentation 4.1.0-Master (60fea48833c)
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 && qgsDoubleNear( exterior->xAt( 0 ), exterior->xAt( 4 ) ) && qgsDoubleNear( exterior->yAt( 0 ), exterior->yAt( 4 ) ) && geom.isGeosValid() )
53 return QgsRectangle( exterior->xAt( 0 ), exterior->yAt( 0 ), exterior->xAt( 2 ), exterior->yAt( 2 ) );
54 }
55 }
56 return QgsRectangle();
57}
58
60{
61 const double xMin = center.x() - width / 2.0;
62 const double xMax = xMin + width;
63 const double yMin = center.y() - height / 2.0;
64 const double yMax = yMin + height;
65 return QgsRectangle( xMin, yMin, xMax, yMax );
66}
67
68QgsRectangle QgsRectangle::scaled( double scaleFactor, const QgsPointXY *center ) const
69{
70 QgsRectangle scaledRect = QgsRectangle( *this );
71 scaledRect.scale( scaleFactor, center );
72 return scaledRect;
73}
74
76{
77 const double xmin = mXmin - v.x();
78 const double xmax = mXmax - v.x();
79 const double ymin = mYmin - v.y();
80 const double ymax = mYmax - v.y();
81 return QgsRectangle( xmin, ymin, xmax, ymax );
82}
83
85{
86 const double xmin = mXmin + v.x();
87 const double xmax = mXmax + v.x();
88 const double ymin = mYmin + v.y();
89 const double ymax = mYmax + v.y();
90 return QgsRectangle( xmin, ymin, xmax, ymax );
91}
92
94{
95 mXmin -= v.x();
96 mXmax -= v.x();
97 mYmin -= v.y();
98 mYmax -= v.y();
99 return *this;
100}
101
103{
104 mXmin += v.x();
105 mXmax += v.x();
106 mYmin += v.y();
107 mYmax += v.y();
108 return *this;
109}
110
112{
113 QString rep = qgsDoubleToString( mXmin ) + ' ' + qgsDoubleToString( mYmin ) + ", "_L1 + qgsDoubleToString( mXmax ) + ' ' + qgsDoubleToString( mYmax );
114
115 return rep;
116}
117
119{
120 if ( isNull() )
121 {
122 return u"Polygon EMPTY"_s;
123 }
124
125 return u"Polygon ((%1 %2, %3 %2, %3 %4, %1 %4, %1 %2))"_s.arg( qgsDoubleToString( mXmin ), qgsDoubleToString( mYmin ), qgsDoubleToString( mXmax ), qgsDoubleToString( mYmax ) );
126}
127
128QString QgsRectangle::toString( int precision ) const
129{
130 QString rep;
131
132 if ( precision < 0 )
133 {
134 precision = 0;
135 if ( ( width() < 10 || height() < 10 ) && ( width() > 0 && height() > 0 ) )
136 {
137 precision = static_cast<int>( std::ceil( -1.0 * std::log10( std::min( width(), height() ) ) ) ) + 1;
138 // sanity check
139 if ( precision > 20 )
140 precision = 20;
141 }
142 }
143
144 if ( isNull() )
145 rep = u"Null"_s;
146 else
147 rep = u"%1,%2 : %3,%4"_s.arg( mXmin, 0, 'f', precision ).arg( mYmin, 0, 'f', precision ).arg( mXmax, 0, 'f', precision ).arg( mYmax, 0, 'f', precision );
148
149 QgsDebugMsgLevel( u"Extents : %1"_s.arg( rep ), 4 );
150
151 return rep;
152}
153
155{
156 if ( isNull() )
157 {
158 return u"EMPTY"_s;
159 }
160
161 QString rep;
162
163 QTextStream foo( &rep );
164
165 foo.setRealNumberPrecision( 8 );
166 foo.setRealNumberNotation( QTextStream::FixedNotation );
167 // NOTE: a polygon isn't a polygon unless its closed. In the case of
168 // a rectangle, that means 5 points (last == first)
169 foo << mXmin << ' ' << mYmin << ", " << mXmin << ' ' << mYmax << ", " << mXmax << ' ' << mYmax << ", " << mXmax << ' ' << mYmin << ", " << mXmin << ' ' << mYmin;
170
171 return rep;
172}
173
174QgsBox3D QgsRectangle::toBox3d( double zMin, double zMax ) const
175{
176 return QgsBox3D( mXmin, mYmin, zMin, mXmax, mYmax, zMax );
177}
178
180{
181 if ( isNull() )
182 return *this;
183
184 // helper function
185 auto gridifyValue = []( double value, double spacing ) -> double {
186 if ( spacing > 0 )
187 return std::round( value / spacing ) * spacing;
188 else
189 return value;
190 };
191
192 return QgsRectangle( gridifyValue( mXmin, spacing ), gridifyValue( mYmin, spacing ), gridifyValue( mXmax, spacing ), gridifyValue( mYmax, spacing ) );
193}
194
195QDataStream &operator<<( QDataStream &out, const QgsRectangle &rectangle )
196{
197 out << rectangle.xMinimum() << rectangle.yMinimum() << rectangle.xMaximum() << rectangle.yMaximum();
198 return out;
199}
200
201QDataStream &operator>>( QDataStream &in, QgsRectangle &rectangle )
202{
203 double xmin, ymin, xmax, ymax;
204 in >> xmin >> ymin >> xmax >> ymax;
205 rectangle.setXMinimum( xmin );
206 rectangle.setYMinimum( ymin );
207 rectangle.setXMaximum( xmax );
208 rectangle.setYMaximum( ymax );
209 return in;
210}
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 rounded to the spec...
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:155
double x() const
Returns the vector's x-component.
Definition qgsvector.h:146
QString qgsDoubleToString(double a, int precision=17)
Returns a string representation of a double.
Definition qgis.h:6893
bool qgsDoubleNear(double a, double b, double epsilon=4 *std::numeric_limits< double >::epsilon())
Compare two doubles (but allow some difference).
Definition qgis.h:6975
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.