45 void QgsGeometryValidator::checkRingIntersections(
49 for (
int i = 0; !mStop && i < ring0.size() - 1; i++ )
53 for (
int j = 0; !mStop && j < ring1.size() - 1; j++ )
58 if ( intersectLines( ring0[i], v, ring1[j], w, s ) )
60 double d = -distLine2Point( ring0[i], v.
perpVector(), s );
62 if ( d >= 0 && d <= v.
length() )
64 d = -distLine2Point( ring1[j], w.
perpVector(), s );
65 if ( d > 0 && d < w.
length() )
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 )
81 void QgsGeometryValidator::validatePolyline(
int i,
QgsPolyline line,
bool ring )
85 if ( line.size() < 4 )
87 QString msg =
QObject::tr(
"ring %1 with less than four points" ).arg( i );
94 if ( line[0] != line[ line.size()-1 ] )
96 QString msg =
QObject::tr(
"ring %1 not closed" ).arg( i );
103 else if ( line.size() < 2 )
105 QString msg =
QObject::tr(
"line %1 with less than two points" ).arg( i );
113 while ( j < line.size() - 1 )
116 while ( j < line.size() - 1 && line[j] == line[j+1] )
124 QString msg =
QObject::tr(
"line %1 contains %n duplicate node(s) at %2",
"number of duplicate nodes", n ).arg( i ).arg( j );
133 for ( j = 0; !mStop && j < line.size() - 3; j++ )
138 int n = ( j == 0 && ring ) ? line.size() - 2 : line.size() - 1;
140 for (
int k = j + 2; !mStop && k < n; k++ )
145 if ( !intersectLines( line[j], v, line[k], w, s ) )
148 double d = -distLine2Point( line[j], v.
perpVector(), s );
149 if ( d < 0 || d > vl )
152 d = -distLine2Point( line[k], w.
perpVector(), s );
153 if ( d <= 0 || d >= w.
length() )
156 QString msg =
QObject::tr(
"segments %1 and %2 of line %3 intersect at %4" ).arg( j ).arg( k ).arg( i ).arg( s.
toString() );
164 void QgsGeometryValidator::validatePolygon(
int idx,
const QgsPolygon &polygon )
167 for (
int i = 1; !mStop && i < polygon.size(); i++ )
169 if ( !ringInRing( polygon[i], polygon[0] ) )
171 QString msg =
QObject::tr(
"ring %1 of polygon %2 not in exterior ring" ).arg( i ).arg( idx );
179 for (
int i = 1; !mStop && i < polygon.size(); i++ )
181 for (
int j = i + 1; !mStop && j < polygon.size(); j++ )
183 checkRingIntersections( idx, i, polygon[i], idx, j, polygon[j] );
188 for (
int i = 0; !mStop && i < polygon.size(); i++ )
190 validatePolyline( i, polygon[i],
true );
197 #if defined(GEOS_VERSION_MAJOR) && defined(GEOS_VERSION_MINOR) && \
198 ( (GEOS_VERSION_MAJOR==3 && GEOS_VERSION_MINOR>=3) || GEOS_VERSION_MAJOR>3)
200 if ( settings.value(
"/qgis/digitizing/validate_geometries", 1 ).toInt() == 2 )
203 const GEOSGeometry *g0 = mG.
asGeos();
211 GEOSGeometry *g1 = 0;
212 if ( GEOSisValidDetail_r( handle, g0, GEOSVALID_ALLOW_SELFTOUCHING_RING_FORMING_HOLE, &r, &g1 ) != 1 )
216 const GEOSCoordSequence *cs = GEOSGeom_getCoordSeq_r( handle, g1 );
219 if ( GEOSCoordSeq_getSize_r( handle, cs, &n ) && n == 1 )
222 GEOSCoordSeq_getX_r( handle, cs, 0, &x );
223 GEOSCoordSeq_getY_r( handle, cs, 0, &y );
228 GEOSGeom_destroy_r( handle, g1 );
236 GEOSFree_r( handle, r );
263 for (
int i = 0; !mStop && i < mp.size(); i++ )
264 validatePolyline( i, mp[i] );
279 for (
int i = 0; !mStop && i < mp.size(); i++ )
281 validatePolygon( i, mp[i] );
284 for (
int i = 0; !mStop && i < mp.size(); i++ )
286 if ( mp[i].isEmpty() )
293 for (
int j = i + 1; !mStop && j < mp.size(); j++ )
295 if ( mp[j].isEmpty() )
298 if ( ringInRing( mp[i][0], mp[j][0] ) )
303 else if ( ringInRing( mp[j][0], mp[i][0] ) )
310 checkRingIntersections( i, 0, mp[i][0], j, 0, mp[j][0] );
331 else if ( mErrorCount > 0 )
369 return ( v.
x()*( q.
y() - p.
y() ) - v.
y()*( q.
x() - p.
x() ) ) / v.
length();
374 double d = v.
y() * w.
x() - v.
x() * w.
y();
379 double dx = q.
x() - p.
x();
380 double dy = q.
y() - p.
y();
381 double k = ( dy * w.
x() - dx * w.
y() ) / d;
391 int j = ring.size() - 1;
393 for (
int i = 0; !mStop && i < ring.size(); i++ )
395 if ( ring[i].x() == p.
x() && ring[i].y() == p.
y() )
398 if (( ring[i].y() < p.
y() && ring[j].y() >= p.
y() ) ||
399 ( ring[j].y() < p.
y() && ring[i].y() >= p.
y() ) )
401 if ( ring[i].x() + ( p.
y() - ring[i].y() ) / ( ring[j].y() - ring[i].y() )*( ring[j].x() - ring[i].x() ) <= p.
x() )
413 for (
int i = 0; !mStop && i < inside.size(); i++ )
415 if ( !pointInRing( outside, inside[i] ) )