Skip to content
Permalink
Browse files

Merge pull request #33852 from Gustry/sld_pixels

Read SLD TextSymbolizer set units to pixels
  • Loading branch information
rldhont committed Jan 16, 2020
2 parents 95135f9 + abc62ad commit a26ddd2505b7257ee0931b9dbbfcbccf4614bb31
@@ -171,7 +171,7 @@ Encodes a render unit into an SLD unit of measure string.
.. seealso:: :py:func:`decodeSldUom`
%End

static QgsUnitTypes::RenderUnit decodeSldUom( const QString &str, double *scaleFactor );
static QgsUnitTypes::RenderUnit decodeSldUom( const QString &str, double *scaleFactor = 0 );
%Docstring
Decodes a SLD unit of measure string to a render unit.

@@ -4330,8 +4330,15 @@ bool QgsVectorLayer::readSldTextSymbolizer( const QDomNode &node, QgsPalLayerSet
return false;
}

QgsUnitTypes::RenderUnit sldUnitSize = QgsUnitTypes::RenderPixels;
if ( textSymbolizerElem.hasAttribute( QStringLiteral( "uom" ) ) )
{
sldUnitSize = QgsSymbolLayerUtils::decodeSldUom( textSymbolizerElem.attribute( QStringLiteral( "uom" ) ) );
}

QString fontFamily = QStringLiteral( "Sans-Serif" );
int fontPointSize = 10;
QgsUnitTypes::RenderUnit fontUnitSize = QgsUnitTypes::RenderPoints;
int fontWeight = -1;
bool fontItalic = false;
bool fontUnderline = false;
@@ -4358,7 +4365,10 @@ bool QgsVectorLayer::readSldTextSymbolizer( const QDomNode &node, QgsPalLayerSet
bool ok;
int fontSize = it.value().toInt( &ok );
if ( ok )
{
fontPointSize = fontSize;
fontUnitSize = sldUnitSize;
}
}
else if ( it.key() == QLatin1String( "font-weight" ) )
{
@@ -4377,6 +4387,7 @@ bool QgsVectorLayer::readSldTextSymbolizer( const QDomNode &node, QgsPalLayerSet
font.setUnderline( fontUnderline );
format.setFont( font );
format.setSize( fontPointSize );
format.setSizeUnit( fontUnitSize );

// Fill
QDomElement fillElem = textSymbolizerElem.firstChildElement( QStringLiteral( "Fill" ) );
@@ -4406,6 +4417,7 @@ bool QgsVectorLayer::readSldTextSymbolizer( const QDomNode &node, QgsPalLayerSet
if ( ok )
{
bufferSettings.setSize( bufferSize );
bufferSettings.setSizeUnit( sldUnitSize );
}
}

@@ -4441,6 +4453,7 @@ bool QgsVectorLayer::readSldTextSymbolizer( const QDomNode &node, QgsPalLayerSet
if ( ok )
{
settings.xOffset = xOffset;
settings.offsetUnits = sldUnitSize;
}
}
QDomElement displacementYElem = displacementElem.firstChildElement( QStringLiteral( "DisplacementY" ) );
@@ -4451,6 +4464,7 @@ bool QgsVectorLayer::readSldTextSymbolizer( const QDomNode &node, QgsPalLayerSet
if ( ok )
{
settings.yOffset = yOffset;
settings.offsetUnits = sldUnitSize;
}
}
}
@@ -4465,6 +4479,7 @@ bool QgsVectorLayer::readSldTextSymbolizer( const QDomNode &node, QgsPalLayerSet
if ( ok )
{
settings.xOffset = xOffset;
settings.offsetUnits = sldUnitSize;
}
}
QDomElement anchorPointYElem = anchorPointElem.firstChildElement( QStringLiteral( "AnchorPointY" ) );
@@ -4475,6 +4490,7 @@ bool QgsVectorLayer::readSldTextSymbolizer( const QDomNode &node, QgsPalLayerSet
if ( ok )
{
settings.yOffset = yOffset;
settings.offsetUnits = sldUnitSize;
}
}
}
@@ -638,6 +638,12 @@ QgsUnitTypes::RenderUnit QgsSymbolLayerUtils::decodeSldUom( const QString &str,
*scaleFactor = 304.8; // from feet to meters
return QgsUnitTypes::RenderMapUnits;
}
else if ( str == QLatin1String( "http://www.opengeospatial.org/se/units/pixel" ) )
{
if ( scaleFactor )
*scaleFactor = 1.0; // from pixels to pixels
return QgsUnitTypes::RenderPixels;
}

// pixel is the SLD default uom. The "standardized rendering pixel
// size" is defined to be 0.28mm x 0.28mm (millimeters).
@@ -191,7 +191,7 @@ class CORE_EXPORT QgsSymbolLayerUtils
* \returns matching render unit
* \see encodeSldUom()
*/
static QgsUnitTypes::RenderUnit decodeSldUom( const QString &str, double *scaleFactor );
static QgsUnitTypes::RenderUnit decodeSldUom( const QString &str, double *scaleFactor = nullptr );

/**
* Returns the size scaled in pixels according to the uom attribute.
@@ -442,10 +442,12 @@ def testLineOpacity():
self.assertFalse(font.italic())

self.assertEqual(format.size(), 18)
self.assertEqual(format.sizeUnit(), QgsUnitTypes.RenderPixels)

self.assertEqual(settings.placement, QgsPalLayerSettings.OverPoint)
self.assertEqual(settings.xOffset, 1)
self.assertEqual(settings.yOffset, 0)
self.assertEqual(settings.offsetUnits, QgsUnitTypes.RenderPixels)


if __name__ == '__main__':
@@ -14,7 +14,8 @@

from qgis.core import (QgsSymbolLayerUtils,
QgsMarkerSymbol,
QgsArrowSymbolLayer)
QgsArrowSymbolLayer,
QgsUnitTypes)
from qgis.PyQt.QtGui import QColor
from qgis.PyQt.QtCore import QSizeF, QPointF
from qgis.testing import unittest, start_app
@@ -210,6 +211,41 @@ def testSymbolToFromMimeData(self):
self.assertTrue(symbol2 is not None)
self.assertEqual(symbol2.color().name(), symbol.color().name())

def testEncodeSldUom(self):
"""
Test Encodes a SLD unit of measure string to a render unit
"""

# millimeter
encode = None
encode = QgsSymbolLayerUtils.encodeSldUom(QgsUnitTypes.RenderMillimeters)
self.assertTupleEqual(encode, ('', 3.571428571428571))

# mapunits
encode = None
encode = QgsSymbolLayerUtils.encodeSldUom(QgsUnitTypes.RenderMapUnits)
self.assertTupleEqual(encode, ('http://www.opengeospatial.org/se/units/metre', 0.001))

def testDecodeSldUom(self):
"""
Test Decodes a SLD unit of measure string to a render unit
"""

# meter
decode = None
decode = QgsSymbolLayerUtils.decodeSldUom("http://www.opengeospatial.org/se/units/metre")
self.assertEqual(decode, (QgsUnitTypes.RenderMapUnits, 1000.0))

# foot
decode = None
decode = QgsSymbolLayerUtils.decodeSldUom("http://www.opengeospatial.org/se/units/foot")
self.assertEqual(decode, (QgsUnitTypes.RenderMapUnits, 304.8))

# pixel
decode = None
decode = QgsSymbolLayerUtils.decodeSldUom("http://www.opengeospatial.org/se/units/pixel")
self.assertEqual(decode, (QgsUnitTypes.RenderPixels, 1.0))


if __name__ == '__main__':
unittest.main()

0 comments on commit a26ddd2

Please sign in to comment.
You can’t perform that action at this time.