QGIS API Documentation  3.6.0-Noosa (5873452)
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 
61 QgsRectangle QgsRectangle::scaled( double scaleFactor, const QgsPointXY *center ) const
62 {
63  QgsRectangle scaledRect = QgsRectangle( *this );
64  scaledRect.scale( scaleFactor, center );
65  return scaledRect;
66 }
67 
69 {
70  double xmin = mXmin - v.x();
71  double xmax = mXmax - v.x();
72  double ymin = mYmin - v.y();
73  double ymax = mYmax - v.y();
74  return QgsRectangle( xmin, ymin, xmax, ymax );
75 }
76 
78 {
79  double xmin = mXmin + v.x();
80  double xmax = mXmax + v.x();
81  double ymin = mYmin + v.y();
82  double ymax = mYmax + v.y();
83  return QgsRectangle( xmin, ymin, xmax, ymax );
84 }
85 
87 {
88  mXmin -= v.x();
89  mXmax -= v.x();
90  mYmin -= v.y();
91  mYmax -= v.y();
92  return *this;
93 }
94 
96 {
97  mXmin += v.x();
98  mXmax += v.x();
99  mYmin += v.y();
100  mYmax += v.y();
101  return *this;
102 }
103 
105 {
106  QString rep =
107  qgsDoubleToString( mXmin ) + ' ' + qgsDoubleToString( mYmin ) + ", " +
108  qgsDoubleToString( mXmax ) + ' ' + qgsDoubleToString( mYmax );
109 
110  return rep;
111 }
112 
114 {
115  QString rep =
116  QStringLiteral( "POLYGON((" ) +
117  qgsDoubleToString( mXmin ) + ' ' + qgsDoubleToString( mYmin ) + ", " +
118  qgsDoubleToString( mXmax ) + ' ' + qgsDoubleToString( mYmin ) + ", " +
119  qgsDoubleToString( mXmax ) + ' ' + qgsDoubleToString( mYmax ) + ", " +
120  qgsDoubleToString( mXmin ) + ' ' + qgsDoubleToString( mYmax ) + ", " +
121  qgsDoubleToString( mXmin ) + ' ' + qgsDoubleToString( mYmin ) +
122  QStringLiteral( "))" );
123 
124  return rep;
125 }
126 
127 QString QgsRectangle::toString( int precision ) const
128 {
129  QString rep;
130 
131  if ( precision < 0 )
132  {
133  precision = 0;
134  if ( ( width() < 10 || height() < 10 ) && ( width() > 0 && height() > 0 ) )
135  {
136  precision = static_cast<int>( std::ceil( -1.0 * std::log10( std::min( width(), height() ) ) ) ) + 1;
137  // sanity check
138  if ( precision > 20 )
139  precision = 20;
140  }
141  }
142 
143  if ( isEmpty() )
144  rep = QStringLiteral( "Empty" );
145  else
146  rep = QStringLiteral( "%1,%2 : %3,%4" )
147  .arg( mXmin, 0, 'f', precision )
148  .arg( mYmin, 0, 'f', precision )
149  .arg( mXmax, 0, 'f', precision )
150  .arg( mYmax, 0, 'f', precision );
151 
152  QgsDebugMsgLevel( QStringLiteral( "Extents : %1" ).arg( rep ), 4 );
153 
154  return rep;
155 }
156 
157 QString QgsRectangle::asPolygon() const
158 {
159 // QString rep = tmp.sprintf("%16f %16f,%16f %16f,%16f %16f,%16f %16f,%16f %16f",
160 // xmin, ymin, xmin, ymax, xmax, ymax, xmax, ymin, xmin, ymin);
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
170  << mXmin << ' ' << mYmin << ", "
171  << mXmin << ' ' << mYmax << ", "
172  << mXmax << ' ' << mYmax << ", "
173  << mXmax << ' ' << mYmin << ", "
174  << mXmin << ' ' << mYmin;
175 
176  return rep;
177 
178 }
179 
180 QgsBox3d QgsRectangle::toBox3d( double zMin, double zMax ) const
181 {
182  return QgsBox3d( mXmin, mYmin, zMin, mXmax, mYmax, zMax );
183 }
184 
186 {
187  // helper function
188  auto gridifyValue = []( double value, double spacing ) -> double
189  {
190  if ( spacing > 0 )
191  return std::round( value / spacing ) * spacing;
192  else
193  return value;
194  };
195 
196  return QgsRectangle(
197  gridifyValue( mXmin, spacing ),
198  gridifyValue( mYmin, spacing ),
199  gridifyValue( mXmax, spacing ),
200  gridifyValue( mYmax, spacing )
201  );
202 }
203 
204 QDataStream &operator<<( QDataStream &out, const QgsRectangle &rectangle )
205 {
206  out << rectangle.xMinimum() << rectangle.yMinimum() << rectangle.xMaximum() << rectangle.yMaximum();
207  return out;
208 }
209 
210 QDataStream &operator>>( QDataStream &in, QgsRectangle &rectangle )
211 {
212  double xmin, ymin, xmax, ymax;
213  in >> xmin >> ymin >> xmax >> ymax;
214  rectangle.setXMinimum( xmin );
215  rectangle.setYMinimum( ymin );
216  rectangle.setXMaximum( xmax );
217  rectangle.setYMaximum( ymax );
218  return in;
219 }
int precision
A rectangle specified with double values.
Definition: qgsrectangle.h:41
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:135
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
void scale(double scaleFactor, const QgsPointXY *c=nullptr)
Scale the rectangle around its center point.
Definition: qgsrectangle.h:235
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:68
A geometry is the spatial representation of a feature.
Definition: qgsgeometry.h:106
A 3-dimensional box composed of x, y, z coordinates.
Definition: qgsbox3d.h:36
QgsRectangle snappedToGrid(double spacing) const
Returns a copy of this rectangle that is snapped to a grid with the specified spacing between the gri...
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:426
QgsPolygonXY asPolygon() const
Returns the contents of the geometry as a polygon.
double width() const
Returns the width of the rectangle.
Definition: qgsrectangle.h:202
QString qgsDoubleToString(double a, int precision=17)
Returns a string representation of a double.
Definition: qgis.h:225
void setYMinimum(double y)
Set the minimum y value.
Definition: qgsrectangle.h:140
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:29
double yMinimum() const
Returns the y minimum value (bottom side of rectangle).
Definition: qgsrectangle.h:177
double xMaximum() const
Returns the x maximum value (right side of rectangle).
Definition: qgsrectangle.h:162
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:44
QgsRectangle operator+(QgsVector v) const
Returns a rectangle offset from this one in the direction of the 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.
Definition: qgsrectangle.h:145
QgsRectangle()=default
Constructor for a null rectangle.
double xMinimum() const
Returns the x minimum value (left side of rectangle).
Definition: qgsrectangle.h:167
double yMaximum() const
Returns the y maximum value (top side of rectangle).
Definition: qgsrectangle.h:172
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:230
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:130
double height() const
Returns the height of the rectangle.
Definition: qgsrectangle.h:209