Skip to content
Permalink
Browse files

#9060: define global unit conversor

  • Loading branch information
ahuarte47 committed Jan 14, 2014
1 parent a31ebb4 commit 65f43a8ba865604ff18e5e6e71575d217f110376
Showing with 52 additions and 0 deletions.
  1. +50 −0 src/core/qgis.cpp
  2. +2 −0 src/core/qgis.h
@@ -112,6 +112,56 @@ QString QGis::tr( QGis::UnitType unit )
return QCoreApplication::translate( "QGis::UnitType", qPrintable( toLiteral( unit ) ) );
}

double QGis::fromUnitToUnitFactor( QGis::UnitType fromUnit, QGis::UnitType toUnit )
{
#define DEGREE_TO_METER 111319.49079327358
#define FEET_TO_METER 0.3048
#define NMILE_TO_METER 1852.0

// Unify degree units
if ( fromUnit == QGis::DecimalDegrees || fromUnit == QGis::DegreesMinutesSeconds || fromUnit == QGis::DegreesDecimalMinutes )
fromUnit = QGis::Degrees;
if ( toUnit == QGis::DecimalDegrees || toUnit == QGis::DegreesMinutesSeconds || toUnit == QGis::DegreesDecimalMinutes )
toUnit = QGis::Degrees;

// Calculate the conversion factor between the specified units
if ( fromUnit != toUnit && fromUnit != QGis::UnknownUnit && toUnit != QGis::UnknownUnit )
{
switch ( fromUnit )
{
case QGis::Meters:
{
if ( toUnit == QGis::Feet ) return 1.0 / FEET_TO_METER;
if ( toUnit == QGis::Degrees ) return 1.0 / DEGREE_TO_METER;
if ( toUnit == QGis::NauticalMiles ) return 1.0 / NMILE_TO_METER;
break;
}
case QGis::Feet:
{
if ( toUnit == QGis::Meters ) return FEET_TO_METER;
if ( toUnit == QGis::Degrees ) return FEET_TO_METER / DEGREE_TO_METER;
if ( toUnit == QGis::NauticalMiles ) return FEET_TO_METER / NMILE_TO_METER;
break;
}
case QGis::Degrees:
{
if ( toUnit == QGis::Meters ) return DEGREE_TO_METER;
if ( toUnit == QGis::Feet ) return DEGREE_TO_METER / FEET_TO_METER;
if ( toUnit == QGis::NauticalMiles ) return DEGREE_TO_METER / NMILE_TO_METER;
break;
}
case QGis::NauticalMiles:
{
if ( toUnit == QGis::Meters ) return NMILE_TO_METER;
if ( toUnit == QGis::Feet ) return NMILE_TO_METER / FEET_TO_METER;
if ( toUnit == QGis::Degrees ) return NMILE_TO_METER / DEGREE_TO_METER;
break;
}
}
}
return 1.0;
}

void *qgsMalloc( size_t size )
{
if ( size == 0 || long( size ) < 0 )
@@ -248,6 +248,8 @@ class CORE_EXPORT QGis
//! Provides translated version of the type value
// Added in version 2.0
static QString tr( QGis::UnitType unit );
//! Returns the conversion factor between the specified units
static double fromUnitToUnitFactor( QGis::UnitType fromUnit, QGis::UnitType toUnit );

//! User defined event types
enum UserEvent

0 comments on commit 65f43a8

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