Skip to content
Permalink
Browse files

Merge pull request #4213 from m-kuhn/formatDistanceAreaNew

Modularize distance and area formatting
  • Loading branch information
nyalldawson committed Mar 8, 2017
2 parents e097904 + 8b2180f commit 9fd7555db310637ea7249079026b02e98f486a62
@@ -88,6 +88,12 @@ def start_test_fold():
in_failure = False
else:
updated_line = colored(updated_line, 'yellow')
elif re.search('\*\*\* Segmentation fault', updated_line):
start_fold('segfault')
updated_line = colored(updated_line, 'magenta')
elif re.match(' Test failed: Segmentation fault', updated_line):
end_fold()

else:
if re.match('FAIL[:\!].*', updated_line):
updated_line = colored(updated_line, 'yellow')
@@ -6,6 +6,7 @@ class QgsDistanceArea
%End

public:

//! Constructor
QgsDistanceArea();

@@ -11,6 +11,17 @@ class QgsUnitTypes
%End

public:
struct DistanceValue
{
double value;
QgsUnitTypes::DistanceUnit unit;
};

struct AreaValue
{
double value;
QgsUnitTypes::AreaUnit unit;
};

//! Units of distance
enum DistanceUnit
@@ -22,6 +33,8 @@ class QgsUnitTypes
DistanceYards, /*!< imperial yards */
DistanceMiles, /*!< terrestrial miles */
DistanceDegrees, /*!< degrees, for planar geographic CRS distance measurements */
DistanceCentimeters,
DistanceMillimeters,
DistanceUnknownUnit, /*!< unknown distance unit */
};

@@ -46,7 +59,9 @@ class QgsUnitTypes
AreaAcres, /*!< acres */
AreaSquareNauticalMiles, /*!< square nautical miles */
AreaSquareDegrees, /*!< square degrees, for planar geographic CRS area measurements */
AreaUnknownUnit, /*!< unknown areal unit */
AreaSquareCentimeters,
AreaSquareMillimeters,
AreaUnknownUnit,
};

//! Units of angles
@@ -191,6 +206,14 @@ class QgsUnitTypes
*/
static QString formatAngle( double angle, int decimals, AngleUnit unit );

static QgsUnitTypes::DistanceValue scaledDistance( double distance, QgsUnitTypes::DistanceUnit unit, int decimals, bool keepBaseUnit = false );

static QgsUnitTypes::AreaValue scaledArea( double area, QgsUnitTypes::AreaUnit unit, int decimals, bool keepBaseUnit = false );

static QString formatDistance( double distance, int decimals, QgsUnitTypes::DistanceUnit unit, bool keepBaseUnit = false );

static QString formatArea( double area, int decimals, QgsUnitTypes::AreaUnit unit, bool keepBaseUnit = false );

// RENDER UNITS

/** Encodes a render unit to a string.
@@ -233,6 +233,17 @@ inline double qgsRound( double x )
return x < 0.0 ? std::ceil( x - 0.5 ) : std::floor( x + 0.5 );
}

/**
* Returns a double \a number, rounded (as close as possible) to the specified number of \a places.
*
* @note Added in QGIS 3.0
*/
inline double qgsRound( double number, double places )
{
int scaleFactor = pow( 10, places );
return static_cast<double>( static_cast<qlonglong>( number * scaleFactor + 0.5 ) ) / scaleFactor;
}

/** Converts a string to a double in a permissive way, e.g., allowing for incorrect
* numbers of digits between thousand separators
* @param string string to convert
@@ -981,240 +981,12 @@ double QgsDistanceArea::computePolygonFlatArea( const QList<QgsPoint> &points )

QString QgsDistanceArea::formatDistance( double distance, int decimals, QgsUnitTypes::DistanceUnit unit, bool keepBaseUnit )
{
QString unitLabel;

switch ( unit )
{
case QgsUnitTypes::DistanceMeters:
if ( keepBaseUnit || qAbs( distance ) == 0.0 )
{
unitLabel = QObject::tr( " m" );
}
else if ( qAbs( distance ) > 1000.0 )
{
unitLabel = QObject::tr( " km" );
distance = distance / 1000;
}
else if ( qAbs( distance ) < 0.01 )
{
unitLabel = QObject::tr( " mm" );
distance = distance * 1000;
}
else if ( qAbs( distance ) < 0.1 )
{
unitLabel = QObject::tr( " cm" );
distance = distance * 100;
}
else
{
unitLabel = QObject::tr( " m" );
}
break;

case QgsUnitTypes::DistanceKilometers:
if ( keepBaseUnit || qAbs( distance ) >= 1.0 )
{
unitLabel = QObject::tr( " km" );
}
else
{
unitLabel = QObject::tr( " m" );
distance = distance * 1000;
}
break;

case QgsUnitTypes::DistanceFeet:
if ( qAbs( distance ) <= 5280.0 || keepBaseUnit )
{
unitLabel = QObject::tr( " ft" );
}
else
{
unitLabel = QObject::tr( " mi" );
distance /= 5280.0;
}
break;

case QgsUnitTypes::DistanceYards:
if ( qAbs( distance ) <= 1760.0 || keepBaseUnit )
{
unitLabel = QObject::tr( " yd" );
}
else
{
unitLabel = QObject::tr( " mi" );
distance /= 1760.0;
}
break;

case QgsUnitTypes::DistanceMiles:
if ( qAbs( distance ) >= 1.0 || keepBaseUnit )
{
unitLabel = QObject::tr( " mi" );
}
else
{
unitLabel = QObject::tr( " ft" );
distance *= 5280.0;
}
break;

case QgsUnitTypes::DistanceNauticalMiles:
unitLabel = QObject::tr( " NM" );
break;

case QgsUnitTypes::DistanceDegrees:

if ( qAbs( distance ) == 1.0 )
unitLabel = QObject::tr( " degree" );
else
unitLabel = QObject::tr( " degrees" );
break;

case QgsUnitTypes::DistanceUnknownUnit:
unitLabel.clear();
break;
default:
QgsDebugMsg( QString( "Error: not picked up map units - actual value = %1" ).arg( unit ) );
break;
}

return QStringLiteral( "%L1%2" ).arg( distance, 0, 'f', decimals ).arg( unitLabel );
return QgsUnitTypes::formatDistance( distance, decimals, unit, keepBaseUnit );
}

QString QgsDistanceArea::formatArea( double area, int decimals, QgsUnitTypes::AreaUnit unit, bool keepBaseUnit )
{
QString unitLabel;

switch ( unit )
{
case QgsUnitTypes::AreaSquareMeters:
{
if ( keepBaseUnit )
{
unitLabel = QObject::trUtf8( "" );
}
else if ( qAbs( area ) > QgsUnitTypes::fromUnitToUnitFactor( QgsUnitTypes::AreaSquareKilometers, QgsUnitTypes::AreaSquareMeters ) )
{
unitLabel = QObject::trUtf8( " km²" );
area = area * QgsUnitTypes::fromUnitToUnitFactor( QgsUnitTypes::AreaSquareMeters, QgsUnitTypes::AreaSquareKilometers );
}
else if ( qAbs( area ) > QgsUnitTypes::fromUnitToUnitFactor( QgsUnitTypes::AreaHectares, QgsUnitTypes::AreaSquareMeters ) )
{
unitLabel = QObject::tr( " ha" );
area = area * QgsUnitTypes::fromUnitToUnitFactor( QgsUnitTypes::AreaSquareMeters, QgsUnitTypes::AreaHectares );
}
else
{
unitLabel = QObject::trUtf8( "" );
}
break;
}

case QgsUnitTypes::AreaSquareKilometers:
{
unitLabel = QObject::trUtf8( " km²" );
break;
}

case QgsUnitTypes::AreaSquareFeet:
{
if ( keepBaseUnit )
{
unitLabel = QObject::trUtf8( " ft²" );
}
else if ( qAbs( area ) > QgsUnitTypes::fromUnitToUnitFactor( QgsUnitTypes::AreaSquareMiles, QgsUnitTypes::AreaSquareFeet ) )
{
unitLabel = QObject::trUtf8( " mi²" );
area = area * QgsUnitTypes::fromUnitToUnitFactor( QgsUnitTypes::AreaSquareFeet, QgsUnitTypes::AreaSquareMiles );
}
else
{
unitLabel = QObject::trUtf8( " ft²" );
}
break;
}

case QgsUnitTypes::AreaSquareYards:
{
if ( keepBaseUnit )
{
unitLabel = QObject::trUtf8( " yd²" );
}
else if ( qAbs( area ) > QgsUnitTypes::fromUnitToUnitFactor( QgsUnitTypes::AreaSquareMiles, QgsUnitTypes::AreaSquareYards ) )
{
unitLabel = QObject::trUtf8( " mi²" );
area = area * QgsUnitTypes::fromUnitToUnitFactor( QgsUnitTypes::AreaSquareYards, QgsUnitTypes::AreaSquareMiles );
}
else
{
unitLabel = QObject::trUtf8( " yd²" );
}
break;
}

case QgsUnitTypes::AreaSquareMiles:
{
unitLabel = QObject::trUtf8( " mi²" );
break;
}

case QgsUnitTypes::AreaHectares:
{
if ( keepBaseUnit )
{
unitLabel = QObject::trUtf8( " ha" );
}
else if ( qAbs( area ) > QgsUnitTypes::fromUnitToUnitFactor( QgsUnitTypes::AreaSquareKilometers, QgsUnitTypes::AreaHectares ) )
{
unitLabel = QObject::trUtf8( " km²" );
area = area * QgsUnitTypes::fromUnitToUnitFactor( QgsUnitTypes::AreaHectares, QgsUnitTypes::AreaSquareKilometers );
}
else
{
unitLabel = QObject::trUtf8( " ha" );
}
break;
}

case QgsUnitTypes::AreaAcres:
{
if ( keepBaseUnit )
{
unitLabel = QObject::trUtf8( " ac" );
}
else if ( qAbs( area ) > QgsUnitTypes::fromUnitToUnitFactor( QgsUnitTypes::AreaSquareMiles, QgsUnitTypes::AreaAcres ) )
{
unitLabel = QObject::trUtf8( " mi²" );
area = area * QgsUnitTypes::fromUnitToUnitFactor( QgsUnitTypes::AreaAcres, QgsUnitTypes::AreaSquareMiles );
}
else
{
unitLabel = QObject::trUtf8( " ac" );
}
break;
}

case QgsUnitTypes::AreaSquareNauticalMiles:
{
unitLabel = QObject::trUtf8( " nm²" );
break;
}

case QgsUnitTypes::AreaSquareDegrees:
{
unitLabel = QObject::tr( " sq.deg." );
break;
}

case QgsUnitTypes::AreaUnknownUnit:
{
unitLabel.clear();
break;
}
}

return QStringLiteral( "%L1%2" ).arg( area, 0, 'f', decimals ).arg( unitLabel );
return QgsUnitTypes::formatArea( area, decimals, unit, keepBaseUnit );
}

double QgsDistanceArea::convertLengthMeasurement( double length, QgsUnitTypes::DistanceUnit toUnits ) const
@@ -39,6 +39,7 @@ General purpose distance and area calculator.
class CORE_EXPORT QgsDistanceArea
{
public:

//! Constructor
QgsDistanceArea();

0 comments on commit 9fd7555

Please sign in to comment.
You can’t perform that action at this time.