Skip to content
Permalink
Browse files

Merge pull request #1056 from ahuarte47/Issue_9060_B

[Feature]: Centralize conversion units in one unique function
  • Loading branch information
mhugent committed Jan 17, 2014
2 parents 8c9f41c + 47d253e commit fb180388b9aa5f34186100486c383e531c0aab03
@@ -139,19 +139,10 @@ void QgsDecorationScaleBar::render( QPainter * theQPainter )
QGis::UnitType myPreferredUnits = QGis::fromLiteral( settings.value( "/qgis/measure/displayunits", QGis::toLiteral( QGis::Meters ) ).toString() );
QGis::UnitType myMapUnits = canvas->mapUnits();

// Adjust units meter/feet or vice versa
if ( myMapUnits == QGis::Meters && myPreferredUnits == QGis::Feet )
{
// From meter to feet
myMapUnits = QGis::Feet;
myMapUnitsPerPixelDouble /= 0.3084;
}
else if ( myMapUnits == QGis::Feet && myPreferredUnits == QGis::Meters )
{
// From feet to meter
myMapUnits = QGis::Meters;
myMapUnitsPerPixelDouble *= 0.3084;
}
// Adjust units meter/feet/... or vice versa
myMapUnitsPerPixelDouble *= QGis::fromUnitToUnitFactor( myMapUnits, myPreferredUnits );
myMapUnits = myPreferredUnits;

//Calculate size of scale bar for preferred number of map units
double myScaleBarWidth = mPreferredSize / myMapUnitsPerPixelDouble;

@@ -458,6 +458,10 @@ QgsOptions::QgsOptions( QWidget *parent, Qt::WFlags fl ) :
{
radNautical->setChecked( true );
}
else if ( myDisplayUnits == QGis::Degrees )
{
radDegrees->setChecked( true );
}
else
{
radMeters->setChecked( true );
@@ -1195,6 +1199,10 @@ void QgsOptions::saveOptions()
{
settings.setValue( "/qgis/measure/displayunits", QGis::toLiteral( QGis::NauticalMiles ) );
}
else if ( radDegrees->isChecked() )
{
settings.setValue( "/qgis/measure/displayunits", QGis::toLiteral( QGis::Degrees ) );
}
else
{
settings.setValue( "/qgis/measure/displayunits", QGis::toLiteral( QGis::Meters ) );
@@ -196,11 +196,11 @@ double QgsComposerScaleBar::mapWidth() const
double measure = da.measureLine( QgsPoint( composerMapRect.xMinimum(), composerMapRect.yMinimum() ), QgsPoint( composerMapRect.xMaximum(), composerMapRect.yMinimum() ) );
if ( mUnits == QgsComposerScaleBar::Feet )
{
measure /= 0.3048;
measure /= QGis::fromUnitToUnitFactor( QGis::Feet, QGis::Meters );
}
else if ( mUnits == QgsComposerScaleBar::NauticalMiles )
{
measure /= 1852.0;
measure /= QGis::fromUnitToUnitFactor( QGis::NauticalMiles, QGis::Meters );
}
return measure;
}
@@ -972,15 +972,7 @@ double QgsDxfExport::scaleToMapUnits( double value, QgsSymbolV2::OutputUnit symb
}

//symbology in mm
value *= mSymbologyScaleDenominator / 1000;
if ( mapUnits == QGis::Feet )
{
value *= 0.3048;
}
else if ( mapUnits == QGis::Degrees )
{
value /= 111120;
}
value *= ( mSymbologyScaleDenominator * QGis::fromUnitToUnitFactor( mapUnits, QGis::Meters ) ) / 1000;
return value;
}

@@ -1255,15 +1247,7 @@ double QgsDxfExport::dashSeparatorSize() const

double QgsDxfExport::sizeToMapUnits( double s ) const
{
double size = s;
if ( mMapUnits == QGis::Feet )
{
size /= 0.3048;
}
else if ( mMapUnits == QGis::Degrees )
{
size /= 111120;
}
double size = s * QGis::fromUnitToUnitFactor( QGis::Meters, mMapUnits );
return size;
}

@@ -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
@@ -989,7 +989,7 @@ QString QgsDistanceArea::textUnit( double value, int decimals, QGis::UnitType u,

void QgsDistanceArea::convertMeasurement( double &measure, QGis::UnitType &measureUnits, QGis::UnitType displayUnits, bool isArea )
{
// Helper for converting between meters and feet
// Helper for converting between meters and feet and degrees and NauticalMiles...
// The parameters measure and measureUnits are in/out

if (( measureUnits == QGis::Degrees || measureUnits == QGis::Feet || measureUnits == QGis::NauticalMiles ) &&
@@ -1001,73 +1001,12 @@ void QgsDistanceArea::convertMeasurement( double &measure, QGis::UnitType &measu
QgsDebugMsg( "We're measuring on an ellipsoid or using projections, the system is returning meters" );
}

// Only convert between meters and feet
if ( measureUnits == QGis::Meters && displayUnits == QGis::Feet )
{
QgsDebugMsg( QString( "Converting %1 meters" ).arg( QString::number( measure ) ) );
measure /= 0.3048;
if ( isArea )
{
measure /= 0.3048;
}
QgsDebugMsg( QString( "to %1 feet" ).arg( QString::number( measure ) ) );
measureUnits = QGis::Feet;
}
if ( measureUnits == QGis::Feet && displayUnits == QGis::Meters )
{
QgsDebugMsg( QString( "Converting %1 feet" ).arg( QString::number( measure ) ) );
measure *= 0.3048;
if ( isArea )
{
measure *= 0.3048;
}
QgsDebugMsg( QString( "to %1 meters" ).arg( QString::number( measure ) ) );
measureUnits = QGis::Meters;
}
// Gets the conversion factor between the specified units
double factorUnits = QGis::fromUnitToUnitFactor( measureUnits, displayUnits );
if ( isArea ) factorUnits *= factorUnits;

if ( measureUnits == QGis::Meters && displayUnits == QGis::NauticalMiles )
{
QgsDebugMsg( QString( "Converting %1 meters" ).arg( QString::number( measure ) ) );
measure /= 1852.0;
if ( isArea )
{
measure /= 1852.0;
}
QgsDebugMsg( QString( "to %1 nautical miles" ).arg( QString::number( measure ) ) );
measureUnits = QGis::NauticalMiles;
}
if ( measureUnits == QGis::NauticalMiles && displayUnits == QGis::Meters )
{
QgsDebugMsg( QString( "Converting %1 nautical miles" ).arg( QString::number( measure ) ) );
measure *= 1852.0;
if ( isArea )
{
measure *= 1852.0;
}
QgsDebugMsg( QString( "to %1 meters" ).arg( QString::number( measure ) ) );
measureUnits = QGis::Meters;
}

if ( measureUnits == QGis::Feet && displayUnits == QGis::NauticalMiles )
{
QgsDebugMsg( QString( "Converting %1 meters" ).arg( QString::number( measure ) ) );
measure /= 6076.12;
if ( isArea )
{
measure /= 6076.12;
}
QgsDebugMsg( QString( "to %1 nautical miles" ).arg( QString::number( measure ) ) );
measureUnits = QGis::Feet;
}
if ( measureUnits == QGis::NauticalMiles && displayUnits == QGis::Feet )
{
QgsDebugMsg( QString( "Converting %1 nautical miles" ).arg( QString::number( measure ) ) );
measure *= 6076.12;
if ( isArea )
{
measure *= 6076.12;
}
QgsDebugMsg( QString( "to %1 meters" ).arg( QString::number( measure ) ) );
measureUnits = QGis::Feet;
}
QgsDebugMsg( QString( "Converting %1 %2" ).arg( QString::number( measure ), QGis::toLiteral( measureUnits ) ) );
measure *= factorUnits;
QgsDebugMsg( QString( "to %1 %2" ).arg( QString::number( measure ), QGis::toLiteral( displayUnits ) ) );
measureUnits = displayUnits;
}
@@ -1064,17 +1064,7 @@ int GRASS_LIB_EXPORT G_get_cellhd( const char *name, const char *mapset, struct

double QgsGrassGisLib::G_database_units_to_meters_factor( void )
{
switch ( mCrs.mapUnits() )
{
case QGis::Meters:
return 1.;
case QGis::Feet:
return .3048;
case QGis::Degrees:
return 1.;
default:
return 0.;
}
return QGis::fromUnitToUnitFactor( mCrs.mapUnits(), QGis::Meters );
}

double QgsGrassGisLib::G_area_of_cell_at_row( int row )
@@ -2776,27 +2776,7 @@ void QgsWmsProvider::parseWMTSContents( QDomElement const &e )

s.wkScaleSet = n0.firstChildElement( "WellKnownScaleSet" ).text();

double metersPerUnit;
switch ( crs.mapUnits() )
{
case QGis::Meters:
metersPerUnit = 1.0;
break;

case QGis::Feet:
metersPerUnit = 0.3048;
break;

case QGis::Degrees:
metersPerUnit = 111319.49079327358;
break;

default:
case QGis::UnknownUnit:
QgsDebugMsg( "Unknown CRS units - assuming meters" );
metersPerUnit = 1.0;
break;
}
double metersPerUnit = QGis::fromUnitToUnitFactor( crs.mapUnits(), QGis::Meters );

s.crs = crs.authid();

@@ -2594,6 +2594,13 @@
</property>
</widget>
</item>
<item row="5" column="4">
<widget class="QRadioButton" name="radDegrees">
<property name="text">
<string>Degrees</string>
</property>
</widget>
</item>
<item row="6" column="0">
<widget class="QLabel" name="mAngleUnitsLabel">
<property name="text">

0 comments on commit fb18038

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