Skip to content
Permalink
Browse files
Merge pull request #4432 from pblottiere/bugfix_sld
[bugfix] Fix unit of sizes when reading a SLD file (uom attribute from SLD 1.1 version is supported)
  • Loading branch information
Hugo Mercier committed May 19, 2017
2 parents fe2350c + 0c72d4b commit 0a82ab51fe726e1aeaf4ad10548af93757607e7a
@@ -90,6 +90,14 @@ class QgsSymbolLayerUtils
*/
static QgsUnitTypes::RenderUnit decodeSldUom( const QString &str, double *scaleFactor );

/** Returns the size scaled in pixels according to the uom attribute.
* \param uom The uom attribute from SLD 1.1 version
* \param size The original size
* \returns the size in pixels
* \since QGIS 3.0
*/
static double sizeInPixelsFromSldUom( const QString &uom, double size );

static QString encodeScaleMethod( QgsSymbol::ScaleMethod scaleMethod );
static QgsSymbol::ScaleMethod decodeScaleMethod( const QString &str );

@@ -451,6 +451,10 @@ QgsSymbolLayer *QgsEllipseSymbolLayer::createFromSld( QDomElement &element )
if ( !QgsSymbolLayerUtils::wellKnownMarkerFromSld( graphicElem, name, fillColor, strokeColor, strokeStyle, strokeWidth, size ) )
return nullptr;

QString uom = element.attribute( QStringLiteral( "uom" ), "" );
size = QgsSymbolLayerUtils::sizeInPixelsFromSldUom( uom, size );
strokeWidth = QgsSymbolLayerUtils::sizeInPixelsFromSldUom( uom, strokeWidth );

double angle = 0.0;
QString angleFunc;
if ( QgsSymbolLayerUtils::rotationFromSldElement( graphicElem, angleFunc ) )
@@ -462,6 +466,7 @@ QgsSymbolLayer *QgsEllipseSymbolLayer::createFromSld( QDomElement &element )
}

QgsEllipseSymbolLayer *m = new QgsEllipseSymbolLayer();
m->setOutputUnit( QgsUnitTypes::RenderUnit::RenderPixels );
m->setSymbolName( name );
m->setFillColor( fillColor );
m->setStrokeColor( strokeColor );
@@ -381,7 +381,13 @@ QgsSymbolLayer *QgsSimpleFillSymbolLayer::createFromSld( QDomElement &element )
QPointF offset;
QgsSymbolLayerUtils::displacementFromSldElement( element, offset );

QString uom = element.attribute( QStringLiteral( "uom" ), "" );
offset.setX( QgsSymbolLayerUtils::sizeInPixelsFromSldUom( uom, offset.x() ) );
offset.setY( QgsSymbolLayerUtils::sizeInPixelsFromSldUom( uom, offset.y() ) );
strokeWidth = QgsSymbolLayerUtils::sizeInPixelsFromSldUom( uom, strokeWidth );

QgsSimpleFillSymbolLayer *sl = new QgsSimpleFillSymbolLayer( color, fillStyle, strokeColor, strokeStyle, strokeWidth );
sl->setOutputUnit( QgsUnitTypes::RenderUnit::RenderPixels );
sl->setOffset( offset );
return sl;
}
@@ -2134,6 +2140,10 @@ QgsSymbolLayer *QgsSVGFillSymbolLayer::createFromSld( QDomElement &element )

QgsSymbolLayerUtils::lineFromSld( graphicElem, penStyle, strokeColor, strokeWidth );

QString uom = element.attribute( QStringLiteral( "uom" ), "" );
size = QgsSymbolLayerUtils::sizeInPixelsFromSldUom( uom, size );
strokeWidth = QgsSymbolLayerUtils::sizeInPixelsFromSldUom( uom, strokeWidth );

double angle = 0.0;
QString angleFunc;
if ( QgsSymbolLayerUtils::rotationFromSldElement( graphicElem, angleFunc ) )
@@ -2145,6 +2155,7 @@ QgsSymbolLayer *QgsSVGFillSymbolLayer::createFromSld( QDomElement &element )
}

QgsSVGFillSymbolLayer *sl = new QgsSVGFillSymbolLayer( path, size, angle );
sl->setOutputUnit( QgsUnitTypes::RenderUnit::RenderPixels );
sl->setSvgFillColor( fillColor );
sl->setSvgStrokeColor( strokeColor );
sl->setSvgStrokeWidth( strokeWidth );
@@ -2950,7 +2961,12 @@ QgsSymbolLayer *QgsLinePatternFillSymbolLayer::createFromSld( QDomElement &eleme
offset = sqrt( pow( vectOffset.x(), 2 ) + pow( vectOffset.y(), 2 ) );
}

QString uom = element.attribute( QStringLiteral( "uom" ), "" );
size = QgsSymbolLayerUtils::sizeInPixelsFromSldUom( uom, size );
lineWidth = QgsSymbolLayerUtils::sizeInPixelsFromSldUom( uom, lineWidth );

QgsLinePatternFillSymbolLayer *sl = new QgsLinePatternFillSymbolLayer();
sl->setOutputUnit( QgsUnitTypes::RenderUnit::RenderPixels );
sl->setColor( lineColor );
sl->setLineWidth( lineWidth );
sl->setLineAngle( angle );
@@ -462,7 +462,12 @@ QgsSymbolLayer *QgsSimpleLineSymbolLayer::createFromSld( QDomElement &element )
offset = d;
}

QString uom = element.attribute( QStringLiteral( "uom" ), "" );
width = QgsSymbolLayerUtils::sizeInPixelsFromSldUom( uom, width );
offset = QgsSymbolLayerUtils::sizeInPixelsFromSldUom( uom, offset );

QgsSimpleLineSymbolLayer *l = new QgsSimpleLineSymbolLayer( color, width, penStyle );
l->setOutputUnit( QgsUnitTypes::RenderUnit::RenderPixels );
l->setOffset( offset );
l->setPenJoinStyle( penJoinStyle );
l->setPenCapStyle( penCapStyle );
@@ -1518,7 +1523,12 @@ QgsSymbolLayer *QgsMarkerLineSymbolLayer::createFromSld( QDomElement &element )
offset = d;
}

QString uom = element.attribute( QStringLiteral( "uom" ), "" );
interval = QgsSymbolLayerUtils::sizeInPixelsFromSldUom( uom, interval );
offset = QgsSymbolLayerUtils::sizeInPixelsFromSldUom( uom, offset );

QgsMarkerLineSymbolLayer *x = new QgsMarkerLineSymbolLayer( rotateMarker );
x->setOutputUnit( QgsUnitTypes::RenderUnit::RenderPixels );
x->setPlacement( placement );
x->setInterval( interval );
x->setSubSymbol( marker.release() );
@@ -1194,7 +1194,13 @@ QgsSymbolLayer *QgsSimpleMarkerSymbolLayer::createFromSld( QDomElement &element

Shape shape = decodeShape( name );

QString uom = element.attribute( QStringLiteral( "uom" ), "" );
size = QgsSymbolLayerUtils::sizeInPixelsFromSldUom( uom, size );
offset.setX( QgsSymbolLayerUtils::sizeInPixelsFromSldUom( uom, offset.x() ) );
offset.setY( QgsSymbolLayerUtils::sizeInPixelsFromSldUom( uom, offset.y() ) );

QgsSimpleMarkerSymbolLayer *m = new QgsSimpleMarkerSymbolLayer( shape, size );
m->setOutputUnit( QgsUnitTypes::RenderUnit::RenderPixels );
m->setColor( color );
m->setStrokeColor( strokeColor );
m->setAngle( angle );
@@ -2217,6 +2223,9 @@ QgsSymbolLayer *QgsSvgMarkerSymbolLayer::createFromSld( QDomElement &element )
if ( !QgsSymbolLayerUtils::externalGraphicFromSld( graphicElem, path, mimeType, fillColor, size ) )
return nullptr;

QString uom = element.attribute( QStringLiteral( "uom" ), "" );
size = QgsSymbolLayerUtils::sizeInPixelsFromSldUom( uom, size );

if ( mimeType != QLatin1String( "image/svg+xml" ) )
return nullptr;

@@ -2234,6 +2243,7 @@ QgsSymbolLayer *QgsSvgMarkerSymbolLayer::createFromSld( QDomElement &element )
QgsSymbolLayerUtils::displacementFromSldElement( graphicElem, offset );

QgsSvgMarkerSymbolLayer *m = new QgsSvgMarkerSymbolLayer( path, size );
m->setOutputUnit( QgsUnitTypes::RenderUnit::RenderPixels );
m->setFillColor( fillColor );
//m->setStrokeColor( strokeColor );
//m->setStrokeWidth( strokeWidth );
@@ -2874,7 +2884,13 @@ QgsSymbolLayer *QgsFontMarkerSymbolLayer::createFromSld( QDomElement &element )
QPointF offset;
QgsSymbolLayerUtils::displacementFromSldElement( graphicElem, offset );

QString uom = element.attribute( QStringLiteral( "uom" ), "" );
offset.setX( QgsSymbolLayerUtils::sizeInPixelsFromSldUom( uom, offset.x() ) );
offset.setY( QgsSymbolLayerUtils::sizeInPixelsFromSldUom( uom, offset.y() ) );
size = QgsSymbolLayerUtils::sizeInPixelsFromSldUom( uom, size );

QgsMarkerSymbolLayer *m = new QgsFontMarkerSymbolLayer( fontFamily, chr, size, color );
m->setOutputUnit( QgsUnitTypes::RenderUnit::RenderPixels );
m->setAngle( angle );
m->setOffset( offset );
return m;
@@ -4037,3 +4037,23 @@ void QgsSymbolLayerUtils::mergeScaleDependencies( int mScaleMinDenom, int mScale
props[ QStringLiteral( "scaleMaxDenom" )] = QString::number( qMin( parentScaleMaxDenom, mScaleMaxDenom ) );
}
}

double QgsSymbolLayerUtils::sizeInPixelsFromSldUom( const QString &uom, double size )
{
double scale = 1.0;

if ( uom == QLatin1String( "http://www.opengeospatial.org/se/units/metre" ) )
{
scale = 1.0 / 0.00028; // from meters to pixels
}
else if ( uom == QLatin1String( "http://www.opengeospatial.org/se/units/foot" ) )
{
scale = 304.8 / 0.28; // from feet to pixels
}
else
{
scale = 1.0; // from pixels to pixels (default unit)
}

return size * scale;
}
@@ -137,6 +137,14 @@ class CORE_EXPORT QgsSymbolLayerUtils
*/
static QgsUnitTypes::RenderUnit decodeSldUom( const QString &str, double *scaleFactor );

/** Returns the size scaled in pixels according to the uom attribute.
* \param uom The uom attribute from SLD 1.1 version
* \param size The original size
* \returns the size in pixels
* \since QGIS 3.0
*/
static double sizeInPixelsFromSldUom( const QString &uom, double size );

static QString encodeScaleMethod( QgsSymbol::ScaleMethod scaleMethod );
static QgsSymbol::ScaleMethod decodeScaleMethod( const QString &str );

0 comments on commit 0a82ab5

Please sign in to comment.