Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Extend geometry test coverage
  • Loading branch information
nyalldawson committed Sep 19, 2017
1 parent d3db082 commit a15340f
Show file tree
Hide file tree
Showing 8 changed files with 105 additions and 12 deletions.
5 changes: 2 additions & 3 deletions python/core/geometry/qgslinestring.sip
Expand Up @@ -12,7 +12,6 @@




class QgsLineString: QgsCurve
{
%Docstring
Expand Down Expand Up @@ -151,12 +150,12 @@ class QgsLineString: QgsCurve
Closes the line string by appending the first point to the end of the line, if it is not already closed.
%End

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

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

void extend( double startDistance, double endDistance );
Expand Down
2 changes: 1 addition & 1 deletion src/core/geometry/qgslinestring.cpp
Expand Up @@ -663,7 +663,7 @@ void QgsLineString::drawAsPolygon( QPainter &p ) const
p.drawPolygon( asQPolygonF() );
}

QgsAbstractGeometry *QgsLineString::toCurveType() const
QgsCompoundCurve *QgsLineString::toCurveType() const
{
QgsCompoundCurve *compoundCurve = new QgsCompoundCurve();
compoundCurve->addCurve( clone() );
Expand Down
4 changes: 2 additions & 2 deletions src/core/geometry/qgslinestring.h
Expand Up @@ -24,7 +24,7 @@
#include "qgis_core.h"
#include "qgis.h"
#include "qgscurve.h"

#include "qgscompoundcurve.h"

/***************************************************************************
* This class is considered CRITICAL and any change MUST be accompanied with
Expand Down Expand Up @@ -150,7 +150,7 @@ class CORE_EXPORT QgsLineString: public QgsCurve

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

/**
* Extends the line geometry by extrapolating out the start or end of the line
Expand Down
57 changes: 57 additions & 0 deletions tests/src/core/testqgsgeometry.cpp
Expand Up @@ -1046,6 +1046,20 @@ void TestQgsGeometry::lineString()
QCOMPARE( fromArray5.yAt( 1 ), 12.0 );
QCOMPARE( fromArray5.xAt( 2 ), 3.0 );
QCOMPARE( fromArray5.yAt( 2 ), 13.0 );
// unbalanced -> z truncated
zz = QVector< double >() << 21 << 22 << 23 << 24;
fromArray5 = QgsLineString( xx, yy, zz );
QCOMPARE( fromArray5.wkbType(), QgsWkbTypes::LineStringZ );
QCOMPARE( fromArray5.numPoints(), 3 );
QCOMPARE( fromArray5.xAt( 0 ), 1.0 );
QCOMPARE( fromArray5.yAt( 0 ), 11.0 );
QCOMPARE( fromArray5.zAt( 0 ), 21.0 );
QCOMPARE( fromArray5.xAt( 1 ), 2.0 );
QCOMPARE( fromArray5.yAt( 1 ), 12.0 );
QCOMPARE( fromArray5.zAt( 1 ), 22.0 );
QCOMPARE( fromArray5.xAt( 2 ), 3.0 );
QCOMPARE( fromArray5.yAt( 2 ), 13.0 );
QCOMPARE( fromArray5.zAt( 2 ), 23.0 );
// with m
QVector< double > mm;
xx = QVector< double >() << 1 << 2 << 3;
Expand Down Expand Up @@ -1074,6 +1088,20 @@ void TestQgsGeometry::lineString()
QCOMPARE( fromArray7.yAt( 1 ), 12.0 );
QCOMPARE( fromArray7.xAt( 2 ), 3.0 );
QCOMPARE( fromArray7.yAt( 2 ), 13.0 );
// unbalanced -> m truncated
mm = QVector< double >() << 21 << 22 << 23 << 24;
fromArray7 = QgsLineString( xx, yy, QVector< double >(), mm );
QCOMPARE( fromArray7.wkbType(), QgsWkbTypes::LineStringM );
QCOMPARE( fromArray7.numPoints(), 3 );
QCOMPARE( fromArray7.xAt( 0 ), 1.0 );
QCOMPARE( fromArray7.yAt( 0 ), 11.0 );
QCOMPARE( fromArray7.mAt( 0 ), 21.0 );
QCOMPARE( fromArray7.xAt( 1 ), 2.0 );
QCOMPARE( fromArray7.yAt( 1 ), 12.0 );
QCOMPARE( fromArray7.mAt( 1 ), 22.0 );
QCOMPARE( fromArray7.xAt( 2 ), 3.0 );
QCOMPARE( fromArray7.yAt( 2 ), 13.0 );
QCOMPARE( fromArray7.mAt( 2 ), 23.0 );
// zm
xx = QVector< double >() << 1 << 2 << 3;
yy = QVector< double >() << 11 << 12 << 13;
Expand All @@ -1096,6 +1124,14 @@ void TestQgsGeometry::lineString()
QCOMPARE( fromArray8.mAt( 2 ), 33.0 );

// from QList<QgsPointXY>
QgsLineString fromPtsA = QgsLineString( QVector< QgsPoint >() );
QVERIFY( fromPtsA.isEmpty() );
QCOMPARE( fromPtsA.wkbType(), QgsWkbTypes::LineString );

fromPtsA = QgsLineString( QVector< QgsPoint >() << QgsPoint( 1, 2, 0, 4, QgsWkbTypes::PointM ) );
QCOMPARE( fromPtsA.numPoints(), 1 );
QCOMPARE( fromPtsA.wkbType(), QgsWkbTypes::LineStringM );

QList<QgsPointXY> ptsA;
ptsA << QgsPointXY( 1, 2 ) << QgsPointXY( 11, 12 ) << QgsPointXY( 21, 22 );
QgsLineString fromPts( ptsA );
Expand Down Expand Up @@ -1541,6 +1577,8 @@ void TestQgsGeometry::lineString()
QVERIFY( !( e5 == e6 ) ); //different m values
QVERIFY( e5 != e6 );

QVERIFY( e6 != QgsCircularString() );

//close/isClosed
QgsLineString l11;
QVERIFY( !l11.isClosed() );
Expand Down Expand Up @@ -2529,6 +2567,25 @@ void TestQgsGeometry::lineString()
QCOMPARE( extend1.pointN( 0 ), QgsPoint( QgsWkbTypes::Point, -1, 0 ) );
QCOMPARE( extend1.pointN( 1 ), QgsPoint( QgsWkbTypes::Point, 1, 0 ) );
QCOMPARE( extend1.pointN( 2 ), QgsPoint( QgsWkbTypes::Point, 1, 3 ) );

// addToPainterPath (note most tests are in test_qgsgeometry.py)
QgsLineString path;
QPainterPath pPath;
path.addToPainterPath( pPath );
QVERIFY( pPath.isEmpty() );
path.setPoints( QgsPointSequence() << QgsPoint( QgsWkbTypes::PointZ, 1, 2, 3 ) << QgsPoint( QgsWkbTypes::PointZ, 11, 12, 13 ) );
path.addToPainterPath( pPath );
QVERIFY( !pPath.isEmpty() );

// toCurveType
QgsLineString curveLine1;
curveLine1.setPoints( QgsPointSequence() << QgsPoint( 1, 2 ) << QgsPoint( 11, 12 ) );
std::unique_ptr< QgsCompoundCurve > curveType( curveLine1.toCurveType() );
QCOMPARE( curveType->wkbType(), QgsWkbTypes::CompoundCurve );
QCOMPARE( curveType->numPoints(), 2 );
QCOMPARE( curveType->vertexAt( QgsVertexId( 0, 0, 0 ) ), QgsPoint( 1, 2 ) );
QCOMPARE( curveType->vertexAt( QgsVertexId( 0, 0, 1 ) ), QgsPoint( 11, 12 ) );

}

void TestQgsGeometry::polygon()
Expand Down
49 changes: 43 additions & 6 deletions tests/src/python/test_qgsgeometry.py
Expand Up @@ -39,7 +39,7 @@
QgsRenderChecker
)
from qgis.PyQt.QtCore import QDir
from qgis.PyQt.QtGui import QImage, QPainter, QPen, QColor, QBrush
from qgis.PyQt.QtGui import QImage, QPainter, QPen, QColor, QBrush, QPainterPath

from qgis.testing import (
start_app,
Expand Down Expand Up @@ -2262,6 +2262,7 @@ def testWkbTypes(self):
self.assertEqual(QgsWkbTypes.displayString(QgsWkbTypes.MultiPoint25D), 'MultiPoint25D')
self.assertEqual(QgsWkbTypes.displayString(QgsWkbTypes.MultiLineString25D), 'MultiLineString25D')
self.assertEqual(QgsWkbTypes.displayString(QgsWkbTypes.MultiPolygon25D), 'MultiPolygon25D')
self.assertEqual(QgsWkbTypes.displayString(9999999), '')

# test parseType method
self.assertEqual(QgsWkbTypes.parseType('point( 1 2 )'), QgsWkbTypes.Point)
Expand Down Expand Up @@ -3211,6 +3212,14 @@ def testWkbTypes(self):
self.assertEqual(QgsWkbTypes.zmType(QgsWkbTypes.MultiSurfaceZM, False, True), QgsWkbTypes.MultiSurfaceM)
self.assertEqual(QgsWkbTypes.zmType(QgsWkbTypes.MultiSurfaceZM, True, True), QgsWkbTypes.MultiSurfaceZM)

def testGeometryDisplayString(self):
self.assertEqual(QgsWkbTypes.geometryDisplayString(QgsWkbTypes.PointGeometry), 'Point')
self.assertEqual(QgsWkbTypes.geometryDisplayString(QgsWkbTypes.LineGeometry), 'Line')
self.assertEqual(QgsWkbTypes.geometryDisplayString(QgsWkbTypes.PolygonGeometry), 'Polygon')
self.assertEqual(QgsWkbTypes.geometryDisplayString(QgsWkbTypes.UnknownGeometry), 'Unknown geometry')
self.assertEqual(QgsWkbTypes.geometryDisplayString(QgsWkbTypes.NullGeometry), 'No geometry')
self.assertEqual(QgsWkbTypes.geometryDisplayString(999999), 'Invalid type')

def testDeleteVertexCircularString(self):

wkt = "CircularString ((0 0,1 1,2 0))"
Expand Down Expand Up @@ -4257,13 +4266,25 @@ 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):
def renderGeometry(self, geom, use_pen, as_polygon=False, as_painter_path=False):
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)
if use_pen:
painter.setPen(QPen(QColor(255, 255, 255), 4))
else:
painter.setBrush(QBrush(QColor(255, 255, 255)))

if as_painter_path:
path = QPainterPath()
geom.geometry().addToPainterPath(path)
painter.drawPath(path)
else:
if as_polygon:
geom.geometry().drawAsPolygon(painter)
else:
geom.draw(painter)
painter.end()
return image

Expand All @@ -4272,14 +4293,30 @@ def testGeometryDraw(self):

tests = [{'name': 'Point',
'wkt': 'Point (40 60)',
'reference_image': 'point'}]
'reference_image': 'point',
'use_pen': False},
{'name': 'LineString',
'wkt': 'LineString (20 30, 50 30, 50 90)',
'reference_image': 'linestring',
'as_polygon_reference_image': 'linestring_aspolygon',
'use_pen': True}
]

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)
rendered_image = self.renderGeometry(geom, test['use_pen'])
assert self.imageCheck(test['name'], test['reference_image'], rendered_image)

if hasattr(geom.geometry(), 'addToPainterPath'):
# also check using painter path
rendered_image = self.renderGeometry(geom, test['use_pen'], as_painter_path=True)
assert self.imageCheck(test['name'], test['reference_image'], rendered_image)

if 'as_polygon_reference_image' in test:
rendered_image = self.renderGeometry(geom, False, True)
assert self.imageCheck(test['name'] + '_aspolygon', test['as_polygon_reference_image'], rendered_image)

def imageCheck(self, name, reference_image, image):
self.report += "<h2>Render {}</h2>\n".format(name)
temp_dir = QDir.tempPath() + '/'
Expand Down
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit a15340f

Please sign in to comment.