|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +"""QGIS Unit tests for QgsFillSymbolLayers. |
| 3 | +
|
| 4 | +.. note:: This program is free software; you can redistribute it and/or modify |
| 5 | +it under the terms of the GNU General Public License as published by |
| 6 | +the Free Software Foundation; either version 2 of the License, or |
| 7 | +(at your option) any later version. |
| 8 | +""" |
| 9 | +__author__ = 'Nyall Dawson' |
| 10 | +__date__ = '2017-01' |
| 11 | +__copyright__ = 'Copyright 2017, The QGIS Project' |
| 12 | +# This will get replaced with a git SHA1 when you do a git archive |
| 13 | +__revision__ = '$Format:%H$' |
| 14 | + |
| 15 | + |
| 16 | +import qgis # NOQA |
| 17 | + |
| 18 | +from qgis.testing import unittest |
| 19 | +from qgis.PyQt.QtCore import QDir |
| 20 | +from qgis.PyQt.QtGui import (QImage, |
| 21 | + QPainter, |
| 22 | + QColor) |
| 23 | +from qgis.core import (QgsRenderChecker, |
| 24 | + QgsSimpleLineSymbolLayer, |
| 25 | + QgsMapSettings, |
| 26 | + QgsFillSymbol, |
| 27 | + QgsGeometry, |
| 28 | + QgsFeature, |
| 29 | + QgsRenderContext) |
| 30 | + |
| 31 | + |
| 32 | +class TestQgsFillSymbolLayers(unittest.TestCase): |
| 33 | + |
| 34 | + def setUp(self): |
| 35 | + self.report = "<h1>Python QgsFillSymbolLayer Tests</h1>\n" |
| 36 | + |
| 37 | + def tearDown(self): |
| 38 | + report_file_path = "%s/qgistest.html" % QDir.tempPath() |
| 39 | + with open(report_file_path, 'a') as report_file: |
| 40 | + report_file.write(self.report) |
| 41 | + |
| 42 | + def imageCheck(self, name, reference_image, image): |
| 43 | + self.report += "<h2>Render {}</h2>\n".format(name) |
| 44 | + temp_dir = QDir.tempPath() + '/' |
| 45 | + file_name = temp_dir + 'symbollayer_' + name + ".png" |
| 46 | + image.save(file_name, "PNG") |
| 47 | + checker = QgsRenderChecker() |
| 48 | + checker.setControlPathPrefix("symbol_layer") |
| 49 | + checker.setControlName("expected_" + reference_image) |
| 50 | + checker.setRenderedImage(file_name) |
| 51 | + checker.setColorTolerance(2) |
| 52 | + result = checker.compareImages(name, 0) |
| 53 | + self.report += checker.report() |
| 54 | + print((self.report)) |
| 55 | + return result |
| 56 | + |
| 57 | + def testSimpleLineWithOffset(self): |
| 58 | + """ test that rendering a polygon with simple line symbol with offset results in closed line""" |
| 59 | + layer = QgsSimpleLineSymbolLayer() |
| 60 | + layer.setOffset(-1) |
| 61 | + |
| 62 | + symbol = QgsFillSymbol() |
| 63 | + symbol.changeSymbolLayer(0, layer) |
| 64 | + |
| 65 | + image = QImage(200, 200, QImage.Format_RGB32) |
| 66 | + painter = QPainter() |
| 67 | + ms = QgsMapSettings() |
| 68 | + |
| 69 | + geom = QgsGeometry.fromWkt('Polygon((0 0, 10 0, 10 10, 0 10, 0 0))') |
| 70 | + f = QgsFeature() |
| 71 | + f.setGeometry(geom) |
| 72 | + |
| 73 | + extent = geom.geometry().boundingBox() |
| 74 | + # buffer extent by 10% |
| 75 | + extent = extent.buffer((extent.height() + extent.width()) / 20.0) |
| 76 | + |
| 77 | + ms.setExtent(extent) |
| 78 | + ms.setOutputSize(image.size()) |
| 79 | + context = QgsRenderContext.fromMapSettings(ms) |
| 80 | + context.setPainter(painter) |
| 81 | + context.setScaleFactor(96 / 25.4) # 96 DPI |
| 82 | + |
| 83 | + painter.begin(image) |
| 84 | + image.fill(QColor(255, 255, 255)) |
| 85 | + |
| 86 | + symbol.startRender(context) |
| 87 | + symbol.renderFeature(f, context) |
| 88 | + symbol.stopRender(context) |
| 89 | + painter.end() |
| 90 | + |
| 91 | + self.assertTrue(self.imageCheck('symbol_layer', 'fill_simpleline_offset', image)) |
| 92 | + |
| 93 | +if __name__ == '__main__': |
| 94 | + unittest.main() |
0 commit comments