Skip to content
Permalink
Browse files

Extend unit tests for QgsPolygonV2, QgsPoint

  • Loading branch information
nyalldawson committed Sep 18, 2017
1 parent 6b07b9b commit d3db082cd2709bc3ed2f6d63e35ff309233c2831
@@ -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/ );
@@ -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/ );
@@ -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() );
@@ -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
@@ -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() );
@@ -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;
@@ -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 )
@@ -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();
@@ -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,
@@ -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()
@@ -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.
You can’t perform that action at this time.