Skip to content

Commit 60bceac

Browse files
author
g_j_m
committed
Fix for ticket #280. All exceptions thrown while trying to transform
points are caught. git-svn-id: http://svn.osgeo.org/qgis/trunk/qgis@6083 c8812cc2-4d05-0410-92ff-de0c093fc19c
1 parent b9950a4 commit 60bceac

File tree

1 file changed

+74
-41
lines changed

1 file changed

+74
-41
lines changed

src/core/qgsdistancearea.cpp

+74-41
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@
1414
***************************************************************************/
1515
/* $Id$ */
1616

17-
#include <math.h>
17+
#include <cmath>
1818
#include <sqlite3.h>
19-
#include <qdir.h>
20-
#include <qsettings.h>
19+
#include <QDir>
20+
#include <QSettings>
2121

2222
#include "qgis.h"
2323
#include "qgspoint.h"
@@ -27,6 +27,7 @@
2727
#include "qgsgeometry.h"
2828
#include "qgsdistancearea.h"
2929
#include "qgsapplication.h"
30+
#include "qgslogger.h"
3031

3132
#define DEG2RAD(x) ((x)*M_PI/180)
3233

@@ -264,24 +265,41 @@ double QgsDistanceArea::measureLine(const std::vector<QgsPoint>& points)
264265
if (points.size() < 2)
265266
return 0;
266267

267-
double total = 0;
268-
QgsPoint p1, p2;
269-
p1 = mCoordTransform->transform(points[0]);
270-
271-
for (int i = 1; i < points.size(); i++)
268+
try
272269
{
273-
p2 = mCoordTransform->transform(points[i]);
274-
total = computeDistanceBearing(p1,p2);
275-
p1 = p2;
270+
double total = 0;
271+
QgsPoint p1, p2;
272+
p1 = mCoordTransform->transform(points[0]);
273+
274+
for (int i = 1; i < points.size(); i++)
275+
{
276+
p2 = mCoordTransform->transform(points[i]);
277+
total = computeDistanceBearing(p1,p2);
278+
p1 = p2;
279+
}
280+
return total;
276281
}
277-
return total;
282+
catch (QgsCsException &cse)
283+
{
284+
QgsLogger::warning(QObject::tr("Caught a coordinate system exception while trying to transform a point. Unable to calculate line length."));
285+
return 0.0;
286+
}
287+
278288
}
279289

280290
double QgsDistanceArea::measureLine(const QgsPoint& p1, const QgsPoint& p2)
281291
{
282-
QgsPoint pp1 = mCoordTransform->transform(p1);
283-
QgsPoint pp2 = mCoordTransform->transform(p2);
284-
return computeDistanceBearing(pp1, pp2);
292+
try
293+
{
294+
QgsPoint pp1 = mCoordTransform->transform(p1);
295+
QgsPoint pp2 = mCoordTransform->transform(p2);
296+
return computeDistanceBearing(pp1, pp2);
297+
}
298+
catch (QgsCsException &cse)
299+
{
300+
QgsLogger::warning(QObject::tr("Caught a coordinate system exception while trying to transform a point. Unable to calculate line length."));
301+
return 0.0;
302+
}
285303
}
286304

287305

@@ -300,46 +318,61 @@ unsigned char* QgsDistanceArea::measurePolygon(unsigned char* feature, double* a
300318
double x,y, areaTmp;
301319
*area = 0;
302320

303-
for (unsigned int idx = 0; idx < numRings; idx++)
321+
try
304322
{
305-
int nPoints = *((int*)ptr);
306-
points.resize(nPoints);
307-
ptr += 4;
308-
309-
// Extract the points from the WKB and store in a pair of
310-
// vectors.
311-
for (unsigned int jdx = 0; jdx < nPoints; jdx++)
323+
for (unsigned int idx = 0; idx < numRings; idx++)
312324
{
313-
x = *((double *) ptr);
314-
ptr += sizeof(double);
315-
y = *((double *) ptr);
316-
ptr += sizeof(double);
317-
318-
points[jdx] = mCoordTransform->transform(QgsPoint(x,y));
319-
}
325+
int nPoints = *((int*)ptr);
326+
points.resize(nPoints);
327+
ptr += 4;
320328

321-
if (points.size() > 2)
322-
{
323-
areaTmp = computePolygonArea(points);
324-
if (idx == 0)
325-
*area += areaTmp; // exterior ring
326-
else
327-
*area -= areaTmp; // interior rings
329+
// Extract the points from the WKB and store in a pair of
330+
// vectors.
331+
for (unsigned int jdx = 0; jdx < nPoints; jdx++)
332+
{
333+
x = *((double *) ptr);
334+
ptr += sizeof(double);
335+
y = *((double *) ptr);
336+
ptr += sizeof(double);
337+
338+
points[jdx] = mCoordTransform->transform(QgsPoint(x,y));
339+
}
340+
341+
if (points.size() > 2)
342+
{
343+
areaTmp = computePolygonArea(points);
344+
if (idx == 0)
345+
*area += areaTmp; // exterior ring
346+
else
347+
*area -= areaTmp; // interior rings
348+
}
328349
}
329350
}
351+
catch (QgsCsException &cse)
352+
{
353+
QgsLogger::warning(QObject::tr("Caught a coordinate system exception while trying to transform a point. Unable to calculate polygon area."));
354+
}
330355

331356
return ptr;
332357
}
333358

334359

335360
double QgsDistanceArea::measurePolygon(const std::vector<QgsPoint>& points)
336361
{
337-
std::vector<QgsPoint> pts(points.size());
338-
for (std::vector<QgsPoint>::size_type i = 0; i < points.size(); i++)
362+
try
363+
{
364+
std::vector<QgsPoint> pts(points.size());
365+
for (std::vector<QgsPoint>::size_type i = 0; i < points.size(); i++)
366+
{
367+
pts[i] = mCoordTransform->transform(points[i]);
368+
}
369+
return computePolygonArea(pts);
370+
}
371+
catch (QgsCsException &cse)
339372
{
340-
pts[i] = mCoordTransform->transform(points[i]);
373+
QgsLogger::warning(QObject::tr("Caught a coordinate system exception while trying to transform a point. Unable to calculate polygon area."));
374+
return 0.0;
341375
}
342-
return computePolygonArea(pts);
343376
}
344377

345378

0 commit comments

Comments
 (0)