Added asWktPolygon method to QgsRectangle with a unit test. Discovered QgsRectangle tests were missing their main method causing them to not run. Then discovered that some of these tests fail, one of which remains unresolved.

This commit is contained in:
Tim Sutton 2012-09-10 16:04:22 +02:00
parent 847cae9111
commit 9b0fee38de
8 changed files with 153 additions and 12 deletions

View File

@ -76,6 +76,9 @@ class QgsRectangle
bool isEmpty() const;
//! returns string representation in Wkt form
QString asWktCoordinates() const;
//! returns string representation as WKT Polygon
//@note added om 2.0
QString asWktPolygon() const;
//! returns string representation of form xmin,ymin xmax,ymax
QString toString(bool automaticPrecision = false) const;
//! overloaded toString that allows precision of numbers to be set

View File

@ -184,6 +184,25 @@ QString QgsRectangle::asWktCoordinates() const
return rep;
}
QString QgsRectangle::asWktPolygon() const
{
QString rep =
QString("POLYGON((") +
QString::number( xmin, 'f', 16 ) + " " +
QString::number( ymin, 'f', 16 ) + ", " +
QString::number( xmax, 'f', 16 ) + " " +
QString::number( ymin, 'f', 16 ) + ", " +
QString::number( xmax, 'f', 16 ) + " " +
QString::number( ymax, 'f', 16 ) + ", " +
QString::number( xmin, 'f', 16 ) + " " +
QString::number( ymax, 'f', 16 ) + ", " +
QString::number( xmin, 'f', 16 ) + " " +
QString::number( ymin, 'f', 16 ) +
QString("))");
return rep;
}
// Return a string representation of the rectangle with automatic or high precision
QString QgsRectangle::toString( bool automaticPrecision ) const
{

View File

@ -97,6 +97,9 @@ class CORE_EXPORT QgsRectangle
bool isEmpty() const;
//! returns string representation in Wkt form
QString asWktCoordinates() const;
//! returns string representation as WKT Polygon
//@note added om 2.0
QString asWktPolygon() const;
//! returns string representation of form xmin,ymin xmax,ymax
QString toString( bool automaticPrecision = false ) const;
//! overloaded toString that allows precision of numbers to be set

View File

@ -10,9 +10,12 @@ QGISAPP, CANVAS, IFACE, PARENT = getQgisTestApp()
class TestQgsRectangle(unittest.TestCase):
def testCtor(self):
rect = QgsRectangle( 5.0, 5.0, 10.0, 10.0)
rect = QgsRectangle(5.0, 5.0, 10.0, 10.0)
assert rect.isEmpty(), "Empty rectangle constructed"
myExpectedResult = True
myResult = rect.isEmpty()
myMessage = ('Expected: %s Got: %s' % (myExpectedResult, myResult))
assert rect.isEmpty(), myMessage
myMessage = ('Expected: %s\nGot: %s\n' %
(5.0, rect.xMinimum()))
@ -65,15 +68,15 @@ class TestQgsRectangle(unittest.TestCase):
assert rect1.intersects(rect2), myMessage
rect3 = rect1.intersect(rect2)
assert rect3.isEmpty(), "Empty rectangle returned"
self.assertFalse(rect3.isEmpty(), "Empty rectangle returned")
myMessage = ('Expected: %s\nGot: %s\n' %
(3.0, rect.width()))
assert rect.width() == 3.0, myMessage
(3.0, rect3.width()))
assert rect3.width() == 3.0, myMessage
myMessage = ('Expected: %s\nGot: %s\n' %
(3.0, rect.height()))
assert rect.height() == 3.0, myMessage
(3.0, rect3.height()))
assert rect3.height() == 3.0, myMessage
def testContains(self):
rect1 = QgsRectangle( 0.0, 0.0, 5.0, 5.0)
@ -106,15 +109,19 @@ class TestQgsRectangle(unittest.TestCase):
myMessage = ('Expected: %s\nGot: %s\n' %
(False, rect1.contains(pnt2)))
assert rect1.contains(pnt2), myMessage
self.assertFalse(rect1.contains(pnt2), myMessage)
myMessage = ('Expected: %s\nGot: %s\n' %
(True, rect2.contains(pnt2)))
assert rect2.contains(pnt2), myMessage
myMessage = ('Expected: %s\nGot: %s\n' %
(True, rect3.contains(pnt2)))
assert rect3.contains(pnt2), myMessage
(False, rect3.contains(pnt2)))
self.assertFalse(rect3.contains(pnt2), myMessage)
myMessage = ('Expected: %s\nGot: %s\n' %
(True, rect3.contains(pnt1)))
self.assertTrue(rect3.contains(pnt1), myMessage)
def testUnion(self):
rect1 = QgsRectangle( 0.0, 0.0, 5.0, 5.0)
@ -135,8 +142,11 @@ class TestQgsRectangle(unittest.TestCase):
(True, rect1.contains(pnt1)))
assert rect1.contains(pnt1), myMessage
print rect1.toString()
assert rect1 == QgsRectangle(0.0, 0.0, 6.0, 6.0), "Wrong combine with point result"
myExpectedResult = QgsRectangle(0.0, 0.0, 6.0, 5.0).toString()
myResult = rect1.toString()
myMessage = ('Expected: %s\nGot: %s\n' %
(myExpectedResult, myResult))
self.assertEquals(myResult, myExpectedResult, myMessage)
rect1 = QgsRectangle( 0.0, 0.0, 5.0, 5.0)
rect1.unionRect(rect2)
@ -145,3 +155,29 @@ class TestQgsRectangle(unittest.TestCase):
assert rect1.contains(rect2), myMessage
assert rect1 == QgsRectangle(0.0, 0.0, 7.0, 7.0), "Wrong union result"
def testAsWktCoordinates(self):
"""Test that we can get a proper wkt representation fo the rect"""
rect1 = QgsRectangle( 0.0, 0.0, 5.0, 5.0)
myExpectedWkt = '0.0000000000000000 0.0000000000000000, 5.0000000000000000 5.0000000000000000'
myWkt = rect1.asWktCoordinates()
myMessage = ('Expected: %s\nGot: %s\n' %
(myExpectedWkt, myWkt))
self.assertEquals(myWkt, myExpectedWkt, myMessage)
def testAsWktPolygon(self):
"""Test that we can get a proper wkt polygon representation fo the rect"""
rect1 = QgsRectangle( 0.0, 0.0, 5.0, 5.0)
myExpectedWkt = ('POLYGON((0.0000000000000000 0.0000000000000000, '
'5.0000000000000000 0.0000000000000000, '
'5.0000000000000000 5.0000000000000000, '
'0.0000000000000000 5.0000000000000000, '
'0.0000000000000000 0.0000000000000000))')
myWkt = rect1.asWktPolygon()
myMessage = ('Expected: %s\nGot: %s\n' %
(myExpectedWkt, myWkt))
self.assertEquals(myWkt, myExpectedWkt, myMessage)
if __name__ == '__main__':
unittest.main()

View File

@ -1,5 +1,15 @@
<PAMDataset>
<PAMRasterBand band="1">
<Histograms>
<HistItem>
<HistMin>-0.498046875</HistMin>
<HistMax>255.498046875</HistMax>
<BucketCount>256</BucketCount>
<IncludeOutOfRange>0</IncludeOutOfRange>
<Approximate>0</Approximate>
<HistCounts>0|0|1|0|0|1|0|0|0|0|1|0|1|0|0|1|0|1|0|0|1|0|1|0|0|1|0|1|0|0|1|0|0|0|0|1|0|0|1|0|1|0|0|1|0|1|0|0|1|0|1|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|1|0|0|1|0|0|0|0|1|0|1|0|0|1|0|1|0|0|1|0|0|1|0|1|0|0|1|0|1|0|0|0|0|1|0|0|1|0|1|0|0|1|0|1|0|0|1|0|0|1|0|1|0|0|1|0|0|0|0|1|0|1|0|0|1|0|1|0|0|1|0|1|0|0|1|0|1|0|0|1|0|0|0|0|1|0|0|1|0|1|0|0|1|0|1|0|0|1|0|1|0|0|1|0|1|0|0|0|0|1|0|0|1|0|0|1|0|1|0|0|1|0|1|0|0|1|0|1|0|0|1|0|0|0|0|1|0|1|0|0|1|0|1|0|0|1|0|0|1|0|1|0|0|1|0|1|0|0|0|0|1|0|0|1|0|1|0|0|1|0|1|0|0|1|0|0|1|19</HistCounts>
</HistItem>
</Histograms>
<Metadata>
<MDI key="COLOR_TABLE_RULE_RGB_0">2.000000e+00 1.280000e+02 0 255 0 255 255 0</MDI>
<MDI key="COLOR_TABLE_RULE_RGB_1">1.280000e+02 2.540000e+02 255 255 0 255 0 0</MDI>

View File

@ -1,5 +1,15 @@
<PAMDataset>
<PAMRasterBand band="1">
<Histograms>
<HistItem>
<HistMin>-32435.39</HistMin>
<HistMax>33091.39</HistMax>
<BucketCount>100</BucketCount>
<IncludeOutOfRange>0</IncludeOutOfRange>
<Approximate>0</Approximate>
<HistCounts>1|1|0|1|1|1|1|1|1|1|1|1|0|1|1|1|1|1|1|1|0|0|0|0|0|0|0|0|0|0|1|1|0|1|1|1|1|1|1|1|1|1|0|1|1|1|1|1|1|1|1|1|0|1|1|1|1|1|1|1|1|1|0|1|1|1|1|1|1|1|1|1|0|1|1|1|1|1|1|1|1|1|0|1|1|1|1|1|1|1|1|1|0|1|1|1|1|1|1|1</HistCounts>
</HistItem>
</Histograms>
<Metadata>
<MDI key="COLOR_TABLE_RULE_RGB_0">-3.211166e+04 3.276700e+02 0 255 0 255 255 0</MDI>
<MDI key="COLOR_TABLE_RULE_RGB_1">3.276700e+02 3.276700e+04 255 255 0 255 0 0</MDI>

View File

@ -1,5 +1,15 @@
<PAMDataset>
<PAMRasterBand band="1">
<Histograms>
<HistItem>
<HistMin>-3.365659928167135e+38</HistMin>
<HistMax>3.433659951548935e+38</HistMax>
<BucketCount>100</BucketCount>
<IncludeOutOfRange>0</IncludeOutOfRange>
<Approximate>0</Approximate>
<HistCounts>1|1|0|1|1|1|1|1|1|1|1|1|0|1|1|1|1|1|1|1|0|0|0|0|0|0|0|0|0|0|1|1|0|1|1|1|1|1|1|1|1|1|0|1|1|1|1|1|1|1|1|1|0|1|1|1|1|1|1|1|1|1|0|1|1|1|1|1|1|1|1|1|0|1|1|1|1|1|1|1|1|1|0|1|1|1|1|1|1|1|1|1|0|1|1|1|1|1|1|1</HistCounts>
</HistItem>
</Histograms>
<Metadata>
<MDI key="COLOR_TABLE_RULE_RGB_0">0.000000e+00 0.000000e+00 255 127 0 255 127 0</MDI>
<MDI key="COLOR_TABLE_RULES_COUNT">1</MDI>
@ -10,6 +20,16 @@
</Metadata>
</PAMRasterBand>
<PAMRasterBand band="2">
<Histograms>
<HistItem>
<HistMin>-3.365659928167135e+38</HistMin>
<HistMax>3.433659951548935e+38</HistMax>
<BucketCount>100</BucketCount>
<IncludeOutOfRange>0</IncludeOutOfRange>
<Approximate>0</Approximate>
<HistCounts>1|1|0|1|1|1|1|1|1|1|1|1|0|1|1|1|1|1|1|1|0|0|0|0|0|0|0|0|0|0|1|1|0|1|1|1|1|1|1|1|1|1|0|1|1|1|1|1|1|1|1|1|0|1|1|1|1|1|1|1|1|1|0|1|1|1|1|1|1|1|1|1|0|1|1|1|1|1|1|1|1|1|0|1|1|1|1|1|1|1|1|1|0|1|1|1|1|1|1|1</HistCounts>
</HistItem>
</Histograms>
<Metadata>
<MDI key="COLOR_TABLE_RULE_RGB_0">0.000000e+00 0.000000e+00 255 127 0 255 127 0</MDI>
<MDI key="COLOR_TABLE_RULES_COUNT">1</MDI>
@ -20,6 +40,16 @@
</Metadata>
</PAMRasterBand>
<PAMRasterBand band="3">
<Histograms>
<HistItem>
<HistMin>-3.365659928167135e+38</HistMin>
<HistMax>3.433659951548935e+38</HistMax>
<BucketCount>100</BucketCount>
<IncludeOutOfRange>0</IncludeOutOfRange>
<Approximate>0</Approximate>
<HistCounts>1|1|0|1|1|1|1|1|1|1|1|1|0|1|1|1|1|1|1|1|0|0|0|0|0|0|0|0|0|0|1|1|0|1|1|1|1|1|1|1|1|1|0|1|1|1|1|1|1|1|1|1|0|1|1|1|1|1|1|1|1|1|0|1|1|1|1|1|1|1|1|1|0|1|1|1|1|1|1|1|1|1|0|1|1|1|1|1|1|1|1|1|0|1|1|1|1|1|1|1</HistCounts>
</HistItem>
</Histograms>
<Metadata>
<MDI key="COLOR_TABLE_RULE_RGB_0">0.000000e+00 0.000000e+00 255 127 0 255 127 0</MDI>
<MDI key="COLOR_TABLE_RULES_COUNT">1</MDI>

View File

@ -1,5 +1,15 @@
<PAMDataset>
<PAMRasterBand band="1">
<Histograms>
<HistItem>
<HistMin>-32435.39</HistMin>
<HistMax>33091.39</HistMax>
<BucketCount>100</BucketCount>
<IncludeOutOfRange>0</IncludeOutOfRange>
<Approximate>0</Approximate>
<HistCounts>1|1|0|1|1|1|1|1|1|1|1|1|0|1|1|1|1|1|1|1|0|0|0|0|0|0|0|0|0|0|1|1|0|1|1|1|1|1|1|1|1|1|0|1|1|1|1|1|1|1|1|1|0|1|1|1|1|1|1|1|1|1|0|1|1|1|1|1|1|1|1|1|0|1|1|1|1|1|1|1|1|1|0|1|1|1|1|1|1|1|1|1|0|1|1|1|1|1|1|1</HistCounts>
</HistItem>
</Histograms>
<Metadata>
<MDI key="COLOR_TABLE_RULE_RGB_0">-3.211166e+04 3.276700e+02 0 255 0 255 255 0</MDI>
<MDI key="COLOR_TABLE_RULE_RGB_1">3.276700e+02 3.276700e+04 255 255 0 255 0 0</MDI>
@ -11,6 +21,16 @@
</Metadata>
</PAMRasterBand>
<PAMRasterBand band="2">
<Histograms>
<HistItem>
<HistMin>-32435.39</HistMin>
<HistMax>33091.39</HistMax>
<BucketCount>100</BucketCount>
<IncludeOutOfRange>0</IncludeOutOfRange>
<Approximate>0</Approximate>
<HistCounts>1|1|0|1|1|1|1|1|1|1|1|1|0|1|1|1|1|1|1|1|0|0|0|0|0|0|0|0|0|0|1|1|0|1|1|1|1|1|1|1|1|1|0|1|1|1|1|1|1|1|1|1|0|1|1|1|1|1|1|1|1|1|0|1|1|1|1|1|1|1|1|1|0|1|1|1|1|1|1|1|1|1|0|1|1|1|1|1|1|1|1|1|0|1|1|1|1|1|1|1</HistCounts>
</HistItem>
</Histograms>
<Metadata>
<MDI key="COLOR_TABLE_RULE_RGB_0">-3.211166e+04 3.276700e+02 0 255 0 255 255 0</MDI>
<MDI key="COLOR_TABLE_RULE_RGB_1">3.276700e+02 3.276700e+04 255 255 0 255 0 0</MDI>
@ -22,6 +42,16 @@
</Metadata>
</PAMRasterBand>
<PAMRasterBand band="3">
<Histograms>
<HistItem>
<HistMin>-32435.39</HistMin>
<HistMax>33091.39</HistMax>
<BucketCount>100</BucketCount>
<IncludeOutOfRange>0</IncludeOutOfRange>
<Approximate>0</Approximate>
<HistCounts>1|1|0|1|1|1|1|1|1|1|1|1|0|1|1|1|1|1|1|1|0|0|0|0|0|0|0|0|0|0|1|1|0|1|1|1|1|1|1|1|1|1|0|1|1|1|1|1|1|1|1|1|0|1|1|1|1|1|1|1|1|1|0|1|1|1|1|1|1|1|1|1|0|1|1|1|1|1|1|1|1|1|0|1|1|1|1|1|1|1|1|1|0|1|1|1|1|1|1|1</HistCounts>
</HistItem>
</Histograms>
<Metadata>
<MDI key="COLOR_TABLE_RULE_RGB_0">-3.211166e+04 3.276700e+02 0 255 0 255 255 0</MDI>
<MDI key="COLOR_TABLE_RULE_RGB_1">3.276700e+02 3.276700e+04 255 255 0 255 0 0</MDI>