Skip to content

Commit 3fb8093

Browse files
committed
Merge pull request #234 from homann/test_qgsclipper
Test qgsclipper, improved testing and added constructor in QgsRectangle.
2 parents 8a347eb + 9629501 commit 3fb8093

File tree

5 files changed

+80
-23
lines changed

5 files changed

+80
-23
lines changed

python/core/qgsrectangle.sip

+8-2
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,15 @@ class QgsRectangle
1010
%TypeHeaderCode
1111
#include <qgsrectangle.h>
1212
%End
13-
13+
1414
public:
1515
//! Constructor
1616
QgsRectangle(double xmin=0, double ymin=0, double xmax=0, double ymax=0);
1717
//! Construct a rectangle from two points. The rectangle is normalized after construction.
1818
QgsRectangle(const QgsPoint & p1, const QgsPoint & p2);
19+
//! Construct a rectangle from a QRectF. The rectangle is normalized after construction.
20+
//@note added in 2.0
21+
QgsRectangle(const QRectF & qRectF );
1922
//! Copy constructor
2023
QgsRectangle(const QgsRectangle &other);
2124
//! Destructor
@@ -77,8 +80,11 @@ class QgsRectangle
7780
//! returns string representation in Wkt form
7881
QString asWktCoordinates() const;
7982
//! returns string representation as WKT Polygon
80-
//@note added om 2.0
83+
//@note added in 2.0
8184
QString asWktPolygon() const;
85+
//! returns a QRectF with same coordinates.
86+
//@note added in 2.0
87+
QRectF QgsRectangle::toRectF() const;
8288
//! returns string representation of form xmin,ymin xmax,ymax
8389
QString toString(bool automaticPrecision = false) const;
8490
//! overloaded toString that allows precision of numbers to be set

src/core/qgsrectangle.cpp

+18-2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include <algorithm>
1919
#include <cmath>
2020
#include <limits>
21+
#include <QRectF>
2122
#include <QString>
2223
#include <QTextStream>
2324
#include <qnumeric.h>
@@ -37,6 +38,14 @@ QgsRectangle::QgsRectangle( QgsPoint const & p1, QgsPoint const & p2 )
3738
set( p1, p2 );
3839
}
3940

41+
QgsRectangle::QgsRectangle( QRectF const & qRectF )
42+
{
43+
xmin = qRectF.topLeft().x();
44+
ymin = qRectF.topLeft().y();
45+
xmax = qRectF.bottomRight().x();
46+
ymax = qRectF.bottomRight().y();
47+
}
48+
4049
QgsRectangle::QgsRectangle( const QgsRectangle &r )
4150
{
4251
xmin = r.xMinimum();
@@ -187,7 +196,7 @@ QString QgsRectangle::asWktCoordinates() const
187196
QString QgsRectangle::asWktPolygon() const
188197
{
189198
QString rep =
190-
QString("POLYGON((") +
199+
QString( "POLYGON((" ) +
191200
QString::number( xmin, 'f', 16 ) + " " +
192201
QString::number( ymin, 'f', 16 ) + ", " +
193202
QString::number( xmax, 'f', 16 ) + " " +
@@ -198,11 +207,18 @@ QString QgsRectangle::asWktPolygon() const
198207
QString::number( ymax, 'f', 16 ) + ", " +
199208
QString::number( xmin, 'f', 16 ) + " " +
200209
QString::number( ymin, 'f', 16 ) +
201-
QString("))");
210+
QString( "))" );
202211

203212
return rep;
204213
}
205214

215+
//! returns a QRectF with same coordinates.
216+
//@note added in 2.0
217+
QRectF QgsRectangle::toRectF() const
218+
{
219+
return QRectF(( qreal )xmin, ( qreal )ymin, ( qreal )xmax - xmin, ( qreal )ymax - ymin );
220+
}
221+
206222
// Return a string representation of the rectangle with automatic or high precision
207223
QString QgsRectangle::toString( bool automaticPrecision ) const
208224
{

src/core/qgsrectangle.h

+8-2
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
#include <iosfwd>
2222

2323
class QString;
24-
24+
class QRectF;
2525
#include "qgspoint.h"
2626

2727

@@ -38,6 +38,9 @@ class CORE_EXPORT QgsRectangle
3838
QgsRectangle( double xmin = 0, double ymin = 0, double xmax = 0, double ymax = 0 );
3939
//! Construct a rectangle from two points. The rectangle is normalized after construction.
4040
QgsRectangle( QgsPoint const & p1, QgsPoint const & p2 );
41+
//! Construct a rectangle from a QRectF. The rectangle is normalized after construction.
42+
//@note added in 2.0
43+
QgsRectangle( const QRectF & qRectF );
4144
//! Copy constructor
4245
QgsRectangle( const QgsRectangle &other );
4346
//! Destructor
@@ -98,8 +101,11 @@ class CORE_EXPORT QgsRectangle
98101
//! returns string representation in Wkt form
99102
QString asWktCoordinates() const;
100103
//! returns string representation as WKT Polygon
101-
//@note added om 2.0
104+
//@note added in 2.0
102105
QString asWktPolygon() const;
106+
//! returns a QRectF with same coordinates.
107+
//@note added in 2.0
108+
QRectF toRectF() const;
103109
//! returns string representation of form xmin,ymin xmax,ymax
104110
QString toString( bool automaticPrecision = false ) const;
105111
//! overloaded toString that allows precision of numbers to be set

tests/src/core/testqgsclipper.cpp

+37-17
Original file line numberDiff line numberDiff line change
@@ -34,36 +34,56 @@ class TestQgsClipper: public QObject
3434
void init() {};// will be called before each testfunction is executed.
3535
void cleanup() {};// will be called after every testfunction.
3636
void basic();
37+
private:
38+
bool TestQgsClipper::checkBoundingBox( QPolygonF polygon, QgsRectangle clipRect );
3739
};
3840

3941
void TestQgsClipper::initTestCase()
4042
{
41-
//
42-
// Runs once before any tests are run
43-
//
44-
// init QGIS's paths - true means that all path will be inited from prefix
45-
// QgsApplication::init();
46-
// QgsApplication::initQgis();
47-
// QgsApplication::showSettings();
43+
4844
}
4945

5046
void TestQgsClipper::basic()
5147
{
52-
// CQgsClipper is static only
53-
//QgsClipper snipsnip;
48+
// QgsClipper is static only
5449

5550
QPolygonF polygon;
56-
polygon << QPointF(10.4, 20.5) << QPointF(20.2, 30.2);
57-
58-
QgsRectangle clipRect(10, 10, 25, 30 );
59-
51+
polygon << QPointF( 10.4, 20.5 ) << QPointF( 20.2, 30.2 );
52+
53+
QgsRectangle clipRect( 10, 10, 25, 30 );
54+
6055
QgsClipper::trimPolygon( polygon, clipRect );
61-
62-
QRectF bBox( polygon.boundingRect() );
63-
QgsRectangle boundingRect( bBox.bottomLeft().x(), bBox.bottomLeft().y(), bBox.topRight().x(), bBox.topRight().y() );
6456

65-
QVERIFY( clipRect.contains( boundingRect ) );
57+
// Check nothing sticks out.
58+
QVERIFY( checkBoundingBox( polygon , clipRect ) );
59+
// Check that it didn't clip too much
60+
QgsRectangle clipRectInner( clipRect );
61+
clipRectInner.scale( 0.999 );
62+
QVERIFY( ! checkBoundingBox( polygon , clipRectInner ) );
63+
64+
// A more complex example
65+
polygon.clear();
66+
polygon << QPointF( 1.0, 9.0 ) << QPointF( 11.0, 11.0 ) << QPointF( 9.0, 1.0 );
67+
clipRect.set( 0.0, 0.0, 10.0, 10.0 );
68+
69+
QgsClipper::trimPolygon( polygon, clipRect );
70+
71+
// We should have 5 vertices now?
72+
QCOMPARE( polygon.size(), 5 );
73+
// Check nothing sticks out.
74+
QVERIFY( checkBoundingBox( polygon , clipRect ) );
75+
// Check that it didn't clip too much
76+
clipRectInner = clipRect;
77+
clipRectInner.scale( 0.999 );
78+
QVERIFY( ! checkBoundingBox( polygon , clipRectInner ) );
6679
};
6780

81+
bool TestQgsClipper::checkBoundingBox( QPolygonF polygon, QgsRectangle clipRect )
82+
{
83+
QgsRectangle bBox( polygon.boundingRect() );
84+
85+
return clipRect.contains( bBox );
86+
}
87+
6888
QTEST_MAIN( TestQgsClipper )
6989
#include "moc_testqgsclipper.cxx"

tests/src/core/testqgsrectangle.cpp

+9
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,15 @@ void TestQgsRectangle::regression6194()
5959
// 100 wide, 200 high
6060
QgsRectangle rect1 = QgsRectangle( 10.0, 20.0, 110.0, 220.0 );
6161

62+
// Test conversion to QRectF and back
63+
QRectF qRectF = rect1.toRectF();
64+
QCOMPARE( qRectF.width(), 100.0 );
65+
QCOMPARE( qRectF.height(), 200.0 );
66+
QCOMPARE( qRectF.x(), 10.0 );
67+
QCOMPARE( qRectF.y(), 20.0 );
68+
QgsRectangle rect4 = QgsRectangle( qRectF );
69+
QCOMPARE( rect4.toString( 2 ), QString( "10.00,20.00 : 110.00,220.00" ) );
70+
6271
// 250 wide, 500 high
6372
QgsRectangle rect2;
6473
rect2.setXMinimum( 10.0 );

0 commit comments

Comments
 (0)