Skip to content

Commit bee5f80

Browse files
authored
Merge pull request #4039 from rldhont/sld-backport-release-2_14-parsing
SLD parsing: handling ogc:Literal within CssParameter - backport 2.14
2 parents dd54688 + e6ec34a commit bee5f80

File tree

4 files changed

+140
-1
lines changed

4 files changed

+140
-1
lines changed

src/core/symbology-ng/qgssymbollayerv2utils.cpp

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2655,14 +2655,31 @@ QDomElement QgsSymbolLayerV2Utils::createSvgParameterElement( QDomDocument &doc,
26552655
QgsStringMap QgsSymbolLayerV2Utils::getSvgParameterList( QDomElement &element )
26562656
{
26572657
QgsStringMap params;
2658+
QString value;
26582659

26592660
QDomElement paramElem = element.firstChildElement();
26602661
while ( !paramElem.isNull() )
26612662
{
26622663
if ( paramElem.localName() == "SvgParameter" || paramElem.localName() == "CssParameter" )
26632664
{
26642665
QString name = paramElem.attribute( "name" );
2665-
QString value = paramElem.firstChild().nodeValue();
2666+
if ( paramElem.firstChild().nodeType() == QDomNode::TextNode )
2667+
{
2668+
value = paramElem.firstChild().nodeValue();
2669+
}
2670+
else
2671+
{
2672+
if ( paramElem.firstChild().nodeType() == QDomNode::ElementNode &&
2673+
paramElem.firstChild().localName() == "Literal" )
2674+
{
2675+
QgsDebugMsg( paramElem.firstChild().localName() );
2676+
value = paramElem.firstChild().firstChild().nodeValue();
2677+
}
2678+
else
2679+
{
2680+
QgsDebugMsg( QString( "unexpected child of %1" ).arg( paramElem.localName() ) );
2681+
}
2682+
}
26662683

26672684
if ( !name.isEmpty() && !value.isEmpty() )
26682685
params[ name ] = value;

tests/src/python/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ ADD_PYTHON_TEST(PyQgsSpatialIndex test_qgsspatialindex.py)
6767
ADD_PYTHON_TEST(PyQgsSpatialiteProvider test_provider_spatialite.py)
6868
ADD_PYTHON_TEST(PyQgsSymbolLayerV2 test_qgssymbollayerv2.py)
6969
ADD_PYTHON_TEST(PyQgsSymbolLayerV2CreateSld test_qgssymbollayerv2_createsld.py)
70+
ADD_PYTHON_TEST(PyQgsSymbolLayerReadSld test_qgssymbollayerv2_readsld.py)
7071
ADD_PYTHON_TEST(PyQgsSymbolExpressionVariables test_qgssymbolexpressionvariables.py)
7172
ADD_PYTHON_TEST(PyQgsSyntacticSugar test_syntactic_sugar.py)
7273
ADD_PYTHON_TEST(PyQgsSymbolV2 test_qgssymbolv2.py)
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
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+
44+
def createLayerWithOneLine():
45+
# create a temporary layer
46+
# linelayer = iface.addVectorLayer("LineString?crs=epsg:4326&field=gid:int&field=name:string", "simple_line", "memory")
47+
linelayer = QgsVectorLayer("LineString?crs=epsg:4326&field=gid:int&field=name:string", "simple_line", "memory")
48+
one = QgsFeature(linelayer.dataProvider().fields(), 0)
49+
one.setAttributes([1, 'one'])
50+
one.setGeometry(QgsGeometry.fromPolyline([QgsPoint(-7, 38), QgsPoint(-8, 42)]))
51+
linelayer.dataProvider().addFeatures([one])
52+
return linelayer
53+
54+
55+
class TestQgsSymbolLayerReadSld(unittest.TestCase):
56+
57+
"""
58+
This class checks if SLD styles are properly applied
59+
"""
60+
61+
def setUp(self):
62+
self.iface = get_iface()
63+
64+
# test <CSSParameter>VALUE<CSSParameter/>
65+
# test <CSSParameter><ogc:Literal>VALUE<ogc:Literal/><CSSParameter/>
66+
def test_Literal_within_CSSParameter(self):
67+
layer = createLayerWithOneLine()
68+
mFilePath = os.path.join(TEST_DATA_DIR, 'symbol_layer/external_sld/simple_streams.sld')
69+
layer.loadSldStyle(mFilePath)
70+
props = layer.rendererV2().symbol().symbolLayers()[0].properties()
71+
72+
def testLineColor():
73+
# stroke CSSParameter within ogc:Literal
74+
# expected color is #003EBA, RGB 0,62,186
75+
self.assertEqual(layer.rendererV2().symbol().symbolLayers()[0].color().name(), '#003eba')
76+
77+
def testLineWidth():
78+
# stroke-width CSSParameter within ogc:Literal
79+
self.assertEqual(props['line_width'], '2')
80+
81+
def testLineOpacity():
82+
# stroke-opacity CSSParameter NOT within ogc:Literal
83+
# stroke-opacity=0.1
84+
self.assertEqual(props['line_color'], '0,62,186,25')
85+
86+
testLineColor()
87+
testLineWidth()
88+
testLineOpacity()
89+
90+
if __name__ == '__main__':
91+
unittest.main()
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<StyledLayerDescriptor version="1.0.0" xmlns="http://www.opengis.net/sld" xmlns:ogc="http://www.opengis.net/ogc"
3+
xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://www.opengis.net/sld http://schemas.opengis.net/sld/1.0.0/StyledLayerDescriptor.xsd">
5+
<NamedLayer>
6+
<Name>Simple Streams</Name>
7+
<UserStyle>
8+
9+
<Title>Default Styler for streams segments</Title>
10+
<Abstract>Blue lines, 2px wide</Abstract>
11+
<FeatureTypeStyle>
12+
<FeatureTypeName>Feature</FeatureTypeName>
13+
<Rule>
14+
<Title>Streams</Title>
15+
<LineSymbolizer>
16+
<Stroke>
17+
<CssParameter name="stroke">
18+
<ogc:Literal>#003EBA</ogc:Literal>
19+
</CssParameter>
20+
<CssParameter name="stroke-width">
21+
<ogc:Literal>2</ogc:Literal>
22+
</CssParameter>
23+
<CssParameter name="stroke-opacity">0.1</CssParameter>
24+
</Stroke>
25+
</LineSymbolizer>
26+
</Rule>
27+
</FeatureTypeStyle>
28+
</UserStyle>
29+
</NamedLayer>
30+
</StyledLayerDescriptor>

0 commit comments

Comments
 (0)