Skip to content

Commit

Permalink
Merge pull request #4432 from pblottiere/bugfix_sld
Browse files Browse the repository at this point in the history
[bugfix] Fix unit of sizes when reading a SLD file (uom attribute from SLD 1.1 version is supported)
  • Loading branch information
Hugo Mercier authored May 19, 2017
2 parents fe2350c + 0c72d4b commit 0a82ab5
Show file tree
Hide file tree
Showing 13 changed files with 460 additions and 1 deletion.
8 changes: 8 additions & 0 deletions python/core/symbology-ng/qgssymbollayerutils.sip
Original file line number Diff line number Diff line change
Expand Up @@ -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 );

Expand Down
5 changes: 5 additions & 0 deletions src/core/symbology-ng/qgsellipsesymbollayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) )
Expand All @@ -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 );
Expand Down
16 changes: 16 additions & 0 deletions src/core/symbology-ng/qgsfillsymbollayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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 ) )
Expand All @@ -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 );
Expand Down Expand Up @@ -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 );
Expand Down
10 changes: 10 additions & 0 deletions src/core/symbology-ng/qgslinesymbollayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand Down Expand Up @@ -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() );
Expand Down
16 changes: 16 additions & 0 deletions src/core/symbology-ng/qgsmarkersymbollayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand Down Expand Up @@ -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;

Expand All @@ -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 );
Expand Down Expand Up @@ -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;
Expand Down
20 changes: 20 additions & 0 deletions src/core/symbology-ng/qgssymbollayerutils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
8 changes: 8 additions & 0 deletions src/core/symbology-ng/qgssymbollayerutils.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 );

Expand Down
Loading

0 comments on commit 0a82ab5

Please sign in to comment.