Skip to content

Commit 65f43a8

Browse files
committed
#9060: define global unit conversor
1 parent a31ebb4 commit 65f43a8

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed

src/core/qgis.cpp

+50
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,56 @@ QString QGis::tr( QGis::UnitType unit )
112112
return QCoreApplication::translate( "QGis::UnitType", qPrintable( toLiteral( unit ) ) );
113113
}
114114

115+
double QGis::fromUnitToUnitFactor( QGis::UnitType fromUnit, QGis::UnitType toUnit )
116+
{
117+
#define DEGREE_TO_METER 111319.49079327358
118+
#define FEET_TO_METER 0.3048
119+
#define NMILE_TO_METER 1852.0
120+
121+
// Unify degree units
122+
if ( fromUnit == QGis::DecimalDegrees || fromUnit == QGis::DegreesMinutesSeconds || fromUnit == QGis::DegreesDecimalMinutes )
123+
fromUnit = QGis::Degrees;
124+
if ( toUnit == QGis::DecimalDegrees || toUnit == QGis::DegreesMinutesSeconds || toUnit == QGis::DegreesDecimalMinutes )
125+
toUnit = QGis::Degrees;
126+
127+
// Calculate the conversion factor between the specified units
128+
if ( fromUnit != toUnit && fromUnit != QGis::UnknownUnit && toUnit != QGis::UnknownUnit )
129+
{
130+
switch ( fromUnit )
131+
{
132+
case QGis::Meters:
133+
{
134+
if ( toUnit == QGis::Feet ) return 1.0 / FEET_TO_METER;
135+
if ( toUnit == QGis::Degrees ) return 1.0 / DEGREE_TO_METER;
136+
if ( toUnit == QGis::NauticalMiles ) return 1.0 / NMILE_TO_METER;
137+
break;
138+
}
139+
case QGis::Feet:
140+
{
141+
if ( toUnit == QGis::Meters ) return FEET_TO_METER;
142+
if ( toUnit == QGis::Degrees ) return FEET_TO_METER / DEGREE_TO_METER;
143+
if ( toUnit == QGis::NauticalMiles ) return FEET_TO_METER / NMILE_TO_METER;
144+
break;
145+
}
146+
case QGis::Degrees:
147+
{
148+
if ( toUnit == QGis::Meters ) return DEGREE_TO_METER;
149+
if ( toUnit == QGis::Feet ) return DEGREE_TO_METER / FEET_TO_METER;
150+
if ( toUnit == QGis::NauticalMiles ) return DEGREE_TO_METER / NMILE_TO_METER;
151+
break;
152+
}
153+
case QGis::NauticalMiles:
154+
{
155+
if ( toUnit == QGis::Meters ) return NMILE_TO_METER;
156+
if ( toUnit == QGis::Feet ) return NMILE_TO_METER / FEET_TO_METER;
157+
if ( toUnit == QGis::Degrees ) return NMILE_TO_METER / DEGREE_TO_METER;
158+
break;
159+
}
160+
}
161+
}
162+
return 1.0;
163+
}
164+
115165
void *qgsMalloc( size_t size )
116166
{
117167
if ( size == 0 || long( size ) < 0 )

src/core/qgis.h

+2
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,8 @@ class CORE_EXPORT QGis
248248
//! Provides translated version of the type value
249249
// Added in version 2.0
250250
static QString tr( QGis::UnitType unit );
251+
//! Returns the conversion factor between the specified units
252+
static double fromUnitToUnitFactor( QGis::UnitType fromUnit, QGis::UnitType toUnit );
251253

252254
//! User defined event types
253255
enum UserEvent

0 commit comments

Comments
 (0)