QGIS API Documentation  2.4.0-Chugiak
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
qgsgeometryvalidator.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsgeometryvalidator.cpp - geometry validation thread
3  -------------------------------------------------------------------
4 Date : 03.01.2012
5 Copyright : (C) 2012 by Juergen E. Fischer
6 email : jef at norbit dot de
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 
16 #include "qgis.h"
17 #include "qgsgeometryvalidator.h"
18 #include "qgsgeometry.h"
19 #include "qgslogger.h"
20 
21 #include <QSettings>
22 
23 QgsGeometryValidator::QgsGeometryValidator( QgsGeometry *g, QList<QgsGeometry::Error> *errors )
24  : QThread()
25  , mErrors( errors )
26  , mStop( false )
27  , mErrorCount( 0 )
28 {
29  Q_ASSERT( g );
30  if ( g )
31  mG = *g;
32 }
33 
35 {
36  stop();
37  wait();
38 }
39 
41 {
42  mStop = true;
43 }
44 
46  int p0, int i0, const QgsPolyline &ring0,
47  int p1, int i1, const QgsPolyline &ring1 )
48 {
49  for ( int i = 0; !mStop && i < ring0.size() - 1; i++ )
50  {
51  QgsVector v = ring0[i+1] - ring0[i];
52 
53  for ( int j = 0; !mStop && j < ring1.size() - 1; j++ )
54  {
55  QgsVector w = ring1[j+1] - ring1[j];
56 
57  QgsPoint s;
58  if ( intersectLines( ring0[i], v, ring1[j], w, s ) )
59  {
60  double d = -distLine2Point( ring0[i], v.perpVector(), s );
61 
62  if ( d >= 0 && d <= v.length() )
63  {
64  d = -distLine2Point( ring1[j], w.perpVector(), s );
65  if ( d > 0 && d < w.length() )
66  {
67  QString msg = QObject::tr( "segment %1 of ring %2 of polygon %3 intersects segment %4 of ring %5 of polygon %6 at %7" )
68  .arg( i0 ).arg( i ).arg( p0 )
69  .arg( i1 ).arg( j ).arg( p1 )
70  .arg( s.toString() );
71  QgsDebugMsg( msg );
72  emit errorFound( QgsGeometry::Error( msg, s ) );
73  mErrorCount++;
74  }
75  }
76  }
77  }
78  }
79 }
80 
82 {
83  if ( ring )
84  {
85  if ( line.size() < 4 )
86  {
87  QString msg = QObject::tr( "ring %1 with less than four points" ).arg( i );
88  QgsDebugMsg( msg );
89  emit errorFound( QgsGeometry::Error( msg ) );
90  mErrorCount++;
91  return;
92  }
93 
94  if ( line[0] != line[ line.size()-1 ] )
95  {
96  QString msg = QObject::tr( "ring %1 not closed" ).arg( i );
97  QgsDebugMsg( msg );
98  emit errorFound( QgsGeometry::Error( msg ) );
99  mErrorCount++;
100  return;
101  }
102  }
103  else if ( line.size() < 2 )
104  {
105  QString msg = QObject::tr( "line %1 with less than two points" ).arg( i );
106  QgsDebugMsg( msg );
107  emit errorFound( QgsGeometry::Error( msg ) );
108  mErrorCount++;
109  return;
110  }
111 
112  int j = 0;
113  while ( j < line.size() - 1 )
114  {
115  int n = 0;
116  while ( j < line.size() - 1 && line[j] == line[j+1] )
117  {
118  line.remove( j );
119  n++;
120  }
121 
122  if ( n > 0 )
123  {
124  QString msg = QObject::tr( "line %1 contains %n duplicate node(s) at %2", "number of duplicate nodes", n ).arg( i ).arg( j );
125  QgsDebugMsg( msg );
126  emit errorFound( QgsGeometry::Error( msg, line[j] ) );
127  mErrorCount++;
128  }
129 
130  j++;
131  }
132 
133  for ( j = 0; !mStop && j < line.size() - 3; j++ )
134  {
135  QgsVector v = line[j+1] - line[j];
136  double vl = v.length();
137 
138  int n = ( j == 0 && ring ) ? line.size() - 2 : line.size() - 1;
139 
140  for ( int k = j + 2; !mStop && k < n; k++ )
141  {
142  QgsVector w = line[k+1] - line[k];
143 
144  QgsPoint s;
145  if ( !intersectLines( line[j], v, line[k], w, s ) )
146  continue;
147 
148  double d = -distLine2Point( line[j], v.perpVector(), s );
149  if ( d < 0 || d > vl )
150  continue;
151 
152  d = -distLine2Point( line[k], w.perpVector(), s );
153  if ( d <= 0 || d >= w.length() )
154  continue;
155 
156  QString msg = QObject::tr( "segments %1 and %2 of line %3 intersect at %4" ).arg( j ).arg( k ).arg( i ).arg( s.toString() );
157  QgsDebugMsg( msg );
158  emit errorFound( QgsGeometry::Error( msg, s ) );
159  mErrorCount++;
160  }
161  }
162 }
163 
164 void QgsGeometryValidator::validatePolygon( int idx, const QgsPolygon &polygon )
165 {
166  // check if holes are inside polygon
167  for ( int i = 1; !mStop && i < polygon.size(); i++ )
168  {
169  if ( !ringInRing( polygon[i], polygon[0] ) )
170  {
171  QString msg = QObject::tr( "ring %1 of polygon %2 not in exterior ring" ).arg( i ).arg( idx );
172  QgsDebugMsg( msg );
173  emit errorFound( QgsGeometry::Error( msg ) );
174  mErrorCount++;
175  }
176  }
177 
178  // check holes for intersections
179  for ( int i = 1; !mStop && i < polygon.size(); i++ )
180  {
181  for ( int j = i + 1; !mStop && j < polygon.size(); j++ )
182  {
183  checkRingIntersections( idx, i, polygon[i], idx, j, polygon[j] );
184  }
185  }
186 
187  // check if rings are self-intersecting
188  for ( int i = 0; !mStop && i < polygon.size(); i++ )
189  {
190  validatePolyline( i, polygon[i], true );
191  }
192 }
193 
195 {
196  mErrorCount = 0;
197 #if defined(GEOS_VERSION_MAJOR) && defined(GEOS_VERSION_MINOR) && \
198  ( (GEOS_VERSION_MAJOR==3 && GEOS_VERSION_MINOR>=3) || GEOS_VERSION_MAJOR>3)
199  QSettings settings;
200  if ( settings.value( "/qgis/digitizing/validate_geometries", 1 ).toInt() == 2 )
201  {
202  char *r = 0;
203  const GEOSGeometry *g0 = mG.asGeos();
204  if ( !g0 )
205  {
206  emit errorFound( QgsGeometry::Error( QObject::tr( "GEOS error:could not produce geometry for GEOS (check log window)" ) ) );
207  }
208  else
209  {
210  GEOSGeometry *g1 = 0;
211  if ( GEOSisValidDetail( g0, GEOSVALID_ALLOW_SELFTOUCHING_RING_FORMING_HOLE, &r, &g1 ) != 1 )
212  {
213  if ( g1 )
214  {
215  const GEOSCoordSequence *cs = GEOSGeom_getCoordSeq( g1 );
216 
217  unsigned int n;
218  if ( GEOSCoordSeq_getSize( cs, &n ) && n == 1 )
219  {
220  double x, y;
221  GEOSCoordSeq_getX( cs, 0, &x );
222  GEOSCoordSeq_getY( cs, 0, &y );
223  emit errorFound( QgsGeometry::Error( QObject::tr( "GEOS error:%1" ).arg( r ), QgsPoint( x, y ) ) );
224  mErrorCount++;
225  }
226 
227  GEOSGeom_destroy( g1 );
228  }
229  else
230  {
231  emit errorFound( QgsGeometry::Error( QObject::tr( "GEOS error:%1" ).arg( r ) ) );
232  mErrorCount++;
233  }
234 
235  GEOSFree( r );
236  }
237  }
238 
239  return;
240  }
241 #endif
242 
243  QgsDebugMsg( "validation thread started." );
244 
245  switch ( mG.wkbType() )
246  {
247  case QGis::WKBPoint:
248  case QGis::WKBPoint25D:
249  case QGis::WKBMultiPoint:
251  break;
252 
253  case QGis::WKBLineString:
256  break;
257 
260  {
262  for ( int i = 0; !mStop && i < mp.size(); i++ )
263  validatePolyline( i, mp[i] );
264  }
265  break;
266 
267  case QGis::WKBPolygon:
268  case QGis::WKBPolygon25D:
269  {
270  validatePolygon( 0, mG.asPolygon() );
271  }
272  break;
273 
276  {
278  for ( int i = 0; !mStop && i < mp.size(); i++ )
279  {
280  validatePolygon( i, mp[i] );
281  }
282 
283  for ( int i = 0; !mStop && i < mp.size(); i++ )
284  {
285  if ( mp[i].isEmpty() )
286  {
287  emit errorFound( QgsGeometry::Error( QObject::tr( "polygon %1 has no rings" ).arg( i ) ) );
288  mErrorCount++;
289  continue;
290  }
291 
292  for ( int j = i + 1; !mStop && j < mp.size(); j++ )
293  {
294  if ( mp[j].isEmpty() )
295  continue;
296 
297  if ( ringInRing( mp[i][0], mp[j][0] ) )
298  {
299  emit errorFound( QgsGeometry::Error( QObject::tr( "polygon %1 inside polygon %2" ).arg( i ).arg( j ) ) );
300  mErrorCount++;
301  }
302  else if ( ringInRing( mp[j][0], mp[i][0] ) )
303  {
304  emit errorFound( QgsGeometry::Error( QObject::tr( "polygon %1 inside polygon %2" ).arg( j ).arg( i ) ) );
305  mErrorCount++;
306  }
307  else
308  {
309  checkRingIntersections( i, 0, mp[i][0], j, 0, mp[j][0] );
310  }
311  }
312  }
313  }
314  break;
315 
316  case QGis::WKBNoGeometry:
317  case QGis::WKBUnknown:
318  QgsDebugMsg( QObject::tr( "Unknown geometry type" ) );
319  emit errorFound( QgsGeometry::Error( QObject::tr( "Unknown geometry type %1" ).arg( mG.wkbType() ) ) );
320  mErrorCount++;
321  break;
322  }
323 
324  QgsDebugMsg( "validation finished." );
325 
326  if ( mStop )
327  {
328  emit errorFound( QObject::tr( "Geometry validation was aborted." ) );
329  }
330  else if ( mErrorCount > 0 )
331  {
332  emit errorFound( QObject::tr( "Geometry has %1 errors." ).arg( mErrorCount ) );
333  }
334 #if 0
335  else
336  {
337  emit errorFound( QObject::tr( "Geometry is valid." ) );
338  }
339 #endif
340 }
341 
343 {
344  if ( mErrors )
345  *mErrors << e;
346 }
347 
348 void QgsGeometryValidator::validateGeometry( QgsGeometry *g, QList<QgsGeometry::Error> &errors )
349 {
350  QgsGeometryValidator *gv = new QgsGeometryValidator( g, &errors );
351  connect( gv, SIGNAL( errorFound( QgsGeometry::Error ) ), gv, SLOT( addError( QgsGeometry::Error ) ) );
352  gv->run();
353  gv->wait();
354 }
355 
356 //
357 // distance of point q from line through p in direction v
358 // return >0 => q lies left of the line
359 // <0 => q lies right of the line
360 //
362 {
363  if ( v.length() == 0 )
364  {
365  throw QgsException( QObject::tr( "invalid line" ) );
366  }
367 
368  return ( v.x()*( q.y() - p.y() ) - v.y()*( q.x() - p.x() ) ) / v.length();
369 }
370 
372 {
373  double d = v.y() * w.x() - v.x() * w.y();
374 
375  if ( d == 0 )
376  return false;
377 
378  double dx = q.x() - p.x();
379  double dy = q.y() - p.y();
380  double k = ( dy * w.x() - dx * w.y() ) / d;
381 
382  s = p + v * k;
383 
384  return true;
385 }
386 
388 {
389  bool inside = false;
390  int j = ring.size() - 1;
391 
392  for ( int i = 0; !mStop && i < ring.size(); i++ )
393  {
394  if ( ring[i].x() == p.x() && ring[i].y() == p.y() )
395  return true;
396 
397  if (( ring[i].y() < p.y() && ring[j].y() >= p.y() ) ||
398  ( ring[j].y() < p.y() && ring[i].y() >= p.y() ) )
399  {
400  if ( ring[i].x() + ( p.y() - ring[i].y() ) / ( ring[j].y() - ring[i].y() )*( ring[j].x() - ring[i].x() ) <= p.x() )
401  inside = !inside;
402  }
403 
404  j = i;
405  }
406 
407  return inside;
408 }
409 
410 bool QgsGeometryValidator::ringInRing( const QgsPolyline &inside, const QgsPolyline &outside )
411 {
412  for ( int i = 0; !mStop && i < inside.size(); i++ )
413  {
414  if ( !pointInRing( outside, inside[i] ) )
415  return false;
416  }
417 
418  return true;
419 }
static void validateGeometry(QgsGeometry *g, QList< QgsGeometry::Error > &errors)
Validate geometry and produce a list of geometry errors.
void validatePolygon(int i, const QgsPolygon &polygon)
void addError(QgsGeometry::Error)
#define QgsDebugMsg(str)
Definition: qgslogger.h:36
QgsMultiPolyline asMultiPolyline() const
return contents of the geometry as a multi linestring if wkbType is WKBMultiLineString, otherwise an empty list
QVector< QgsPoint > QgsPolyline
polyline is represented as a vector of points
Definition: qgsgeometry.h:38
void validatePolyline(int i, QgsPolyline polyline, bool ring=false)
QgsPolygon asPolygon() const
return contents of the geometry as a polygon if wkbType is WKBPolygon, otherwise an empty list ...
double y() const
Definition: qgspoint.cpp:69
double distLine2Point(QgsPoint p, QgsVector v, QgsPoint q)
bool ringInRing(const QgsPolyline &inside, const QgsPolyline &outside)
QList< QgsGeometry::Error > * mErrors
double x() const
Definition: qgspoint.h:110
const GEOSGeometry * asGeos() const
Returns a geos geomtry.
QgsMultiPolygon asMultiPolygon() const
return contents of the geometry as a multi polygon if wkbType is WKBMultiPolygon, otherwise an empty ...
QgsVector perpVector() const
Definition: qgspoint.cpp:75
QVector< QgsPolygon > QgsMultiPolygon
a collection of QgsPolygons that share a common collection of attributes
Definition: qgsgeometry.h:53
QString toString() const
String representation of the point (x,y)
Definition: qgspoint.cpp:121
QgsGeometryValidator(QgsGeometry *g, QList< QgsGeometry::Error > *errors=0)
Constructor.
QGis::WkbType wkbType() const
Returns type of wkb (point / linestring / polygon etc.)
QVector< QgsPolyline > QgsPolygon
polygon: first item of the list is outer ring, inner rings (if any) start from second item ...
Definition: qgsgeometry.h:44
bool pointInRing(const QgsPolyline &ring, const QgsPoint &p)
A class to represent a point geometry.
Definition: qgspoint.h:63
A class to represent a vector.
Definition: qgspoint.h:32
double length() const
Definition: qgspoint.cpp:59
QVector< QgsPolyline > QgsMultiPolyline
a collection of QgsPolylines that share a common collection of attributes
Definition: qgsgeometry.h:50
QgsPolyline asPolyline() const
return contents of the geometry as a polyline if wkbType is WKBLineString, otherwise an empty list ...
void checkRingIntersections(int p0, int i0, const QgsPolyline &ring0, int p1, int i1, const QgsPolyline &ring1)
void errorFound(QgsGeometry::Error)
double y() const
Definition: qgspoint.h:118
double x() const
Definition: qgspoint.cpp:64
Defines a qgis exception class.
Definition: qgsexception.h:25
bool intersectLines(QgsPoint p, QgsVector v, QgsPoint q, QgsVector w, QgsPoint &s)
#define tr(sourceText)