32using namespace Qt::StringLiterals;
36 const QString
type = element.attribute( u
"type"_s );
37 if (
type ==
"rule-based"_L1 )
41 else if (
type ==
"simple"_L1 )
55 toSld( parent, context );
60 context.
pushError( QObject::tr(
"%1 labeling cannot be exported to SLD" ).arg(
type() ) );
116 QDomElement elem = doc.createElement( u
"labeling"_s );
117 elem.setAttribute( u
"type"_s, u
"simple"_s );
118 elem.appendChild( mSettings->writeXml( doc, context ) );
124 Q_UNUSED( providerId )
133 if ( !visitor->
visit( &entity ) )
150 || mSettings->format().hasNonDefaultCompositionMode();
155 const QDomElement settingsElem = element.firstChildElement( u
"settings"_s );
156 if ( !settingsElem.isNull() )
159 settings.readXml( settingsElem, context );
168 double quadOffsetX = 0.5, quadOffsetY = 0.5;
171 switch ( quadrantPosition )
209 return QPointF( quadOffsetX, quadOffsetY );
215void appendSimpleFunction( QDomDocument &doc, QDomElement &parent,
const QString &name,
const QString &attribute )
217 QDomElement function = doc.createElement( u
"ogc:Function"_s );
218 function.setAttribute( u
"name"_s, name );
219 parent.appendChild( function );
220 QDomElement
property = doc.createElement( u
"ogc:PropertyName"_s );
221 property.appendChild( doc.createTextNode( attribute ) );
222 function.appendChild( property );
227 std::unique_ptr<QgsMarkerSymbolLayer> layer;
228 switch ( settings.
type() )
256 switch ( settings.
type() )
274 layer.reset( marker );
277 layer->setEnabled(
true );
279 const QSizeF size = settings.
size();
280 layer->setSize( std::max( 1., std::max( size.width(), size.height() ) ) );
281 layer->setSizeUnit( settings.
sizeUnit() );
287 const int alpha = std::round( settings.
opacity() * 255 );
288 fillColor.setAlpha( alpha );
289 strokeColor.setAlpha( alpha );
291 layer->setFillColor( fillColor );
292 layer->setStrokeColor( strokeColor );
296 layer->setAngle( settings.
rotation() );
299 layer->setOffset( settings.
offset() );
300 layer->setOffsetUnit( settings.
offsetUnit() );
314 QDomDocument doc = parent.ownerDocument();
317 QDomElement textSymbolizerElement = doc.createElement( u
"se:TextSymbolizer"_s );
318 parent.appendChild( textSymbolizerElement );
322 const QFont font = format.
font();
323 QDomElement labelElement = doc.createElement( u
"se:Label"_s );
324 textSymbolizerElement.appendChild( labelElement );
332 if ( !expressionElem.isNull() )
334 labelElement.appendChild( expressionElem );
338 context.
pushError( QObject::tr(
"Complex expressions in labels cannot be exported to SLD: %1. Skipping label '%2'" ).arg( errorMsg,
settings.getLabelExpression()->dump() ) );
339 labelElement.appendChild( doc.createTextNode( u
"Placeholder"_s ) );
362 QDomElement propertyNameElement = doc.createElement( u
"ogc:PropertyName"_s );
363 propertyNameElement.appendChild( doc.createTextNode(
settings.fieldName ) );
364 labelElement.appendChild( propertyNameElement );
369 QDomElement fontElement = doc.createElement( u
"se:Font"_s );
370 textSymbolizerElement.appendChild( fontElement );
375 if ( format.
font().italic() )
379 if ( format.
font().bold() )
385 QDomElement labelPlacement = doc.createElement( u
"se:LabelPlacement"_s );
386 textSymbolizerElement.appendChild( labelPlacement );
387 double maxDisplacement = 0;
388 double repeatDistance = 0;
393 QDomElement pointPlacement = doc.createElement(
"se:PointPlacement" );
394 labelPlacement.appendChild( pointPlacement );
409 QDomElement rotation = doc.createElement(
"se:Rotation" );
410 pointPlacement.appendChild( rotation );
411 rotation.appendChild( doc.createTextNode( QString::number(
settings.angleOffset ) ) );
418 QDomElement pointPlacement = doc.createElement(
"se:PointPlacement" );
419 labelPlacement.appendChild( pointPlacement );
426 const double offset = std::sqrt( radius * radius / 2 );
427 maxDisplacement = radius + 1;
436 QDomElement pointPlacement = doc.createElement(
"se:PointPlacement" );
437 labelPlacement.appendChild( pointPlacement );
448 QDomElement linePlacement = doc.createElement(
"se:LinePlacement" );
449 labelPlacement.appendChild( linePlacement );
456 QDomElement perpendicular = doc.createElement(
"se:PerpendicularOffset" );
457 linePlacement.appendChild( perpendicular );
464 QDomElement repeat = doc.createElement(
"se:Repeat" );
465 linePlacement.appendChild( repeat );
466 repeat.appendChild( doc.createTextNode( u
"true"_s ) );
467 QDomElement gap = doc.createElement(
"se:Gap" );
468 linePlacement.appendChild( gap );
474 QDomElement generalize = doc.createElement(
"se:GeneralizeLine" );
475 linePlacement.appendChild( generalize );
476 generalize.appendChild( doc.createTextNode( u
"true"_s ) );
485 QDomElement haloElement = doc.createElement( u
"se:Halo"_s );
486 textSymbolizerElement.appendChild( haloElement );
488 QDomElement radiusElement = doc.createElement( u
"se:Radius"_s );
489 haloElement.appendChild( radiusElement );
494 QDomElement fillElement = doc.createElement( u
"se:Fill"_s );
495 haloElement.appendChild( fillElement );
504 QDomElement fillElement = doc.createElement( u
"se:Fill"_s );
505 textSymbolizerElement.appendChild( fillElement );
517 layer->writeSldMarker( doc, textSymbolizerElement, context );
524 QDomElement priorityElement = doc.createElement( u
"se:Priority"_s );
525 textSymbolizerElement.appendChild( priorityElement );
526 int priority = 500 + 1000 *
settings.zIndex + (
settings.priority - 5 ) * 100;
532 priorityElement.appendChild( doc.createTextNode( QString::number( priority ) ) );
536 if ( font.underline() )
539 textSymbolizerElement.appendChild( vo );
541 if ( font.strikeOut() )
544 textSymbolizerElement.appendChild( vo );
547 if ( maxDisplacement > 0 )
550 textSymbolizerElement.appendChild( vo );
559 textSymbolizerElement.appendChild( vo );
560 if (
settings.maxCurvedCharAngleIn > 0 ||
settings.maxCurvedCharAngleOut > 0 )
563 const double angle = std::min( std::fabs(
settings.maxCurvedCharAngleIn ), std::fabs(
settings.maxCurvedCharAngleOut ) );
565 textSymbolizerElement.appendChild( vo );
580 if ( repeatDistance > 0 )
583 textSymbolizerElement.appendChild( vo );
586 switch (
settings.placementSettings().overlapHandling() )
593 textSymbolizerElement.appendChild( vo );
599 textSymbolizerElement.appendChild( vo );
601 if (
settings.lineSettings().mergeLines() )
604 textSymbolizerElement.appendChild( vo );
605 switch (
settings.placementSettings().multiPartBehavior() )
613 textSymbolizerElement.appendChild( vo );
629 resizeType = u
"stretch"_s;
633 resizeType = u
"proportional"_s;
636 textSymbolizerElement.appendChild( voResize );
639 const QSizeF size = background.
size();
640 if ( size.width() > 0 || size.height() > 0 )
653 textSymbolizerElement.appendChild( voMargin );
670 toSld( parent, context );
675 if ( mSettings->drawLabels )
677 QDomDocument doc = parent.ownerDocument();
679 QDomElement ruleElement = doc.createElement( u
"se:Rule"_s );
680 parent.appendChild( ruleElement );
683 if ( mSettings->scaleVisibility )
685 QVariantMap scaleProps = QVariantMap();
688 scaleProps.insert(
"scaleMinDenom",
qgsDoubleToString( mSettings->maximumScale ) );
689 scaleProps.insert(
"scaleMaxDenom",
qgsDoubleToString( mSettings->minimumScale ) );
702 mSettings->setFormat( format );
707 Q_UNUSED( providerId )
@ LabelLargestPartOnly
Place a label only on the largest part from the geometry.
@ SplitLabelTextLinesOverParts
Splits the label text over the parts of the geometry, such that each consecutive part is labeled with...
@ LabelEveryPartWithEntireLabel
Place the (same) entire label over every part from the geometry.
@ FromSymbolBounds
Offset distance applies from rendered symbol bounds.
@ OverPoint
Arranges candidates over a point (or centroid of a polygon), or at a preset offset from the point....
@ Curved
Arranges candidates following the curvature of a line feature. Applies to line layers only.
@ AroundPoint
Arranges candidates in a circle around a point (or centroid of a polygon). Applies to point or polygo...
@ Line
Arranges candidates parallel to a generalised line representing the feature or parallel to a polygon'...
@ Free
Arranges candidates scattered throughout a polygon feature. Candidates are rotated to respect the pol...
@ OrderedPositionsAroundPoint
Candidates are placed in predefined positions around a point. Preference is given to positions with g...
@ Horizontal
Arranges horizontal candidates scattered throughout a polygon feature. Applies to polygon layers only...
@ PerimeterCurved
Arranges candidates following the curvature of a polygon's boundary. Applies to polygon layers only.
@ OutsidePolygons
Candidates are placed outside of polygon boundaries. Applies to polygon layers only.
MarkerShape
Marker shapes.
Capitalization
String capitalization options.
@ MixedCase
Mixed case, ie no change.
@ AllLowercase
Convert all characters to lowercase.
@ ForceFirstLetterToCapital
Convert just the first letter of each word to uppercase, leave the rest untouched.
@ AllUppercase
Convert all characters to uppercase.
LabelQuadrantPosition
Label quadrant positions.
RenderUnit
Rendering size units.
@ AllowOverlapAtNoCost
Labels may freely overlap other labels, at no cost.
@ AllowOverlapIfRequired
Avoids overlapping labels when possible, but permit overlaps if labels for features cannot otherwise ...
@ PreventOverlap
Do not allow labels to overlap other labels.
@ AlwaysAllowUpsideDown
Show upside down for all labels, including dynamic ones.
virtual bool accept(QgsStyleEntityVisitorInterface *visitor) const
Accepts the specified symbology visitor, causing it to visit all symbols associated with the labeling...
virtual Q_DECL_DEPRECATED void toSld(QDomNode &parent, const QVariantMap &props) const
Writes the SE 1.1 TextSymbolizer element based on the current layer labeling settings.
virtual Q_DECL_DEPRECATED void writeTextSymbolizer(QDomNode &parent, QgsPalLayerSettings &settings, const QVariantMap &props) const
Writes a TextSymbolizer element contents based on the provided labeling settings.
static QgsAbstractVectorLayerLabeling * create(const QDomElement &element, const QgsReadWriteContext &context)
Try to create instance of an implementation based on the XML data.
virtual QgsPalLayerSettings settings(const QString &providerId=QString()) const =0
Gets associated label settings.
QgsAbstractVectorLayerLabeling()=default
static QgsPalLayerSettings defaultSettingsForLayer(const QgsVectorLayer *layer)
Returns the default layer settings to use for the specified vector layer.
virtual QString type() const =0
Unique type string of the labeling configuration implementation.
QgsProject * project() const
Returns the parent project if this map layer is added to a project.
Abstract base class for marker symbol layers.
static QDomElement expressionToOgcExpression(const QgsExpression &exp, QDomDocument &doc, QString *errorMessage=nullptr, bool requiresFilterElement=false)
Creates an OGC expression XML element from the exp expression with default values for the geometry na...
Contains settings for how a map layer will be labeled.
@ FontBlendMode
Text blend mode.
bool containsAdvancedEffects() const
Returns true if any component of the label settings requires advanced effects such as blend modes,...
A container for the context for various read/write operations on objects.
static QgsRuleBasedLabeling * create(const QDomElement &element, const QgsReadWriteContext &context)
Create the instance from a DOM element with saved configuration.
void setShape(Qgis::MarkerShape shape)
Sets the rendered marker shape.
Simple marker symbol layer, consisting of a rendered shape with solid fill color and a stroke.
void setStrokeWidthUnit(Qgis::RenderUnit u)
Sets the unit for the width of the marker's stroke.
void setStrokeWidth(double w)
Sets the width of the marker's stroke.
Holds SLD export options and other information related to SLD export of a QGIS layer style.
void setExtraProperties(const QVariantMap &properties)
Sets the open ended set of properties that can drive/inform the SLD encoding.
QVariantMap extraProperties() const
Returns the open ended set of properties that can drive/inform the SLD encoding.
void pushError(const QString &error)
Pushes a error message generated during the conversion.
An interface for classes which can visit style entity (e.g.
virtual bool visit(const QgsStyleEntityVisitorInterface::StyleLeaf &entity)
Called when the visitor will visit a style entity.
A label settings entity for QgsStyle databases.
static QgsTextFormat defaultTextFormatForProject(QgsProject *project, QgsStyle::TextFormatContext context=QgsStyle::TextFormatContext::Labeling)
Returns the default text format to use for new text based objects for the specified project,...
A marker symbol layer which renders an SVG graphic.
void setStrokeWidth(double w)
void setStrokeWidthUnit(Qgis::RenderUnit unit)
Sets the units for the stroke width.
static void createAnchorPointElement(QDomDocument &doc, QDomElement &element, QPointF anchor)
Creates a SE 1.1 anchor point element as a child of the specified element.
static void applyScaleDependency(QDomDocument &doc, QDomElement &ruleElem, QVariantMap &props)
Checks if the properties contain scaleMinDenom and scaleMaxDenom, if available, they are added into t...
static double rescaleUom(double size, Qgis::RenderUnit unit, const QVariantMap &props)
Rescales the given size based on the uomScale found in the props, if any is found,...
static QDomElement createVendorOptionElement(QDomDocument &doc, const QString &name, const QString &value)
static void createDisplacementElement(QDomDocument &doc, QDomElement &element, QPointF offset)
static QDomElement createSvgParameterElement(QDomDocument &doc, const QString &name, const QString &value)
virtual QgsSymbolLayer * clone() const =0
Shall be reimplemented by subclasses to create a deep copy of the instance.
QgsSymbolLayer * symbolLayer(int layer)
Returns the symbol layer at the specified index.
int symbolLayerCount() const
Returns the total number of symbol layers contained in the symbol.
Container for settings relating to a text background object.
RotationType rotationType() const
Returns the method used for rotating the background shape.
QString svgFile() const
Returns the absolute path to the background SVG file, if set.
QSizeF size() const
Returns the size of the background shape.
@ SizePercent
Shape size is determined by percent of text size.
@ SizeBuffer
Shape size is determined by adding a buffer margin around text.
bool enabled() const
Returns whether the background is enabled.
double opacity() const
Returns the background shape's opacity.
double rotation() const
Returns the rotation for the background shape, in degrees clockwise.
QColor fillColor() const
Returns the color used for filing the background shape.
SizeType sizeType() const
Returns the method used to determine the size of the background shape (e.g., fixed size or buffer aro...
Qgis::RenderUnit strokeWidthUnit() const
Returns the units used for the shape's stroke width.
ShapeType type() const
Returns the type of background shape (e.g., square, ellipse, SVG).
double strokeWidth() const
Returns the width of the shape's stroke (stroke).
@ ShapeMarkerSymbol
Marker symbol.
@ ShapeSquare
Square - buffered sizes only.
@ ShapeRectangle
Rectangle.
Qgis::RenderUnit offsetUnit() const
Returns the units used for the shape's offset.
QColor strokeColor() const
Returns the color used for outlining the background shape.
Qgis::RenderUnit sizeUnit() const
Returns the units used for the shape's size.
@ RotationFixed
Shape rotation is a fixed angle.
QgsMarkerSymbol * markerSymbol() const
Returns the marker symbol to be rendered in the background.
QPointF offset() const
Returns the offset used for drawing the background shape.
Container for settings relating to a text buffer.
Qgis::RenderUnit sizeUnit() const
Returns the units for the buffer size.
double size() const
Returns the size of the buffer.
bool enabled() const
Returns whether the buffer is enabled.
double opacity() const
Returns the buffer opacity.
QColor color() const
Returns the color of the buffer.
Container for all settings relating to text rendering.
void multiplyOpacity(double opacityFactor)
Multiply opacity by opacityFactor.
Qgis::Capitalization capitalization() const
Returns the text capitalization style.
QgsTextBackgroundSettings & background()
Returns a reference to the text background settings.
Qgis::RenderUnit sizeUnit() const
Returns the units for the size of rendered text.
double opacity() const
Returns the text's opacity.
double size() const
Returns the size for rendered text.
QColor color() const
Returns the color that text will be rendered in.
QFont font() const
Returns the font used for rendering text.
QgsTextBufferSettings & buffer()
Returns a reference to the text buffer settings.
Implements a label provider for vector layers.
QString type() const override
Unique type string of the labeling configuration implementation.
void multiplyOpacity(double opacityFactor) override
Multiply opacity by opacityFactor.
bool accept(QgsStyleEntityVisitorInterface *visitor) const override
Accepts the specified symbology visitor, causing it to visit all symbols associated with the labeling...
QgsPalLayerSettings settings(const QString &providerId=QString()) const override
Gets associated label settings.
QDomElement save(QDomDocument &doc, const QgsReadWriteContext &context) const override
Returns labeling configuration as XML element.
bool requiresAdvancedEffects() const override
Returns true if drawing labels requires advanced effects like composition modes, which could prevent ...
QgsVectorLayerSimpleLabeling(const QgsPalLayerSettings &settings)
Constructs simple labeling configuration with given initial settings.
void setSettings(QgsPalLayerSettings *settings, const QString &providerId=QString()) override
Set pal settings (takes ownership).
QgsVectorLayerLabelProvider * provider(QgsVectorLayer *layer) const override
static QgsVectorLayerSimpleLabeling * create(const QDomElement &element, const QgsReadWriteContext &context)
Create the instance from a DOM element with saved configuration.
QgsAbstractVectorLayerLabeling * clone() const override
Returns a new copy of the object.
Q_DECL_DEPRECATED void toSld(QDomNode &parent, const QVariantMap &props) const override
Writes the SE 1.1 TextSymbolizer element based on the current layer labeling settings.
bool hasNonDefaultCompositionMode() const override
Returns true the labeling requires a non-default composition mode.
Represents a vector layer which manages a vector based dataset.
Q_INVOKABLE Qgis::GeometryType geometryType() const
Returns point, line or polygon.
QString displayField() const
This is a shorthand for accessing the displayExpression if it is a simple field.
QString qgsDoubleToString(double a, int precision=17)
Returns a string representation of a double.
std::unique_ptr< QgsMarkerSymbolLayer > backgroundToMarkerLayer(const QgsTextBackgroundSettings &settings)
void appendSimpleFunction(QDomDocument &doc, QDomElement &parent, const QString &name, const QString &attribute)
QPointF quadOffsetToSldAnchor(Qgis::LabelQuadrantPosition quadrantPosition)