QGIS API Documentation 4.1.0-Master (3b8ef1f72a3)
Loading...
Searching...
No Matches
qgssymbolconvertersld.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgssymbolconvertersld.cpp
3 ----------------------
4 begin : February 2026
5 copyright : (C) 2026 by Nyall Dawson
6 email : nyall dot dawson at gmail dot com
7***************************************************************************
8* *
9* This program is free software; you can redistribute it and/or modify *
10* it under the terms of the GNU General Public License as published by *
11* the Free Software Foundation; either version 2 of the License, or *
12* (at your option) any later version. *
13* *
14***************************************************************************/
15
17
18#include "qgsfillsymbol.h"
19#include "qgslinesymbol.h"
20#include "qgsmarkersymbol.h"
21#include "qgsreadwritecontext.h"
22#include "qgssldexportcontext.h"
23#include "qgssymbol.h"
24#include "qgssymbollayerutils.h"
25
26#include <QDomDocument>
27#include <QDomElement>
28#include <QObject>
29#include <QString>
30#include <QVariant>
31
32using namespace Qt::StringLiterals;
33
38
40{
41 return u"sld"_s;
42}
43
45{
46 return QObject::tr( "Styled Layer Descriptor (SLD)" );
47}
48
50{
51 if ( !symbol )
52 return QVariant();
53
54 QDomDocument doc;
55
56 QgsSldExportContext sldExportContext;
57
58 QDomElement root = doc.createElement( u"Rule"_s );
59 symbol->toSld( doc, root, sldExportContext );
60 doc.appendChild( root );
61
62 // copy errors, warnings from SLD export context
63 for ( const QString &error : sldExportContext.errors() )
64 {
65 context.pushError( error );
66 }
67 for ( const QString &warning : sldExportContext.warnings() )
68 {
69 context.pushWarning( warning );
70 }
71
72 return doc.toString();
73}
74
75std::unique_ptr< QgsSymbol > QgsSymbolConverterSld::createSymbol( const QVariant &variant, QgsSymbolConverterContext &context ) const
76{
77 if ( variant.isNull() || !variant.canConvert<QString>() )
78 return nullptr;
79
80 QString xmlString = variant.toString();
81 if ( xmlString.isEmpty() )
82 return nullptr;
83
84 if ( !xmlString.contains( "xmlns:se="_L1 ) )
85 {
86 // wrap in a dummy tag to get se xmlns processing
87 xmlString = u"<?xml version=\"1.0\" encoding=\"UTF-8\"?><tmp xmlns:se=\"http://www.opengis.net/se\">%1</tmp>"_s.arg( xmlString );
88 }
89
90 QDomDocument doc;
91 QString errorMsg;
92 int errorLine, errorColumn;
93 if ( !doc.setContent( xmlString, true, &errorMsg, &errorLine, &errorColumn ) )
94 {
95 context.pushError( QObject::tr( "Error parsing SLD content: %1" ).arg( errorMsg ) );
96 return nullptr;
97 }
98
99 const QDomElement ruleElem = doc.documentElement().firstChildElement( u"Rule"_s );
100 if ( ruleElem.isNull() )
101 {
102 context.pushError( QObject::tr( "Error parsing SLD content: no Rule elements found" ) );
103 return nullptr;
104 }
105
106 QgsSymbolLayerList layers;
107
109 switch ( context.typeHint() )
110 {
112 geomType = Qgis::GeometryType::Point;
113 break;
115 geomType = Qgis::GeometryType::Line;
116 break;
119 break;
121 break;
122 }
123
124 // retrieve the Rule element child nodes
125 QDomElement childElem = ruleElem.firstChildElement();
126 while ( !childElem.isNull() )
127 {
128 if ( childElem.localName().endsWith( "Symbolizer"_L1 ) )
129 {
130 QgsSymbolLayerUtils::createSymbolLayerListFromSld( childElem, geomType, layers );
131 }
132
133 childElem = childElem.nextSiblingElement();
134 }
135
136 if ( layers.isEmpty() )
137 return nullptr;
138
139 std::unique_ptr< QgsSymbol > symbol;
140 switch ( geomType )
141 {
143 symbol = std::make_unique< QgsLineSymbol >( layers );
144 break;
145
147 symbol = std::make_unique< QgsFillSymbol >( layers );
148 break;
149
151 symbol = std::make_unique< QgsMarkerSymbol >( layers );
152 break;
153
154 default:
155 context.pushError( QObject::tr( "Invalid geometry type: found %1" ).arg( qgsEnumValueToKey( geomType ) ) );
156 return nullptr;
157 }
158
159 return symbol;
160}
GeometryType
The geometry types are used to group Qgis::WkbType in a coarse way.
Definition qgis.h:379
@ Point
Points.
Definition qgis.h:380
@ Line
Lines.
Definition qgis.h:381
@ Polygon
Polygons.
Definition qgis.h:382
@ Unknown
Unknown types.
Definition qgis.h:383
QFlags< SymbolConverterCapability > SymbolConverterCapabilities
Symbol converter capabilities.
Definition qgis.h:824
@ Marker
Marker symbol.
Definition qgis.h:637
@ Line
Line symbol.
Definition qgis.h:638
@ Fill
Fill symbol.
Definition qgis.h:639
@ Hybrid
Hybrid symbol.
Definition qgis.h:640
@ WriteSymbol
Allows writing symbols to variants.
Definition qgis.h:814
@ ReadSymbol
Allows reading symbols from variants.
Definition qgis.h:813
Holds SLD export options and other information related to SLD export of a QGIS layer style.
QStringList warnings() const
Returns a list of warnings which occurred during the conversion.
QStringList errors() const
Returns a list of errors which occurred during the conversion.
Represents the context in which a QgsSymbolConverter conversion occurs.
Qgis::SymbolType typeHint() const
Returns the symbol type hint, or Qgis::SymbolType::Hybrid if unknown.
void pushWarning(const QString &warning)
Pushes a warning message generated during the conversion.
void pushError(const QString &error)
Pushes a error message generated during the conversion.
std::unique_ptr< QgsSymbol > createSymbol(const QVariant &variant, QgsSymbolConverterContext &context) const override
Creates a new QgsSymbol from a QVariant representation.
QVariant toVariant(const QgsSymbol *symbol, QgsSymbolConverterContext &context) const override
Converts a symbol into a QVariant representation.
QString formatName() const override
Returns a translated, user-friendly name for the converter's data format.
Qgis::SymbolConverterCapabilities capabilities() const override
Returns the capabilities of the converter.
QString name() const override
Returns the unique name for the converter.
static bool createSymbolLayerListFromSld(QDomElement &element, Qgis::GeometryType geomType, QList< QgsSymbolLayer * > &layers)
Creates a symbol layer list from a DOM element.
Abstract base class for all rendered symbols.
Definition qgssymbol.h:227
Q_DECL_DEPRECATED void toSld(QDomDocument &doc, QDomElement &element, QVariantMap props) const
Converts the symbol to a SLD representation.
QString qgsEnumValueToKey(const T &value, bool *returnOk=nullptr)
Returns the value for the given key of an enum.
Definition qgis.h:7259
QList< QgsSymbolLayer * > QgsSymbolLayerList
Definition qgssymbol.h:30