Skip to content

Commit

Permalink
A quick hack to investigate measurements when projection is off.
Browse files Browse the repository at this point in the history
Feedback welcome!


git-svn-id: http://svn.osgeo.org/qgis/trunk@6446 c8812cc2-4d05-0410-92ff-de0c093fc19c
  • Loading branch information
homann committed Jan 20, 2007
1 parent 1a661ae commit 51087d3
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 26 deletions.
99 changes: 76 additions & 23 deletions src/app/qgsmeasure.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ QgsMeasure::QgsMeasure(bool measureArea, QgsMapCanvas *mc, Qt::WFlags f)
mTable->setNumRows(1);
mTable->setText(0, 0, QString::number(0, 'f',1));

mTable->horizontalHeader()->setLabel( 0, tr("Segments (in meters)") );
//mTable->horizontalHeader()->setLabel( 0, tr("Segments (in meters)") );
//mTable->horizontalHeader()->setLabel( 1, tr("Total") );
//mTable->horizontalHeader()->setLabel( 2, tr("Azimuth") );

Expand All @@ -64,7 +64,7 @@ QgsMeasure::QgsMeasure(bool measureArea, QgsMapCanvas *mc, Qt::WFlags f)

connect( mMapCanvas, SIGNAL(renderComplete(QPainter*)), this, SLOT(mapCanvasChanged()) );

mCalc = new QgsDistanceArea;
//mCalc = new QgsDistanceArea;

mRubberBand = new QgsRubberBand(mMapCanvas, mMeasureArea);

Expand All @@ -84,7 +84,7 @@ void QgsMeasure::activate()

// If we suspect that they have data that is projected, yet the
// map SRS is set to a geographic one, warn them.
if (mCalc->geographic() &&
if (mCanvas->mapRender()->distArea()->geographic() &&
(mMapCanvas->extent().height() > 360 ||
mMapCanvas->extent().width() > 720))
{
Expand Down Expand Up @@ -119,7 +119,7 @@ void QgsMeasure::setMeasureArea(bool measureArea)

QgsMeasure::~QgsMeasure()
{
delete mCalc;
// delete mCalc;
delete mRubberBand;
}

Expand Down Expand Up @@ -169,7 +169,7 @@ void QgsMeasure::addPoint(QgsPoint &point)

if (mMeasureArea && mPoints.size() > 2)
{
double area = mCalc->measurePolygon(mPoints);
double area = mCanvas->mapRender()->distArea()->measurePolygon(mPoints);
editTotal->setText(formatArea(area));
}
else if (!mMeasureArea && mPoints.size() > 1)
Expand All @@ -178,7 +178,7 @@ void QgsMeasure::addPoint(QgsPoint &point)

QgsPoint p1 = mPoints[last], p2 = mPoints[last+1];

double d = mCalc->measureLine(p1,p2);
double d = mCanvas->mapRender()->distArea()->measureLine(p1,p2);

mTotal += d;
editTotal->setText(formatDistance(mTotal));
Expand Down Expand Up @@ -223,15 +223,15 @@ void QgsMeasure::mouseMove(QgsPoint &point)
tmpPoints.push_back(point);
if (mMeasureArea && tmpPoints.size() > 2)
{
double area = mCalc->measurePolygon(tmpPoints);
double area = mCanvas->mapRender()->distArea()->measurePolygon(tmpPoints);
editTotal->setText(formatArea(area));
}
else if (!mMeasureArea && tmpPoints.size() > 1)
{
int last = tmpPoints.size()-2;
QgsPoint p1 = tmpPoints[last], p2 = tmpPoints[last+1];

double d = mCalc->measureLine(p1,p2);
double d = mCanvas->mapRender()->distArea()->measureLine(p1,p2);
mTable->setText(last, 0, QString::number(d, 'f',1));
editTotal->setText(formatDistance(mTotal + d));
}
Expand Down Expand Up @@ -296,16 +296,52 @@ void QgsMeasure::on_btnHelp_clicked()
QString QgsMeasure::formatDistance(double distance)
{
QString txt;
if (distance < 1000)
{
txt = QString::number(distance,'f',0);
txt += " m";
}
else
QString unitLabel;

QGis::units myMapUnits = mCanvas->mapUnits();
switch (myMapUnits)
{
txt = QString::number(distance/1000,'f',1);
txt += " km";
}
case QGis::METERS:
if (distance > 1000.0)
{
unitLabel=tr(" km");
distance = distance/1000;
}
else if (distance < 0.01)
{
unitLabel=tr(" mm");
distance = distance*1000;
}
else if (distance < 0.1)
{
unitLabel=tr(" cm");
distance = distance*100;
}
else
unitLabel=tr(" m");
break;
case QGis::FEET:
if (distance == 1.0)
unitLabel=tr(" foot");
else
unitLabel=tr(" feet");
break;
case QGis::DEGREES:
if (distance == 1.0)
unitLabel=tr(" degree");
else
unitLabel=tr(" degrees");
break;
case QGis::UNKNOWN:
unitLabel=tr(" unknown");
default:
std::cout << "Error: not picked up map units - actual value = "
<< myMapUnits << std::endl;
};

txt = QString::number(distance,'f',1);
txt += unitLabel;

return txt;
}

Expand All @@ -327,6 +363,23 @@ QString QgsMeasure::formatArea(double area)

void QgsMeasure::updateUi()
{

QGis::units myMapUnits = mCanvas->mapUnits();
switch (myMapUnits)
{
case QGis::METERS:
mTable->horizontalHeader()->setLabel( 0, tr("Segments (in meters)") );
break;
case QGis::FEET:
mTable->horizontalHeader()->setLabel( 0, tr("Segments (in feet)") );
break;
case QGis::DEGREES:
mTable->horizontalHeader()->setLabel( 0, tr("Segments (in degrees)") );
break;
case QGis::UNKNOWN:
mTable->horizontalHeader()->setLabel( 0, tr("Segments") );
};

if (mMeasureArea)
{
mTable->hide();
Expand All @@ -344,14 +397,14 @@ void QgsMeasure::updateProjection()
{
// set ellipsoid
QSettings settings;
QString ellipsoid = settings.readEntry("/qgis/measure/ellipsoid", "WGS84");
mCalc->setEllipsoid(ellipsoid);
// QString ellipsoid = settings.readEntry("/qgis/measure/ellipsoid", "WGS84");
// mCalc->setEllipsoid(ellipsoid);

// set source SRS and projections enabled flag
QgsMapRender* mapRender = mCanvas->mapRender();
mCalc->setProjectionsEnabled(mapRender->projectionsEnabled());
int srsid = mapRender->destinationSrs().srsid();
mCalc->setSourceSRS(srsid);
// QgsMapRender* mapRender = mCanvas->mapRender();
// mCalc->setProjectionsEnabled(mapRender->projectionsEnabled());
// int srsid = mapRender->destinationSrs().srsid();
// mCalc->setSourceSRS(srsid);

int myRed = settings.value("/qgis/default_measure_color_red", 180).toInt();
int myGreen = settings.value("/qgis/default_measure_color_green", 180).toInt();
Expand Down
2 changes: 1 addition & 1 deletion src/app/qgsmeasure.h
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ public slots:
QgsMapCanvas *mMapCanvas;

//! distance/area calculator
QgsDistanceArea* mCalc;
//QgsDistanceArea* mCalc;

std::vector<QgsPoint> mPoints;

Expand Down
6 changes: 5 additions & 1 deletion src/core/qgsdistancearea.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -279,8 +279,12 @@ double QgsDistanceArea::measureLine(const QgsPoint& p1, const QgsPoint& p2)
{
pp1 = mCoordTransform->transform(p1);
pp2 = mCoordTransform->transform(p2);
return computeDistanceBearing(pp1, pp2);
}
else
{
return sqrt((p2.x()-p1.x())*(p2.x()-p1.x()) + (p2.y()-p1.y())*(p2.y()-p1.y()));
}
return computeDistanceBearing(pp1, pp2);
}
catch (QgsCsException &cse)
{
Expand Down
8 changes: 7 additions & 1 deletion src/core/qgsmaprender.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include "qgsmaptopixel.h"
#include "qgsmaplayer.h"
#include "qgsmaplayerregistry.h"
#include "qgsdistancearea.h"
//#include "qgsspatialrefsys.h"

#include <QDomDocument>
Expand All @@ -37,6 +38,7 @@ QgsMapRender::QgsMapRender()
{
mCoordXForm = new QgsMapToPixel;
mScaleCalculator = new QgsScaleCalculator;
mDistArea = new QgsDistanceArea;

mDrawing = false;
mOverview = false;
Expand All @@ -55,6 +57,7 @@ QgsMapRender::~QgsMapRender()
{
delete mCoordXForm;
delete mScaleCalculator;
delete mDistArea;
delete mDestSRS;
}

Expand Down Expand Up @@ -365,6 +368,8 @@ void QgsMapRender::setProjectionsEnabled(bool enabled)
if (mProjectionsEnabled != enabled)
{
mProjectionsEnabled = enabled;
QgsDebugMsg("Adjusting DistArea projection on/off");
mDistArea->setProjectionsEnabled(enabled);
updateFullExtent();
emit projectionsEnabled(enabled);
}
Expand All @@ -382,7 +387,8 @@ void QgsMapRender::setDestinationSrs(const QgsSpatialRefSys& srs)
QgsDebugMsg("* DestSRS.proj4() = " + srs.proj4String());
if (*mDestSRS != srs)
{
QgsDebugMsg("No, changed my mind!");
QgsDebugMsg("Setting DistArea SRS to " + QString::number(srs.srsid()));
mDistArea->setSourceSRS(srs.srsid());
*mDestSRS = srs;
updateFullExtent();
emit destinationSrsChanged();
Expand Down
5 changes: 5 additions & 0 deletions src/core/qgsmaprender.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ class QgsMapToPixel;
class QgsMapLayer;
class QgsScaleCalculator;
class QgsSpatialRefSys;
class QgsDistanceArea;

/**
* \class QgsMapRender
Expand Down Expand Up @@ -68,6 +69,8 @@ class CORE_EXPORT QgsMapRender : public QObject
//! Recalculate the map scale
void updateScale();

//! Return the measuring object
QgsDistanceArea* distArea() { return mDistArea; }
QGis::units mapUnits() const;
void setMapUnits(QGis::units u);

Expand Down Expand Up @@ -191,6 +194,8 @@ class CORE_EXPORT QgsMapRender : public QObject
//! full extent of the layer set
QgsRect mFullExtent;

//! tool for measuring
QgsDistanceArea* mDistArea;
};

#endif
Expand Down

0 comments on commit 51087d3

Please sign in to comment.