23 : mGeometry( geometry )
42 void QgsGeometryValidator::checkRingIntersections(
46 for (
int i = 0; !mStop && i < ring0.size() - 1; i++ )
50 for (
int j = 0; !mStop && j < ring1.size() - 1; j++ )
55 if ( intersectLines( ring0[i], v, ring1[j], w, s ) )
57 double d = -distLine2Point( ring0[i], v.
perpVector(), s );
59 if ( d >= 0 && d <= v.
length() )
61 d = -distLine2Point( ring1[j], w.
perpVector(), s );
62 if ( d > 0 && d < w.
length() &&
63 ring0[i + 1] != ring1[j + 1] && ring0[i + 1] != ring1[j] &&
64 ring0[i + 0] != ring1[j + 1] && ring0[i + 0] != ring1[j] )
66 QString msg = QObject::tr(
"segment %1 of ring %2 of polygon %3 intersects segment %4 of ring %5 of polygon %6 at %7" )
67 .arg( i0 ).arg( i ).arg( p0 )
68 .arg( i1 ).arg( j ).arg( p1 )
80 void QgsGeometryValidator::validatePolyline(
int i,
QgsPolylineXY line,
bool ring )
84 if ( line.size() < 4 )
86 QString msg = QObject::tr(
"ring %1 with less than four points" ).arg( i );
93 if ( line[0] != line[ line.size() - 1 ] )
95 QString msg = QObject::tr(
"ring %1 not closed" ).arg( i );
102 else if ( line.size() < 2 )
104 QString msg = QObject::tr(
"line %1 with less than two points" ).arg( i );
112 while ( j < line.size() - 1 )
115 while ( j < line.size() - 1 && line[j] == line[j + 1] )
123 QString msg = QObject::tr(
"line %1 contains %n duplicate node(s) at %2",
"number of duplicate nodes", n ).arg( i ).arg( j );
132 for ( j = 0; !mStop && j < line.size() - 3; j++ )
137 int n = ( j == 0 && ring ) ? line.size() - 2 : line.size() - 1;
139 for (
int k = j + 2; !mStop && k < n; k++ )
144 if ( !intersectLines( line[j], v, line[k], w, s ) )
150 d = -distLine2Point( line[j], v.
perpVector(), s );
158 if ( d < 0 || d > vl )
163 d = -distLine2Point( line[k], w.
perpVector(), s );
172 if ( d <= 0 || d >= w.
length() )
175 QString msg = QObject::tr(
"segments %1 and %2 of line %3 intersect at %4" ).arg( j ).arg( k ).arg( i ).arg( s.
toString() );
183 void QgsGeometryValidator::validatePolygon(
int idx,
const QgsPolygonXY &polygon )
186 for (
int i = 1; !mStop && i < polygon.size(); i++ )
188 if ( !ringInRing( polygon[i], polygon[0] ) )
190 QString msg = QObject::tr(
"ring %1 of polygon %2 not in exterior ring" ).arg( i ).arg( idx );
198 for (
int i = 1; !mStop && i < polygon.size(); i++ )
200 for (
int j = i + 1; !mStop && j < polygon.size(); j++ )
202 checkRingIntersections( idx, i, polygon[i], idx, j, polygon[j] );
207 for (
int i = 0; !mStop && i < polygon.size(); i++ )
209 validatePolyline( i, polygon[i],
true );
229 GEOSGeometry *g1 =
nullptr;
230 char res = GEOSisValidDetail_r( handle, g0.get(), GEOSVALID_ALLOW_SELFTOUCHING_RING_FORMING_HOLE, &r, &g1 );
235 const GEOSCoordSequence *cs = GEOSGeom_getCoordSeq_r( handle, g1 );
238 if ( GEOSCoordSeq_getSize_r( handle, cs, &n ) && n == 1 )
241 GEOSCoordSeq_getX_r( handle, cs, 0, &x );
242 GEOSCoordSeq_getY_r( handle, cs, 0, &y );
247 GEOSGeom_destroy_r( handle, g1 );
255 GEOSFree_r( handle, r );
269 validatePolyline( 0, mGeometry.
asPolyline() );
274 for (
int i = 0; !mStop && i < mp.size(); i++ )
275 validatePolyline( i, mp[i] );
279 validatePolygon( 0, mGeometry.
asPolygon() );
284 for (
int i = 0; !mStop && i < mp.size(); i++ )
286 validatePolygon( i, mp[i] );
289 for (
int i = 0; !mStop && i < mp.size(); i++ )
291 if ( mp[i].isEmpty() )
298 for (
int j = i + 1; !mStop && j < mp.size(); j++ )
300 if ( mp[j].isEmpty() )
303 if ( ringInRing( mp[i][0], mp[j][0] ) )
308 else if ( ringInRing( mp[j][0], mp[i][0] ) )
315 checkRingIntersections( i, 0, mp[i][0], j, 0, mp[j][0] );
323 QgsDebugMsg( QObject::tr(
"Unknown geometry type" ) );
332 else if ( mErrorCount > 0 )
373 return ( v.
x() * ( q.
y() - p.
y() ) - v.
y() * ( q.
x() - p.
x() ) ) / v.
length();
378 double d = v.
y() * w.
x() - v.
x() * w.
y();
383 double dx = q.
x() - p.
x();
384 double dy = q.
y() - p.
y();
385 double k = ( dy * w.
x() - dx * w.
y() ) / d;
395 int j = ring.size() - 1;
397 for (
int i = 0; !mStop && i < ring.size(); i++ )
402 if ( ( ring[i].y() < p.
y() && ring[j].y() >= p.
y() ) ||
403 ( ring[j].y() < p.
y() && ring[i].y() >= p.
y() ) )
405 if ( ring[i].x() + ( p.
y() - ring[i].y() ) / ( ring[j].y() - ring[i].y() ) * ( ring[j].x() - ring[i].x() ) <= p.
x() )
417 for (
int i = 0; !mStop && i < inside.size(); i++ )
419 if ( !pointInRing( outside, inside[i] ) )
static void validateGeometry(const QgsGeometry &geometry, QVector< QgsGeometry::Error > &errors, QgsGeometry::ValidationMethod method=QgsGeometry::ValidatorQgisInternal)
Validate geometry and produce a list of geometry errors.
Use GEOS validation methods.
void addError(const QgsGeometry::Error &)
QgsWkbTypes::Type wkbType() const
Returns type of the geometry as a WKB type (point / linestring / polygon etc.)
A class to represent a 2D point.
QVector< QgsPolylineXY > QgsPolygonXY
Polygon: first item of the list is outer ring, inner rings (if any) start from second item...
QgsGeometryValidator(const QgsGeometry &geoemtry, QVector< QgsGeometry::Error > *errors=nullptr, QgsGeometry::ValidationMethod method=QgsGeometry::ValidatorQgisInternal)
Constructor for QgsGeometryValidator.
A geometry is the spatial representation of a feature.
QString toString(int precision=-1) const
Returns a string representation of the point (x, y) with a preset precision.
~QgsGeometryValidator() override
bool qgsDoubleNear(double a, double b, double epsilon=4 *DBL_EPSILON)
Compare two doubles (but allow some difference)
QVector< QgsPolygonXY > QgsMultiPolygonXY
A collection of QgsPolygons that share a common collection of attributes.
QVector< QgsPolylineXY > QgsMultiPolylineXY
A collection of QgsPolylines that share a common collection of attributes.
QgsMultiPolylineXY asMultiPolyline() const
Returns contents of the geometry as a multi linestring if wkbType is WKBMultiLineString, otherwise an empty list.
Type
The WKB type describes the number of dimensions a geometry has.
QgsPolygonXY asPolygon() const
Returns contents of the geometry as a polygon if wkbType is WKBPolygon, otherwise an empty list...
std::unique_ptr< GEOSGeometry, GeosDeleter > unique_ptr
Scoped GEOS pointer.
Use internal QgsGeometryValidator method.
void errorFound(const QgsGeometry::Error &)
double length() const
Returns the length of the vector.
A class to represent a vector.
QVector< QgsPointXY > QgsPolylineXY
Polyline as represented as a vector of two-dimensional points.
QgsVector perpVector() const
Returns the perpendicular vector to this vector (rotated 90 degrees counter-clockwise) ...
static GEOSContextHandle_t getGEOSHandler()
Return GEOS context handle.
GEOSGeometry * exportToGeos(double precision=0) const
Returns a geos geometry - caller takes ownership of the object (should be deleted with GEOSGeom_destr...
ValidationMethod
Available methods for validating geometries.
QgsPolylineXY asPolyline() const
Returns contents of the geometry as a polyline if wkbType is WKBLineString, otherwise an empty list...
double x() const
Returns the vector's x-component.
static Type flatType(Type type)
Returns the flat type for a WKB type.
QgsMultiPolygonXY asMultiPolygon() const
Returns contents of the geometry as a multi polygon if wkbType is WKBMultiPolygon, otherwise an empty list.
double y() const
Returns the vector's y-component.
Defines a QGIS exception class.