Skip to content

Commit 20591ba

Browse files
author
mhugent
committed
[FEATURE] A reshape tool to apply to line/polygon geometries. The part of a geometry between the first and last intersection of the reshape line will be replaced
git-svn-id: http://svn.osgeo.org/qgis/trunk@11500 c8812cc2-4d05-0410-92ff-de0c093fc19c
1 parent 61604a3 commit 20591ba

File tree

9 files changed

+719
-24
lines changed

9 files changed

+719
-24
lines changed
1.82 KB
Loading

python/core/qgsgeometry.sip

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,11 @@ not disjoint with existing polygons of the feature*/
204204
@return 0 in case of success, 1 if geometry has not been split, error else*/
205205
int splitGeometry(const QList<QgsPoint>& splitLine, QList<QgsGeometry*>& newGeometries, bool topological, QList<QgsPoint>& topologyTestPoints);
206206

207+
/**Replaces a part of this geometry with another line
208+
@return 0 in case of success
209+
@note: this function was added in version 1.3*/
210+
int reshapeGeometry( const QList<QgsPoint>& reshapeWithLine );
211+
207212
/**Changes this geometry such that it does not intersect the other geometry
208213
@param other geometry that should not be intersect
209214
@return 0 in case of success*/

src/app/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ SET(QGIS_APP_SRCS
3939
qgsmaptoolmovefeature.cpp
4040
qgsmaptoolmovevertex.cpp
4141
qgsmaptoolnodetool.cpp
42+
qgsmaptoolreshape.cpp
4243
qgsmaptoolselect.cpp
4344
qgsmaptoolsimplify.cpp
4445
qgsmaptoolsplitfeatures.cpp

src/app/qgisapp.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,7 @@
171171
#include "qgsmaptoolnodetool.h"
172172
#include "qgsmaptoolpan.h"
173173
#include "qgsmaptoolselect.h"
174+
#include "qgsmaptoolreshape.h"
174175
#include "qgsmaptoolsplitfeatures.h"
175176
#include "qgsmaptoolvertexedit.h"
176177
#include "qgsmaptoolzoom.h"
@@ -473,6 +474,7 @@ QgisApp::~QgisApp()
473474
delete mMapTools.mCaptureLine;
474475
delete mMapTools.mCapturePolygon;
475476
delete mMapTools.mMoveFeature;
477+
delete mMapTools.mReshapeFeatures;
476478
delete mMapTools.mSplitFeatures;
477479
delete mMapTools.mSelect;
478480
delete mMapTools.mVertexAdd;
@@ -660,6 +662,12 @@ void QgisApp::createActions()
660662
connect( mActionMoveFeature, SIGNAL( triggered() ), this, SLOT( moveFeature() ) );
661663
mActionMoveFeature->setEnabled( false );
662664

665+
mActionReshapeFeatures = new QAction( getThemeIcon( "mActionReshape.png" ), tr( "Reshape Features" ), this );
666+
shortcuts->registerAction( mActionReshapeFeatures );
667+
mActionReshapeFeatures->setStatusTip( tr( "Reshape Features" ) );
668+
connect( mActionReshapeFeatures, SIGNAL( triggered() ), this, SLOT( reshapeFeatures() ) );
669+
mActionReshapeFeatures->setEnabled( false );
670+
663671
mActionSplitFeatures = new QAction( getThemeIcon( "mActionSplitFeatures.png" ), tr( "Split Features" ), this );
664672
shortcuts->registerAction( mActionSplitFeatures );
665673
mActionSplitFeatures->setStatusTip( tr( "Split Features" ) );
@@ -1062,6 +1070,8 @@ void QgisApp::createActionGroups()
10621070
mMapToolGroup->addAction( mActionCapturePolygon );
10631071
mActionMoveFeature->setCheckable( true );
10641072
mMapToolGroup->addAction( mActionMoveFeature );
1073+
mActionReshapeFeatures->setCheckable( true );
1074+
mMapToolGroup->addAction( mActionReshapeFeatures );
10651075
mActionSplitFeatures->setCheckable( true );
10661076
mMapToolGroup->addAction( mActionSplitFeatures );
10671077
mMapToolGroup->addAction( mActionDeleteSelected );
@@ -1175,6 +1185,7 @@ void QgisApp::createMenus()
11751185
mEditMenu->addAction( mActionAddIsland );
11761186
mEditMenu->addAction( mActionDeleteRing );
11771187
mEditMenu->addAction( mActionDeletePart );
1188+
mEditMenu->addAction( mActionReshapeFeatures );
11781189
mEditMenu->addAction( mActionSplitFeatures );
11791190
mEditMenu->addAction( mActionMergeFeatures );
11801191
mEditMenu->addAction( mActionNodeTool );
@@ -1383,6 +1394,7 @@ void QgisApp::createToolBars()
13831394
mAdvancedDigitizeToolBar->addAction( mActionAddIsland );
13841395
mAdvancedDigitizeToolBar->addAction( mActionDeleteRing );
13851396
mAdvancedDigitizeToolBar->addAction( mActionDeletePart );
1397+
mAdvancedDigitizeToolBar->addAction( mActionReshapeFeatures );
13861398
mAdvancedDigitizeToolBar->addAction( mActionSplitFeatures );
13871399
mAdvancedDigitizeToolBar->addAction( mActionMergeFeatures );
13881400
mAdvancedDigitizeToolBar->addAction( mActionNodeTool );
@@ -1605,6 +1617,7 @@ void QgisApp::setTheme( QString theThemeName )
16051617
mActionCaptureLine->setIcon( getThemeIcon( "/mActionCaptureLine.png" ) );
16061618
mActionCapturePolygon->setIcon( getThemeIcon( "/mActionCapturePolygon.png" ) );
16071619
mActionMoveFeature->setIcon( getThemeIcon( "/mActionMoveFeature.png" ) );
1620+
mActionReshapeFeatures->setIcon( getThemeIcon( "/mActionReshape.png" ) );
16081621
mActionSplitFeatures->setIcon( getThemeIcon( "/mActionSplitFeatures.png" ) );
16091622
mActionDeleteSelected->setIcon( getThemeIcon( "/mActionDeleteSelected.png" ) );
16101623
mActionAddVertex->setIcon( getThemeIcon( "/mActionAddVertex.png" ) );
@@ -1735,6 +1748,8 @@ void QgisApp::createCanvas()
17351748
mActionCapturePolygon->setVisible( false );
17361749
mMapTools.mMoveFeature = new QgsMapToolMoveFeature( mMapCanvas );
17371750
mMapTools.mMoveFeature->setAction( mActionMoveFeature );
1751+
mMapTools.mReshapeFeatures = new QgsMapToolReshape( mMapCanvas );
1752+
mMapTools.mReshapeFeatures->setAction( mActionReshapeFeatures );
17381753
mMapTools.mSplitFeatures = new QgsMapToolSplitFeatures( mMapCanvas );
17391754
mMapTools.mSplitFeatures->setAction( mActionSplitFeatures );
17401755
mMapTools.mSelect = new QgsMapToolSelect( mMapCanvas );
@@ -4337,6 +4352,11 @@ void QgisApp::splitFeatures()
43374352
mMapCanvas->setMapTool( mMapTools.mSplitFeatures );
43384353
}
43394354

4355+
void QgisApp::reshapeFeatures()
4356+
{
4357+
mMapCanvas->setMapTool( mMapTools.mReshapeFeatures );
4358+
}
4359+
43404360
void QgisApp::capturePoint()
43414361
{
43424362
if ( mMapCanvas && mMapCanvas->isDrawing() )
@@ -5634,6 +5654,7 @@ void QgisApp::activateDeactivateLayerRelatedActions( QgsMapLayer* layer )
56345654
mActionMoveVertex->setEnabled( false );
56355655
mActionAddRing->setEnabled( false );
56365656
mActionAddIsland->setEnabled( false );
5657+
mActionReshapeFeatures->setEnabled( false );
56375658
mActionSplitFeatures->setEnabled( false );
56385659
mActionSimplifyFeature->setEnabled( false );
56395660
mActionDeleteRing->setEnabled( false );
@@ -5650,6 +5671,7 @@ void QgisApp::activateDeactivateLayerRelatedActions( QgsMapLayer* layer )
56505671
{
56515672
mActionCaptureLine->setEnabled( true );
56525673
mActionCaptureLine->setVisible( true );
5674+
mActionReshapeFeatures->setEnabled( true );
56535675
mActionSplitFeatures->setEnabled( true );
56545676
mActionSimplifyFeature->setEnabled( true );
56555677
mActionDeletePart->setEnabled( true );
@@ -5659,6 +5681,7 @@ void QgisApp::activateDeactivateLayerRelatedActions( QgsMapLayer* layer )
56595681
{
56605682
mActionCaptureLine->setEnabled( false );
56615683
mActionCaptureLine->setVisible( false );
5684+
mActionReshapeFeatures->setEnabled( false );
56625685
mActionSplitFeatures->setEnabled( false );
56635686
mActionSimplifyFeature->setEnabled( false );
56645687
mActionDeletePart->setEnabled( false );
@@ -5679,6 +5702,7 @@ void QgisApp::activateDeactivateLayerRelatedActions( QgsMapLayer* layer )
56795702
mActionCapturePolygon->setVisible( true );
56805703
mActionAddRing->setEnabled( true );
56815704
mActionAddIsland->setEnabled( true );
5705+
mActionReshapeFeatures->setEnabled( true );
56825706
mActionSplitFeatures->setEnabled( true );
56835707
mActionSimplifyFeature->setEnabled( true );
56845708
mActionDeleteRing->setEnabled( true );
@@ -5690,6 +5714,7 @@ void QgisApp::activateDeactivateLayerRelatedActions( QgsMapLayer* layer )
56905714
mActionCapturePolygon->setVisible( false );
56915715
mActionAddRing->setEnabled( false );
56925716
mActionAddIsland->setEnabled( false );
5717+
mActionReshapeFeatures->setEnabled( false );
56935718
mActionSplitFeatures->setEnabled( false );
56945719
mActionSimplifyFeature->setEnabled( false );
56955720
mActionDeleteRing->setEnabled( false );

src/app/qgisapp.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -497,6 +497,8 @@ class QgisApp : public QMainWindow
497497
void deleteSelected();
498498
//! activates the move feature tool
499499
void moveFeature();
500+
//! activates the reshape features tool
501+
void reshapeFeatures();
500502
//! activates the split features tool
501503
void splitFeatures();
502504
//! activates the add vertex tool
@@ -725,6 +727,7 @@ class QgisApp : public QMainWindow
725727
QAction *mActionCapturePolygon;
726728
QAction *mActionDeleteSelected;
727729
QAction *mActionMoveFeature;
730+
QAction *mActionReshapeFeatures;
728731
QAction *mActionSplitFeatures;
729732
QAction *mActionAddVertex;
730733
QAction *mActionDeleteVertex;
@@ -847,6 +850,7 @@ class QgisApp : public QMainWindow
847850
QgsMapTool* mCaptureLine;
848851
QgsMapTool* mCapturePolygon;
849852
QgsMapTool* mMoveFeature;
853+
QgsMapTool* mReshapeFeatures;
850854
QgsMapTool* mSplitFeatures;
851855
QgsMapTool* mSelect;
852856
QgsMapTool* mVertexAdd;

src/app/qgsmaptoolreshape.cpp

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
/***************************************************************************
2+
qgsmaptoolreshape.cpp
3+
---------------------------
4+
begin : Juli 2009
5+
copyright : (C) 2009 by Marco Hugentobler
6+
email : marco dot hugentobler at karto dot baug dot ethz dot ch
7+
***************************************************************************
8+
* *
9+
* This program is free software; you can redistribute it and/or modify *
10+
* it under the terms of the GNU General Public License as published by *
11+
* the Free Software Foundation; either version 2 of the License, or *
12+
* (at your option) any later version. *
13+
* *
14+
***************************************************************************/
15+
/* $Id$ */
16+
17+
#include "qgsmaptoolreshape.h"
18+
#include "qgsgeometry.h"
19+
#include "qgsmapcanvas.h"
20+
#include "qgsrubberband.h"
21+
#include "qgsvectorlayer.h"
22+
#include <QMessageBox>
23+
#include <QMouseEvent>
24+
25+
QgsMapToolReshape::QgsMapToolReshape( QgsMapCanvas* canvas ): QgsMapToolCapture( canvas, QgsMapToolCapture::CaptureLine )
26+
{
27+
28+
}
29+
30+
QgsMapToolReshape::~QgsMapToolReshape()
31+
{
32+
33+
}
34+
35+
void QgsMapToolReshape::canvasReleaseEvent( QMouseEvent * e )
36+
{
37+
//check if we operate on a vector layer //todo: move this to a function in parent class to avoid duplication
38+
QgsVectorLayer *vlayer = dynamic_cast <QgsVectorLayer*>( mCanvas->currentLayer() );
39+
40+
if ( !vlayer )
41+
{
42+
QMessageBox::information( 0, tr( "Not a vector layer" ),
43+
tr( "The current layer is not a vector layer" ) );
44+
return;
45+
}
46+
47+
if ( !vlayer->isEditable() )
48+
{
49+
QMessageBox::information( 0, tr( "Layer not editable" ),
50+
tr( "Cannot edit the vector layer. To make it editable, go to the file item "
51+
"of the layer, right click and check 'Allow Editing'." ) );
52+
return;
53+
}
54+
55+
//add point to list and to rubber band
56+
int error = addVertex( e->pos() );
57+
if ( error == 1 )
58+
{
59+
//current layer is not a vector layer
60+
return;
61+
}
62+
else if ( error == 2 )
63+
{
64+
//problem with coordinate transformation
65+
QMessageBox::information( 0, tr( "Coordinate transform error" ),
66+
tr( "Cannot transform the point to the layers coordinate system" ) );
67+
return;
68+
}
69+
70+
if ( e->button() == Qt::LeftButton )
71+
{
72+
mCapturing = TRUE;
73+
}
74+
else if ( e->button() == Qt::RightButton )
75+
{
76+
mCapturing = FALSE;
77+
delete mRubberBand;
78+
mRubberBand = 0;
79+
80+
//find out bounding box of mCaptureList
81+
if(mCaptureList.size() < 1)
82+
{
83+
return;
84+
}
85+
QgsPoint firstPoint = mCaptureList.at(0);
86+
QgsRectangle bbox(firstPoint.x(), firstPoint.y(), firstPoint.x(), firstPoint.y());
87+
for(int i = 1; i < mCaptureList.size(); ++i)
88+
{
89+
bbox.combineExtentWith(mCaptureList.at(i).x(), mCaptureList.at(i).y());
90+
}
91+
92+
//query all the features that intersect bounding box of capture line
93+
vlayer->select(QgsAttributeList(), bbox, true, false);
94+
QgsFeature f;
95+
int reshapeReturn;
96+
bool reshapeDone = false;
97+
98+
vlayer->beginEditCommand( tr( "Reshape" ) );
99+
while(vlayer->nextFeature(f))
100+
{
101+
//query geometry
102+
//call geometry->reshape(mCaptureList)
103+
//register changed geometry in vector layer
104+
QgsGeometry* geom = f.geometry();
105+
if(geom)
106+
{
107+
reshapeReturn = geom->reshapeGeometry(mCaptureList);
108+
if(reshapeReturn == 0)
109+
{
110+
vlayer->changeGeometry(f.id(), geom);
111+
reshapeDone = true;
112+
}
113+
}
114+
}
115+
116+
if(reshapeDone)
117+
{
118+
vlayer->endEditCommand();
119+
}
120+
else
121+
{
122+
vlayer->destroyEditCommand();
123+
}
124+
125+
mCaptureList.clear();
126+
mCanvas->refresh();
127+
}
128+
}

src/app/qgsmaptoolreshape.h

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/***************************************************************************
2+
qgsmaptoolreshape.h
3+
---------------------
4+
begin : Juli 2009
5+
copyright : (C) 2009 by Marco Hugentobler
6+
email : marco.hugentobler at karto dot baug dot ethz dot ch
7+
***************************************************************************
8+
* *
9+
* This program is free software; you can redistribute it and/or modify *
10+
* it under the terms of the GNU General Public License as published by *
11+
* the Free Software Foundation; either version 2 of the License, or *
12+
* (at your option) any later version. *
13+
* *
14+
***************************************************************************/
15+
/* $Id$ */
16+
17+
#ifndef QGSMAPTOOLRESHAPE_H
18+
#define QGSMAPTOOLRESHAPE_H
19+
20+
#include "qgsmaptoolcapture.h"
21+
22+
/**A map tool that draws a line and splits the features cut by the line*/
23+
class QgsMapToolReshape: public QgsMapToolCapture
24+
{
25+
public:
26+
QgsMapToolReshape( QgsMapCanvas* canvas );
27+
virtual ~QgsMapToolReshape();
28+
void canvasReleaseEvent( QMouseEvent * e );
29+
};
30+
31+
#endif

0 commit comments

Comments
 (0)