Skip to content
Permalink
Browse files
Fix duplicate QgsRectangle.toString methods
Condenses the 2 duplicate methods to a single method which is usable from the Python bindings.
  • Loading branch information
jdugge authored and nyalldawson committed Mar 21, 2017
1 parent b54fe1e commit 45f2cd5583ad4115bd8cbd687729d465910696ca
Showing with 64 additions and 23 deletions.
  1. +6 −4 python/core/qgsrectangle.sip
  2. +9 −14 src/core/qgsrectangle.cpp
  3. +7 −4 src/core/qgsrectangle.h
  4. +42 −1 tests/src/python/test_qgsrectangle.py
@@ -90,10 +90,12 @@ class QgsRectangle
QString asWktPolygon() const;
//! returns a QRectF with same coordinates.
QRectF toRectF() 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
QString toString( int precision ) const;
/**
* returns a string representation of form xmin,ymin : xmax,ymax
* Coordinates will be truncated to the specified precision.
* If the specified precision is less than 0, a suitable minimum precision is used.
*/
QString toString( int precision = 16 ) const;
//! returns rectangle as a polygon
QString asPolygon() const;
/** Comparison operator
@@ -265,30 +265,25 @@ QRectF QgsRectangle::toRectF() const
return QRectF( static_cast< qreal >( xmin ), static_cast< qreal >( ymin ), static_cast< qreal >( xmax - xmin ), static_cast< qreal >( ymax - ymin ) );
}

// Return a string representation of the rectangle with automatic or high precision
QString QgsRectangle::toString( bool automaticPrecision ) const
// Returns a string representation of form xmin,ymin : xmax,ymax. Coordinates will be truncated
// to the specified \a precision. If \a precision is less than 0 then a suitable minimum precision
// will be automatically calculated.
QString QgsRectangle::toString( int precision ) const
{
if ( automaticPrecision )
QString rep;

if ( precision < 0 )
{
int precision = 0;
if ( ( width() < 1 || height() < 1 ) && ( width() > 0 && height() > 0 ) )
precision = 0;
if ( ( width() < 10 || height() < 10 ) && ( width() > 0 && height() > 0 ) )
{
precision = static_cast<int>( ceil( -1.0 * log10( qMin( width(), height() ) ) ) ) + 1;
// sanity check
if ( precision > 20 )
precision = 20;
}
return toString( precision );
}
else
return toString( 16 );
}

// overloaded version of above fn to allow precision to be set
// Return a string representation of the rectangle with high precision
QString QgsRectangle::toString( int precision ) const
{
QString rep;
if ( isEmpty() )
rep = QStringLiteral( "Empty" );
else
@@ -115,10 +115,13 @@ class CORE_EXPORT QgsRectangle
QString asWktPolygon() const;
//! returns a QRectF with same coordinates.
QRectF toRectF() 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
QString toString( int precision ) const;

/**
* returns a string representation of form xmin,ymin : xmax,ymax
* Coordinates will be truncated to the specified precision.
* If the specified precision is less than 0, a suitable minimum precision is used.
*/
QString toString( int precision = 16 ) const;
//! returns rectangle as a polygon
QString asPolygon() const;

@@ -151,7 +151,8 @@ def testUnion(self):
assert rect1.contains(rect2), myMessage

print((rect1.toString()))
assert rect1 == QgsRectangle(0.0, 0.0, 7.0, 7.0), 'Wrong combine with rectangle result'
assert rect1 == QgsRectangle(
0.0, 0.0, 7.0, 7.0), 'Wrong combine with rectangle result'

rect1 = QgsRectangle(0.0, 0.0, 5.0, 5.0)
rect1.combineExtentWith(6.0, 2.0)
@@ -196,6 +197,46 @@ def testAsWktPolygon(self):
(myExpectedWkt, myWkt))
assert compareWkt(myWkt, myExpectedWkt), myMessage

def testToString(self):
"""Test the different string representations"""
rect = QgsRectangle(0, 0.1, 0.2, 0.3)
myExpectedString = '0.0000000000000000,0.1000000000000000 : 0.2000000000000000,0.3000000000000000'
myString = rect.toString()
myMessage = ('Expected: %s\nGot: %s\n' %
(myExpectedString, myString))
assert myString == myExpectedString, myMessage

myExpectedString = '0,0 : 0,0'
myString = rect.toString(0)
myMessage = ('Expected: %s\nGot: %s\n' %
(myExpectedString, myString))
assert myString == myExpectedString, myMessage

myExpectedString = '0.00,0.10 : 0.20,0.30'
myString = rect.toString(2)
myMessage = ('Expected: %s\nGot: %s\n' %
(myExpectedString, myString))
assert myString == myExpectedString, myMessage

myExpectedString = '0.0,0.1 : 0.2,0.3'
myString = rect.toString(1)
myMessage = ('Expected: %s\nGot: %s\n' %
(myExpectedString, myString))
assert myString == myExpectedString, myMessage

myExpectedString = '0.00,0.10 : 0.20,0.30'
myString = rect.toString(-1)
myMessage = ('Expected: %s\nGot: %s\n' %
(myExpectedString, myString))
assert myString == myExpectedString, myMessage

rect = QgsRectangle(5000000.01111, -0.3, 5000000.44111, 99.8)
myExpectedString = '5000000.01,-0.30 : 5000000.44,99.80'
myString = rect.toString(-1)
myMessage = ('Expected: %s\nGot: %s\n' %
(myExpectedString, myString))
assert myString == myExpectedString, myMessage


if __name__ == '__main__':
unittest.main()

0 comments on commit 45f2cd5

Please sign in to comment.