Skip to content

Commit

Permalink
Use compound curve in capture tool to store captured geometry
Browse files Browse the repository at this point in the history
  • Loading branch information
mhugent committed Aug 13, 2015
1 parent 45d4dbe commit 9b31f34
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 15 deletions.
96 changes: 87 additions & 9 deletions src/app/qgsmaptoolcapture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,12 @@
#include "qgscursors.h"
#include "qgsgeometryvalidator.h"
#include "qgslayertreeview.h"
#include "qgslinestringv2.h"
#include "qgslogger.h"
#include "qgsmapcanvas.h"
#include "qgsmapmouseevent.h"
#include "qgsmaprenderer.h"
#include "qgspolygonv2.h"
#include "qgsrubberband.h"
#include "qgsvectorlayer.h"
#include "qgsvertexmarker.h"
Expand Down Expand Up @@ -188,7 +190,7 @@ int QgsMapToolCapture::addVertex( const QgsPoint& point )
mRubberBand = createRubberBand( mCaptureMode == CapturePolygon ? QGis::Polygon : QGis::Line );
}
mRubberBand->addPoint( point );
mCaptureList.append( layerPoint );
mCaptureCurve.addVertex( QgsPointV2( layerPoint.x(), layerPoint.y() ) );

if ( !mTempRubberBand )
{
Expand All @@ -215,14 +217,58 @@ int QgsMapToolCapture::addVertex( const QgsPoint& point )
return 0;
}

int QgsMapToolCapture::addCurve( QgsCurveV2* c )
{
return 1; //todo...

#if 0
if ( !c )
{
return 1;
}

if ( !mRubberBand )
{
mRubberBand = createRubberBand( mCaptureMode == CapturePolygon ? QGis::Polygon : QGis::Line );
}

QgsLineStringV2* lineString = c->curveToLine();
QList<QgsPointV2> linePoints;
lineString->points( linePoints );
delete lineString;
QList<QgsPointV2>::const_iterator ptIt = linePoints.constBegin();
for ( ; ptIt != linePoints.constEnd(); ++ptIt )
{
mRubberBand->addPoint( QgsPoint( ptIt->x(), ptIt->y() ) );
}

if ( !mTempRubberBand )
{
mTempRubberBand = createRubberBand( mCaptureMode == CapturePolygon ? QGis::Polygon : QGis::Line, true );
}
else
{
mTempRubberBand->reset();
}
QgsPointV2 endPt = c->endPoint();
mTempRubberBand->addPoint( QgsPoint( endPt.x(), endPt.y() ) ); //add last point of c

//const QgsCoordinateTransform* ct = mapCanvas()->mapSettings()->layerTransform( QgsMapLayer *layer ) const
//c->transform( ct, QgsCoordinateTransform::ReverseTransform);
//mCaptureCurve.addCurve( transformed curve );

return 0;
#endif //0
}


void QgsMapToolCapture::undo()
{
if ( mRubberBand )
{
int rubberBandSize = mRubberBand->numberOfVertices();
int tempRubberBandSize = mTempRubberBand->numberOfVertices();
int captureListSize = mCaptureList.size();
int captureListSize = size();

if ( rubberBandSize < 1 || captureListSize < 1 )
{
Expand All @@ -244,7 +290,9 @@ void QgsMapToolCapture::undo()
mTempRubberBand->reset( mCaptureMode == CapturePolygon ? true : false );
}

mCaptureList.removeLast();
QgsVertexId vertexToRemove;
vertexToRemove.part = 0; vertexToRemove.ring = 0; vertexToRemove.vertex = size() - 1;
mCaptureCurve.deleteVertex( vertexToRemove );

validateGeometry();
}
Expand Down Expand Up @@ -298,7 +346,7 @@ void QgsMapToolCapture::stopCapturing()
#endif

mCapturing = false;
mCaptureList.clear();
mCaptureCurve.clear();
mCanvas->refresh();
}

Expand All @@ -313,7 +361,7 @@ void QgsMapToolCapture::deleteTempRubberBand()

void QgsMapToolCapture::closePolygon()
{
mCaptureList.append( mCaptureList[0] );
mCaptureCurve.close();
}

void QgsMapToolCapture::validateGeometry()
Expand Down Expand Up @@ -343,14 +391,18 @@ void QgsMapToolCapture::validateGeometry()
case CapturePoint:
return;
case CaptureLine:
if ( mCaptureList.size() < 2 )
if ( size() < 2 )
return;
g = QgsGeometry::fromPolyline( mCaptureList.toVector() );
g = new QgsGeometry( mCaptureCurve.curveToLine() );
break;
case CapturePolygon:
if ( mCaptureList.size() < 3 )
if ( size() < 3 )
return;
g = QgsGeometry::fromPolygon( QgsPolygon() << ( QgsPolyline() << mCaptureList.toVector() << mCaptureList[0] ) );
QgsLineStringV2* exteriorRing = mCaptureCurve.curveToLine();
exteriorRing->close();
QgsPolygonV2* polygon = new QgsPolygonV2();
polygon->setExteriorRing( exteriorRing );
g = new QgsGeometry( polygon );
break;
}

Expand Down Expand Up @@ -402,3 +454,29 @@ void QgsMapToolCapture::validationFinished()
QStatusBar *sb = QgisApp::instance()->statusBar();
sb->showMessage( tr( "Validation finished." ) );
}

int QgsMapToolCapture::size()
{
return mCaptureCurve.numPoints();
}

QList<QgsPoint> QgsMapToolCapture::points()
{
QList<QgsPointV2> pts;
QList<QgsPoint> points;
mCaptureCurve.points( pts );
QgsGeometry::convertPointList( pts, points );
return points;
}

void QgsMapToolCapture::setPoints( const QList<QgsPoint>& pointList )
{
QList<QgsPointV2> pts;
QgsGeometry::convertPointList( pointList, pts );

QgsLineStringV2* line = new QgsLineStringV2();
line->setPoints( pts );

mCaptureCurve.clear();
mCaptureCurve.addCurve( line );
}
14 changes: 8 additions & 6 deletions src/app/qgsmaptoolcapture.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@


#include "qgsmaptooledit.h"
#include "qgscompoundcurvev2.h"
#include "qgspoint.h"
#include "qgsgeometry.h"

Expand Down Expand Up @@ -64,6 +65,9 @@ class APP_EXPORT QgsMapToolCapture : public QgsMapToolEdit
@return 0 in case of success, 1 if current layer is not a vector layer, 2 if coordinate transformation failed*/
int addVertex( const QgsPoint& point );

/** Adds a whole curve (e.g. circularstring) to the captured geometry. Curve must be in map CRS*/
int addCurve( QgsCurveV2* c );

/** Removes the last vertex from mRubberBand and mCaptureList*/
void undo();

Expand All @@ -72,11 +76,9 @@ class APP_EXPORT QgsMapToolCapture : public QgsMapToolEdit
void stopCapturing();
void deleteTempRubberBand();

int size() { return mCaptureList.size(); }
QList<QgsPoint>::iterator begin() { return mCaptureList.begin(); }
QList<QgsPoint>::iterator end() { return mCaptureList.end(); }
const QList<QgsPoint> &points() { return mCaptureList; }
void setPoints( const QList<QgsPoint>& pointList ) { mCaptureList = pointList; }
int size();
QList<QgsPoint> points();
void setPoints( const QList<QgsPoint>& pointList );
void closePolygon();

private:
Expand All @@ -90,7 +92,7 @@ class APP_EXPORT QgsMapToolCapture : public QgsMapToolEdit
QgsRubberBand* mTempRubberBand;

/** List to store the points of digitised lines and polygons (in layer coordinates)*/
QList<QgsPoint> mCaptureList;
QgsCompoundCurveV2 mCaptureCurve;

void validateGeometry();
QString mTip;
Expand Down

0 comments on commit 9b31f34

Please sign in to comment.