mirror of
https://github.com/qgis/QGIS.git
synced 2025-02-25 00:58:06 -05:00
Fix duplicate QgsRectangle.toString methods
Condenses the 2 duplicate methods to a single method which is usable from the Python bindings.
This commit is contained in:
parent
b54fe1e3a5
commit
45f2cd5583
@ -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 @@ class TestQgsRectangle(unittest.TestCase):
|
||||
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 @@ class TestQgsRectangle(unittest.TestCase):
|
||||
(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()
|
||||
|
Loading…
x
Reference in New Issue
Block a user