Tests for SVG parameters in the SLD import

This commit is contained in:
Alessandro Pasotti 2022-09-21 12:12:27 +02:00
parent 7632de8780
commit fffcb1dd81
2 changed files with 35 additions and 13 deletions

View File

@ -2706,31 +2706,50 @@ QgsSymbolLayer *QgsSvgMarkerSymbolLayer::createFromSld( QDomElement &element )
QgsSymbolLayerUtils::displacementFromSldElement( graphicElem, offset );
// Extract parameters from URL
const QUrl svgUrl { path };
QgsSvgMarkerSymbolLayer *m = new QgsSvgMarkerSymbolLayer( svgUrl.path(), size );
QString realPath { path };
QUrl svgUrl { path };
const QUrlQuery queryString { svgUrl.query( ) };
// Because color definition can start with '#', the url parsing won't recognize the query string entirely
QUrlQuery queryString;
if ( svgUrl.hasQuery() && svgUrl.hasFragment() )
{
const QString queryPart { path.mid( path.indexOf( '?' ) + 1 ) };
queryString.setQuery( queryPart );
}
// Remove query for simple file paths
if ( svgUrl.scheme().isEmpty() || svgUrl.isLocalFile() )
{
svgUrl.setQuery( QString() );
realPath = svgUrl.path();
}
QgsSvgMarkerSymbolLayer *m = new QgsSvgMarkerSymbolLayer( realPath, size );
QMap<QString, QgsProperty> params;
bool ok;
if ( queryString.hasQueryItem( QStringLiteral( "fill" ) ) )
{
m->setFillColor( { queryString.queryItemValue( QStringLiteral( "fill" ) ) } );
const QColor fillColor { queryString.queryItemValue( QStringLiteral( "fill" ) ) };
m->setFillColor( fillColor );
}
bool ok;
if ( queryString.hasQueryItem( QStringLiteral( "fill-opacity" ) ) )
{
const double alpha { queryString.queryItemValue( QStringLiteral( "fill-opacity" ) ).toDouble( &ok ) };
if ( ok )
{
QColor c { m->color() };
c.setAlpha( alpha );
m->setFillColor( c );
params.insert( QStringLiteral( "fill-opacity" ), QgsProperty::fromValue( alpha ) );
}
}
if ( queryString.hasQueryItem( QStringLiteral( "outline" ) ) )
{
m->setStrokeColor( { queryString.queryItemValue( QStringLiteral( "outline" ) ) } );
const QColor strokeColor { queryString.queryItemValue( QStringLiteral( "outline" ) ) };
m->setStrokeColor( strokeColor );
}
if ( queryString.hasQueryItem( QStringLiteral( "outline-opacity" ) ) )
@ -2738,9 +2757,7 @@ QgsSymbolLayer *QgsSvgMarkerSymbolLayer::createFromSld( QDomElement &element )
const double alpha { queryString.queryItemValue( QStringLiteral( "outline-opacity" ) ).toDouble( &ok ) };
if ( ok )
{
QColor c { m->strokeColor() };
c.setAlpha( alpha );
m->setStrokeColor( c );
params.insert( QStringLiteral( "outline-opacity" ), QgsProperty::fromValue( alpha ) );
}
}
@ -2753,6 +2770,11 @@ QgsSymbolLayer *QgsSvgMarkerSymbolLayer::createFromSld( QDomElement &element )
}
}
if ( ! params.isEmpty() )
{
m->setParameters( params );
}
m->setOutputUnit( sldUnitSize );
m->setAngle( angle );
m->setOffset( offset );

View File

@ -10,7 +10,7 @@
<se:PointSymbolizer>
<se:Graphic>
<se:ExternalGraphic>
<OnlineResource xlink:type="simple" xlink:href="file:///gpsicons/skull.svg"/>
<OnlineResource xlink:type="simple" xlink:href="file:///gpsicons/skull.svg?fill=%2300ff00&amp;fill-opacity=0.9&amp;outline=%23ff0000&amp;outline-opacity=0.8&amp;outline-width=2"/>
<Format>image/svg+xml</Format>
</se:ExternalGraphic>
<se:Size>12</se:Size>