Navigation Menu

Skip to content

Commit

Permalink
Add unit tests for marker/line/polygon items
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Aug 5, 2020
1 parent c31ee84 commit c924ec3
Show file tree
Hide file tree
Showing 16 changed files with 611 additions and 0 deletions.
Expand Up @@ -42,6 +42,24 @@ Constructor for QgsAnnotationLineStringItem, with the specified ``linestring`` i
virtual QgsAnnotationLineStringItem *clone() /Factory/;


QgsLineString lineString() const;
%Docstring
Returns the line string geometry of the item.

The coordinate reference system for the line can be retrieved through :py:func:`~QgsAnnotationLineStringItem.crs`.

.. seealso:: :py:func:`setLineString`
%End

void setLineString( const QgsLineString &lineString );
%Docstring
Sets the ``lineString`` geometry of the item.

The coordinate reference system for the line can be set through :py:func:`~QgsAnnotationLineStringItem.setCrs`.

.. seealso:: :py:func:`lineString`
%End

const QgsLineSymbol *symbol() const;
%Docstring
Returns the symbol used to render the item.
Expand Down
Expand Up @@ -41,6 +41,24 @@ Constructor for QgsAnnotationMarkerItem, at the specified ``point`` in the given
virtual QgsAnnotationMarkerItem *clone() /Factory/;


QgsPointXY point() const;
%Docstring
Returns the point location of the marker.

The coordinate reference system for the point can be retrieved through :py:func:`~QgsAnnotationMarkerItem.crs`.

.. seealso:: :py:func:`setPoint`
%End

void setPoint( QgsPointXY point );
%Docstring
Sets the ``point`` location of the marker.

The coordinate reference system for the point can be set through :py:func:`~QgsAnnotationMarkerItem.setCrs`.

.. seealso:: :py:func:`point`
%End

const QgsMarkerSymbol *symbol() const;
%Docstring
Returns the symbol used to render the marker item.
Expand Down
Expand Up @@ -41,6 +41,24 @@ Constructor for QgsAnnotationPolygonItem, with the specified ``polygon`` in the
virtual QgsAnnotationPolygonItem *clone() /Factory/;


QgsPolygon polygon() const;
%Docstring
Returns the polygon geometry of the item.

The coordinate reference system for the polygon can be retrieved through :py:func:`~QgsAnnotationPolygonItem.crs`.

.. seealso:: :py:func:`setPolygon`
%End

void setPolygon( const QgsPolygon &polygon );
%Docstring
Sets the ``polygon`` geometry of the item.

The coordinate reference system for the polygon can be set through :py:func:`~QgsAnnotationPolygonItem.setCrs`.

.. seealso:: :py:func:`polygon`
%End

const QgsFillSymbol *symbol() const;
%Docstring
Returns the symbol used to render the item.
Expand Down
18 changes: 18 additions & 0 deletions src/core/annotations/qgsannotationlinestringitem.h
Expand Up @@ -47,6 +47,24 @@ class CORE_EXPORT QgsAnnotationLineStringItem : public QgsAnnotationItem

QgsAnnotationLineStringItem *clone() override SIP_FACTORY;

/**
* Returns the line string geometry of the item.
*
* The coordinate reference system for the line can be retrieved through crs().
*
* \see setLineString()
*/
QgsLineString lineString() const { return mLineString; }

/**
* Sets the \a lineString geometry of the item.
*
* The coordinate reference system for the line can be set through setCrs().
*
* \see lineString()
*/
void setLineString( const QgsLineString &lineString ) { mLineString = lineString; }

/**
* Returns the symbol used to render the item.
*
Expand Down
18 changes: 18 additions & 0 deletions src/core/annotations/qgsannotationmarkeritem.h
Expand Up @@ -46,6 +46,24 @@ class CORE_EXPORT QgsAnnotationMarkerItem : public QgsAnnotationItem
bool readXml( const QDomElement &element, const QgsReadWriteContext &context ) override;
QgsAnnotationMarkerItem *clone() override SIP_FACTORY;

/**
* Returns the point location of the marker.
*
* The coordinate reference system for the point can be retrieved through crs().
*
* \see setPoint()
*/
QgsPointXY point() const { return mPoint; }

/**
* Sets the \a point location of the marker.
*
* The coordinate reference system for the point can be set through setCrs().
*
* \see point()
*/
void setPoint( QgsPointXY point ) { mPoint = point; }

/**
* Returns the symbol used to render the marker item.
*
Expand Down
18 changes: 18 additions & 0 deletions src/core/annotations/qgsannotationpolygonitem.h
Expand Up @@ -46,6 +46,24 @@ class CORE_EXPORT QgsAnnotationPolygonItem : public QgsAnnotationItem
bool readXml( const QDomElement &element, const QgsReadWriteContext &context ) override;
QgsAnnotationPolygonItem *clone() override SIP_FACTORY;

/**
* Returns the polygon geometry of the item.
*
* The coordinate reference system for the polygon can be retrieved through crs().
*
* \see setPolygon()
*/
QgsPolygon polygon() const { return mPolygon; }

/**
* Sets the \a polygon geometry of the item.
*
* The coordinate reference system for the polygon can be set through setCrs().
*
* \see polygon()
*/
void setPolygon( const QgsPolygon &polygon ) { mPolygon = polygon; }

/**
* Returns the symbol used to render the item.
*
Expand Down
3 changes: 3 additions & 0 deletions tests/src/python/CMakeLists.txt
Expand Up @@ -17,6 +17,9 @@ ADD_PYTHON_TEST(PyQgsAlignmentComboBox test_qgsalignmentcombobox.py)
ADD_PYTHON_TEST(PyQgsPythonProvider test_provider_python.py)
ADD_PYTHON_TEST(PyQgsAggregateCalculator test_qgsaggregatecalculator.py)
ADD_PYTHON_TEST(PyQgsAnnotation test_qgsannotation.py)
ADD_PYTHON_TEST(PyQgsAnnotationLineStringItem test_qgsannotationlinestringitem.py)
ADD_PYTHON_TEST(PyQgsAnnotationMarkerItem test_qgsannotationmarkeritem.py)
ADD_PYTHON_TEST(PyQgsAnnotationPolygonItem test_qgsannotationpolygonitem.py)
ADD_PYTHON_TEST(PyQgsApplication test_qgsapplication.py)
ADD_PYTHON_TEST(PyQgsAttributeFormEditorWidget test_qgsattributeformeditorwidget.py)
ADD_PYTHON_TEST(PyQgsAttributeForm test_qgsattributeform.py)
Expand Down
167 changes: 167 additions & 0 deletions tests/src/python/test_qgsannotationlinestringitem.py
@@ -0,0 +1,167 @@
# -*- coding: utf-8 -*-
"""QGIS Unit tests for QgsAnnotationLineStringItem.
.. note:: This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
"""
__author__ = '(C) 2020 by Nyall Dawson'
__date__ = '29/07/2020'
__copyright__ = 'Copyright 2020, The QGIS Project'

import qgis # NOQA

from qgis.PyQt.QtCore import (QSize,
QDir)
from qgis.PyQt.QtGui import (QImage,
QPainter,
QColor)
from qgis.core import (QgsMapSettings,
QgsCoordinateTransform,
QgsProject,
QgsPoint,
QgsCoordinateReferenceSystem,
QgsLineSymbol,
QgsRenderChecker,
QgsReadWriteContext,
QgsRenderContext,
QgsAnnotationLineStringItem,
QgsRectangle,
QgsLineString
)
from qgis.PyQt.QtXml import QDomDocument

from qgis.testing import start_app, unittest
from utilities import unitTestDataPath

start_app()
TEST_DATA_DIR = unitTestDataPath()


class TestQgsAnnotationLineStringItem(unittest.TestCase):

@classmethod
def setUpClass(cls):
cls.report = "<h1>Python QgsAnnotationLineStringItem Tests</h1>\n"

@classmethod
def tearDownClass(cls):
report_file_path = "%s/qgistest.html" % QDir.tempPath()
with open(report_file_path, 'a') as report_file:
report_file.write(cls.report)

def testBasic(self):
item = QgsAnnotationLineStringItem(QgsLineString([QgsPoint(12, 13), QgsPoint(14, 13), QgsPoint(14, 15)]),
QgsCoordinateReferenceSystem('EPSG:4326'))

self.assertEqual(item.crs().authid(), 'EPSG:4326')
self.assertEqual(item.lineString().asWkt(), 'LineString (12 13, 14 13, 14 15)')

item.setCrs(QgsCoordinateReferenceSystem('EPSG:3111'))
item.setLineString(QgsLineString([QgsPoint(22, 23), QgsPoint(24, 23), QgsPoint(24, 25)]))
self.assertEqual(item.crs().authid(), 'EPSG:3111')
self.assertEqual(item.lineString().asWkt(), 'LineString (22 23, 24 23, 24 25)')

item.setSymbol(QgsLineSymbol.createSimple({'color': '#ffff00', 'line_width': '3'}))
self.assertEqual(item.symbol()[0].color(), QColor(255, 255, 0))

def testReadWriteXml(self):
doc = QDomDocument("testdoc")
elem = doc.createElement('test')

item = QgsAnnotationLineStringItem(QgsLineString([QgsPoint(12, 13), QgsPoint(14, 13), QgsPoint(14, 15)]),
QgsCoordinateReferenceSystem('EPSG:4326'))
item.setSymbol(QgsLineSymbol.createSimple({'color': '#ffff00', 'line_width': '3'}))

self.assertTrue(item.writeXml(elem, doc, QgsReadWriteContext()))

s2 = QgsAnnotationLineStringItem.create()
self.assertTrue(s2.readXml(elem, QgsReadWriteContext()))

self.assertEqual(s2.crs().authid(), 'EPSG:4326')
self.assertEqual(s2.lineString().asWkt(), 'LineString (12 13, 14 13, 14 15)')
self.assertEqual(s2.symbol()[0].color(), QColor(255, 255, 0))

def testClone(self):
item = QgsAnnotationLineStringItem(QgsLineString([QgsPoint(12, 13), QgsPoint(14, 13), QgsPoint(14, 15)]),
QgsCoordinateReferenceSystem('EPSG:4326'))
item.setSymbol(QgsLineSymbol.createSimple({'color': '#ffff00', 'line_width': '3'}))

item2 = item.clone()
self.assertEqual(item2.crs().authid(), 'EPSG:4326')
self.assertEqual(item2.lineString().asWkt(), 'LineString (12 13, 14 13, 14 15)')
self.assertEqual(item2.symbol()[0].color(), QColor(255, 255, 0))

def testRenderLineString(self):
item = QgsAnnotationLineStringItem(QgsLineString([QgsPoint(12, 13), QgsPoint(14, 13), QgsPoint(14, 15)]),
QgsCoordinateReferenceSystem('EPSG:4326'))
item.setSymbol(QgsLineSymbol.createSimple({'color': '#ffff00', 'line_width': '3'}))

settings = QgsMapSettings()
settings.setDestinationCrs(QgsCoordinateReferenceSystem('EPSG:4326'))
settings.setExtent(QgsRectangle(10, 10, 18, 18))
settings.setOutputSize(QSize(300, 300))

settings.setFlag(QgsMapSettings.Antialiasing, False)

rc = QgsRenderContext.fromMapSettings(settings)
image = QImage(200, 200, QImage.Format_ARGB32)
image.setDotsPerMeterX(96 / 25.4 * 1000)
image.setDotsPerMeterY(96 / 25.4 * 1000)
image.fill(QColor(255, 255, 255))
painter = QPainter(image)
rc.setPainter(painter)

try:
item.render(rc, None)
finally:
painter.end()

self.assertTrue(self.imageCheck('linestring_item', 'linestring_item', image))

def testRenderWithTransform(self):
item = QgsAnnotationLineStringItem(QgsLineString([QgsPoint(11, 13), QgsPoint(12, 13), QgsPoint(12, 15)]),
QgsCoordinateReferenceSystem('EPSG:4326'))
item.setSymbol(QgsLineSymbol.createSimple({'color': '#ffff00', 'line_width': '3'}))

settings = QgsMapSettings()
settings.setDestinationCrs(QgsCoordinateReferenceSystem('EPSG:3857'))
settings.setExtent(QgsRectangle(1250958, 1386945, 1420709, 1532518))
settings.setOutputSize(QSize(300, 300))

settings.setFlag(QgsMapSettings.Antialiasing, False)

rc = QgsRenderContext.fromMapSettings(settings)
rc.setCoordinateTransform(QgsCoordinateTransform(item.crs(), settings.destinationCrs(), QgsProject.instance()))
image = QImage(200, 200, QImage.Format_ARGB32)
image.setDotsPerMeterX(96 / 25.4 * 1000)
image.setDotsPerMeterY(96 / 25.4 * 1000)
image.fill(QColor(255, 255, 255))
painter = QPainter(image)
rc.setPainter(painter)

try:
item.render(rc, None)
finally:
painter.end()

self.assertTrue(self.imageCheck('linestring_item_transform', 'linestring_item_transform', image))

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


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

0 comments on commit c924ec3

Please sign in to comment.