77 changes: 63 additions & 14 deletions src/core/qgsdistancearea.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
QgsDistanceArea::QgsDistanceArea()
{
// init with default settings
mProjectionsEnabled = false;
mEllipsoidalMode = false;
mCoordTransform = new QgsCoordinateTransform;
setSourceCrs( GEOCRS_ID ); // WGS 84
setEllipsoid( "WGS84" );
Expand All @@ -53,10 +53,9 @@ QgsDistanceArea::~QgsDistanceArea()
delete mCoordTransform;
}


void QgsDistanceArea::setProjectionsEnabled( bool flag )
void QgsDistanceArea::setEllipsoidalMode( bool flag )
{
mProjectionsEnabled = flag;
mEllipsoidalMode = flag;
}

void QgsDistanceArea::setSourceCrs( long srsid )
Expand Down Expand Up @@ -338,14 +337,14 @@ double QgsDistanceArea::measureLine( const QList<QgsPoint>& points )

try
{
if ( mProjectionsEnabled && ( mEllipsoid != "NONE" ) )
if ( mEllipsoidalMode && ( mEllipsoid != "NONE" ) )
p1 = mCoordTransform->transform( points[0] );
else
p1 = points[0];

for ( QList<QgsPoint>::const_iterator i = points.begin(); i != points.end(); ++i )
{
if ( mProjectionsEnabled && ( mEllipsoid != "NONE" ) )
if ( mEllipsoidalMode && ( mEllipsoid != "NONE" ) )
{
p2 = mCoordTransform->transform( *i );
total += computeDistanceBearing( p1, p2 );
Expand All @@ -372,26 +371,37 @@ double QgsDistanceArea::measureLine( const QList<QgsPoint>& points )

double QgsDistanceArea::measureLine( const QgsPoint& p1, const QgsPoint& p2 )
{
double result;

try
{
QgsPoint pp1 = p1, pp2 = p2;
if ( mProjectionsEnabled && ( mEllipsoid != "NONE" ) )

QgsDebugMsg( QString( "Measuring from %1 to %2" ).arg( p1.toString( 4 ) ).arg( p2.toString( 4 ) ) );
if ( mEllipsoidalMode && ( mEllipsoid != "NONE" ) )
{
QgsDebugMsg( QString( "Ellipsoidal calculations is enabled, using ellipsoid %1" ).arg( mEllipsoid ) );
QgsDebugMsg( QString( "From proj4 : %1" ).arg( mCoordTransform->sourceCrs().toProj4() ) );
QgsDebugMsg( QString( "To proj4 : %1" ).arg( mCoordTransform->destCRS().toProj4() ) );
pp1 = mCoordTransform->transform( p1 );
pp2 = mCoordTransform->transform( p2 );
return computeDistanceBearing( pp1, pp2 );
QgsDebugMsg( QString( "New points are %1 and %2, calculating..." ).arg( pp1.toString( 4 ) ).arg( pp2.toString( 4 ) ) );
result = computeDistanceBearing( pp1, pp2 );
}
else
{
return sqrt(( p2.x() - p1.x() )*( p2.x() - p1.x() ) + ( p2.y() - p1.y() )*( p2.y() - p1.y() ) );
QgsDebugMsg( "Cartesian calculation on canvas coordinates" );
result = sqrt(( p2.x() - p1.x() ) * ( p2.x() - p1.x() ) + ( p2.y() - p1.y() ) * ( p2.y() - p1.y() ) );
}
}
catch ( QgsCsException &cse )
{
Q_UNUSED( cse );
QgsMessageLog::logMessage( QObject::tr( "Caught a coordinate system exception while trying to transform a point. Unable to calculate line length." ) );
return 0.0;
result = 0.0;
}
QgsDebugMsg( QString( "The result was %1" ).arg( result ) );
return result;
}


Expand Down Expand Up @@ -437,7 +447,7 @@ unsigned char* QgsDistanceArea::measurePolygon( unsigned char* feature, double*

pnt = QgsPoint( x, y );

if ( mProjectionsEnabled && ( mEllipsoid != "NONE" ) )
if ( mEllipsoidalMode && ( mEllipsoid != "NONE" ) )
{
pnt = mCoordTransform->transform( pnt );
}
Expand Down Expand Up @@ -489,7 +499,7 @@ double QgsDistanceArea::measurePolygon( const QList<QgsPoint>& points )

try
{
if ( mProjectionsEnabled && ( mEllipsoid != "NONE" ) )
if ( mEllipsoidalMode && ( mEllipsoid != "NONE" ) )
{
QList<QgsPoint> pts;
for ( QList<QgsPoint>::const_iterator i = points.begin(); i != points.end(); ++i )
Expand Down Expand Up @@ -517,7 +527,7 @@ double QgsDistanceArea::bearing( const QgsPoint& p1, const QgsPoint& p2 )
QgsPoint pp1 = p1, pp2 = p2;
double bearing;

if ( mProjectionsEnabled && ( mEllipsoid != "NONE" ) )
if ( mEllipsoidalMode && ( mEllipsoid != "NONE" ) )
{
pp1 = mCoordTransform->transform( p1 );
pp2 = mCoordTransform->transform( p2 );
Expand Down Expand Up @@ -679,7 +689,7 @@ double QgsDistanceArea::computePolygonArea( const QList<QgsPoint>& points )
double area;

QgsDebugMsgLevel( "Ellipsoid: " + mEllipsoid, 3 );
if (( ! mProjectionsEnabled ) || ( mEllipsoid == "NONE" ) )
if (( ! mEllipsoidalMode ) || ( mEllipsoid == "NONE" ) )
{
return computePolygonFlatArea( points );
}
Expand Down Expand Up @@ -868,3 +878,42 @@ QString QgsDistanceArea::textUnit( double value, int decimals, QGis::UnitType u,

return QLocale::system().toString( value, 'f', decimals ) + unitLabel;
}

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

if (( measureUnits == QGis::Degrees || measureUnits == QGis::Feet ) &&
mEllipsoid != "NONE" &&
mEllipsoidalMode )
{
// Measuring on an ellipsoid returned meters. Force!
measureUnits = QGis::Meters;
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;
}
}
11 changes: 8 additions & 3 deletions src/core/qgsdistancearea.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,12 @@ class CORE_EXPORT QgsDistanceArea
~QgsDistanceArea();

//! sets whether coordinates must be projected to ellipsoid before measuring
void setProjectionsEnabled( bool flag );
void setEllipsoidalMode( bool flag );
Q_DECL_DEPRECATED void setProjectionsEnabled( bool flag ) { setEllipsoidalMode( flag ); };

//! returns projections enabled flag
bool hasCrsTransformEnabled() { return mProjectionsEnabled; }
bool ellipsoidalEnabled() const { return mEllipsoidalMode; }
Q_DECL_DEPRECATED bool hasCrsTransformEnabled() { return mEllipsoidalMode; }

//! sets source spatial reference system (by QGIS CRS)
void setSourceCrs( long srsid );
Expand Down Expand Up @@ -93,6 +95,9 @@ class CORE_EXPORT QgsDistanceArea

static QString textUnit( double value, int decimals, QGis::UnitType u, bool isArea, bool keepBaseUnit = false );

//! Helper for conversion between physical units
void convertMeasurement( double &measure, QGis::UnitType &measureUnits, QGis::UnitType displayUnits, bool isArea );

protected:
//! measures line distance, line points are extracted from WKB
unsigned char* measureLine( unsigned char* feature, double* area, bool hasZptr = false );
Expand Down Expand Up @@ -134,7 +139,7 @@ class CORE_EXPORT QgsDistanceArea
QgsCoordinateTransform* mCoordTransform;

//! indicates whether we will transform coordinates
bool mProjectionsEnabled;
bool mEllipsoidalMode;

//! id of the source spatial reference system
long mSourceRefSys;
Expand Down
2 changes: 1 addition & 1 deletion src/core/qgsexpression.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -913,7 +913,7 @@ bool QgsExpression::needsGeometry()
void QgsExpression::initGeomCalculator()
{
mCalc = new QgsDistanceArea;
mCalc->setProjectionsEnabled( false );
mCalc->setEllipsoidalMode( false );
QSettings settings;
QString ellipsoid = settings.value( "/qgis/measure/ellipsoid", "WGS84" ).toString();
mCalc->setEllipsoid( ellipsoid );
Expand Down
5 changes: 2 additions & 3 deletions src/core/qgsmaprenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ QgsMapRenderer::~QgsMapRenderer()
delete mCachedTr;
}


QgsRectangle QgsMapRenderer::extent() const
{
return mExtent;
Expand Down Expand Up @@ -663,14 +662,14 @@ void QgsMapRenderer::setProjectionsEnabled( bool enabled )
{
mProjectionsEnabled = enabled;
QgsDebugMsg( "Adjusting DistArea projection on/off" );
mDistArea->setProjectionsEnabled( enabled );
mDistArea->setEllipsoidalMode( enabled );
updateFullExtent();
mLastExtent.setMinimal();
emit hasCrsTransformEnabled( enabled );
}
}

bool QgsMapRenderer::hasCrsTransformEnabled()
bool QgsMapRenderer::hasCrsTransformEnabled() const
{
return mProjectionsEnabled;
}
Expand Down
2 changes: 1 addition & 1 deletion src/core/qgsmaprenderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ class CORE_EXPORT QgsMapRenderer : public QObject
void setProjectionsEnabled( bool enabled );

//! returns true if projections are enabled for this layer set
bool hasCrsTransformEnabled();
bool hasCrsTransformEnabled() const;

/** sets destination coordinate reference system
* @note deprecated by qgis 1.7
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/heatmap/heatmap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ float Heatmap::mapUnitsOf( float meters, QgsCoordinateReferenceSystem layerCrs )
da.setEllipsoid( layerCrs.ellipsoidAcronym() );
if ( da.geographic() )
{
da.setProjectionsEnabled( true );
da.setEllipsoidalMode( true );
}
return meters / da.measureLine( QgsPoint( 0.0, 0.0 ), QgsPoint( 0.0, 1.0 ) );
}
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/heatmap/heatmapgui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ float HeatmapGui::mapUnitsOf( float meters, QgsCoordinateReferenceSystem layerCr
da.setEllipsoid( layerCrs.ellipsoidAcronym() );
if ( da.geographic() )
{
da.setProjectionsEnabled( true );
da.setEllipsoidalMode( true );
}
double unitDistance = da.measureLine( QgsPoint( 0.0, 0.0 ), QgsPoint( 0.0, 1.0 ) );
QgsDebugMsg( QString( "Converted %1 meters to %2 mapunits" ).arg( meters ).arg( meters / unitDistance ) );
Expand Down
14 changes: 11 additions & 3 deletions src/ui/qgsprojectpropertiesbase.ui
Original file line number Diff line number Diff line change
Expand Up @@ -193,8 +193,15 @@
</item>
<item row="1" column="0">
<widget class="QGroupBox" name="btnGrpMapUnits">
<property name="toolTip">
<string>&lt;!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.0//EN&quot; &quot;http://www.w3.org/TR/REC-html40/strict.dtd&quot;&gt;
&lt;html&gt;&lt;head&gt;&lt;meta name=&quot;qrichtext&quot; content=&quot;1&quot; /&gt;&lt;style type=&quot;text/css&quot;&gt;
p, li { white-space: pre-wrap; }
&lt;/style&gt;&lt;/head&gt;&lt;body style=&quot; font-family:'MS Shell Dlg 2'; font-size:8.25pt; font-weight:400; font-style:normal;&quot;&gt;
&lt;p style=&quot; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;&quot;&gt;&lt;span style=&quot; font-size:8pt;&quot;&gt;Used when CRS transformation is turned off.&lt;/span&gt;&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</string>
</property>
<property name="title">
<string>Layer units</string>
<string>Canvas units</string>
</property>
<layout class="QGridLayout" name="gridLayout_27">
<item row="0" column="0">
Expand Down Expand Up @@ -780,8 +787,8 @@
<rect>
<x>0</x>
<y>0</y>
<width>660</width>
<height>792</height>
<width>583</width>
<height>671</height>
</rect>
</property>
<layout class="QGridLayout" name="gridLayout">
Expand Down Expand Up @@ -1156,6 +1163,7 @@
</tabstops>
<resources>
<include location="../../images/images.qrc"/>
<include location="../../images/images.qrc"/>
</resources>
<connections>
<connection>
Expand Down