Skip to content

Commit

Permalink
Fix duplicate QgsRectangle.toString methods
Browse files Browse the repository at this point in the history
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 45f2cd5
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 23 deletions.
10 changes: 6 additions & 4 deletions python/core/qgsrectangle.sip
Expand Up @@ -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
Expand Down
23 changes: 9 additions & 14 deletions src/core/qgsrectangle.cpp
Expand Up @@ -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
Expand Down
11 changes: 7 additions & 4 deletions src/core/qgsrectangle.h
Expand Up @@ -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;

Expand Down
43 changes: 42 additions & 1 deletion tests/src/python/test_qgsrectangle.py
Expand Up @@ -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)
Expand Down Expand Up @@ -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.