Skip to content

Commit ad963e6

Browse files
authored
Merge pull request #3996 from jgrocha/literal
SLD parsing: handling ogc:Literal within CssParameter
2 parents 2bedaf8 + 65c0177 commit ad963e6

File tree

4 files changed

+138
-1
lines changed

4 files changed

+138
-1
lines changed

src/core/symbology-ng/qgssymbollayerutils.cpp

+18-1
Original file line numberDiff line numberDiff line change
@@ -2566,14 +2566,31 @@ QDomElement QgsSymbolLayerUtils::createSvgParameterElement( QDomDocument &doc, c
25662566
QgsStringMap QgsSymbolLayerUtils::getSvgParameterList( QDomElement &element )
25672567
{
25682568
QgsStringMap params;
2569+
QString value;
25692570

25702571
QDomElement paramElem = element.firstChildElement();
25712572
while ( !paramElem.isNull() )
25722573
{
25732574
if ( paramElem.localName() == QLatin1String( "SvgParameter" ) || paramElem.localName() == QLatin1String( "CssParameter" ) )
25742575
{
25752576
QString name = paramElem.attribute( QStringLiteral( "name" ) );
2576-
QString value = paramElem.firstChild().nodeValue();
2577+
if ( paramElem.firstChild().nodeType() == QDomNode::TextNode )
2578+
{
2579+
value = paramElem.firstChild().nodeValue();
2580+
}
2581+
else
2582+
{
2583+
if ( paramElem.firstChild().nodeType() == QDomNode::ElementNode &&
2584+
paramElem.firstChild().localName() == QLatin1String( "Literal" ) )
2585+
{
2586+
QgsDebugMsg( paramElem.firstChild().localName() );
2587+
value = paramElem.firstChild().firstChild().nodeValue();
2588+
}
2589+
else
2590+
{
2591+
QgsDebugMsg( QString( "unexpected child of %1" ).arg( paramElem.localName() ) );
2592+
}
2593+
}
25772594

25782595
if ( !name.isEmpty() && !value.isEmpty() )
25792596
params[ name ] = value;

tests/src/python/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ ADD_PYTHON_TEST(PyQgsSQLStatement test_qgssqlstatement.py)
105105
ADD_PYTHON_TEST(PyQgsStringStatisticalSummary test_qgsstringstatisticalsummary.py)
106106
ADD_PYTHON_TEST(PyQgsSymbolLayer test_qgssymbollayer.py)
107107
ADD_PYTHON_TEST(PyQgsSymbolLayerCreateSld test_qgssymbollayer_createsld.py)
108+
ADD_PYTHON_TEST(PyQgsSymbolLayerReadSld test_qgssymbollayer_readsld.py)
108109
ADD_PYTHON_TEST(PyQgsArrowSymbolLayer test_qgsarrowsymbollayer.py)
109110
ADD_PYTHON_TEST(PyQgsSymbolExpressionVariables test_qgssymbolexpressionvariables.py)
110111
ADD_PYTHON_TEST(PyQgsSyntacticSugar test_syntactic_sugar.py)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
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()
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)