Skip to content

Commit

Permalink
Changed usage of std::vector to QList in QgsDistance area.
Browse files Browse the repository at this point in the history
Added the modified functions to Python bindings.


git-svn-id: http://svn.osgeo.org/qgis/trunk@6760 c8812cc2-4d05-0410-92ff-de0c093fc19c
  • Loading branch information
wonder committed Mar 4, 2007
1 parent 1746d7d commit e863ecc
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 32 deletions.
4 changes: 2 additions & 2 deletions python/core/qgsdistancearea.sip
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,13 @@ class QgsDistanceArea
double measure(QgsGeometry* geometry);

//! measures line with more segments
// TODO wrap double measureLine(const std::vector<QgsPoint>& points);
double measureLine(const QList<QgsPoint>& points);

//! measures line with one segment
double measureLine(const QgsPoint& p1, const QgsPoint& p2);

//! measures polygon area
// TODO wrap double measurePolygon(const std::vector<QgsPoint>& points);
double measurePolygon(const QList<QgsPoint>& points);

//! compute bearing - in radians
double getBearing(const QgsPoint& p1, const QgsPoint& p2);
Expand Down
8 changes: 4 additions & 4 deletions src/app/qgsmeasure.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ QgsMeasure::~QgsMeasure()
void QgsMeasure::restart(void )
{
updateProjection();
mPoints.resize(0);
mPoints.clear();
// Set one cell row where to update current distance
// If measuring area, the table doesn't get shown
mTable->setNumRows(1);
Expand Down Expand Up @@ -166,7 +166,7 @@ void QgsMeasure::addPoint(QgsPoint &point)
return;

QgsPoint pnt(point);
mPoints.push_back(pnt);
mPoints.append(pnt);

if (mMeasureArea && mPoints.size() > 2)
{
Expand Down Expand Up @@ -219,8 +219,8 @@ void QgsMeasure::mouseMove(QgsPoint &point)
// show current distance/area while moving the point
// by creating a temporary copy of point array
// and adding moving point at the end
std::vector<QgsPoint> tmpPoints = mPoints;
tmpPoints.push_back(point);
QList<QgsPoint> tmpPoints = mPoints;
tmpPoints.append(point);
if (mMeasureArea && tmpPoints.size() > 2)
{
double area = mCanvas->mapRender()->distArea()->measurePolygon(tmpPoints);
Expand Down
3 changes: 1 addition & 2 deletions src/app/qgsmeasure.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
#include <QWidget>
#include "qgsmaptool.h"
#include "qgspoint.h"
#include <vector>

class QgsDistanceArea;
class QgsMapCanvas;
Expand Down Expand Up @@ -113,7 +112,7 @@ public slots:
//! distance/area calculator
//QgsDistanceArea* mCalc;

std::vector<QgsPoint> mPoints;
QList<QgsPoint> mPoints;

double mTotal;

Expand Down
39 changes: 21 additions & 18 deletions src/core/qgsdistancearea.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -229,24 +229,25 @@ unsigned char* QgsDistanceArea::measureLine(unsigned char* feature, double* area
unsigned int nPoints = *((int*)ptr);
ptr = feature + 9;

std::vector<QgsPoint> points(nPoints);
QList<QgsPoint> points;
double x,y;

QgsDebugMsg("This feature WKB has " + QString::number(nPoints) + " points");
// Extract the points from the WKB format into the vector
for (unsigned int i = 0; i < nPoints; ++i)
{
QgsPoint& p = points[i];
p.setX(*((double *) ptr));
x = *((double *) ptr);
ptr += sizeof(double);
p.setY(*((double *) ptr));
y = *((double *) ptr);
ptr += sizeof(double);
points.append(QgsPoint(x,y));
}

*area = measureLine(points);
return ptr;
}

double QgsDistanceArea::measureLine(const std::vector<QgsPoint>& points)
double QgsDistanceArea::measureLine(const QList<QgsPoint>& points)
{
if (points.size() < 2)
return 0;
Expand All @@ -261,16 +262,16 @@ double QgsDistanceArea::measureLine(const std::vector<QgsPoint>& points)
else
p1 = points[0];

for (std::vector<QgsPoint>::size_type i = 1; i < points.size(); i++)
for (QList<QgsPoint>::const_iterator i = points.begin(); i != points.end(); ++i)
{
if (mProjectionsEnabled && (mEllipsoid != "NONE"))
{
p2 = mCoordTransform->transform(points[i]);
p2 = mCoordTransform->transform(*i);
total += computeDistanceBearing(p1,p2);
}
else
{
p2 = points[i];
p2 = *i;
total += measureLine(p1,p2);
}

Expand Down Expand Up @@ -322,7 +323,8 @@ unsigned char* QgsDistanceArea::measurePolygon(unsigned char* feature, double* a
// Set pointer to the first ring
unsigned char* ptr = feature + 1 + 2 * sizeof(int);

std::vector<QgsPoint> points;
QList<QgsPoint> points;
QgsPoint pnt;
double x,y, areaTmp;
*area = 0;

Expand All @@ -331,7 +333,6 @@ unsigned char* QgsDistanceArea::measurePolygon(unsigned char* feature, double* a
for (unsigned int idx = 0; idx < numRings; idx++)
{
int nPoints = *((int*)ptr);
points.resize(nPoints);
ptr += 4;

// Extract the points from the WKB and store in a pair of
Expand All @@ -342,12 +343,14 @@ unsigned char* QgsDistanceArea::measurePolygon(unsigned char* feature, double* a
ptr += sizeof(double);
y = *((double *) ptr);
ptr += sizeof(double);

pnt = QgsPoint(x,y);

points[jdx] = QgsPoint(x,y);
if (mProjectionsEnabled && (mEllipsoid != "NONE"))
{
points[jdx] = mCoordTransform->transform(points[jdx]);
pnt = mCoordTransform->transform(pnt);
}
points.append(pnt);
}

if (points.size() > 2)
Expand All @@ -369,17 +372,17 @@ unsigned char* QgsDistanceArea::measurePolygon(unsigned char* feature, double* a
}


double QgsDistanceArea::measurePolygon(const std::vector<QgsPoint>& points)
double QgsDistanceArea::measurePolygon(const QList<QgsPoint>& points)
{

try
{
if (mProjectionsEnabled && (mEllipsoid != "NONE"))
{
std::vector<QgsPoint> pts(points.size());
for (std::vector<QgsPoint>::size_type i = 0; i < points.size(); i++)
QList<QgsPoint> pts;
for (QList<QgsPoint>::const_iterator i = points.begin(); i != points.end(); ++i)
{
pts[i] = mCoordTransform->transform(points[i]);
pts.append(mCoordTransform->transform(*i));
}
return computePolygonArea(pts);
}
Expand Down Expand Up @@ -540,7 +543,7 @@ void QgsDistanceArea::computeAreaInit()
}


double QgsDistanceArea::computePolygonArea(const std::vector<QgsPoint>& points)
double QgsDistanceArea::computePolygonArea(const QList<QgsPoint>& points)
{
double x1,y1,x2,y2,dx,dy;
double Qbar1, Qbar2;
Expand Down Expand Up @@ -595,7 +598,7 @@ double QgsDistanceArea::computePolygonArea(const std::vector<QgsPoint>& points)
return area;
}

double QgsDistanceArea::computePolygonFlatArea(const std::vector<QgsPoint>& points)
double QgsDistanceArea::computePolygonFlatArea(const QList<QgsPoint>& points)
{
// Normal plane area calculations.
double area = 0.0;
Expand Down
12 changes: 6 additions & 6 deletions src/core/qgsdistancearea.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
#ifndef QGSDISTANCEAREA_H
#define QGSDISTANCEAREA_H

#include <vector>
#include <QList>
#include "qgscoordinatetransform.h"

class QgsGeometry;
Expand All @@ -28,7 +28,7 @@ General purpose distance and area calculator
- it's possible to pass points/features in any SRS, coordinates are transformed
- two options how to use it
+ use measure() takes QgsGeometry as a parameter and calculates distance or area
+ use directly measureLine(), measurePolygon() which take vector of QgsPoints
+ use directly measureLine(), measurePolygon() which take list of QgsPoints
(both cases transform the coordinates from source SRS to the ellipse coords)
- returned values are in meters resp. square meters
*/
Expand Down Expand Up @@ -74,12 +74,12 @@ class CORE_EXPORT QgsDistanceArea
double measure(QgsGeometry* geometry);

//! measures line with more segments
double measureLine(const std::vector<QgsPoint>& points);
double measureLine(const QList<QgsPoint>& points);
//! measures line with one segment
double measureLine(const QgsPoint& p1, const QgsPoint& p2);

//! measures polygon area
double measurePolygon(const std::vector<QgsPoint>& points);
double measurePolygon(const QList<QgsPoint>& points);

//! compute bearing - in radians
double getBearing(const QgsPoint& p1, const QgsPoint& p2);
Expand Down Expand Up @@ -112,9 +112,9 @@ class CORE_EXPORT QgsDistanceArea
algorithm has been taken from GRASS: gis/area_poly1.c
*/
double computePolygonArea(const std::vector<QgsPoint>& points);
double computePolygonArea(const QList<QgsPoint>& points);

double computePolygonFlatArea(const std::vector<QgsPoint>& points);
double computePolygonFlatArea(const QList<QgsPoint>& points);

/**
precalculates some values
Expand Down

0 comments on commit e863ecc

Please sign in to comment.