Skip to content

Commit 51087d3

Browse files
author
homann
committedJan 20, 2007
A quick hack to investigate measurements when projection is off.
Feedback welcome! git-svn-id: http://svn.osgeo.org/qgis/trunk@6446 c8812cc2-4d05-0410-92ff-de0c093fc19c
1 parent 1a661ae commit 51087d3

File tree

5 files changed

+94
-26
lines changed

5 files changed

+94
-26
lines changed
 

‎src/app/qgsmeasure.cpp

Lines changed: 76 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ QgsMeasure::QgsMeasure(bool measureArea, QgsMapCanvas *mc, Qt::WFlags f)
5252
mTable->setNumRows(1);
5353
mTable->setText(0, 0, QString::number(0, 'f',1));
5454

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

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

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

67-
mCalc = new QgsDistanceArea;
67+
//mCalc = new QgsDistanceArea;
6868

6969
mRubberBand = new QgsRubberBand(mMapCanvas, mMeasureArea);
7070

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

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

120120
QgsMeasure::~QgsMeasure()
121121
{
122-
delete mCalc;
122+
// delete mCalc;
123123
delete mRubberBand;
124124
}
125125

@@ -169,7 +169,7 @@ void QgsMeasure::addPoint(QgsPoint &point)
169169

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

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

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

183183
mTotal += d;
184184
editTotal->setText(formatDistance(mTotal));
@@ -223,15 +223,15 @@ void QgsMeasure::mouseMove(QgsPoint &point)
223223
tmpPoints.push_back(point);
224224
if (mMeasureArea && tmpPoints.size() > 2)
225225
{
226-
double area = mCalc->measurePolygon(tmpPoints);
226+
double area = mCanvas->mapRender()->distArea()->measurePolygon(tmpPoints);
227227
editTotal->setText(formatArea(area));
228228
}
229229
else if (!mMeasureArea && tmpPoints.size() > 1)
230230
{
231231
int last = tmpPoints.size()-2;
232232
QgsPoint p1 = tmpPoints[last], p2 = tmpPoints[last+1];
233233

234-
double d = mCalc->measureLine(p1,p2);
234+
double d = mCanvas->mapRender()->distArea()->measureLine(p1,p2);
235235
mTable->setText(last, 0, QString::number(d, 'f',1));
236236
editTotal->setText(formatDistance(mTotal + d));
237237
}
@@ -296,16 +296,52 @@ void QgsMeasure::on_btnHelp_clicked()
296296
QString QgsMeasure::formatDistance(double distance)
297297
{
298298
QString txt;
299-
if (distance < 1000)
300-
{
301-
txt = QString::number(distance,'f',0);
302-
txt += " m";
303-
}
304-
else
299+
QString unitLabel;
300+
301+
QGis::units myMapUnits = mCanvas->mapUnits();
302+
switch (myMapUnits)
305303
{
306-
txt = QString::number(distance/1000,'f',1);
307-
txt += " km";
308-
}
304+
case QGis::METERS:
305+
if (distance > 1000.0)
306+
{
307+
unitLabel=tr(" km");
308+
distance = distance/1000;
309+
}
310+
else if (distance < 0.01)
311+
{
312+
unitLabel=tr(" mm");
313+
distance = distance*1000;
314+
}
315+
else if (distance < 0.1)
316+
{
317+
unitLabel=tr(" cm");
318+
distance = distance*100;
319+
}
320+
else
321+
unitLabel=tr(" m");
322+
break;
323+
case QGis::FEET:
324+
if (distance == 1.0)
325+
unitLabel=tr(" foot");
326+
else
327+
unitLabel=tr(" feet");
328+
break;
329+
case QGis::DEGREES:
330+
if (distance == 1.0)
331+
unitLabel=tr(" degree");
332+
else
333+
unitLabel=tr(" degrees");
334+
break;
335+
case QGis::UNKNOWN:
336+
unitLabel=tr(" unknown");
337+
default:
338+
std::cout << "Error: not picked up map units - actual value = "
339+
<< myMapUnits << std::endl;
340+
};
341+
342+
txt = QString::number(distance,'f',1);
343+
txt += unitLabel;
344+
309345
return txt;
310346
}
311347

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

328364
void QgsMeasure::updateUi()
329365
{
366+
367+
QGis::units myMapUnits = mCanvas->mapUnits();
368+
switch (myMapUnits)
369+
{
370+
case QGis::METERS:
371+
mTable->horizontalHeader()->setLabel( 0, tr("Segments (in meters)") );
372+
break;
373+
case QGis::FEET:
374+
mTable->horizontalHeader()->setLabel( 0, tr("Segments (in feet)") );
375+
break;
376+
case QGis::DEGREES:
377+
mTable->horizontalHeader()->setLabel( 0, tr("Segments (in degrees)") );
378+
break;
379+
case QGis::UNKNOWN:
380+
mTable->horizontalHeader()->setLabel( 0, tr("Segments") );
381+
};
382+
330383
if (mMeasureArea)
331384
{
332385
mTable->hide();
@@ -344,14 +397,14 @@ void QgsMeasure::updateProjection()
344397
{
345398
// set ellipsoid
346399
QSettings settings;
347-
QString ellipsoid = settings.readEntry("/qgis/measure/ellipsoid", "WGS84");
348-
mCalc->setEllipsoid(ellipsoid);
400+
// QString ellipsoid = settings.readEntry("/qgis/measure/ellipsoid", "WGS84");
401+
// mCalc->setEllipsoid(ellipsoid);
349402

350403
// set source SRS and projections enabled flag
351-
QgsMapRender* mapRender = mCanvas->mapRender();
352-
mCalc->setProjectionsEnabled(mapRender->projectionsEnabled());
353-
int srsid = mapRender->destinationSrs().srsid();
354-
mCalc->setSourceSRS(srsid);
404+
// QgsMapRender* mapRender = mCanvas->mapRender();
405+
// mCalc->setProjectionsEnabled(mapRender->projectionsEnabled());
406+
// int srsid = mapRender->destinationSrs().srsid();
407+
// mCalc->setSourceSRS(srsid);
355408

356409
int myRed = settings.value("/qgis/default_measure_color_red", 180).toInt();
357410
int myGreen = settings.value("/qgis/default_measure_color_green", 180).toInt();

‎src/app/qgsmeasure.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ public slots:
111111
QgsMapCanvas *mMapCanvas;
112112

113113
//! distance/area calculator
114-
QgsDistanceArea* mCalc;
114+
//QgsDistanceArea* mCalc;
115115

116116
std::vector<QgsPoint> mPoints;
117117

‎src/core/qgsdistancearea.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -279,8 +279,12 @@ double QgsDistanceArea::measureLine(const QgsPoint& p1, const QgsPoint& p2)
279279
{
280280
pp1 = mCoordTransform->transform(p1);
281281
pp2 = mCoordTransform->transform(p2);
282+
return computeDistanceBearing(pp1, pp2);
283+
}
284+
else
285+
{
286+
return sqrt((p2.x()-p1.x())*(p2.x()-p1.x()) + (p2.y()-p1.y())*(p2.y()-p1.y()));
282287
}
283-
return computeDistanceBearing(pp1, pp2);
284288
}
285289
catch (QgsCsException &cse)
286290
{

‎src/core/qgsmaprender.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include "qgsmaptopixel.h"
2525
#include "qgsmaplayer.h"
2626
#include "qgsmaplayerregistry.h"
27+
#include "qgsdistancearea.h"
2728
//#include "qgsspatialrefsys.h"
2829

2930
#include <QDomDocument>
@@ -37,6 +38,7 @@ QgsMapRender::QgsMapRender()
3738
{
3839
mCoordXForm = new QgsMapToPixel;
3940
mScaleCalculator = new QgsScaleCalculator;
41+
mDistArea = new QgsDistanceArea;
4042

4143
mDrawing = false;
4244
mOverview = false;
@@ -55,6 +57,7 @@ QgsMapRender::~QgsMapRender()
5557
{
5658
delete mCoordXForm;
5759
delete mScaleCalculator;
60+
delete mDistArea;
5861
delete mDestSRS;
5962
}
6063

@@ -365,6 +368,8 @@ void QgsMapRender::setProjectionsEnabled(bool enabled)
365368
if (mProjectionsEnabled != enabled)
366369
{
367370
mProjectionsEnabled = enabled;
371+
QgsDebugMsg("Adjusting DistArea projection on/off");
372+
mDistArea->setProjectionsEnabled(enabled);
368373
updateFullExtent();
369374
emit projectionsEnabled(enabled);
370375
}
@@ -382,7 +387,8 @@ void QgsMapRender::setDestinationSrs(const QgsSpatialRefSys& srs)
382387
QgsDebugMsg("* DestSRS.proj4() = " + srs.proj4String());
383388
if (*mDestSRS != srs)
384389
{
385-
QgsDebugMsg("No, changed my mind!");
390+
QgsDebugMsg("Setting DistArea SRS to " + QString::number(srs.srsid()));
391+
mDistArea->setSourceSRS(srs.srsid());
386392
*mDestSRS = srs;
387393
updateFullExtent();
388394
emit destinationSrsChanged();

‎src/core/qgsmaprender.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ class QgsMapToPixel;
3232
class QgsMapLayer;
3333
class QgsScaleCalculator;
3434
class QgsSpatialRefSys;
35+
class QgsDistanceArea;
3536

3637
/**
3738
* \class QgsMapRender
@@ -68,6 +69,8 @@ class CORE_EXPORT QgsMapRender : public QObject
6869
//! Recalculate the map scale
6970
void updateScale();
7071

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

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

197+
//! tool for measuring
198+
QgsDistanceArea* mDistArea;
194199
};
195200

196201
#endif

0 commit comments

Comments
 (0)
Please sign in to comment.