Skip to content
Permalink
Browse files
Merge pull request #4039 from rldhont/sld-backport-release-2_14-parsing
SLD parsing: handling ogc:Literal within CssParameter - backport 2.14
  • Loading branch information
rldhont committed Jan 23, 2017
2 parents dd54688 + e6ec34a commit bee5f8063794cb7e5fc48b5e0562c20de3249d74
@@ -2655,14 +2655,31 @@ QDomElement QgsSymbolLayerV2Utils::createSvgParameterElement( QDomDocument &doc,
QgsStringMap QgsSymbolLayerV2Utils::getSvgParameterList( QDomElement &element )
{
QgsStringMap params;
QString value;

QDomElement paramElem = element.firstChildElement();
while ( !paramElem.isNull() )
{
if ( paramElem.localName() == "SvgParameter" || paramElem.localName() == "CssParameter" )
{
QString name = paramElem.attribute( "name" );
QString value = paramElem.firstChild().nodeValue();
if ( paramElem.firstChild().nodeType() == QDomNode::TextNode )
{
value = paramElem.firstChild().nodeValue();
}
else
{
if ( paramElem.firstChild().nodeType() == QDomNode::ElementNode &&
paramElem.firstChild().localName() == "Literal" )
{
QgsDebugMsg( paramElem.firstChild().localName() );
value = paramElem.firstChild().firstChild().nodeValue();
}
else
{
QgsDebugMsg( QString( "unexpected child of %1" ).arg( paramElem.localName() ) );
}
}

if ( !name.isEmpty() && !value.isEmpty() )
params[ name ] = value;
@@ -67,6 +67,7 @@ ADD_PYTHON_TEST(PyQgsSpatialIndex test_qgsspatialindex.py)
ADD_PYTHON_TEST(PyQgsSpatialiteProvider test_provider_spatialite.py)
ADD_PYTHON_TEST(PyQgsSymbolLayerV2 test_qgssymbollayerv2.py)
ADD_PYTHON_TEST(PyQgsSymbolLayerV2CreateSld test_qgssymbollayerv2_createsld.py)
ADD_PYTHON_TEST(PyQgsSymbolLayerReadSld test_qgssymbollayerv2_readsld.py)
ADD_PYTHON_TEST(PyQgsSymbolExpressionVariables test_qgssymbolexpressionvariables.py)
ADD_PYTHON_TEST(PyQgsSyntacticSugar test_syntactic_sugar.py)
ADD_PYTHON_TEST(PyQgsSymbolV2 test_qgssymbolv2.py)
@@ -0,0 +1,91 @@
# -*- coding: utf-8 -*-

"""
***************************************************************************
test_qgssymbollayer_readsld.py
---------------------
Date : January 2017
Copyright : (C) 2017, Jorge Gustavo Rocha
Email : jgr at di dot uminho dot pt
***************************************************************************
* *
* 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__ = 'Jorge Gustavo Rocha'
__date__ = 'January 2017'
__copyright__ = '(C) 2017, Jorge Gustavo Rocha'
# This will get replaced with a git SHA1 when you do a git archive
__revision__ = '$Format:%H$'

import qgis # NOQA

import os
from qgis.testing import start_app, unittest
from qgis.core import (QgsVectorLayer,
QgsFeature,
QgsGeometry,
QgsPoint
)
from qgis.testing import unittest
from qgis.testing.mocked import get_iface
from utilities import unitTestDataPath

start_app()

TEST_DATA_DIR = unitTestDataPath()


def createLayerWithOneLine():
# create a temporary layer
# linelayer = iface.addVectorLayer("LineString?crs=epsg:4326&field=gid:int&field=name:string", "simple_line", "memory")
linelayer = QgsVectorLayer("LineString?crs=epsg:4326&field=gid:int&field=name:string", "simple_line", "memory")
one = QgsFeature(linelayer.dataProvider().fields(), 0)
one.setAttributes([1, 'one'])
one.setGeometry(QgsGeometry.fromPolyline([QgsPoint(-7, 38), QgsPoint(-8, 42)]))
linelayer.dataProvider().addFeatures([one])
return linelayer


class TestQgsSymbolLayerReadSld(unittest.TestCase):

"""
This class checks if SLD styles are properly applied
"""

def setUp(self):
self.iface = get_iface()

# test <CSSParameter>VALUE<CSSParameter/>
# test <CSSParameter><ogc:Literal>VALUE<ogc:Literal/><CSSParameter/>
def test_Literal_within_CSSParameter(self):
layer = createLayerWithOneLine()
mFilePath = os.path.join(TEST_DATA_DIR, 'symbol_layer/external_sld/simple_streams.sld')
layer.loadSldStyle(mFilePath)
props = layer.rendererV2().symbol().symbolLayers()[0].properties()

def testLineColor():
# stroke CSSParameter within ogc:Literal
# expected color is #003EBA, RGB 0,62,186
self.assertEqual(layer.rendererV2().symbol().symbolLayers()[0].color().name(), '#003eba')

def testLineWidth():
# stroke-width CSSParameter within ogc:Literal
self.assertEqual(props['line_width'], '2')

def testLineOpacity():
# stroke-opacity CSSParameter NOT within ogc:Literal
# stroke-opacity=0.1
self.assertEqual(props['line_color'], '0,62,186,25')

testLineColor()
testLineWidth()
testLineOpacity()

if __name__ == '__main__':
unittest.main()
@@ -0,0 +1,30 @@
<?xml version="1.0" encoding="UTF-8"?>
<StyledLayerDescriptor version="1.0.0" xmlns="http://www.opengis.net/sld" xmlns:ogc="http://www.opengis.net/ogc"
xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.opengis.net/sld http://schemas.opengis.net/sld/1.0.0/StyledLayerDescriptor.xsd">
<NamedLayer>
<Name>Simple Streams</Name>
<UserStyle>

<Title>Default Styler for streams segments</Title>
<Abstract>Blue lines, 2px wide</Abstract>
<FeatureTypeStyle>
<FeatureTypeName>Feature</FeatureTypeName>
<Rule>
<Title>Streams</Title>
<LineSymbolizer>
<Stroke>
<CssParameter name="stroke">
<ogc:Literal>#003EBA</ogc:Literal>
</CssParameter>
<CssParameter name="stroke-width">
<ogc:Literal>2</ogc:Literal>
</CssParameter>
<CssParameter name="stroke-opacity">0.1</CssParameter>
</Stroke>
</LineSymbolizer>
</Rule>
</FeatureTypeStyle>
</UserStyle>
</NamedLayer>
</StyledLayerDescriptor>

0 comments on commit bee5f80

Please sign in to comment.