Skip to content

Commit

Permalink
[bugfix] backport from 3.0 Fix unit of sizes when reading a SLD file (f…
Browse files Browse the repository at this point in the history
…ixes #8978)
  • Loading branch information
luipir committed Sep 12, 2017
1 parent 592151f commit 4beeb31
Show file tree
Hide file tree
Showing 12 changed files with 467 additions and 5 deletions.
8 changes: 8 additions & 0 deletions python/core/symbology-ng/qgssymbollayerv2utils.sip
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,14 @@ class QgsSymbolLayerV2Utils
static QString encodeSldUom( QgsSymbolV2::OutputUnit unit, double *scaleFactor );
static QgsSymbolV2::OutputUnit 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( QgsSymbolV2::ScaleMethod scaleMethod );
static QgsSymbolV2::ScaleMethod decodeScaleMethod( const QString& str );

Expand Down
5 changes: 5 additions & 0 deletions src/core/symbology-ng/qgsellipsesymbollayerv2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -490,6 +490,10 @@ QgsSymbolLayerV2* QgsEllipseSymbolLayerV2::createFromSld( QDomElement &element )
if ( !QgsSymbolLayerV2Utils::wellKnownMarkerFromSld( graphicElem, name, fillColor, borderColor, borderStyle, borderWidth, size ) )
return nullptr;

QString uom = element.attribute( QString( "uom" ), "" );
size = QgsSymbolLayerV2Utils::sizeInPixelsFromSldUom( uom, size );
borderWidth = QgsSymbolLayerV2Utils::sizeInPixelsFromSldUom( uom, borderWidth );

double angle = 0.0;
QString angleFunc;
if ( QgsSymbolLayerV2Utils::rotationFromSldElement( graphicElem, angleFunc ) )
Expand All @@ -501,6 +505,7 @@ QgsSymbolLayerV2* QgsEllipseSymbolLayerV2::createFromSld( QDomElement &element )
}

QgsEllipseSymbolLayerV2 *m = new QgsEllipseSymbolLayerV2();
m->setOutputUnit( QgsSymbolV2::Pixel );
m->setSymbolName( name );
m->setFillColor( fillColor );
m->setOutlineColor( borderColor );
Expand Down
16 changes: 16 additions & 0 deletions src/core/symbology-ng/qgsfillsymbollayerv2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,13 @@ QgsSymbolLayerV2* QgsSimpleFillSymbolLayerV2::createFromSld( QDomElement &elemen
QPointF offset;
QgsSymbolLayerV2Utils::displacementFromSldElement( element, offset );

QString uom = element.attribute( QString( "uom" ), "" );
offset.setX( QgsSymbolLayerV2Utils::sizeInPixelsFromSldUom( uom, offset.x() ) );
offset.setY( QgsSymbolLayerV2Utils::sizeInPixelsFromSldUom( uom, offset.y() ) );
borderWidth = QgsSymbolLayerV2Utils::sizeInPixelsFromSldUom( uom, borderWidth );

QgsSimpleFillSymbolLayerV2* sl = new QgsSimpleFillSymbolLayerV2( color, fillStyle, borderColor, borderStyle, borderWidth );
sl->setOutputUnit( QgsSymbolV2::Pixel );
sl->setOffset( offset );
return sl;
}
Expand Down Expand Up @@ -2159,6 +2165,10 @@ QgsSymbolLayerV2* QgsSVGFillSymbolLayer::createFromSld( QDomElement &element )

QgsSymbolLayerV2Utils::lineFromSld( graphicElem, penStyle, borderColor, borderWidth );

QString uom = element.attribute( QString( "uom" ), "" );
size = QgsSymbolLayerV2Utils::sizeInPixelsFromSldUom( uom, size );
borderWidth = QgsSymbolLayerV2Utils::sizeInPixelsFromSldUom( uom, borderWidth );

double angle = 0.0;
QString angleFunc;
if ( QgsSymbolLayerV2Utils::rotationFromSldElement( graphicElem, angleFunc ) )
Expand All @@ -2170,6 +2180,7 @@ QgsSymbolLayerV2* QgsSVGFillSymbolLayer::createFromSld( QDomElement &element )
}

QgsSVGFillSymbolLayer* sl = new QgsSVGFillSymbolLayer( path, size, angle );
sl->setOutputUnit( QgsSymbolV2::Pixel );
sl->setSvgFillColor( fillColor );
sl->setSvgOutlineColor( borderColor );
sl->setSvgOutlineWidth( borderWidth );
Expand Down Expand Up @@ -3018,7 +3029,12 @@ QgsSymbolLayerV2* QgsLinePatternFillSymbolLayer::createFromSld( QDomElement &ele
offset = sqrt( pow( vectOffset.x(), 2 ) + pow( vectOffset.y(), 2 ) );
}

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

QgsLinePatternFillSymbolLayer* sl = new QgsLinePatternFillSymbolLayer();
sl->setOutputUnit( QgsSymbolV2::Pixel );
sl->setColor( lineColor );
sl->setLineWidth( lineWidth );
sl->setLineAngle( angle );
Expand Down
10 changes: 10 additions & 0 deletions src/core/symbology-ng/qgslinesymbollayerv2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -471,7 +471,12 @@ QgsSymbolLayerV2* QgsSimpleLineSymbolLayerV2::createFromSld( QDomElement &elemen
offset = d;
}

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

QgsSimpleLineSymbolLayerV2* l = new QgsSimpleLineSymbolLayerV2( color, width, penStyle );
l->setOutputUnit( QgsSymbolV2::Pixel );
l->setOffset( offset );
l->setPenJoinStyle( penJoinStyle );
l->setPenCapStyle( penCapStyle );
Expand Down Expand Up @@ -1551,7 +1556,12 @@ QgsSymbolLayerV2* QgsMarkerLineSymbolLayerV2::createFromSld( QDomElement &elemen
offset = d;
}

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

QgsMarkerLineSymbolLayerV2* x = new QgsMarkerLineSymbolLayerV2( rotateMarker );
x->setOutputUnit( QgsSymbolV2::Pixel );
x->setPlacement( placement );
x->setInterval( interval );
x->setSubSymbol( marker );
Expand Down
16 changes: 16 additions & 0 deletions src/core/symbology-ng/qgsmarkersymbollayerv2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1239,7 +1239,13 @@ QgsSymbolLayerV2* QgsSimpleMarkerSymbolLayerV2::createFromSld( QDomElement &elem

Shape shape = decodeShape( name );

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

QgsSimpleMarkerSymbolLayerV2 *m = new QgsSimpleMarkerSymbolLayerV2( shape, size );
m->setOutputUnit( QgsSymbolV2::Pixel );
m->setColor( color );
m->setBorderColor( borderColor );
m->setAngle( angle );
Expand Down Expand Up @@ -2266,6 +2272,9 @@ QgsSymbolLayerV2* QgsSvgMarkerSymbolLayerV2::createFromSld( QDomElement &element
if ( !QgsSymbolLayerV2Utils::externalGraphicFromSld( graphicElem, path, mimeType, fillColor, size ) )
return nullptr;

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

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

Expand All @@ -2283,6 +2292,7 @@ QgsSymbolLayerV2* QgsSvgMarkerSymbolLayerV2::createFromSld( QDomElement &element
QgsSymbolLayerV2Utils::displacementFromSldElement( graphicElem, offset );

QgsSvgMarkerSymbolLayerV2* m = new QgsSvgMarkerSymbolLayerV2( path, size );
m->setOutputUnit( QgsSymbolV2::Pixel );
m->setFillColor( fillColor );
//m->setOutlineColor( outlineColor );
//m->setOutlineWidth( outlineWidth );
Expand Down Expand Up @@ -2945,7 +2955,13 @@ QgsSymbolLayerV2* QgsFontMarkerSymbolLayerV2::createFromSld( QDomElement &elemen
QPointF offset;
QgsSymbolLayerV2Utils::displacementFromSldElement( graphicElem, offset );

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

QgsMarkerSymbolLayerV2 *m = new QgsFontMarkerSymbolLayerV2( fontFamily, chr, size, color );
m->setOutputUnit( QgsSymbolV2::Pixel );
m->setAngle( angle );
m->setOffset( offset );
return m;
Expand Down
20 changes: 20 additions & 0 deletions src/core/symbology-ng/qgssymbollayerv2utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4231,3 +4231,23 @@ void QgsSymbolLayerV2Utils::mergeScaleDependencies( int mScaleMinDenom, int mSca
props[ "scaleMaxDenom" ] = QString::number( qMin( parentScaleMaxDenom, mScaleMaxDenom ) );
}
}

double QgsSymbolLayerV2Utils::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/qgssymbollayerv2utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,14 @@ class CORE_EXPORT QgsSymbolLayerV2Utils
static QString encodeScaleMethod( QgsSymbolV2::ScaleMethod scaleMethod );
static QgsSymbolV2::ScaleMethod decodeScaleMethod( const QString& str );

/** 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
* @note added in QGIS 2.18 and 3.0
*/
static double sizeInPixelsFromSldUom( const QString &uom, double size );

static QPainter::CompositionMode decodeBlendMode( const QString& s );

static QIcon symbolPreviewIcon( QgsSymbolV2* symbol, QSize size );
Expand Down
Loading

0 comments on commit 4beeb31

Please sign in to comment.