Skip to content

Commit f1b1598

Browse files
author
jef
committedJan 24, 2010
[FEATURE] add validation to capture tool
git-svn-id: http://svn.osgeo.org/qgis/trunk/qgis@12830 c8812cc2-4d05-0410-92ff-de0c093fc19c

11 files changed

+339
-219
lines changed
 

‎python/core/qgsgeometry.sip

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,16 @@ class QgsGeometry
9090
/** Returns true if wkb of the geometry is of WKBMulti* type */
9191
bool isMultipart();
9292

93+
/** compare geometries using GEOS
94+
@note added in 1.5
95+
*/
96+
bool isGeosEqual( QgsGeometry & );
97+
98+
/** check validity using GEOS
99+
@note added in 1.5
100+
*/
101+
bool isGeosValid();
102+
93103
/**
94104
Set the geometry, feeding in a geometry in GEOS format.
95105
*/

‎src/app/qgsmaptooladdfeature.cpp

Lines changed: 47 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,7 @@ void QgsMapToolAddFeature::canvasReleaseEvent( QMouseEvent * e )
5959
layerWKBType == QGis::WKBMultiLineString25D || layerWKBType == QGis::WKBPoint25D || layerWKBType == QGis::WKBMultiPoint25D )
6060
{
6161
QMessageBox::critical( 0, tr( "2.5D shape type not supported" ), tr( "Adding features to 2.5D shapetypes is not supported yet" ) );
62-
delete mRubberBand;
63-
mRubberBand = NULL;
64-
mCapturing = FALSE;
65-
mCaptureList.clear();
66-
mCanvas->refresh();
62+
stopCapturing();
6763
return;
6864
}
6965

@@ -85,7 +81,7 @@ void QgsMapToolAddFeature::canvasReleaseEvent( QMouseEvent * e )
8581
}
8682

8783
// POINT CAPTURING
88-
if ( mCaptureMode == CapturePoint )
84+
if ( mode() == CapturePoint )
8985
{
9086
//check we only use this tool for point/multipoint layers
9187
if ( vlayer->geometryType() != QGis::Point )
@@ -129,7 +125,7 @@ void QgsMapToolAddFeature::canvasReleaseEvent( QMouseEvent * e )
129125
QgsFeature* f = new QgsFeature( 0, "WKBPoint" );
130126

131127
int size = 0;
132-
char end = QgsApplication::endian();
128+
char endian = QgsApplication::endian();
133129
unsigned char *wkb = NULL;
134130
int wkbtype = 0;
135131
double x = savePoint.x();
@@ -140,7 +136,7 @@ void QgsMapToolAddFeature::canvasReleaseEvent( QMouseEvent * e )
140136
size = 1 + sizeof( int ) + 2 * sizeof( double );
141137
wkb = new unsigned char[size];
142138
wkbtype = QGis::WKBPoint;
143-
memcpy( &wkb[0], &end, 1 );
139+
memcpy( &wkb[0], &endian, 1 );
144140
memcpy( &wkb[1], &wkbtype, sizeof( int ) );
145141
memcpy( &wkb[5], &x, sizeof( double ) );
146142
memcpy( &wkb[5] + sizeof( double ), &y, sizeof( double ) );
@@ -151,14 +147,14 @@ void QgsMapToolAddFeature::canvasReleaseEvent( QMouseEvent * e )
151147
wkb = new unsigned char[size];
152148
wkbtype = QGis::WKBMultiPoint;
153149
int position = 0;
154-
memcpy( &wkb[position], &end, 1 );
150+
memcpy( &wkb[position], &endian, 1 );
155151
position += 1;
156152
memcpy( &wkb[position], &wkbtype, sizeof( int ) );
157153
position += sizeof( int );
158154
int npoint = 1;
159155
memcpy( &wkb[position], &npoint, sizeof( int ) );
160156
position += sizeof( int );
161-
memcpy( &wkb[position], &end, 1 );
157+
memcpy( &wkb[position], &endian, 1 );
162158
position += 1;
163159
int pointtype = QGis::WKBPoint;
164160
memcpy( &wkb[position], &pointtype, sizeof( int ) );
@@ -209,18 +205,18 @@ void QgsMapToolAddFeature::canvasReleaseEvent( QMouseEvent * e )
209205
}
210206

211207
}
212-
else if ( mCaptureMode == CaptureLine || mCaptureMode == CapturePolygon )
208+
else if ( mode() == CaptureLine || mode() == CapturePolygon )
213209
{
214210
//check we only use the line tool for line/multiline layers
215-
if ( mCaptureMode == CaptureLine && vlayer->geometryType() != QGis::Line )
211+
if ( mode() == CaptureLine && vlayer->geometryType() != QGis::Line )
216212
{
217213
QMessageBox::information( 0, tr( "Wrong editing tool" ),
218214
tr( "Cannot apply the 'capture line' tool on this vector layer" ) );
219215
return;
220216
}
221217

222218
//check we only use the polygon tool for polygon/multipolygon layers
223-
if ( mCaptureMode == CapturePolygon && vlayer->geometryType() != QGis::Polygon )
219+
if ( mode() == CapturePolygon && vlayer->geometryType() != QGis::Polygon )
224220
{
225221
QMessageBox::information( 0, tr( "Wrong editing tool" ),
226222
tr( "Cannot apply the 'capture polygon' tool on this vector layer" ) );
@@ -244,52 +240,46 @@ void QgsMapToolAddFeature::canvasReleaseEvent( QMouseEvent * e )
244240

245241
if ( e->button() == Qt::LeftButton )
246242
{
247-
mCapturing = TRUE;
243+
startCapturing();
248244
}
249245
else if ( e->button() == Qt::RightButton )
250246
{
251247
// End of string
252248

253-
mCapturing = FALSE;
254-
255249
//lines: bail out if there are not at least two vertices
256-
if ( mCaptureMode == CaptureLine && mCaptureList.size() < 2 )
250+
if ( mode() == CaptureLine && size() < 2 )
257251
{
258-
delete mRubberBand;
259-
mRubberBand = NULL;
260-
mCaptureList.clear();
252+
stopCapturing();
261253
return;
262254
}
263255

264256
//polygons: bail out if there are not at least two vertices
265-
if ( mCaptureMode == CapturePolygon && mCaptureList.size() < 3 )
257+
if ( mode() == CapturePolygon && size() < 3 )
266258
{
267-
delete mRubberBand;
268-
mRubberBand = NULL;
269-
mCaptureList.clear();
259+
stopCapturing();
270260
return;
271261
}
272262

273263
//create QgsFeature with wkb representation
274264
QgsFeature* f = new QgsFeature( 0, "WKBLineString" );
275265
unsigned char* wkb;
276-
int size;
277-
char end = QgsApplication::endian();
266+
int wkbsize;
267+
char endian = QgsApplication::endian();
278268

279-
if ( mCaptureMode == CaptureLine )
269+
if ( mode() == CaptureLine )
280270
{
281271
if ( layerWKBType == QGis::WKBLineString )
282272
{
283-
size = 1 + 2 * sizeof( int ) + 2 * mCaptureList.size() * sizeof( double );
284-
wkb = new unsigned char[size];
273+
wkbsize = 1 + 2 * sizeof( int ) + 2 * size() * sizeof( double );
274+
wkb = new unsigned char[wkbsize];
285275
int wkbtype = QGis::WKBLineString;
286-
int length = mCaptureList.size();
287-
memcpy( &wkb[0], &end, 1 );
276+
int length = size();
277+
memcpy( &wkb[0], &endian, 1 );
288278
memcpy( &wkb[1], &wkbtype, sizeof( int ) );
289279
memcpy( &wkb[1+sizeof( int )], &length, sizeof( int ) );
290280
int position = 1 + 2 * sizeof( int );
291281
double x, y;
292-
for ( QList<QgsPoint>::iterator it = mCaptureList.begin(); it != mCaptureList.end(); ++it )
282+
for ( QList<QgsPoint>::iterator it = begin(); it != end(); ++it )
293283
{
294284
QgsPoint savePoint = *it;
295285
x = savePoint.x();
@@ -304,27 +294,27 @@ void QgsMapToolAddFeature::canvasReleaseEvent( QMouseEvent * e )
304294
}
305295
else if ( layerWKBType == QGis::WKBMultiLineString )
306296
{
307-
size = 1 + 2 * sizeof( int ) + 1 + 2 * sizeof( int ) + 2 * mCaptureList.size() * sizeof( double );
308-
wkb = new unsigned char[size];
297+
wkbsize = 1 + 2 * sizeof( int ) + 1 + 2 * sizeof( int ) + 2 * size() * sizeof( double );
298+
wkb = new unsigned char[wkbsize];
309299
int position = 0;
310300
int wkbtype = QGis::WKBMultiLineString;
311-
memcpy( &wkb[position], &end, 1 );
301+
memcpy( &wkb[position], &endian, 1 );
312302
position += 1;
313303
memcpy( &wkb[position], &wkbtype, sizeof( int ) );
314304
position += sizeof( int );
315305
int nlines = 1;
316306
memcpy( &wkb[position], &nlines, sizeof( int ) );
317307
position += sizeof( int );
318-
memcpy( &wkb[position], &end, 1 );
308+
memcpy( &wkb[position], &endian, 1 );
319309
position += 1;
320310
int linewkbtype = QGis::WKBLineString;
321311
memcpy( &wkb[position], &linewkbtype, sizeof( int ) );
322312
position += sizeof( int );
323-
int length = mCaptureList.size();
313+
int length = size();
324314
memcpy( &wkb[position], &length, sizeof( int ) );
325315
position += sizeof( int );
326316
double x, y;
327-
for ( QList<QgsPoint>::iterator it = mCaptureList.begin(); it != mCaptureList.end(); ++it )
317+
for ( QList<QgsPoint>::iterator it = begin(); it != end(); ++it )
328318
{
329319
QgsPoint savePoint = *it;
330320
x = savePoint.x();
@@ -340,30 +330,28 @@ void QgsMapToolAddFeature::canvasReleaseEvent( QMouseEvent * e )
340330
else
341331
{
342332
QMessageBox::critical( 0, tr( "Error" ), tr( "Cannot add feature. Unknown WKB type" ) );
343-
delete mRubberBand;
344-
mRubberBand = NULL;
345-
mCaptureList.clear();
333+
stopCapturing();
346334
return; //unknown wkbtype
347335
}
348-
f->setGeometryAndOwnership( &wkb[0], size );
336+
f->setGeometryAndOwnership( &wkb[0], wkbsize );
349337
}
350338
else // polygon
351339
{
352340
if ( layerWKBType == QGis::WKBPolygon )
353341
{
354-
size = 1 + 3 * sizeof( int ) + 2 * ( mCaptureList.size() + 1 ) * sizeof( double );
355-
wkb = new unsigned char[size];
342+
wkbsize = 1 + 3 * sizeof( int ) + 2 * ( size() + 1 ) * sizeof( double );
343+
wkb = new unsigned char[wkbsize];
356344
int wkbtype = QGis::WKBPolygon;
357-
int length = mCaptureList.size() + 1;//+1 because the first point is needed twice
345+
int length = size() + 1;//+1 because the first point is needed twice
358346
int numrings = 1;
359-
memcpy( &wkb[0], &end, 1 );
347+
memcpy( &wkb[0], &endian, 1 );
360348
memcpy( &wkb[1], &wkbtype, sizeof( int ) );
361349
memcpy( &wkb[1+sizeof( int )], &numrings, sizeof( int ) );
362350
memcpy( &wkb[1+2*sizeof( int )], &length, sizeof( int ) );
363351
int position = 1 + 3 * sizeof( int );
364352
double x, y;
365353
QList<QgsPoint>::iterator it;
366-
for ( it = mCaptureList.begin(); it != mCaptureList.end(); ++it )
354+
for ( it = begin(); it != end(); ++it )
367355
{
368356
QgsPoint savePoint = *it;
369357
x = savePoint.x();
@@ -376,7 +364,7 @@ void QgsMapToolAddFeature::canvasReleaseEvent( QMouseEvent * e )
376364
position += sizeof( double );
377365
}
378366
// close the polygon
379-
it = mCaptureList.begin();
367+
it = begin();
380368
QgsPoint savePoint = *it;
381369
x = savePoint.x();
382370
y = savePoint.y();
@@ -388,21 +376,21 @@ void QgsMapToolAddFeature::canvasReleaseEvent( QMouseEvent * e )
388376
}
389377
else if ( layerWKBType == QGis::WKBMultiPolygon )
390378
{
391-
size = 2 + 5 * sizeof( int ) + 2 * ( mCaptureList.size() + 1 ) * sizeof( double );
392-
wkb = new unsigned char[size];
379+
wkbsize = 2 + 5 * sizeof( int ) + 2 * ( size() + 1 ) * sizeof( double );
380+
wkb = new unsigned char[wkbsize];
393381
int wkbtype = QGis::WKBMultiPolygon;
394382
int polygontype = QGis::WKBPolygon;
395-
int length = mCaptureList.size() + 1;//+1 because the first point is needed twice
383+
int length = size() + 1;//+1 because the first point is needed twice
396384
int numrings = 1;
397385
int numpolygons = 1;
398386
int position = 0; //pointer position relative to &wkb[0]
399-
memcpy( &wkb[position], &end, 1 );
387+
memcpy( &wkb[position], &endian, 1 );
400388
position += 1;
401389
memcpy( &wkb[position], &wkbtype, sizeof( int ) );
402390
position += sizeof( int );
403391
memcpy( &wkb[position], &numpolygons, sizeof( int ) );
404392
position += sizeof( int );
405-
memcpy( &wkb[position], &end, 1 );
393+
memcpy( &wkb[position], &endian, 1 );
406394
position += 1;
407395
memcpy( &wkb[position], &polygontype, sizeof( int ) );
408396
position += sizeof( int );
@@ -412,7 +400,7 @@ void QgsMapToolAddFeature::canvasReleaseEvent( QMouseEvent * e )
412400
position += sizeof( int );
413401
double x, y;
414402
QList<QgsPoint>::iterator it;
415-
for ( it = mCaptureList.begin(); it != mCaptureList.end(); ++it )//add the captured points to the polygon
403+
for ( it = begin(); it != end(); ++it )//add the captured points to the polygon
416404
{
417405
QgsPoint savePoint = *it;
418406
x = savePoint.x();
@@ -425,7 +413,7 @@ void QgsMapToolAddFeature::canvasReleaseEvent( QMouseEvent * e )
425413
position += sizeof( double );
426414
}
427415
// close the polygon
428-
it = mCaptureList.begin();
416+
it = begin();
429417
QgsPoint savePoint = *it;
430418
x = savePoint.x();
431419
y = savePoint.y();
@@ -436,12 +424,10 @@ void QgsMapToolAddFeature::canvasReleaseEvent( QMouseEvent * e )
436424
else
437425
{
438426
QMessageBox::critical( 0, tr( "Error" ), tr( "Cannot add feature. Unknown WKB type" ) );
439-
delete mRubberBand;
440-
mRubberBand = NULL;
441-
mCaptureList.clear();
427+
stopCapturing();
442428
return; //unknown wkbtype
443429
}
444-
f->setGeometryAndOwnership( &wkb[0], size );
430+
f->setGeometryAndOwnership( &wkb[0], wkbsize );
445431

446432
int avoidIntersectionsReturn = f->geometry()->avoidIntersections();
447433
if ( avoidIntersectionsReturn == 1 )
@@ -453,9 +439,7 @@ void QgsMapToolAddFeature::canvasReleaseEvent( QMouseEvent * e )
453439
//bail out...
454440
QMessageBox::critical( 0, tr( "Error" ), tr( "The feature could not be added because removing the polygon intersections would change the geometry type" ) );
455441
delete f;
456-
delete mRubberBand;
457-
mRubberBand = 0;
458-
mCaptureList.clear();
442+
stopCapturing();
459443
return;
460444
}
461445
else if ( avoidIntersectionsReturn == 3 )
@@ -510,12 +494,7 @@ void QgsMapToolAddFeature::canvasReleaseEvent( QMouseEvent * e )
510494
}
511495
delete f;
512496

513-
delete mRubberBand;
514-
mRubberBand = NULL;
515-
516-
// delete the elements of mCaptureList
517-
mCaptureList.clear();
518-
mCanvas->refresh();
497+
stopCapturing();
519498
}
520499
}
521500
}

‎src/app/qgsmaptooladdisland.cpp

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,7 @@ void QgsMapToolAddIsland::canvasReleaseEvent( QMouseEvent * e )
7070
if ( !selectionErrorMsg.isEmpty() )
7171
{
7272
QMessageBox::critical( 0, tr( "Error, could not add island" ), selectionErrorMsg );
73-
mCaptureList.clear();
74-
delete mRubberBand;
75-
mRubberBand = 0;
73+
stopCapturing();
7674
return;
7775
}
7876

@@ -94,18 +92,15 @@ void QgsMapToolAddIsland::canvasReleaseEvent( QMouseEvent * e )
9492

9593
if ( e->button() == Qt::LeftButton )
9694
{
97-
mCapturing = TRUE;
95+
startCapturing();
9896
}
9997
else if ( e->button() == Qt::RightButton )
10098
{
101-
mCapturing = FALSE;
102-
delete mRubberBand;
103-
mRubberBand = 0;
104-
10599
//close polygon
106-
mCaptureList.push_back( *mCaptureList.begin() );
100+
closePolygon();
101+
107102
vlayer->beginEditCommand( tr( "Part added" ) );
108-
int errorCode = vlayer->addIsland( mCaptureList );
103+
int errorCode = vlayer->addIsland( points() );
109104
QString errorMessage;
110105

111106
if ( errorCode != 0 )
@@ -144,13 +139,12 @@ void QgsMapToolAddIsland::canvasReleaseEvent( QMouseEvent * e )
144139
int topologicalEditing = QgsProject::instance()->readNumEntry( "Digitizing", "/TopologicalEditing", 0 );
145140
if ( topologicalEditing )
146141
{
147-
addTopologicalPoints( mCaptureList );
142+
addTopologicalPoints( points() );
148143
}
149144

150145
vlayer->endEditCommand();
151146
}
152147

153-
mCaptureList.clear();
154-
mCanvas->refresh();
148+
stopCapturing();
155149
}
156150
}

0 commit comments

Comments
 (0)
Please sign in to comment.