Skip to content

Commit d3db082

Browse files
committed
Extend unit tests for QgsPolygonV2, QgsPoint
1 parent 6b07b9b commit d3db082

File tree

8 files changed

+69
-8
lines changed

8 files changed

+69
-8
lines changed

python/core/geometry/qgspolygon.sip

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,12 @@ class QgsPolygonV2: QgsCurvePolygon
4242
virtual QgsPolygonV2 *surfaceToPolygon() const /Factory/;
4343

4444

45-
virtual QgsAbstractGeometry *toCurveType() const /Factory/;
45+
virtual QgsCurvePolygon *toCurveType() const /Factory/;
4646

4747
%Docstring
4848
Returns the geometry converted to the more generic curve type QgsCurvePolygon
4949
:return: the converted geometry. Caller takes ownership*
50-
:rtype: QgsAbstractGeometry
50+
:rtype: QgsCurvePolygon
5151
%End
5252

5353
virtual void addInteriorRing( QgsCurve *ring /Transfer/ );

python/core/geometry/qgstriangle.sip

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ class QgsTriangle : QgsPolygonV2
6969
virtual QgsPolygonV2 *surfaceToPolygon() const /Factory/;
7070

7171

72-
virtual QgsAbstractGeometry *toCurveType() const /Factory/;
72+
virtual QgsCurvePolygon *toCurveType() const /Factory/;
7373

7474

7575
virtual void addInteriorRing( QgsCurve *ring /Transfer/ );

src/core/geometry/qgspolygon.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,7 @@ QgsPolygonV2 *QgsPolygonV2::surfaceToPolygon() const
300300
return clone();
301301
}
302302

303-
QgsAbstractGeometry *QgsPolygonV2::toCurveType() const
303+
QgsCurvePolygon *QgsPolygonV2::toCurveType() const
304304
{
305305
QgsCurvePolygon *curvePolygon = new QgsCurvePolygon();
306306
curvePolygon->setExteriorRing( mExteriorRing->clone() );

src/core/geometry/qgspolygon.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ class CORE_EXPORT QgsPolygonV2: public QgsCurvePolygon
5353

5454
/** Returns the geometry converted to the more generic curve type QgsCurvePolygon
5555
\returns the converted geometry. Caller takes ownership*/
56-
QgsAbstractGeometry *toCurveType() const override SIP_FACTORY;
56+
QgsCurvePolygon *toCurveType() const override SIP_FACTORY;
5757

5858
void addInteriorRing( QgsCurve *ring SIP_TRANSFER ) override;
5959
//overridden to handle LineString25D rings

src/core/geometry/qgstriangle.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ QgsPolygonV2 *QgsTriangle::surfaceToPolygon() const
231231
return toPolygon();
232232
}
233233

234-
QgsAbstractGeometry *QgsTriangle::toCurveType() const
234+
QgsCurvePolygon *QgsTriangle::toCurveType() const
235235
{
236236
std::unique_ptr<QgsCurvePolygon> curvePolygon( new QgsCurvePolygon() );
237237
curvePolygon->setExteriorRing( mExteriorRing->clone() );

src/core/geometry/qgstriangle.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ class CORE_EXPORT QgsTriangle : public QgsPolygonV2
7676

7777
QgsPolygonV2 *surfaceToPolygon() const override SIP_FACTORY;
7878

79-
QgsAbstractGeometry *toCurveType() const override SIP_FACTORY;
79+
QgsCurvePolygon *toCurveType() const override SIP_FACTORY;
8080

8181
//! Inherited method not used. You cannot add an interior ring into a triangle.
8282
void addInteriorRing( QgsCurve *ring SIP_TRANSFER ) override;

tests/src/core/testqgsgeometry.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3010,6 +3010,7 @@ void TestQgsGeometry::polygon()
30103010
p10.addInteriorRing( ring );
30113011
QVERIFY( !( p10 == p10b ) );
30123012
QVERIFY( p10 != p10b );
3013+
30133014
ring = new QgsLineString();
30143015
ring->setPoints( QgsPointSequence() << QgsPoint( 2, 1 )
30153016
<< QgsPoint( 2, 9 ) << QgsPoint( 9, 9 )
@@ -3068,6 +3069,23 @@ void TestQgsGeometry::polygon()
30683069
std::unique_ptr< QgsPolygonV2 > surface( p12.surfaceToPolygon() );
30693070
QCOMPARE( *surface, p12 );
30703071

3072+
//toCurveType
3073+
std::unique_ptr< QgsCurvePolygon > curveType( p12.toCurveType() );
3074+
QCOMPARE( curveType->wkbType(), QgsWkbTypes::CurvePolygonZM );
3075+
QCOMPARE( curveType->exteriorRing()->numPoints(), 5 );
3076+
QCOMPARE( curveType->exteriorRing()->vertexAt( QgsVertexId( 0, 0, 0 ) ), QgsPoint( QgsWkbTypes::PointZM, 0, 0, 1, 5 ) );
3077+
QCOMPARE( curveType->exteriorRing()->vertexAt( QgsVertexId( 0, 0, 1 ) ), QgsPoint( QgsWkbTypes::PointZM, 0, 10, 2, 6 ) );
3078+
QCOMPARE( curveType->exteriorRing()->vertexAt( QgsVertexId( 0, 0, 2 ) ), QgsPoint( QgsWkbTypes::PointZM, 10, 10, 3, 7 ) );
3079+
QCOMPARE( curveType->exteriorRing()->vertexAt( QgsVertexId( 0, 0, 3 ) ), QgsPoint( QgsWkbTypes::PointZM, 10, 0, 4, 8 ) );
3080+
QCOMPARE( curveType->exteriorRing()->vertexAt( QgsVertexId( 0, 0, 4 ) ), QgsPoint( QgsWkbTypes::PointZM, 0, 0, 1, 9 ) );
3081+
QCOMPARE( curveType->numInteriorRings(), 1 );
3082+
QCOMPARE( curveType->interiorRing( 0 )->numPoints(), 5 );
3083+
QCOMPARE( curveType->interiorRing( 0 )->vertexAt( QgsVertexId( 0, 0, 0 ) ), QgsPoint( QgsWkbTypes::PointZM, 1, 1, 1, 2 ) );
3084+
QCOMPARE( curveType->interiorRing( 0 )->vertexAt( QgsVertexId( 0, 0, 1 ) ), QgsPoint( QgsWkbTypes::PointZM, 1, 9, 2, 3 ) );
3085+
QCOMPARE( curveType->interiorRing( 0 )->vertexAt( QgsVertexId( 0, 0, 2 ) ), QgsPoint( QgsWkbTypes::PointZM, 9, 9, 3, 6 ) );
3086+
QCOMPARE( curveType->interiorRing( 0 )->vertexAt( QgsVertexId( 0, 0, 3 ) ), QgsPoint( QgsWkbTypes::PointZM, 9, 1, 4, 4 ) );
3087+
QCOMPARE( curveType->interiorRing( 0 )->vertexAt( QgsVertexId( 0, 0, 4 ) ), QgsPoint( QgsWkbTypes::PointZM, 1, 1, 1, 7 ) );
3088+
30713089
//to/fromWKB
30723090
QgsPolygonV2 p16;
30733091
ext = new QgsLineString();

tests/src/python/test_qgsgeometry.py

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,11 @@
3535
QgsPolygonV2,
3636
QgsCoordinateTransform,
3737
QgsRectangle,
38-
QgsWkbTypes
38+
QgsWkbTypes,
39+
QgsRenderChecker
3940
)
41+
from qgis.PyQt.QtCore import QDir
42+
from qgis.PyQt.QtGui import QImage, QPainter, QPen, QColor, QBrush
4043

4144
from qgis.testing import (
4245
start_app,
@@ -57,6 +60,9 @@
5760

5861
class TestQgsGeometry(unittest.TestCase):
5962

63+
def setUp(self):
64+
self.report = "<h1>Python QgsGeometry Tests</h1>\n"
65+
6066
def testBool(self):
6167
""" Test boolean evaluation of QgsGeometry """
6268
g = QgsGeometry()
@@ -4251,6 +4257,43 @@ def testHausdorffDensify(self):
42514257
self.assertAlmostEqual(o, exp, 5,
42524258
"mismatch for {} to {}, expected:\n{}\nGot:\n{}\n".format(t[0], t[1], exp, o))
42534259

4260+
def renderGeometry(self, geom):
4261+
image = QImage(200, 200, QImage.Format_RGB32)
4262+
image.fill(QColor(0, 0, 0))
4263+
4264+
painter = QPainter(image)
4265+
painter.setBrush(QBrush(QColor(255, 255, 255)))
4266+
geom.draw(painter)
4267+
painter.end()
4268+
return image
4269+
4270+
def testGeometryDraw(self):
4271+
'''Tests drawing geometries'''
4272+
4273+
tests = [{'name': 'Point',
4274+
'wkt': 'Point (40 60)',
4275+
'reference_image': 'point'}]
4276+
4277+
for test in tests:
4278+
geom = QgsGeometry.fromWkt(test['wkt'])
4279+
self.assertTrue(geom and not geom.isNull(), 'Could not create geometry {}'.format(test['wkt']))
4280+
rendered_image = self.renderGeometry(geom)
4281+
assert self.imageCheck(test['name'], test['reference_image'], rendered_image)
4282+
4283+
def imageCheck(self, name, reference_image, image):
4284+
self.report += "<h2>Render {}</h2>\n".format(name)
4285+
temp_dir = QDir.tempPath() + '/'
4286+
file_name = temp_dir + 'geometry_' + name + ".png"
4287+
image.save(file_name, "PNG")
4288+
checker = QgsRenderChecker()
4289+
checker.setControlPathPrefix("geometry")
4290+
checker.setControlName("expected_" + reference_image)
4291+
checker.setRenderedImage(file_name)
4292+
checker.setColorTolerance(2)
4293+
result = checker.compareImages(name, 20)
4294+
self.report += checker.report()
4295+
print((self.report))
4296+
return result
42544297

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

0 commit comments

Comments
 (0)