|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | + |
| 3 | +""" |
| 4 | +*************************************************************************** |
| 5 | + test_qgssymbollayer_readsld.py |
| 6 | + --------------------- |
| 7 | + Date : January 2017 |
| 8 | + Copyright : (C) 2017, Jorge Gustavo Rocha |
| 9 | + Email : jgr at di dot uminho dot pt |
| 10 | +*************************************************************************** |
| 11 | +* * |
| 12 | +* This program is free software; you can redistribute it and/or modify * |
| 13 | +* it under the terms of the GNU General Public License as published by * |
| 14 | +* the Free Software Foundation; either version 2 of the License, or * |
| 15 | +* (at your option) any later version. * |
| 16 | +* * |
| 17 | +*************************************************************************** |
| 18 | +""" |
| 19 | + |
| 20 | +__author__ = 'Jorge Gustavo Rocha' |
| 21 | +__date__ = 'January 2017' |
| 22 | +__copyright__ = '(C) 2017, Jorge Gustavo Rocha' |
| 23 | +# This will get replaced with a git SHA1 when you do a git archive |
| 24 | +__revision__ = '$Format:%H$' |
| 25 | + |
| 26 | +import qgis # NOQA |
| 27 | + |
| 28 | +import os |
| 29 | +from qgis.testing import start_app, unittest |
| 30 | +from qgis.core import (QgsVectorLayer, |
| 31 | + QgsFeature, |
| 32 | + QgsGeometry, |
| 33 | + QgsPoint |
| 34 | + ) |
| 35 | +from qgis.testing import unittest |
| 36 | +from qgis.testing.mocked import get_iface |
| 37 | +from utilities import unitTestDataPath |
| 38 | + |
| 39 | +start_app() |
| 40 | + |
| 41 | +TEST_DATA_DIR = unitTestDataPath() |
| 42 | + |
| 43 | +def createLayerWithOneLine(): |
| 44 | + # create a temporary layer |
| 45 | + # linelayer = iface.addVectorLayer("LineString?crs=epsg:4326&field=gid:int&field=name:string", "simple_line", "memory") |
| 46 | + linelayer = QgsVectorLayer("LineString?crs=epsg:4326&field=gid:int&field=name:string", "simple_line", "memory") |
| 47 | + one = QgsFeature(linelayer.dataProvider().fields(), 0) |
| 48 | + one.setAttributes([1, 'one']) |
| 49 | + one.setGeometry(QgsGeometry.fromPolyline([QgsPoint(-7, 38), QgsPoint(-8, 42)])) |
| 50 | + linelayer.dataProvider().addFeatures([one]) |
| 51 | + return linelayer |
| 52 | + |
| 53 | +class TestQgsSymbolLayerReadSld(unittest.TestCase): |
| 54 | + |
| 55 | + """ |
| 56 | + This class checks if SLD styles are properly applied |
| 57 | + """ |
| 58 | + |
| 59 | + def setUp(self): |
| 60 | + self.iface = get_iface() |
| 61 | + |
| 62 | + # test <CSSParameter>VALUE<CSSParameter/> |
| 63 | + # test <CSSParameter><ogc:Literal>VALUE<ogc:Literal/><CSSParameter/> |
| 64 | + def test_Literal_within_CSSParameter(self): |
| 65 | + layer = createLayerWithOneLine() |
| 66 | + mFilePath = os.path.join(TEST_DATA_DIR, 'symbol_layer/external_sld/simple_streams.sld') |
| 67 | + layer.loadSldStyle(mFilePath) |
| 68 | + props = layer.renderer().symbol().symbolLayers()[0].properties() |
| 69 | + |
| 70 | + def testLineColor(): |
| 71 | + # stroke CSSParameter within ogc:Literal |
| 72 | + # expected color is #003EBA, RGB 0,62,186 |
| 73 | + self.assertEqual(layer.renderer().symbol().symbolLayers()[0].color().name(), '#003eba') |
| 74 | + |
| 75 | + def testLineWidth(): |
| 76 | + # stroke-width CSSParameter within ogc:Literal |
| 77 | + self.assertEqual(props['line_width'], '2') |
| 78 | + |
| 79 | + def testLineOpacity(): |
| 80 | + # stroke-opacity CSSParameter NOT within ogc:Literal |
| 81 | + # stroke-opacity=0.1 |
| 82 | + self.assertEqual(props['line_color'], '0,62,186,25') |
| 83 | + |
| 84 | + testLineColor() |
| 85 | + testLineWidth() |
| 86 | + testLineOpacity() |
| 87 | + |
| 88 | +if __name__ == '__main__': |
| 89 | + unittest.main() |
0 commit comments