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() &&
66 ring0[i+1] != ring1[j+1] && ring0[i+1] != ring1[j] &&
67 ring0[i+0] != ring1[j+1] && ring0[i+0] != ring1[j] )
69 QString msg =
QObject::tr(
"segment %1 of ring %2 of polygon %3 intersects segment %4 of ring %5 of polygon %6 at %7" )
83 void QgsGeometryValidator::validatePolyline(
int i,
QgsPolyline line,
bool ring )
87 if ( line.
size() < 4 )
96 if ( line[0] != line[ line.
size()-1 ] )
105 else if ( line.
size() < 2 )
115 while ( j < line.
size() - 1 )
118 while ( j < line.
size() - 1 && line[j] == line[j+1] )
126 QString msg =
QObject::tr(
"line %1 contains %n duplicate node(s) at %2",
"number of duplicate nodes", n ).
arg( i ).
arg( j );
135 for ( j = 0; !mStop && j < line.
size() - 3; j++ )
140 int n = ( j == 0 && ring ) ? line.size() - 2 : line.size() - 1;
142 for (
int k = j + 2; !mStop && k < n; k++ )
147 if ( !intersectLines( line[j], v, line[k], w, s ) )
153 d = -distLine2Point( line[j], v.
perpVector(), s );
161 if ( d < 0 || d > vl )
166 d = -distLine2Point( line[k], w.
perpVector(), s );
175 if ( d <= 0 || d >= w.
length() )
186 void QgsGeometryValidator::validatePolygon(
int idx,
const QgsPolygon &polygon )
189 for (
int i = 1; !mStop && i < polygon.
size(); i++ )
191 if ( !ringInRing( polygon[i], polygon[0] ) )
201 for (
int i = 1; !mStop && i < polygon.
size(); i++ )
203 for (
int j = i + 1; !mStop && j < polygon.
size(); j++ )
205 checkRingIntersections( idx, i, polygon[i], idx, j, polygon[j] );
210 for (
int i = 0; !mStop && i < polygon.
size(); i++ )
212 validatePolyline( i, polygon[i],
true );
219 #if defined(GEOS_VERSION_MAJOR) && defined(GEOS_VERSION_MINOR) && \ 220 ( (GEOS_VERSION_MAJOR==3 && GEOS_VERSION_MINOR>=3) || GEOS_VERSION_MAJOR>3) 222 if ( settings.
value(
"/qgis/digitizing/validate_geometries", 1 ).
toInt() == 2 )
225 const GEOSGeometry *g0 = mG.
asGeos();
233 GEOSGeometry *g1 =
nullptr;
234 if ( GEOSisValidDetail_r( handle, g0, GEOSVALID_ALLOW_SELFTOUCHING_RING_FORMING_HOLE, &r, &g1 ) != 1 )
238 const GEOSCoordSequence *cs = GEOSGeom_getCoordSeq_r( handle, g1 );
241 if ( GEOSCoordSeq_getSize_r( handle, cs, &n ) && n == 1 )
244 GEOSCoordSeq_getX_r( handle, cs, 0, &x );
245 GEOSCoordSeq_getY_r( handle, cs, 0, &y );
250 GEOSGeom_destroy_r( handle, g1 );
258 GEOSFree_r( handle, r );
285 for (
int i = 0; !mStop && i < mp.
size(); i++ )
286 validatePolyline( i, mp[i] );
301 for (
int i = 0; !mStop && i < mp.
size(); i++ )
303 validatePolygon( i, mp[i] );
306 for (
int i = 0; !mStop && i < mp.
size(); i++ )
308 if ( mp[i].isEmpty() )
315 for (
int j = i + 1; !mStop && j < mp.
size(); j++ )
317 if ( mp[j].isEmpty() )
320 if ( ringInRing( mp[i][0], mp[j][0] ) )
325 else if ( ringInRing( mp[j][0], mp[i][0] ) )
332 checkRingIntersections( i, 0, mp[i][0], j, 0, mp[j][0] );
353 else if ( mErrorCount > 0 )
391 return ( v.
x()*( q.
y() - p.
y() ) - v.
y()*( q.
x() - p.
x() ) ) / v.
length();
396 double d = v.
y() * w.
x() - v.
x() * w.
y();
401 double dx = q.
x() - p.
x();
402 double dy = q.
y() - p.
y();
403 double k = ( dy * w.
x() - dx * w.
y() ) / d;
413 int j = ring.
size() - 1;
415 for (
int i = 0; !mStop && i < ring.
size(); i++ )
420 if (( ring[i].y() < p.
y() && ring[j].y() >= p.
y() ) ||
421 ( ring[j].y() < p.
y() && ring[i].y() >= p.
y() ) )
423 if ( ring[i].x() + ( p.
y() - ring[i].y() ) / ( ring[j].y() - ring[i].y() )*( ring[j].x() - ring[i].x() ) <= p.
x() )
435 for (
int i = 0; !mStop && i < inside.
size(); i++ )
437 if ( !pointInRing( outside, inside[i] ) )
QgsPolygon asPolygon() const
Return contents of the geometry as a polygon if wkbType is WKBPolygon, otherwise an empty list...
QgsMultiPolyline asMultiPolyline() const
Return contents of the geometry as a multi linestring if wkbType is WKBMultiLineString, otherwise an empty list.
QGis::WkbType wkbType() const
Returns type of the geometry as a WKB type (point / linestring / polygon etc.)
void addError(const QgsGeometry::Error &)
A geometry is the spatial representation of a feature.
QString tr(const char *sourceText, const char *disambiguation, int n)
bool qgsDoubleNear(double a, double b, double epsilon=4 *DBL_EPSILON)
Compare two doubles (but allow some difference)
QgsPolyline asPolyline() const
Return contents of the geometry as a polyline if wkbType is WKBLineString, otherwise an empty list...
double y() const
Get the y value of the point.
int toInt(bool *ok) const
void errorFound(const QgsGeometry::Error &)
A class to represent a point.
QString toString() const
String representation of the point (x,y)
double length() const
Returns the length of the vector.
A class to represent a vector.
static void validateGeometry(const QgsGeometry *g, QList< QgsGeometry::Error > &errors)
Validate geometry and produce a list of geometry errors.
QVariant value(const QString &key, const QVariant &defaultValue) const
QgsVector perpVector() const
Returns the perpendicular vector to this vector (rotated 90 degrees counter-clockwise) ...
static GEOSContextHandle_t getGEOSHandler()
Return GEOS context handle.
bool wait(unsigned long time)
QgsMultiPolygon asMultiPolygon() const
Return contents of the geometry as a multi polygon if wkbType is WKBMultiPolygon, otherwise an empty ...
const GEOSGeometry * asGeos(double precision=0) const
Returns a geos geometry.
double x() const
Returns the vector's x-component.
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
QString arg(qlonglong a, int fieldWidth, int base, const QChar &fillChar) const
QgsGeometryValidator(const QgsGeometry *g, QList< QgsGeometry::Error > *errors=nullptr)
Constructor.
double y() const
Returns the vector's y-component.
Defines a qgis exception class.
double x() const
Get the x value of the point.