QGIS API Documentation  2.11.0-Master
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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 <QRectF>
22 #include <QString>
23 #include <QTextStream>
24 #include <QTransform>
25 #include <QRegExp>
26 #include <qnumeric.h>
27 
28 #include "qgspoint.h"
29 #include "qgsrectangle.h"
30 #include "qgslogger.h"
31 
32 QgsRectangle::QgsRectangle( double newxmin, double newymin, double newxmax, double newymax )
33  : xmin( newxmin ), ymin( newymin ), xmax( newxmax ), ymax( newymax )
34 {
35  normalize();
36 }
37 
38 QgsRectangle::QgsRectangle( QgsPoint const & p1, QgsPoint const & p2 )
39 {
40  set( p1, p2 );
41 }
42 
44 {
45  xmin = qRectF.topLeft().x();
46  ymin = qRectF.topLeft().y();
47  xmax = qRectF.bottomRight().x();
48  ymax = qRectF.bottomRight().y();
49 }
50 
52 {
53  xmin = r.xMinimum();
54  ymin = r.yMinimum();
55  xmax = r.xMaximum();
56  ymax = r.yMaximum();
57 }
58 
59 
60 void QgsRectangle::set( const QgsPoint& p1, const QgsPoint& p2 )
61 {
62  xmin = p1.x();
63  xmax = p2.x();
64  ymin = p1.y();
65  ymax = p2.y();
66  normalize();
67 }
68 
69 void QgsRectangle::set( double xmin_, double ymin_, double xmax_, double ymax_ )
70 {
71  xmin = xmin_;
72  ymin = ymin_;
73  xmax = xmax_;
74  ymax = ymax_;
75  normalize();
76 }
77 
79 {
80  if ( isNull() )
81  return;
82 
83  if ( xmin > xmax )
84  {
85  std::swap( xmin, xmax );
86  }
87  if ( ymin > ymax )
88  {
89  std::swap( ymin, ymax );
90  }
91 } // QgsRectangle::normalize()
92 
93 
95 {
100 }
101 
102 void QgsRectangle::scale( double scaleFactor, const QgsPoint * cp )
103 {
104  // scale from the center
105  double centerX, centerY;
106  if ( cp )
107  {
108  centerX = cp->x();
109  centerY = cp->y();
110  }
111  else
112  {
113  centerX = xmin + width() / 2;
114  centerY = ymin + height() / 2;
115  }
116  scale( scaleFactor, centerX, centerY );
117 }
118 
119 void QgsRectangle::scale( double scaleFactor, double centerX, double centerY )
120 {
121  double newWidth = width() * scaleFactor;
122  double newHeight = height() * scaleFactor;
123  xmin = centerX - newWidth / 2.0;
124  xmax = centerX + newWidth / 2.0;
125  ymin = centerY - newHeight / 2.0;
126  ymax = centerY + newHeight / 2.0;
127 }
128 
130 {
131  return QgsRectangle( xmin - width, ymin - width, xmax + width, ymax + width );
132 }
133 
135 {
136  QgsRectangle intersection = QgsRectangle();
137  //If they don't actually intersect an empty QgsRectangle should be returned
138  if ( !rect || !intersects( *rect ) )
139  {
140  return intersection;
141  }
142 
143  intersection.setXMinimum( xmin > rect->xMinimum() ? xmin : rect->xMinimum() );
144  intersection.setXMaximum( xmax < rect->xMaximum() ? xmax : rect->xMaximum() );
145  intersection.setYMinimum( ymin > rect->yMinimum() ? ymin : rect->yMinimum() );
146  intersection.setYMaximum( ymax < rect->yMaximum() ? ymax : rect->yMaximum() );
147  return intersection;
148 }
149 
150 bool QgsRectangle::intersects( const QgsRectangle& rect ) const
151 {
152  double x1 = ( xmin > rect.xmin ? xmin : rect.xmin );
153  double x2 = ( xmax < rect.xmax ? xmax : rect.xmax );
154  if ( x1 > x2 )
155  return false;
156  double y1 = ( ymin > rect.ymin ? ymin : rect.ymin );
157  double y2 = ( ymax < rect.ymax ? ymax : rect.ymax );
158  if ( y1 > y2 )
159  return false;
160  return true;
161 }
162 
163 bool QgsRectangle::contains( const QgsRectangle& rect ) const
164 {
165  return ( rect.xmin >= xmin && rect.xmax <= xmax && rect.ymin >= ymin && rect.ymax <= ymax );
166 }
167 
168 bool QgsRectangle::contains( const QgsPoint &p ) const
169 {
170  return xmin <= p.x() && p.x() <= xmax &&
171  ymin <= p.y() && p.y() <= ymax;
172 }
173 
175 {
176 
177  xmin = (( xmin < rect->xMinimum() ) ? xmin : rect->xMinimum() );
178  xmax = (( xmax > rect->xMaximum() ) ? xmax : rect->xMaximum() );
179 
180  ymin = (( ymin < rect->yMinimum() ) ? ymin : rect->yMinimum() );
181  ymax = (( ymax > rect->yMaximum() ) ? ymax : rect->yMaximum() );
182 
183 }
184 
185 void QgsRectangle::combineExtentWith( double x, double y )
186 {
187 
188  xmin = (( xmin < x ) ? xmin : x );
189  xmax = (( xmax > x ) ? xmax : x );
190 
191  ymin = (( ymin < y ) ? ymin : y );
192  ymax = (( ymax > y ) ? ymax : y );
193 
194 }
195 
197 {
198  return xmax <= xmin || ymax <= ymin;
199 }
200 
202 {
203  // rectangle created QgsRectangle() or with rect.setMinimal() ?
204  return ( xmin == 0 && xmax == 0 && ymin == 0 && ymax == 0 ) ||
207 }
208 
210 {
211  QString rep =
212  qgsDoubleToString( xmin ) + " " + qgsDoubleToString( ymin ) + ", " +
214 
215  return rep;
216 }
217 
219 {
220  QString rep =
221  QString( "POLYGON((" ) +
222  qgsDoubleToString( xmin ) + " " + qgsDoubleToString( ymin ) + ", " +
223  qgsDoubleToString( xmax ) + " " + qgsDoubleToString( ymin ) + ", " +
224  qgsDoubleToString( xmax ) + " " + qgsDoubleToString( ymax ) + ", " +
225  qgsDoubleToString( xmin ) + " " + qgsDoubleToString( ymax ) + ", " +
227  QString( "))" );
228 
229  return rep;
230 }
231 
234 {
235  return QRectF(( qreal )xmin, ( qreal )ymin, ( qreal )xmax - xmin, ( qreal )ymax - ymin );
236 }
237 
238 // Return a string representation of the rectangle with automatic or high precision
239 QString QgsRectangle::toString( bool automaticPrecision ) const
240 {
241  if ( automaticPrecision )
242  {
243  int precision = 0;
244  if (( width() < 1 || height() < 1 ) && ( width() > 0 && height() > 0 ) )
245  {
246  precision = static_cast<int>( ceil( -1.0 * log10( qMin( width(), height() ) ) ) ) + 1;
247  // sanity check
248  if ( precision > 20 )
249  precision = 20;
250  }
251  return toString( precision );
252  }
253  else
254  return toString( 16 );
255 }
256 
257 // overloaded version of above fn to allow precision to be set
258 // Return a string representation of the rectangle with high precision
259 QString QgsRectangle::toString( int thePrecision ) const
260 {
261  QString rep;
262  if ( isEmpty() )
263  rep = "Empty";
264  else
265  rep = QString( "%1,%2 : %3,%4" )
266  .arg( xmin, 0, 'f', thePrecision )
267  .arg( ymin, 0, 'f', thePrecision )
268  .arg( xmax, 0, 'f', thePrecision )
269  .arg( ymax, 0, 'f', thePrecision );
270 
271  QgsDebugMsgLevel( QString( "Extents : %1" ).arg( rep ), 4 );
272 
273  return rep;
274 }
275 
276 
277 // Return the rectangle as a set of polygon coordinates
279 {
280 // QString rep = tmp.sprintf("%16f %16f,%16f %16f,%16f %16f,%16f %16f,%16f %16f",
281 // xmin, ymin, xmin, ymax, xmax, ymax, xmax, ymin, xmin, ymin);
282  QString rep;
283 
284  QTextStream foo( &rep );
285 
286  foo.setRealNumberPrecision( 8 );
287  foo.setRealNumberNotation( QTextStream::FixedNotation );
288  // NOTE: a polygon isn't a polygon unless its closed. In the case of
289  // a rectangle, that means 5 points (last == first)
290  foo
291  << xmin << " " << ymin << ", "
292  << xmin << " " << ymax << ", "
293  << xmax << " " << ymax << ", "
294  << xmax << " " << ymin << ", "
295  << xmin << " " << ymin;
296 
297  return rep;
298 
299 } // QgsRectangle::asPolygon() const
300 
301 
302 bool QgsRectangle::operator==( const QgsRectangle & r1 ) const
303 {
304  return r1.xMaximum() == xMaximum() &&
305  r1.xMinimum() == xMinimum() &&
306  r1.yMaximum() == yMaximum() &&
307  r1.yMinimum() == yMinimum();
308 }
309 
310 
311 bool QgsRectangle::operator!=( const QgsRectangle & r1 ) const
312 {
313  return ( ! operator==( r1 ) );
314 }
315 
316 
318 {
319  if ( &r != this )
320  {
321  xmax = r.xMaximum();
322  xmin = r.xMinimum();
323  ymax = r.yMaximum();
324  ymin = r.yMinimum();
325  }
326 
327  return *this;
328 }
329 
330 
332 {
333  if ( r.xMinimum() < xMinimum() )
334  setXMinimum( r.xMinimum() );
335  if ( r.xMaximum() > xMaximum() )
336  setXMaximum( r.xMaximum() );
337  if ( r.yMinimum() < yMinimum() )
338  setYMinimum( r.yMinimum() );
339  if ( r.yMaximum() > yMaximum() )
340  setYMaximum( r.yMaximum() );
341 }
342 
344 {
345  if ( qIsInf( xmin ) || qIsInf( ymin ) || qIsInf( xmax ) || qIsInf( ymax ) )
346  {
347  return false;
348  }
349  if ( qIsNaN( xmin ) || qIsNaN( ymin ) || qIsNaN( xmax ) || qIsNaN( ymax ) )
350  {
351  return false;
352  }
353  return true;
354 }
355 
357 {
358  double tmp;
359  tmp = xmin; xmin = ymin; ymin = tmp;
360  tmp = xmax; xmax = ymax; ymax = tmp;
361 }
362 
363 QDataStream& operator<<( QDataStream& out, const QgsRectangle& rectangle )
364 {
365  out << rectangle.xMinimum() << rectangle.yMinimum() << rectangle.xMaximum() << rectangle.yMaximum();
366  return out;
367 }
368 
370 {
371  double xmin, ymin, xmax, ymax;
372  in >> xmin >> ymin >> xmax >> ymax;
373  rectangle.setXMinimum( xmin );
374  rectangle.setYMinimum( ymin );
375  rectangle.setXMaximum( xmax );
376  rectangle.setYMaximum( ymax );
377  return in;
378 }
void unionRect(const QgsRectangle &rect)
updates rectangle to include passed argument
QgsRectangle & operator=(const QgsRectangle &r1)
bool intersects(const QgsRectangle &rect) const
returns true when rectangle intersects with other rectangle
A rectangle specified with double values.
Definition: qgsrectangle.h:35
bool isEmpty() const
test if rectangle is empty.
QRectF toRectF() const
returns a QRectF with same coordinates.
void setMinimal()
Set a rectangle so that min corner is at max and max corner is at min.
void setRealNumberNotation(RealNumberNotation notation)
void setXMaximum(double x)
Set the maximum x value.
Definition: qgsrectangle.h:167
QgsRectangle buffer(double width)
Get rectangle enlarged by buffer.
bool isFinite() const
Returns true if the rectangle has finite boundaries.
double yMaximum() const
Get the y maximum value (top side of rectangle)
Definition: qgsrectangle.h:192
void setRealNumberPrecision(int precision)
bool contains(const QgsRectangle &rect) const
return true when rectangle contains other rectangle
QDataStream & operator>>(QDataStream &in, QgsRectangle &rectangle)
Reads a rectangle from stream in into rectangle.
bool isNull() const
test if the rectangle is null (all coordinates zero or after call to setMinimal()).
double x() const
Definition: qgspoint.h:126
void set(const QgsPoint &p1, const QgsPoint &p2)
Set the rectangle from two QgsPoints.
double ANALYSIS_EXPORT max(double x, double y)
returns the maximum of two doubles or the first argument if both are equal
void combineExtentWith(QgsRectangle *rect)
expand the rectangle so that covers both the original rectangle and the given rectangle ...
qreal x() const
qreal y() const
QgsRectangle(double xmin=0, double ymin=0, double xmax=0, double ymax=0)
Constructor.
double yMinimum() const
Get the y minimum value (bottom side of rectangle)
Definition: qgsrectangle.h:197
double xMaximum() const
Get the x maximum value (right side of rectangle)
Definition: qgsrectangle.h:182
#define QgsDebugMsgLevel(str, level)
Definition: qgslogger.h:34
void setYMinimum(double y)
Set the minimum y value.
Definition: qgsrectangle.h:172
QPointF topLeft() const
QString asWktCoordinates() const
returns string representation in Wkt form
A class to represent a point.
Definition: qgspoint.h:63
bool operator!=(const QgsRectangle &r1) const
QString qgsDoubleToString(const double &a, const int &precision=17)
Definition: qgis.h:339
QString asWktPolygon() const
returns string representation as WKT Polygon
QString asPolygon() const
returns rectangle as a polygon
void setYMaximum(double y)
Set the maximum y value.
Definition: qgsrectangle.h:177
QgsRectangle intersect(const QgsRectangle *rect) const
return the intersection with the given rectangle
double y() const
Definition: qgspoint.h:134
QPointF bottomRight() const
void normalize()
Normalize the rectangle so it has non-negative width/height.
double width() const
Width of the rectangle.
Definition: qgsrectangle.h:202
QString toString(bool automaticPrecision=false) const
returns string representation of form xmin,ymin xmax,ymax
QString arg(qlonglong a, int fieldWidth, int base, const QChar &fillChar) const
double xMinimum() const
Get the x minimum value (left side of rectangle)
Definition: qgsrectangle.h:187
QDataStream & operator<<(QDataStream &out, const QgsRectangle &rectangle)
Writes the list rectangle to stream out.
void setXMinimum(double x)
Set the minimum x value.
Definition: qgsrectangle.h:162
double height() const
Height of the rectangle.
Definition: qgsrectangle.h:207
void invert()
swap x/y
bool operator==(const QgsRectangle &r1) const
void scale(double scaleFactor, const QgsPoint *c=0)
Scale the rectangle around its center point.