Skip to content

Commit

Permalink
Applied patch from ticket #3240 with some tweaks. Allows unprojected …
Browse files Browse the repository at this point in the history
…measurement of distances

git-svn-id: http://svn.osgeo.org/qgis/trunk/qgis@14956 c8812cc2-4d05-0410-92ff-de0c093fc19c
  • Loading branch information
timlinux committed Dec 21, 2010
1 parent 3014bbb commit ebd47cd
Show file tree
Hide file tree
Showing 8 changed files with 206 additions and 29 deletions.
24 changes: 24 additions & 0 deletions src/app/qgsdisplayangle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,29 @@
QgsDisplayAngle::QgsDisplayAngle( QWidget * parent, Qt::WindowFlags f ): QDialog( parent, f )
{
setupUi( this );
QSettings settings;
int s = settings.value( "/qgis/measure/projectionEnabled", "2" ).toInt();
if ( s == 2 )
mcbProjectionEnabled->setCheckState( Qt::Checked );
else
mcbProjectionEnabled->setCheckState( Qt::Unchecked );

connect( mcbProjectionEnabled, SIGNAL( stateChanged(int) ),
this, SLOT( changeState() ) );
connect( mcbProjectionEnabled, SIGNAL( stateChanged(int) ),
this, SIGNAL( changeProjectionEnabledState() ) );
}

QgsDisplayAngle::~QgsDisplayAngle()
{

}

bool QgsDisplayAngle::projectionEnabled()
{
return mcbProjectionEnabled->isChecked();
}

void QgsDisplayAngle::setValueInRadians( double value )
{
QSettings settings;
Expand All @@ -45,3 +61,11 @@ void QgsDisplayAngle::setValueInRadians( double value )
}
}

void QgsDisplayAngle::changeState()
{
QSettings settings;
if ( mcbProjectionEnabled->isChecked() )
settings.setValue( "/qgis/measure/projectionEnabled", 2);
else
settings.setValue( "/qgis/measure/projectionEnabled", 0);
}
9 changes: 9 additions & 0 deletions src/app/qgsdisplayangle.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,15 @@ class QgsDisplayAngle: public QDialog, private Ui::QgsDisplayAngleBase
/**Sets the measured angle value (in radians). The value is going to
be converted to degrees / gon automatically if necessary*/
void setValueInRadians( double value );

bool projectionEnabled();

signals:
void changeProjectionEnabledState();

private slots:
void changeState();

};

#endif // QGSDISPLAYANGLE_H
62 changes: 51 additions & 11 deletions src/app/qgsmaptoolmeasureangle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,26 @@ void QgsMapToolMeasureAngle::canvasMoveEvent( QMouseEvent * e )
QgsDistanceArea* distArea = mCanvas->mapRenderer()->distanceArea();
if ( distArea )
{
double azimutOne = distArea->bearing( mAnglePoints.at( 1 ), mAnglePoints.at( 0 ) );
double azimutTwo = distArea->bearing( mAnglePoints.at( 1 ), point );
double resultAngle = azimutTwo - azimutOne;
//show angle in dialog
if ( !mResultDisplay )
{
mResultDisplay = new QgsDisplayAngle( mCanvas->topLevelWidget() );
QObject::connect( mResultDisplay, SIGNAL( rejected() ), this, SLOT( stopMeasuring() ) );
QObject::connect( mResultDisplay, SIGNAL( changeProjectionEnabledState() ),
this, SLOT( changeProjectionEnabledState() ) );
mResultDisplay->move( e->pos() - QPoint( 100, 100 ) );
}
mResultDisplay->show();

QgsDistanceArea myDa;
myDa.setSourceCrs( mCanvas->mapRenderer()->destinationSrs().srsid() );
myDa.setEllipsoid( distArea->ellipsoid() );
myDa.setProjectionsEnabled( mResultDisplay->projectionEnabled() );

//angle calculation
double azimuthOne = myDa.bearing( mAnglePoints.at( 1 ), mAnglePoints.at( 0 ) );
double azimuthTwo = myDa.bearing( mAnglePoints.at( 1 ), point );
double resultAngle = azimuthTwo - azimuthOne;
QgsDebugMsg( QString::number( qAbs( resultAngle ) ) );
QgsDebugMsg( QString::number( M_PI ) );
if ( qAbs( resultAngle ) > M_PI )
Expand All @@ -66,14 +83,6 @@ void QgsMapToolMeasureAngle::canvasMoveEvent( QMouseEvent * e )
}
}

//show angle in dialog
if ( !mResultDisplay )
{
mResultDisplay = new QgsDisplayAngle( mCanvas->topLevelWidget() );
QObject::connect( mResultDisplay, SIGNAL( rejected() ), this, SLOT( stopMeasuring() ) );
mResultDisplay->move( e->pos() - QPoint( 100, 100 ) );
}
mResultDisplay->show();
mResultDisplay->setValueInRadians( resultAngle );
}
}
Expand Down Expand Up @@ -145,5 +154,36 @@ QgsPoint QgsMapToolMeasureAngle::snapPoint( const QPoint& p )
}
}

void QgsMapToolMeasureAngle::changeProjectionEnabledState()
{
if ( mAnglePoints.size() != 3 )
return;
if ( !mResultDisplay )
return;

QgsDistanceArea myDa;
myDa.setSourceCrs( mCanvas->mapRenderer()->destinationSrs().srsid() );
myDa.setEllipsoid( mCanvas->mapRenderer()->distanceArea()->ellipsoid() );
myDa.setProjectionsEnabled( mResultDisplay->projectionEnabled() );

//angle calculation
double azimuthOne = myDa.bearing( mAnglePoints.at( 1 ), mAnglePoints.at( 0 ) );
double azimuthTwo = myDa.bearing( mAnglePoints.at( 1 ), mAnglePoints.at( 2 ) );
double resultAngle = azimuthTwo - azimuthOne;
QgsDebugMsg( QString::number( fabs( resultAngle ) ) );
QgsDebugMsg( QString::number( M_PI ) );
if ( fabs( resultAngle ) > M_PI )
{
if ( resultAngle < 0 )
{
resultAngle = M_PI + ( resultAngle + M_PI );
}
else
{
resultAngle = -M_PI + ( resultAngle - M_PI );
}
}
mResultDisplay->setValueInRadians( resultAngle );

}

3 changes: 3 additions & 0 deletions src/app/qgsmaptoolmeasureangle.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ class QgsMapToolMeasureAngle: public QgsMapTool
/**Deletes the rubber band and the dialog*/
void stopMeasuring();

/** recalculate angle if projection state changed*/
void changeProjectionEnabledState();

};

#endif // QGSMAPTOOLMEASUREANGLE_H
100 changes: 92 additions & 8 deletions src/app/qgsmeasuredialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,14 @@ QgsMeasureDialog::QgsMeasureDialog( QgsMeasureTool* tool, Qt::WFlags f )
//mTable->setHeaderLabels(QStringList() << tr("Segments (in meters)") << tr("Total") << tr("Azimuth") );

QSettings settings;
int s = settings.value( "/qgis/measure/projectionEnabled", "2" ).toInt();
if ( s == 2 )
mcbProjectionEnabled->setCheckState( Qt::Checked );
else
mcbProjectionEnabled->setCheckState( Qt::Unchecked );

connect( mcbProjectionEnabled, SIGNAL( stateChanged(int) ),
this, SLOT( changeProjectionEnabledState() ));

updateUi();
}
Expand Down Expand Up @@ -91,21 +98,27 @@ void QgsMeasureDialog::mouseMove( QgsPoint &point )
QSettings settings;
int decimalPlaces = settings.value( "/qgis/measure/decimalplaces", "3" ).toInt();

// Create QgsDistance Area for customization ProjectionEnabled setting
QgsDistanceArea myDa;
myDa.setSourceCrs( mTool->canvas()->mapRenderer()->destinationSrs().srsid() );
myDa.setEllipsoid( mTool->canvas()->mapRenderer()->distanceArea()->ellipsoid() );
myDa.setProjectionsEnabled( mcbProjectionEnabled->isChecked() );

// show current distance/area while moving the point
// by creating a temporary copy of point array
// and adding moving point at the end
// and adding moving point at the end
if ( mMeasureArea && mTool->points().size() > 1 )
{
QList<QgsPoint> tmpPoints = mTool->points();
tmpPoints.append( point );
double area = mTool->canvas()->mapRenderer()->distanceArea()->measurePolygon( tmpPoints );
double area = myDa.measurePolygon( tmpPoints );
editTotal->setText( formatArea( area, decimalPlaces ) );
}
else if ( !mMeasureArea && mTool->points().size() > 0 )
{
QgsPoint p1( mTool->points().last() ), p2( point );

double d = mTool->canvas()->mapRenderer()->distanceArea()->measureLine( p1, p2 );
double d = myDa.measureLine( p1, p2 );
editTotal->setText( formatDistance( mTotal + d, decimalPlaces ) );
QGis::UnitType myDisplayUnits;
// Ignore units
Expand All @@ -120,10 +133,16 @@ void QgsMeasureDialog::addPoint( QgsPoint &point )
QSettings settings;
int decimalPlaces = settings.value( "/qgis/measure/decimalplaces", "3" ).toInt();

// Create QgsDistance Area for customization ProjectionEnabled setting
QgsDistanceArea myDa;
myDa.setSourceCrs( mTool->canvas()->mapRenderer()->destinationSrs().srsid() );
myDa.setEllipsoid( mTool->canvas()->mapRenderer()->distanceArea()->ellipsoid() );
myDa.setProjectionsEnabled( mcbProjectionEnabled->isChecked() );

int numPoints = mTool->points().size();
if ( mMeasureArea && numPoints > 2 )
{
double area = mTool->canvas()->mapRenderer()->distanceArea()->measurePolygon( mTool->points() );
double area = myDa.measurePolygon( mTool->points() );
editTotal->setText( formatArea( area, decimalPlaces ) );
}
else if ( !mMeasureArea && numPoints > 1 )
Expand All @@ -132,7 +151,7 @@ void QgsMeasureDialog::addPoint( QgsPoint &point )

QgsPoint p1 = mTool->points()[last], p2 = mTool->points()[last+1];

double d = mTool->canvas()->mapRenderer()->distanceArea()->measureLine( p1, p2 );
double d = myDa.measureLine( p1, p2 );

mTotal += d;
editTotal->setText( formatDistance( mTotal, decimalPlaces ) );
Expand Down Expand Up @@ -252,17 +271,17 @@ void QgsMeasureDialog::convertMeasurement( double &measure, QGis::UnitType &u, b
QGis::UnitType myUnits = mTool->canvas()->mapUnits();
if (( myUnits == QGis::Degrees || myUnits == QGis::Feet ) &&
mTool->canvas()->mapRenderer()->distanceArea()->ellipsoid() != "NONE" &&
mTool->canvas()->mapRenderer()->distanceArea()->hasCrsTransformEnabled() )
mcbProjectionEnabled->isChecked() )
{
// Measuring on an ellipsoid returns meters, and so does using projections???
myUnits = QGis::Meters;
QgsDebugMsg( "We're measuring on an ellipsoid or using projections, the system is returning meters" );
}

// Get the units for display
QSettings settings;
QString myDisplayUnitsTxt = settings.value( "/qgis/measure/displayunits", "meters" ).toString();

// Only convert between meters and feet
if ( myUnits == QGis::Meters && myDisplayUnitsTxt == "feet" )
{
Expand All @@ -289,3 +308,68 @@ void QgsMeasureDialog::convertMeasurement( double &measure, QGis::UnitType &u, b

u = myUnits;
}

void QgsMeasureDialog::changeProjectionEnabledState()
{
// store value
QSettings settings;
if ( mcbProjectionEnabled->isChecked() )
settings.setValue( "/qgis/measure/projectionEnabled", 2);
else
settings.setValue( "/qgis/measure/projectionEnabled", 0);

// clear interface
mTable->clear();
QTreeWidgetItem* item = new QTreeWidgetItem( QStringList( QString::number( 0, 'f', 1 ) ) );
item->setTextAlignment( 0, Qt::AlignRight );
mTable->addTopLevelItem( item );
mTotal = 0;
updateUi();

int decimalPlaces = settings.value( "/qgis/measure/decimalplaces", "3" ).toInt();

// create DistanceArea
QgsDistanceArea myDa;
myDa.setSourceCrs( mTool->canvas()->mapRenderer()->destinationSrs().srsid() );
myDa.setEllipsoid( mTool->canvas()->mapRenderer()->distanceArea()->ellipsoid() );
myDa.setProjectionsEnabled( mcbProjectionEnabled->isChecked() );

if ( mMeasureArea )
{
double area = 0.0;
if ( mTool->points().size() > 1 )
{
area = myDa.measurePolygon( mTool->points() );
}
editTotal->setText( formatArea( area, decimalPlaces ) );
}else
{
QList<QgsPoint>::const_iterator it;
bool b = true; // first point

QgsPoint p1,p2;

for (it=mTool->points().constBegin(); it != mTool->points().constEnd(); ++it)
{
p2 = *it;
if ( !b )
{
double d = myDa.measureLine( p1, p2 );
mTotal += d;
editTotal->setText( formatDistance( mTotal, decimalPlaces ) );
QGis::UnitType myDisplayUnits;

convertMeasurement( d, myDisplayUnits, false );

QTreeWidgetItem *item = mTable->topLevelItem( mTable->topLevelItemCount() - 1 );
item->setText( 0, QLocale::system().toString( d, 'f', decimalPlaces ) );
item = new QTreeWidgetItem( QStringList( QLocale::system().toString( 0.0, 'f', decimalPlaces ) ) );
item->setTextAlignment( 0, Qt::AlignRight );
mTable->addTopLevelItem( item );
mTable->scrollToItem( item );
}
p1 = p2;
b = false;
}
}
}
3 changes: 3 additions & 0 deletions src/app/qgsmeasuredialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ class QgsMeasureDialog : public QDialog, private Ui::QgsMeasureBase

//! Show the help for the dialog
void on_buttonBox_helpRequested() { QgsContextHelp::run( metaObject()->className() ); }
private slots:
//! on change state projection enable
void changeProjectionEnabledState();

private:

Expand Down
17 changes: 12 additions & 5 deletions src/ui/qgsdisplayanglebase.ui
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,22 @@
<rect>
<x>0</x>
<y>0</y>
<width>276</width>
<height>78</height>
<width>293</width>
<height>105</height>
</rect>
</property>
<property name="windowTitle">
<string>Angle</string>
</property>
<layout class="QGridLayout" name="gridLayout">
<item row="0" column="0" colspan="2">
<item row="1" column="0" colspan="2">
<widget class="QLineEdit" name="mAngleLineEdit">
<property name="readOnly">
<bool>true</bool>
</property>
</widget>
</item>
<item row="1" column="0">
<item row="2" column="0">
<spacer name="horizontalSpacer">
<property name="orientation">
<enum>Qt::Horizontal</enum>
Expand All @@ -34,7 +34,7 @@
</property>
</spacer>
</item>
<item row="1" column="1">
<item row="2" column="1">
<widget class="QDialogButtonBox" name="buttonBox">
<property name="orientation">
<enum>Qt::Horizontal</enum>
Expand All @@ -44,6 +44,13 @@
</property>
</widget>
</item>
<item row="0" column="0">
<widget class="QCheckBox" name="mcbProjectionEnabled">
<property name="text">
<string>Ellipsoidal (WGS84)</string>
</property>
</widget>
</item>
</layout>
</widget>
<resources/>
Expand Down
Loading

0 comments on commit ebd47cd

Please sign in to comment.