Skip to content

Commit

Permalink
Extend unit tests for QgsPolygonV2, QgsPoint
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Sep 19, 2017
1 parent 6b07b9b commit d3db082
Show file tree
Hide file tree
Showing 8 changed files with 69 additions and 8 deletions.
4 changes: 2 additions & 2 deletions python/core/geometry/qgspolygon.sip
Expand Up @@ -42,12 +42,12 @@ class QgsPolygonV2: QgsCurvePolygon
virtual QgsPolygonV2 *surfaceToPolygon() const /Factory/;


virtual QgsAbstractGeometry *toCurveType() const /Factory/;
virtual QgsCurvePolygon *toCurveType() const /Factory/;

%Docstring
Returns the geometry converted to the more generic curve type QgsCurvePolygon
:return: the converted geometry. Caller takes ownership*
:rtype: QgsAbstractGeometry
:rtype: QgsCurvePolygon
%End

virtual void addInteriorRing( QgsCurve *ring /Transfer/ );
Expand Down
2 changes: 1 addition & 1 deletion python/core/geometry/qgstriangle.sip
Expand Up @@ -69,7 +69,7 @@ class QgsTriangle : QgsPolygonV2
virtual QgsPolygonV2 *surfaceToPolygon() const /Factory/;


virtual QgsAbstractGeometry *toCurveType() const /Factory/;
virtual QgsCurvePolygon *toCurveType() const /Factory/;


virtual void addInteriorRing( QgsCurve *ring /Transfer/ );
Expand Down
2 changes: 1 addition & 1 deletion src/core/geometry/qgspolygon.cpp
Expand Up @@ -300,7 +300,7 @@ QgsPolygonV2 *QgsPolygonV2::surfaceToPolygon() const
return clone();
}

QgsAbstractGeometry *QgsPolygonV2::toCurveType() const
QgsCurvePolygon *QgsPolygonV2::toCurveType() const
{
QgsCurvePolygon *curvePolygon = new QgsCurvePolygon();
curvePolygon->setExteriorRing( mExteriorRing->clone() );
Expand Down
2 changes: 1 addition & 1 deletion src/core/geometry/qgspolygon.h
Expand Up @@ -53,7 +53,7 @@ class CORE_EXPORT QgsPolygonV2: public QgsCurvePolygon

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

void addInteriorRing( QgsCurve *ring SIP_TRANSFER ) override;
//overridden to handle LineString25D rings
Expand Down
2 changes: 1 addition & 1 deletion src/core/geometry/qgstriangle.cpp
Expand Up @@ -231,7 +231,7 @@ QgsPolygonV2 *QgsTriangle::surfaceToPolygon() const
return toPolygon();
}

QgsAbstractGeometry *QgsTriangle::toCurveType() const
QgsCurvePolygon *QgsTriangle::toCurveType() const
{
std::unique_ptr<QgsCurvePolygon> curvePolygon( new QgsCurvePolygon() );
curvePolygon->setExteriorRing( mExteriorRing->clone() );
Expand Down
2 changes: 1 addition & 1 deletion src/core/geometry/qgstriangle.h
Expand Up @@ -76,7 +76,7 @@ class CORE_EXPORT QgsTriangle : public QgsPolygonV2

QgsPolygonV2 *surfaceToPolygon() const override SIP_FACTORY;

QgsAbstractGeometry *toCurveType() const override SIP_FACTORY;
QgsCurvePolygon *toCurveType() const override SIP_FACTORY;

//! Inherited method not used. You cannot add an interior ring into a triangle.
void addInteriorRing( QgsCurve *ring SIP_TRANSFER ) override;
Expand Down
18 changes: 18 additions & 0 deletions tests/src/core/testqgsgeometry.cpp
Expand Up @@ -3010,6 +3010,7 @@ void TestQgsGeometry::polygon()
p10.addInteriorRing( ring );
QVERIFY( !( p10 == p10b ) );
QVERIFY( p10 != p10b );

ring = new QgsLineString();
ring->setPoints( QgsPointSequence() << QgsPoint( 2, 1 )
<< QgsPoint( 2, 9 ) << QgsPoint( 9, 9 )
Expand Down Expand Up @@ -3068,6 +3069,23 @@ void TestQgsGeometry::polygon()
std::unique_ptr< QgsPolygonV2 > surface( p12.surfaceToPolygon() );
QCOMPARE( *surface, p12 );

//toCurveType
std::unique_ptr< QgsCurvePolygon > curveType( p12.toCurveType() );
QCOMPARE( curveType->wkbType(), QgsWkbTypes::CurvePolygonZM );
QCOMPARE( curveType->exteriorRing()->numPoints(), 5 );
QCOMPARE( curveType->exteriorRing()->vertexAt( QgsVertexId( 0, 0, 0 ) ), QgsPoint( QgsWkbTypes::PointZM, 0, 0, 1, 5 ) );
QCOMPARE( curveType->exteriorRing()->vertexAt( QgsVertexId( 0, 0, 1 ) ), QgsPoint( QgsWkbTypes::PointZM, 0, 10, 2, 6 ) );
QCOMPARE( curveType->exteriorRing()->vertexAt( QgsVertexId( 0, 0, 2 ) ), QgsPoint( QgsWkbTypes::PointZM, 10, 10, 3, 7 ) );
QCOMPARE( curveType->exteriorRing()->vertexAt( QgsVertexId( 0, 0, 3 ) ), QgsPoint( QgsWkbTypes::PointZM, 10, 0, 4, 8 ) );
QCOMPARE( curveType->exteriorRing()->vertexAt( QgsVertexId( 0, 0, 4 ) ), QgsPoint( QgsWkbTypes::PointZM, 0, 0, 1, 9 ) );
QCOMPARE( curveType->numInteriorRings(), 1 );
QCOMPARE( curveType->interiorRing( 0 )->numPoints(), 5 );
QCOMPARE( curveType->interiorRing( 0 )->vertexAt( QgsVertexId( 0, 0, 0 ) ), QgsPoint( QgsWkbTypes::PointZM, 1, 1, 1, 2 ) );
QCOMPARE( curveType->interiorRing( 0 )->vertexAt( QgsVertexId( 0, 0, 1 ) ), QgsPoint( QgsWkbTypes::PointZM, 1, 9, 2, 3 ) );
QCOMPARE( curveType->interiorRing( 0 )->vertexAt( QgsVertexId( 0, 0, 2 ) ), QgsPoint( QgsWkbTypes::PointZM, 9, 9, 3, 6 ) );
QCOMPARE( curveType->interiorRing( 0 )->vertexAt( QgsVertexId( 0, 0, 3 ) ), QgsPoint( QgsWkbTypes::PointZM, 9, 1, 4, 4 ) );
QCOMPARE( curveType->interiorRing( 0 )->vertexAt( QgsVertexId( 0, 0, 4 ) ), QgsPoint( QgsWkbTypes::PointZM, 1, 1, 1, 7 ) );

//to/fromWKB
QgsPolygonV2 p16;
ext = new QgsLineString();
Expand Down
45 changes: 44 additions & 1 deletion tests/src/python/test_qgsgeometry.py
Expand Up @@ -35,8 +35,11 @@
QgsPolygonV2,
QgsCoordinateTransform,
QgsRectangle,
QgsWkbTypes
QgsWkbTypes,
QgsRenderChecker
)
from qgis.PyQt.QtCore import QDir
from qgis.PyQt.QtGui import QImage, QPainter, QPen, QColor, QBrush

from qgis.testing import (
start_app,
Expand All @@ -57,6 +60,9 @@

class TestQgsGeometry(unittest.TestCase):

def setUp(self):
self.report = "<h1>Python QgsGeometry Tests</h1>\n"

def testBool(self):
""" Test boolean evaluation of QgsGeometry """
g = QgsGeometry()
Expand Down Expand Up @@ -4251,6 +4257,43 @@ def testHausdorffDensify(self):
self.assertAlmostEqual(o, exp, 5,
"mismatch for {} to {}, expected:\n{}\nGot:\n{}\n".format(t[0], t[1], exp, o))

def renderGeometry(self, geom):
image = QImage(200, 200, QImage.Format_RGB32)
image.fill(QColor(0, 0, 0))

painter = QPainter(image)
painter.setBrush(QBrush(QColor(255, 255, 255)))
geom.draw(painter)
painter.end()
return image

def testGeometryDraw(self):
'''Tests drawing geometries'''

tests = [{'name': 'Point',
'wkt': 'Point (40 60)',
'reference_image': 'point'}]

for test in tests:
geom = QgsGeometry.fromWkt(test['wkt'])
self.assertTrue(geom and not geom.isNull(), 'Could not create geometry {}'.format(test['wkt']))
rendered_image = self.renderGeometry(geom)
assert self.imageCheck(test['name'], test['reference_image'], rendered_image)

def imageCheck(self, name, reference_image, image):
self.report += "<h2>Render {}</h2>\n".format(name)
temp_dir = QDir.tempPath() + '/'
file_name = temp_dir + 'geometry_' + name + ".png"
image.save(file_name, "PNG")
checker = QgsRenderChecker()
checker.setControlPathPrefix("geometry")
checker.setControlName("expected_" + reference_image)
checker.setRenderedImage(file_name)
checker.setColorTolerance(2)
result = checker.compareImages(name, 20)
self.report += checker.report()
print((self.report))
return result

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

0 comments on commit d3db082

Please sign in to comment.