QGIS API Documentation  3.2.0-Bonn (bc43194)
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 <algorithm>
19 #include <cmath>
20 #include <limits>
21 #include <QString>
22 #include <QTextStream>
23 #include <QTransform>
24 #include <QRegExp>
25 
26 #include "qgsgeometry.h"
27 #include "qgspointxy.h"
28 #include "qgsrectangle.h"
29 #include "qgslogger.h"
30 #include "qgsbox3d.h"
31 #include "qgspolygon.h"
32 #include "qgslinestring.h"
33 
34 QgsRectangle QgsRectangle::fromWkt( const QString &wkt )
35 {
36  QgsGeometry geom = QgsGeometry::fromWkt( wkt );
37  if ( geom.isMultipart() )
38  return QgsRectangle();
39 
40  QgsPolygonXY poly = geom.asPolygon();
41 
42  if ( poly.size() != 1 )
43  return QgsRectangle();
44 
45  QgsPolylineXY polyline = geom.asPolygon().at( 0 );
46  if ( polyline.size() == 5 && polyline.at( 0 ) == polyline.at( 4 ) && geom.isGeosValid() )
47  return QgsRectangle( polyline.at( 0 ).x(), polyline.at( 0 ).y(), polyline.at( 2 ).x(), polyline.at( 2 ).y() );
48  else
49  return QgsRectangle();
50 }
51 
53 {
54  double xMin = center.x() - width / 2.0;
55  double xMax = xMin + width;
56  double yMin = center.y() - height / 2.0;
57  double yMax = yMin + height;
58  return QgsRectangle( xMin, yMin, xMax, yMax );
59 }
60 
62 {
63  double xmin = mXmin - v.x();
64  double xmax = mXmax - v.x();
65  double ymin = mYmin - v.y();
66  double ymax = mYmax - v.y();
67  return QgsRectangle( xmin, ymin, xmax, ymax );
68 }
69 
71 {
72  double xmin = mXmin + v.x();
73  double xmax = mXmax + v.x();
74  double ymin = mYmin + v.y();
75  double ymax = mYmax + v.y();
76  return QgsRectangle( xmin, ymin, xmax, ymax );
77 }
78 
80 {
81  mXmin -= v.x();
82  mXmax -= v.x();
83  mYmin -= v.y();
84  mYmax -= v.y();
85  return *this;
86 }
87 
89 {
90  mXmin += v.x();
91  mXmax += v.x();
92  mYmin += v.y();
93  mYmax += v.y();
94  return *this;
95 }
96 
98 {
99  QString rep =
100  qgsDoubleToString( mXmin ) + ' ' + qgsDoubleToString( mYmin ) + ", " +
101  qgsDoubleToString( mXmax ) + ' ' + qgsDoubleToString( mYmax );
102 
103  return rep;
104 }
105 
107 {
108  QString rep =
109  QStringLiteral( "POLYGON((" ) +
110  qgsDoubleToString( mXmin ) + ' ' + qgsDoubleToString( mYmin ) + ", " +
111  qgsDoubleToString( mXmax ) + ' ' + qgsDoubleToString( mYmin ) + ", " +
112  qgsDoubleToString( mXmax ) + ' ' + qgsDoubleToString( mYmax ) + ", " +
113  qgsDoubleToString( mXmin ) + ' ' + qgsDoubleToString( mYmax ) + ", " +
114  qgsDoubleToString( mXmin ) + ' ' + qgsDoubleToString( mYmin ) +
115  QStringLiteral( "))" );
116 
117  return rep;
118 }
119 
120 QString QgsRectangle::toString( int precision ) const
121 {
122  QString rep;
123 
124  if ( precision < 0 )
125  {
126  precision = 0;
127  if ( ( width() < 10 || height() < 10 ) && ( width() > 0 && height() > 0 ) )
128  {
129  precision = static_cast<int>( std::ceil( -1.0 * std::log10( std::min( width(), height() ) ) ) ) + 1;
130  // sanity check
131  if ( precision > 20 )
132  precision = 20;
133  }
134  }
135 
136  if ( isEmpty() )
137  rep = QStringLiteral( "Empty" );
138  else
139  rep = QStringLiteral( "%1,%2 : %3,%4" )
140  .arg( mXmin, 0, 'f', precision )
141  .arg( mYmin, 0, 'f', precision )
142  .arg( mXmax, 0, 'f', precision )
143  .arg( mYmax, 0, 'f', precision );
144 
145  QgsDebugMsgLevel( QString( "Extents : %1" ).arg( rep ), 4 );
146 
147  return rep;
148 }
149 
150 QString QgsRectangle::asPolygon() const
151 {
152 // QString rep = tmp.sprintf("%16f %16f,%16f %16f,%16f %16f,%16f %16f,%16f %16f",
153 // xmin, ymin, xmin, ymax, xmax, ymax, xmax, ymin, xmin, ymin);
154  QString rep;
155 
156  QTextStream foo( &rep );
157 
158  foo.setRealNumberPrecision( 8 );
159  foo.setRealNumberNotation( QTextStream::FixedNotation );
160  // NOTE: a polygon isn't a polygon unless its closed. In the case of
161  // a rectangle, that means 5 points (last == first)
162  foo
163  << mXmin << ' ' << mYmin << ", "
164  << mXmin << ' ' << mYmax << ", "
165  << mXmax << ' ' << mYmax << ", "
166  << mXmax << ' ' << mYmin << ", "
167  << mXmin << ' ' << mYmin;
168 
169  return rep;
170 
171 }
172 
173 QgsBox3d QgsRectangle::toBox3d( double zMin, double zMax ) const
174 {
175  return QgsBox3d( mXmin, mYmin, zMin, mXmax, mYmax, zMax );
176 }
177 
178 QDataStream &operator<<( QDataStream &out, const QgsRectangle &rectangle )
179 {
180  out << rectangle.xMinimum() << rectangle.yMinimum() << rectangle.xMaximum() << rectangle.yMaximum();
181  return out;
182 }
183 
184 QDataStream &operator>>( QDataStream &in, QgsRectangle &rectangle )
185 {
186  double xmin, ymin, xmax, ymax;
187  in >> xmin >> ymin >> xmax >> ymax;
188  rectangle.setXMinimum( xmin );
189  rectangle.setYMinimum( ymin );
190  rectangle.setXMaximum( xmax );
191  rectangle.setYMaximum( ymax );
192  return in;
193 }
A rectangle specified with double values.
Definition: qgsrectangle.h:40
bool isMultipart() const
Returns true if WKB of the geometry is of WKBMulti* type.
void setXMaximum(double x)
Set the maximum x value.
Definition: qgsrectangle.h:134
static QgsRectangle fromWkt(const QString &wkt)
Creates a new rectangle from a wkt string.
double y
Definition: qgspointxy.h:48
QgsRectangle operator-(QgsVector v) const
Returns a rectangle offset from this one in the direction of the reversed vector. ...
A class to represent a 2D point.
Definition: qgspointxy.h:43
QDataStream & operator>>(QDataStream &in, QgsRectangle &rectangle)
Reads a rectangle from stream in into rectangle.
QVector< QgsPolylineXY > QgsPolygonXY
Polygon: first item of the list is outer ring, inner rings (if any) start from second item...
Definition: qgsgeometry.h:66
A geometry is the spatial representation of a feature.
Definition: qgsgeometry.h:104
A 3-dimensional box composed of x, y, z coordinates.
Definition: qgsbox3d.h:35
bool isGeosValid() const
Checks validity of the geometry using GEOS.
QString asWktPolygon() const
Returns a string representation of the rectangle as a WKT Polygon.
static QgsRectangle fromCenterAndSize(QgsPointXY center, double width, double height)
Creates a new rectangle, given the specified center point and width and height.
#define QgsDebugMsgLevel(str, level)
Definition: qgslogger.h:39
bool isEmpty() const
Returns true if the rectangle is empty.
Definition: qgsrectangle.h:419
QgsPolygonXY asPolygon() const
Returns contents of the geometry as a polygon if wkbType is WKBPolygon, otherwise an empty list...
double width() const
Returns the width of the rectangle.
Definition: qgsrectangle.h:201
QString qgsDoubleToString(double a, int precision=17)
Returns a string representation of a double.
Definition: qgis.h:237
void setYMinimum(double y)
Set the minimum y value.
Definition: qgsrectangle.h:139
QString asPolygon() const
Returns the rectangle as a polygon.
QString toString(int precision=16) const
Returns a string representation of form xmin,ymin : xmax,ymax Coordinates will be truncated to the sp...
QgsRectangle & operator+=(QgsVector v)
Moves this rectangle in the direction of the vector.
QString asWktCoordinates() const
Returns a string representation of the rectangle in WKT format.
double x
Definition: qgspointxy.h:47
A class to represent a vector.
Definition: qgsvector.h:28
double yMinimum() const
Returns the y minimum value (bottom side of rectangle).
Definition: qgsrectangle.h:176
double xMaximum() const
Returns the x maximum value (right side of rectangle).
Definition: qgsrectangle.h:161
static QgsGeometry fromWkt(const QString &wkt)
Creates a new geometry from a WKT string.
QVector< QgsPointXY > QgsPolylineXY
Polyline as represented as a vector of two-dimensional points.
Definition: qgsgeometry.h:42
QgsRectangle operator+(QgsVector v) const
Returns a rectangle offset from this one in the direction of the vector.
void setYMaximum(double y)
Set the maximum y value.
Definition: qgsrectangle.h:144
QgsRectangle()=default
Constructor for a null rectangle.
double xMinimum() const
Returns the x minimum value (left side of rectangle).
Definition: qgsrectangle.h:166
double yMaximum() const
Returns the y maximum value (top side of rectangle).
Definition: qgsrectangle.h:171
double x() const
Returns the vector&#39;s x-component.
Definition: qgsvector.cpp:76
QgsPointXY center() const
Returns the center point of the rectangle.
Definition: qgsrectangle.h:229
QgsRectangle & operator-=(QgsVector v)
Moves this rectangle in the direction of the reversed vector.
double y() const
Returns the vector&#39;s y-component.
Definition: qgsvector.cpp:81
QDataStream & operator<<(QDataStream &out, const QgsRectangle &rectangle)
Writes the list rectangle to stream out.
QgsBox3d toBox3d(double zMin, double zMax) const
Converts the rectangle to a 3D box, with the specified zMin and zMax z values.
void setXMinimum(double x)
Set the minimum x value.
Definition: qgsrectangle.h:129
double height() const
Returns the height of the rectangle.
Definition: qgsrectangle.h:208