Skip to content

Commit

Permalink
Add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
pblottiere committed May 16, 2017
1 parent e2ee153 commit 0c72d4b
Show file tree
Hide file tree
Showing 6 changed files with 377 additions and 1 deletion.
294 changes: 293 additions & 1 deletion tests/src/python/test_qgssymbollayer_readsld.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,23 @@
import qgis # NOQA

import os
from qgis.PyQt.QtXml import QDomDocument
from qgis.testing import start_app, unittest
from qgis.core import (QgsVectorLayer,
QgsFeature,
QgsGeometry,
QgsPoint
QgsUnitTypes,
QgsPoint,
QgsSvgMarkerSymbolLayer,
QgsEllipseSymbolLayer,
QgsSimpleFillSymbolLayer,
QgsSVGFillSymbolLayer,
QgsSvgMarkerSymbolLayer,
QgsLinePatternFillSymbolLayer,
QgsSimpleLineSymbolLayer,
QgsMarkerLineSymbolLayer,
QgsSimpleMarkerSymbolLayer,
QgsFontMarkerSymbolLayer
)
from qgis.testing.mocked import get_iface
from utilities import unitTestDataPath
Expand All @@ -51,6 +63,28 @@ def createLayerWithOneLine():
return linelayer


def createLayerWithOnePoint():
layer = QgsVectorLayer("Point?field=fldtxt:string&field=fldint:integer",
"addfeat", "memory")
pr = layer.dataProvider()
f = QgsFeature()
f.setAttributes(["test", 123])
f.setGeometry(QgsGeometry.fromPoint(QgsPoint(100, 200)))
assert pr.addFeatures([f])
assert layer.pendingFeatureCount() == 1
return layer


def createLayerWithOnePolygon():
layer = QgsVectorLayer("Polygon?crs=epsg:3111&field=pk:int", "vl", "memory")
assert layer.isValid()
f1 = QgsFeature(layer.dataProvider().fields(), 1)
f1.setAttribute("pk", 1)
f1.setGeometry(QgsGeometry.fromPolygon([[QgsPoint(2484588, 2425722), QgsPoint(2482767, 2398853), QgsPoint(2520109, 2397715), QgsPoint(2520792, 2425494), QgsPoint(2484588, 2425722)]]))
assert layer.dataProvider().addFeatures([f1])
return layer


class TestQgsSymbolLayerReadSld(unittest.TestCase):

"""
Expand Down Expand Up @@ -111,6 +145,264 @@ def testSimpleMarkerRotation(self):
props = layer.renderer().symbol().symbolLayers()[0].properties()
self.assertEqual(props['angle'], '50')

def testSymbolSizeUom(self):
# create a layer
layer = createLayerWithOnePoint()

# load a sld with marker size without uom attribute (pixels)
sld = 'symbol_layer/QgsSvgMarkerSymbolLayer.sld'
mFilePath = os.path.join(TEST_DATA_DIR, sld)
layer.loadSldStyle(mFilePath)

sld_size_px = 12

sl = layer.renderer().symbol().symbolLayers()[0]
size = sl.size()
unit = sl.outputUnit()
self.assertEqual(unit, QgsUnitTypes.RenderPixels)
self.assertEqual(size, sld_size_px)

# load a sld with marker size with uom attribute in pixel
sld = 'symbol_layer/QgsSvgMarkerSymbolLayerUomPixel.sld'
mFilePath = os.path.join(TEST_DATA_DIR, sld)
layer.loadSldStyle(mFilePath)

sld_size_px = 12

sl = layer.renderer().symbol().symbolLayers()[0]
size = sl.size()
unit = sl.outputUnit()
self.assertEqual(unit, QgsUnitTypes.RenderPixels)
self.assertEqual(size, sld_size_px)

# load a sld with marker size with uom attribute in meter
sld = 'symbol_layer/QgsSvgMarkerSymbolLayerUomMetre.sld'
mFilePath = os.path.join(TEST_DATA_DIR, sld)
layer.loadSldStyle(mFilePath)

sld_size_px = 12 / (0.28 * 0.001)

sl = layer.renderer().symbol().symbolLayers()[0]
size = sl.size()
unit = sl.outputUnit()
self.assertEqual(unit, QgsUnitTypes.RenderPixels)
self.assertAlmostEqual(size, sld_size_px, delta=0.1)

# load a sld with marker size with uom attribute in foot
sld = 'symbol_layer/QgsSvgMarkerSymbolLayerUomFoot.sld'
mFilePath = os.path.join(TEST_DATA_DIR, sld)
layer.loadSldStyle(mFilePath)

sld_size_px = 12 * (304.8 / 0.28)

sl = layer.renderer().symbol().symbolLayers()[0]
size = sl.size()
unit = sl.outputUnit()
self.assertEqual(unit, QgsUnitTypes.RenderPixels)
self.assertAlmostEqual(size, sld_size_px, delta=0.1)

def testSymbolSize(self):
# create a layers
layer = createLayerWithOnePoint()
player = createLayerWithOnePolygon()

# size test for QgsEllipseSymbolLayer
sld = 'symbol_layer/QgsEllipseSymbolLayer.sld'
mFilePath = os.path.join(TEST_DATA_DIR, sld)
layer.loadSldStyle(mFilePath)

sld_size_px = 7
sld_stroke_width_px = 1

sl = layer.renderer().symbol().symbolLayers()[0]
size = sl.symbolWidth()
stroke_width = sl.strokeWidth()
unit = sl.outputUnit()
self.assertTrue(isinstance(sl, QgsEllipseSymbolLayer))
self.assertEqual(unit, QgsUnitTypes.RenderPixels)
self.assertEqual(size, sld_size_px)
self.assertEqual(stroke_width, sld_stroke_width_px)

# size test for QgsVectorFieldSymbolLayer
# createFromSld not implemented

# size test for QgsSimpleFillSymbolLayer
sld = 'symbol_layer/QgsSimpleFillSymbolLayer.sld'
mFilePath = os.path.join(TEST_DATA_DIR, sld)
player.loadSldStyle(mFilePath)

sld_stroke_width_px = 0.26

sl = player.renderer().symbol().symbolLayers()[0]
stroke_width = sl.strokeWidth()
unit = sl.outputUnit()
self.assertTrue(isinstance(sl, QgsSimpleFillSymbolLayer))
self.assertEqual(unit, QgsUnitTypes.RenderPixels)
self.assertEqual(stroke_width, sld_stroke_width_px)

# size test for QgsSVGFillSymbolLayer
sld = 'symbol_layer/QgsSVGFillSymbolLayer.sld'
mFilePath = os.path.join(TEST_DATA_DIR, sld)
player.loadSldStyle(mFilePath)

sld_size_px = 6
sld_stroke_width_px = 3

sl = player.renderer().symbol().symbolLayers()[0]
size = sl.patternWidth()
stroke_width = sl.svgStrokeWidth()
unit = sl.outputUnit()
self.assertTrue(isinstance(sl, QgsSVGFillSymbolLayer))
self.assertEqual(unit, QgsUnitTypes.RenderPixels)
self.assertEqual(size, sld_size_px)
self.assertEqual(stroke_width, sld_stroke_width_px)

# size test for QgsSvgMarkerSymbolLayer
sld = 'symbol_layer/QgsSvgMarkerSymbolLayer.sld'
mFilePath = os.path.join(TEST_DATA_DIR, sld)
layer.loadSldStyle(mFilePath)

sld_size_px = 12

sl = layer.renderer().symbol().symbolLayers()[0]
size = sl.size()
unit = sl.outputUnit()
self.assertTrue(isinstance(sl, QgsSvgMarkerSymbolLayer))
self.assertEqual(unit, QgsUnitTypes.RenderPixels)
self.assertEqual(size, sld_size_px)

# size test for QgsPointPatternFillSymbolLayer
# createFromSld not implemented

# size test for QgsLinePatternFillSymbolLayer
sld = 'symbol_layer/QgsLinePatternFillSymbolLayer.sld'
mFilePath = os.path.join(TEST_DATA_DIR, sld)
player.loadSldStyle(mFilePath)

sld_size_px = 4
sld_stroke_width_px = 1.5

sl = player.renderer().symbol().symbolLayers()[0]
size = sl.distance()
stroke_width = sl.lineWidth()
unit = sl.outputUnit()
self.assertTrue(isinstance(sl, QgsLinePatternFillSymbolLayer))
self.assertEqual(unit, QgsUnitTypes.RenderPixels)
self.assertEqual(size, sld_size_px)
self.assertEqual(stroke_width, sld_stroke_width_px)

# test size for QgsSimpleLineSymbolLayer
sld = 'symbol_layer/QgsSimpleLineSymbolLayer.sld'
mFilePath = os.path.join(TEST_DATA_DIR, sld)
player.loadSldStyle(mFilePath)

sld_stroke_width_px = 1.26

sl = player.renderer().symbol().symbolLayers()[0]
stroke_width = sl.width()
unit = sl.outputUnit()
self.assertTrue(isinstance(sl, QgsSimpleLineSymbolLayer))
self.assertEqual(unit, QgsUnitTypes.RenderPixels)
self.assertEqual(stroke_width, sld_stroke_width_px)

# test size for QgsMarkerLineSymbolLayer
sld = 'symbol_layer/QgsMarkerLineSymbolLayer.sld'
mFilePath = os.path.join(TEST_DATA_DIR, sld)
player.loadSldStyle(mFilePath)

sld_interval_px = 3.3
sld_offset_px = 6.6

sl = player.renderer().symbol().symbolLayers()[0]
interval = sl.interval()
offset = sl.offset()
unit = sl.outputUnit()
self.assertTrue(isinstance(sl, QgsMarkerLineSymbolLayer))
self.assertEqual(unit, QgsUnitTypes.RenderPixels)
self.assertEqual(interval, sld_interval_px)
self.assertEqual(offset, sld_offset_px)

# test size for QgsSimpleMarkerSymbolLayer
sld = 'symbol_layer/QgsSimpleMarkerSymbolLayer.sld'
mFilePath = os.path.join(TEST_DATA_DIR, sld)
layer.loadSldStyle(mFilePath)

sld_size_px = 6
sld_displacement_x_px = 3.3
sld_displacement_y_px = 6.6

sl = layer.renderer().symbol().symbolLayers()[0]
size = sl.size()
offset = sl.offset()
unit = sl.outputUnit()
self.assertTrue(isinstance(sl, QgsSimpleMarkerSymbolLayer))
self.assertEqual(unit, QgsUnitTypes.RenderPixels)
self.assertEqual(size, sld_size_px)
self.assertEqual(offset.x(), sld_displacement_x_px)
self.assertEqual(offset.y(), sld_displacement_y_px)

# test size for QgsSVGMarkerSymbolLayer
sld = 'symbol_layer/QgsSvgMarkerSymbolLayer.sld'
mFilePath = os.path.join(TEST_DATA_DIR, sld)
layer.loadSldStyle(mFilePath)

sld_size_px = 12

sl = layer.renderer().symbol().symbolLayers()[0]
size = sl.size()
self.assertTrue(isinstance(sl, QgsSvgMarkerSymbolLayer))
self.assertEqual(unit, QgsUnitTypes.RenderPixels)
self.assertEqual(size, sld_size_px)

# test size for QgsFontMarkerSymbolLayer
sld = 'symbol_layer/QgsFontMarkerSymbolLayer.sld'
mFilePath = os.path.join(TEST_DATA_DIR, sld)
layer.loadSldStyle(mFilePath)

sld_size_px = 6.23

sl = layer.renderer().symbol().symbolLayers()[0]
size = sl.size()
self.assertTrue(isinstance(sl, QgsFontMarkerSymbolLayer))
self.assertEqual(unit, QgsUnitTypes.RenderPixels)
self.assertEqual(size, sld_size_px)

def testSymbolSizeAfterReload(self):
# create a layer
layer = createLayerWithOnePoint()

# load a sld with marker size
sld = 'symbol_layer/QgsSvgMarkerSymbolLayer.sld'
mFilePath = os.path.join(TEST_DATA_DIR, sld)
layer.loadSldStyle(mFilePath)

# get the size and unit of the symbol
sl = layer.renderer().symbol().symbolLayers()[0]
first_size = sl.size()
first_unit = sl.outputUnit() # in pixels

# export sld into a qdomdocument with namespace processing activated
doc = QDomDocument()
msg = ""
layer.exportSldStyle(doc, msg)
doc.setContent(doc.toString(), True)
self.assertTrue(msg == "")

# reload the same sld
root = doc.firstChildElement("StyledLayerDescriptor")
el = root.firstChildElement("NamedLayer")
layer.readSld(el, msg)

# extract the size and unit of symbol
sl = layer.renderer().symbol().symbolLayers()[0]
second_size = sl.size()
second_unit = sl.outputUnit()

# size and unit should be the same after export and reload the same
# sld description
self.assertEqual(first_size, second_size)
self.assertEqual(first_unit, second_unit)


if __name__ == '__main__':
unittest.main()
2 changes: 2 additions & 0 deletions tests/testdata/symbol_layer/QgsMarkerLineSymbolLayer.sld
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
<VendorOption name="placement">centralPoint</VendorOption>
<se:Stroke>
<se:GraphicStroke>
<se:Gap>3.3</se:Gap>
<se:PerpendicularOffset>6.6</se:PerpendicularOffset>
<se:Graphic>
<se:Mark>
<se:WellKnownName>circle</se:WellKnownName>
Expand Down
4 changes: 4 additions & 0 deletions tests/testdata/symbol_layer/QgsSimpleMarkerSymbolLayer.sld
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
<se:Name>Single symbol</se:Name>
<se:PointSymbolizer>
<se:Graphic>
<se:Displacement>
<se:DisplacementX>3.3</se:DisplacementX>
<se:DisplacementY>6.6</se:DisplacementY>
</se:Displacement>
<se:Mark>
<se:WellKnownName>pentagon</se:WellKnownName>
<se:Fill>
Expand Down
26 changes: 26 additions & 0 deletions tests/testdata/symbol_layer/QgsSvgMarkerSymbolLayerUomFoot.sld
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?xml version="1.0" encoding="UTF-8"?>
<StyledLayerDescriptor xmlns="http://www.opengis.net/sld" xmlns:ogc="http://www.opengis.net/ogc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1.1.0" xmlns:xlink="http://www.w3.org/1999/xlink" xsi:schemaLocation="http://www.opengis.net/sld http://schemas.opengis.net/sld/1.1.0/StyledLayerDescriptor.xsd" xmlns:se="http://www.opengis.net/se">
<NamedLayer>
<se:Name>022cs000</se:Name>
<UserStyle>
<se:Name>022cs000</se:Name>
<se:FeatureTypeStyle>
<se:Rule>
<se:Name>Single symbol</se:Name>
<se:PointSymbolizer uom="http://www.opengeospatial.org/se/units/foot">
<se:Graphic>
<se:ExternalGraphic>
<OnlineResource xlink:type="simple" xlink:href="file:///gpsicons/skull.svg"/>
<Format>image/svg+xml</Format>
</se:ExternalGraphic>
<se:Size>12</se:Size>
<se:Rotation>
<ogc:Literal>45</ogc:Literal>
</se:Rotation>
</se:Graphic>
</se:PointSymbolizer>
</se:Rule>
</se:FeatureTypeStyle>
</UserStyle>
</NamedLayer>
</StyledLayerDescriptor>
26 changes: 26 additions & 0 deletions tests/testdata/symbol_layer/QgsSvgMarkerSymbolLayerUomMetre.sld
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?xml version="1.0" encoding="UTF-8"?>
<StyledLayerDescriptor xmlns="http://www.opengis.net/sld" xmlns:ogc="http://www.opengis.net/ogc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1.1.0" xmlns:xlink="http://www.w3.org/1999/xlink" xsi:schemaLocation="http://www.opengis.net/sld http://schemas.opengis.net/sld/1.1.0/StyledLayerDescriptor.xsd" xmlns:se="http://www.opengis.net/se">
<NamedLayer>
<se:Name>022cs000</se:Name>
<UserStyle>
<se:Name>022cs000</se:Name>
<se:FeatureTypeStyle>
<se:Rule>
<se:Name>Single symbol</se:Name>
<se:PointSymbolizer uom="http://www.opengeospatial.org/se/units/metre">
<se:Graphic>
<se:ExternalGraphic>
<OnlineResource xlink:type="simple" xlink:href="file:///gpsicons/skull.svg"/>
<Format>image/svg+xml</Format>
</se:ExternalGraphic>
<se:Size>12</se:Size>
<se:Rotation>
<ogc:Literal>45</ogc:Literal>
</se:Rotation>
</se:Graphic>
</se:PointSymbolizer>
</se:Rule>
</se:FeatureTypeStyle>
</UserStyle>
</NamedLayer>
</StyledLayerDescriptor>
Loading

0 comments on commit 0c72d4b

Please sign in to comment.