Skip to content
Permalink
Browse files

More unit tests for geometry validation

  • Loading branch information
nyalldawson committed Nov 4, 2020
1 parent 2f10e50 commit c7ab2d207c9e9e8d52757b49430693d08577cc3a
Showing with 158 additions and 5 deletions.
  1. +4 −5 src/core/qgsgeometryvalidator.cpp
  2. +154 −0 tests/src/python/test_qgsgeometryvalidator.py
@@ -68,11 +68,10 @@ void QgsGeometryValidator::checkRingIntersections( int partIndex0, int ringIndex
ring0->pointN( i + 1 ) != ring1->pointN( j + 1 ) && ring0->pointN( i + 1 ) != ring1->pointN( j ) &&
ring0->pointN( i + 0 ) != ring1->pointN( j + 1 ) && ring0->pointN( i + 0 ) != ring1->pointN( j ) )
{
QString msg = QObject::tr( "segment %1 of ring %2 of polygon %3 intersects segment %4 of ring %5 of polygon %6 at %7, %8" )
.arg( i ).arg( ringIndex0 ).arg( partIndex0 )
.arg( j ).arg( ringIndex1 ).arg( partIndex1 )
.arg( sX ).arg( sY );
QgsDebugMsg( msg );
const QString msg = QObject::tr( "segment %1 of ring %2 of polygon %3 intersects segment %4 of ring %5 of polygon %6 at %7, %8" )
.arg( i ).arg( ringIndex0 ).arg( partIndex0 )
.arg( j ).arg( ringIndex1 ).arg( partIndex1 )
.arg( sX ).arg( sY );
emit errorFound( QgsGeometry::Error( msg, QgsPointXY( sX, sY ) ) );
mErrorCount++;
}
@@ -108,6 +108,160 @@ def test_ring_intersections(self):
self.assertEqual(spy[1][0].where(), QgsPointXY(2, 7))
self.assertEqual(spy[1][0].what(), 'segment 1 of ring 1 of polygon 0 intersects segment 2 of ring 2 of polygon 0 at 2, 7')

def test_line_vertices(self):
# valid line
g = QgsGeometry.fromWkt("LineString (0 0, 10 0)")

validator = QgsGeometryValidator(g)
spy = QSignalSpy(validator.errorFound)
validator.run()
self.assertEqual(len(spy), 0)

# not enough vertices
g = QgsGeometry.fromWkt("LineString (1 0)")

validator = QgsGeometryValidator(g)
spy = QSignalSpy(validator.errorFound)
validator.run()
self.assertEqual(len(spy), 1)

self.assertEqual(spy[0][0].where(), QgsPointXY())
self.assertEqual(spy[0][0].what(), 'line 0 with less than two points')

g = QgsGeometry.fromWkt("LineString ()")

validator = QgsGeometryValidator(g)
spy = QSignalSpy(validator.errorFound)
validator.run()
self.assertEqual(len(spy), 1)

self.assertEqual(spy[0][0].where(), QgsPointXY())
self.assertEqual(spy[0][0].what(), 'line 0 with less than two points')

def test_ring_vertex_count(self):
# valid ring
g = QgsGeometry.fromWkt("Polygon ((0 0, 10 0, 10 10, 0 0))")

validator = QgsGeometryValidator(g)
spy = QSignalSpy(validator.errorFound)
validator.run()
self.assertEqual(len(spy), 0)

# not enough vertices
g = QgsGeometry.fromWkt("Polygon ((0 0, 10 0, 10 10))")

validator = QgsGeometryValidator(g)
spy = QSignalSpy(validator.errorFound)
validator.run()
self.assertEqual(len(spy), 1)

self.assertEqual(spy[0][0].where(), QgsPointXY())
self.assertEqual(spy[0][0].what(), 'ring 0 with less than four points')

g = QgsGeometry.fromWkt("Polygon ((0 0, 10 0, 0 0))")

validator = QgsGeometryValidator(g)
spy = QSignalSpy(validator.errorFound)
validator.run()
self.assertEqual(len(spy), 1)

self.assertEqual(spy[0][0].where(), QgsPointXY())
self.assertEqual(spy[0][0].what(), 'ring 0 with less than four points')

g = QgsGeometry.fromWkt("Polygon ((0 0, 10 0))")

validator = QgsGeometryValidator(g)
spy = QSignalSpy(validator.errorFound)
validator.run()
self.assertEqual(len(spy), 1)

self.assertEqual(spy[0][0].where(), QgsPointXY())
self.assertEqual(spy[0][0].what(), 'ring 0 with less than four points')

g = QgsGeometry.fromWkt("Polygon ((0 0))")

validator = QgsGeometryValidator(g)
spy = QSignalSpy(validator.errorFound)
validator.run()
self.assertEqual(len(spy), 1)

self.assertEqual(spy[0][0].where(), QgsPointXY())
self.assertEqual(spy[0][0].what(), 'ring 0 with less than four points')

g = QgsGeometry.fromWkt("Polygon (())")

validator = QgsGeometryValidator(g)
spy = QSignalSpy(validator.errorFound)
validator.run()
self.assertEqual(len(spy), 0)

g = QgsGeometry.fromWkt("Polygon ((0 0, 10 0, 10 10, 0 0),(1 1, 2 1, 2 2))")

validator = QgsGeometryValidator(g)
spy = QSignalSpy(validator.errorFound)
validator.run()
self.assertEqual(len(spy), 1)

self.assertEqual(spy[0][0].where(), QgsPointXY())
self.assertEqual(spy[0][0].what(), 'ring 1 with less than four points')

g = QgsGeometry.fromWkt("Polygon ((0 0, 10 0, 10 10, 0 0),(1 1, 2 1, 2 2, 1 1))")

validator = QgsGeometryValidator(g)
spy = QSignalSpy(validator.errorFound)
validator.run()
self.assertEqual(len(spy), 0)

g = QgsGeometry.fromWkt("Polygon ((0 0, 10 0, 10 10, 0 10, 0 0),(1 1, 2 1, 2 2, 1 1),(3 3, 3 4, 4 4))")

validator = QgsGeometryValidator(g)
spy = QSignalSpy(validator.errorFound)
validator.run()
self.assertEqual(len(spy), 1)

self.assertEqual(spy[0][0].where(), QgsPointXY())
self.assertEqual(spy[0][0].what(), 'ring 2 with less than four points')

def test_ring_closed(self):
# valid ring
g = QgsGeometry.fromWkt("Polygon ((0 0, 10 0, 10 10, 0 0))")

validator = QgsGeometryValidator(g)
spy = QSignalSpy(validator.errorFound)
validator.run()
self.assertEqual(len(spy), 0)

# not closed
g = QgsGeometry.fromWkt("Polygon ((0 0, 10 0, 10 10, 1 1))")

validator = QgsGeometryValidator(g)
spy = QSignalSpy(validator.errorFound)
validator.run()
self.assertEqual(len(spy), 1)

self.assertEqual(spy[0][0].where(), QgsPointXY())
self.assertEqual(spy[0][0].what(), 'ring 0 not closed')

g = QgsGeometry.fromWkt("Polygon ((0 0, 10 0, 10 10, 0 0),(1 1, 2 1, 2 2, 1.1 1))")

validator = QgsGeometryValidator(g)
spy = QSignalSpy(validator.errorFound)
validator.run()
self.assertEqual(len(spy), 1)

self.assertEqual(spy[0][0].where(), QgsPointXY())
self.assertEqual(spy[0][0].what(), 'ring 1 not closed')

g = QgsGeometry.fromWkt("Polygon ((0 0, 10 0, 10 10, 0 10, 0 0),(1 1, 2 1, 2 2, 1 1),(3 3, 3 4, 4 4, 3.1 3))")

validator = QgsGeometryValidator(g)
spy = QSignalSpy(validator.errorFound)
validator.run()
self.assertEqual(len(spy), 1)

self.assertEqual(spy[0][0].where(), QgsPointXY())
self.assertEqual(spy[0][0].what(), 'ring 2 not closed')


if __name__ == '__main__':
unittest.main()

0 comments on commit c7ab2d2

Please sign in to comment.
You can’t perform that action at this time.