/
qgsmaptooladdfeature.cpp
327 lines (287 loc) · 10.6 KB
/
qgsmaptooladdfeature.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
/***************************************************************************
qgsmaptooladdfeature.cpp
------------------------
begin : April 2007
copyright : (C) 2007 by Marco Hugentobler
email : marco dot hugentobler at karto dot baug dot ethz dot ch
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include "qgsmaptooladdfeature.h"
#include "qgsapplication.h"
#include "qgsattributedialog.h"
#include "qgscsexception.h"
#include "qgsfield.h"
#include "qgsgeometry.h"
#include "qgsmapcanvas.h"
#include "qgsmaplayerregistry.h"
#include "qgsproject.h"
#include "qgsvectordataprovider.h"
#include "qgsvectorlayer.h"
#include "qgslogger.h"
#include "qgsfeatureaction.h"
#include <QMessageBox>
#include <QMouseEvent>
#include <QSettings>
QgsMapToolAddFeature::QgsMapToolAddFeature( QgsMapCanvas* canvas ): QgsMapToolCapture( canvas )
{
}
QgsMapToolAddFeature::~QgsMapToolAddFeature()
{
}
bool QgsMapToolAddFeature::addFeature( QgsVectorLayer *vlayer, QgsFeature *f )
{
QgsFeatureAction action( tr( "add feature" ), *f, vlayer, -1, -1, this );
return action.addFeature();
}
void QgsMapToolAddFeature::activate()
{
QgsVectorLayer *vlayer = qobject_cast<QgsVectorLayer *>( mCanvas->currentLayer() );
if ( vlayer && vlayer->geometryType() == QGis::NoGeometry )
{
QgsFeature f;
addFeature( vlayer, &f );
return;
}
QgsMapTool::activate();
}
void QgsMapToolAddFeature::canvasReleaseEvent( QMouseEvent * e )
{
QgsDebugMsg( "entered." );
QgsVectorLayer *vlayer = qobject_cast<QgsVectorLayer *>( mCanvas->currentLayer() );
if ( !vlayer )
{
notifyNotVectorLayer();
return;
}
QGis::WkbType layerWKBType = vlayer->wkbType();
QgsVectorDataProvider* provider = vlayer->dataProvider();
if ( !( provider->capabilities() & QgsVectorDataProvider::AddFeatures ) )
{
QMessageBox::information( 0, tr( "Layer cannot be added to" ),
tr( "The data provider for this layer does not support the addition of features." ) );
return;
}
if ( !vlayer->isEditable() )
{
notifyNotEditableLayer();
return;
}
// POINT CAPTURING
if ( mode() == CapturePoint )
{
if ( e->button() != Qt::LeftButton )
return;
//check we only use this tool for point/multipoint layers
if ( vlayer->geometryType() != QGis::Point )
{
QMessageBox::information( 0, tr( "Wrong editing tool" ),
tr( "Cannot apply the 'capture point' tool on this vector layer" ) );
return;
}
QgsPoint idPoint; //point in map coordinates
QList<QgsSnappingResult> snapResults;
QgsPoint savePoint; //point in layer coordinates
if ( mSnapper.snapToBackgroundLayers( e->pos(), snapResults ) == 0 )
{
idPoint = snapPointFromResults( snapResults, e->pos() );
try
{
savePoint = toLayerCoordinates( vlayer, idPoint );
QgsDebugMsg( "savePoint = " + savePoint.toString() );
}
catch ( QgsCsException &cse )
{
Q_UNUSED( cse );
QMessageBox::information( 0, tr( "Coordinate transform error" ),
tr( "Cannot transform the point to the layers coordinate system" ) );
return;
}
}
//only do the rest for provider with feature addition support
//note that for the grass provider, this will return false since
//grass provider has its own mechanism of feature addition
if ( provider->capabilities() & QgsVectorDataProvider::AddFeatures )
{
QgsFeature f( vlayer->pendingFields(), 0 );
QgsGeometry *g = 0;
if ( layerWKBType == QGis::WKBPoint || layerWKBType == QGis::WKBPoint25D )
{
g = QgsGeometry::fromPoint( savePoint );
}
else if ( layerWKBType == QGis::WKBMultiPoint || layerWKBType == QGis::WKBMultiPoint25D )
{
g = QgsGeometry::fromMultiPoint( QgsMultiPoint() << savePoint );
}
f.setGeometry( g );
addFeature( vlayer, &f );
mCanvas->refresh();
}
}
// LINE AND POLYGON CAPTURING
else if ( mode() == CaptureLine || mode() == CapturePolygon )
{
//check we only use the line tool for line/multiline layers
if ( mode() == CaptureLine && vlayer->geometryType() != QGis::Line )
{
QMessageBox::information( 0, tr( "Wrong editing tool" ),
tr( "Cannot apply the 'capture line' tool on this vector layer" ) );
return;
}
//check we only use the polygon tool for polygon/multipolygon layers
if ( mode() == CapturePolygon && vlayer->geometryType() != QGis::Polygon )
{
QMessageBox::information( 0, tr( "Wrong editing tool" ),
tr( "Cannot apply the 'capture polygon' tool on this vector layer" ) );
return;
}
//add point to list and to rubber band
if ( e->button() == Qt::LeftButton )
{
int error = addVertex( e->pos() );
if ( error == 1 )
{
//current layer is not a vector layer
return;
}
else if ( error == 2 )
{
//problem with coordinate transformation
QMessageBox::information( 0, tr( "Coordinate transform error" ),
tr( "Cannot transform the point to the layers coordinate system" ) );
return;
}
startCapturing();
}
else if ( e->button() == Qt::RightButton )
{
// End of string
deleteTempRubberBand();
//lines: bail out if there are not at least two vertices
if ( mode() == CaptureLine && size() < 2 )
{
stopCapturing();
return;
}
//polygons: bail out if there are not at least two vertices
if ( mode() == CapturePolygon && size() < 3 )
{
stopCapturing();
return;
}
//create QgsFeature with wkb representation
QgsFeature* f = new QgsFeature( vlayer->pendingFields(), 0 );
QgsGeometry *g;
if ( mode() == CaptureLine )
{
if ( layerWKBType == QGis::WKBLineString || layerWKBType == QGis::WKBLineString25D )
{
g = QgsGeometry::fromPolyline( points().toVector() );
}
else if ( layerWKBType == QGis::WKBMultiLineString || layerWKBType == QGis::WKBMultiLineString25D )
{
g = QgsGeometry::fromMultiPolyline( QgsMultiPolyline() << points().toVector() );
}
else
{
QMessageBox::critical( 0, tr( "Error" ), tr( "Cannot add feature. Unknown WKB type" ) );
stopCapturing();
return; //unknown wkbtype
}
f->setGeometry( g );
}
else // polygon
{
if ( layerWKBType == QGis::WKBPolygon || layerWKBType == QGis::WKBPolygon25D )
{
g = QgsGeometry::fromPolygon( QgsPolygon() << points().toVector() );
}
else if ( layerWKBType == QGis::WKBMultiPolygon || layerWKBType == QGis::WKBMultiPolygon25D )
{
g = QgsGeometry::fromMultiPolygon( QgsMultiPolygon() << ( QgsPolygon() << points().toVector() ) );
}
else
{
QMessageBox::critical( 0, tr( "Error" ), tr( "Cannot add feature. Unknown WKB type" ) );
stopCapturing();
return; //unknown wkbtype
}
if ( !g )
{
stopCapturing();
delete f;
return; // invalid geometry; one possibility is from duplicate points
}
f->setGeometry( g );
int avoidIntersectionsReturn = f->geometry()->avoidIntersections();
if ( avoidIntersectionsReturn == 1 )
{
//not a polygon type. Impossible to get there
}
#if 0
else if ( avoidIntersectionsReturn == 2 ) //MH120131: disable this error message until there is a better way to cope with the single type / multi type problem
{
//bail out...
QMessageBox::critical( 0, tr( "Error" ), tr( "The feature could not be added because removing the polygon intersections would change the geometry type" ) );
delete f;
stopCapturing();
return;
}
#endif
else if ( avoidIntersectionsReturn == 3 )
{
QMessageBox::critical( 0, tr( "Error" ), tr( "An error was reported during intersection removal" ) );
}
if ( !f->geometry()->asWkb() ) //avoid intersection might have removed the whole geometry
{
QString reason;
if ( avoidIntersectionsReturn != 2 )
{
reason = tr( "The feature cannot be added because it's geometry is empty" );
}
else
{
reason = tr( "The feature cannot be added because it's geometry collapsed due to intersection avoidance" );
}
QMessageBox::critical( 0, tr( "Error" ), reason );
delete f;
stopCapturing();
return;
}
}
if ( addFeature( vlayer, f ) )
{
//add points to other features to keep topology up-to-date
int topologicalEditing = QgsProject::instance()->readNumEntry( "Digitizing", "/TopologicalEditing", 0 );
//use always topological editing for avoidIntersection.
//Otherwise, no way to guarantee the geometries don't have a small gap in between.
QStringList intersectionLayers = QgsProject::instance()->readListEntry( "Digitizing", "/AvoidIntersectionsList" );
bool avoidIntersection = !intersectionLayers.isEmpty();
if ( avoidIntersection ) //try to add topological points also to background layers
{
QStringList::const_iterator lIt = intersectionLayers.constBegin();
for ( ; lIt != intersectionLayers.constEnd(); ++lIt )
{
QgsMapLayer* ml = QgsMapLayerRegistry::instance()->mapLayer( *lIt );
QgsVectorLayer* vl = qobject_cast<QgsVectorLayer*>( ml );
//can only add topological points if background layer is editable...
if ( vl && vl->geometryType() == QGis::Polygon && vl->isEditable() )
{
vl->addTopologicalPoints( f->geometry() );
}
}
}
else if ( topologicalEditing )
{
vlayer->addTopologicalPoints( f->geometry() );
}
}
stopCapturing();
}
}
}