diff --git a/src/core/labeling/qgsvectorlayerlabeling.cpp b/src/core/labeling/qgsvectorlayerlabeling.cpp
index d5f1c1bb06ea..c28de1777c74 100644
--- a/src/core/labeling/qgsvectorlayerlabeling.cpp
+++ b/src/core/labeling/qgsvectorlayerlabeling.cpp
@@ -348,7 +348,7 @@ void QgsAbstractVectorLayerLabeling::writeTextSymbolizer( QDomNode &parent, QgsP
const QPointF anchor = quadOffsetToSldAnchor( settings.pointSettings().quadrant() );
QgsSymbolLayerUtils::createAnchorPointElement( doc, pointPlacement, anchor );
// displacement
- if ( settings.xOffset > 0 || settings.yOffset > 0 )
+ if ( settings.xOffset != 0 || settings.yOffset != 0 )
{
const Qgis::RenderUnit offsetUnit = settings.offsetUnits;
const double dx = QgsSymbolLayerUtils::rescaleUom( settings.xOffset, offsetUnit, props );
diff --git a/tests/src/python/test_qgsserver_wms.py b/tests/src/python/test_qgsserver_wms.py
index 93537d9e09c5..9303996540ea 100644
--- a/tests/src/python/test_qgsserver_wms.py
+++ b/tests/src/python/test_qgsserver_wms.py
@@ -27,9 +27,22 @@
import osgeo.gdal # NOQA
from owslib.wms import WebMapService
-from qgis.core import QgsProject
-from qgis.PyQt.QtCore import QUrl
-from qgis.server import QgsBufferServerResponse, QgsServer, QgsServerRequest
+from qgis.core import (
+ QgsProject,
+ QgsMemoryProviderUtils,
+ QgsWkbTypes,
+ QgsCoordinateReferenceSystem,
+ QgsFields,
+ QgsField,
+ QgsPalLayerSettings,
+ QgsVectorLayerSimpleLabeling,
+)
+from qgis.PyQt.QtCore import QUrl, QUrlQuery, QVariant
+from qgis.server import (
+ QgsBufferServerResponse,
+ QgsServer,
+ QgsServerRequest,
+)
from qgis.testing import unittest
from test_qgsserver import QgsServerTestBase
@@ -420,6 +433,37 @@ def test_getcapabilities_owslib(self):
rootLayerName = 'QGIS Test Project'
self.assertIn(rootLayerName, w.contents.keys())
+ def test_get_styles_sld_label_negative_offset(self):
+ """Test issue GH #458862"""
+
+ # Create a point layer with a negative offset for the label
+ project = QgsProject()
+ fields = QgsFields()
+ fields.append(QgsField('int', QVariant.Int))
+ layer = QgsMemoryProviderUtils.createMemoryLayer('pointlabel', fields, QgsWkbTypes.Point, QgsCoordinateReferenceSystem(4326))
+ layer.setLabelsEnabled(True)
+ settings = QgsPalLayerSettings()
+ settings.xOffset = -10
+ settings.yOffset = -5
+ settings.fieldName = 'int'
+ settings.enabled = True
+ settings.placement = QgsPalLayerSettings.Placement.OverPoint
+
+ labeling = QgsVectorLayerSimpleLabeling(settings)
+ layer.setLabeling(labeling)
+
+ project.addMapLayer(layer)
+
+ # Test GetStyles with labeling
+ server = QgsServer()
+ request = QgsServerRequest()
+ request.setUrl(QUrl('?SERVICE=WMS&REQUEST=GetStyles&LAYERS=pointlabel'))
+ response = QgsBufferServerResponse()
+ server.handleRequest(request, response, project)
+ body = response.body().data().decode('utf8').replace('\n', '')
+ self.assertIn('-36', body)
+ self.assertIn('-18', body)
+
if __name__ == '__main__':
unittest.main()
diff --git a/tests/src/python/test_qgssymbollayer_createsld.py b/tests/src/python/test_qgssymbollayer_createsld.py
index 234d558b1c2e..9de983114773 100644
--- a/tests/src/python/test_qgssymbollayer_createsld.py
+++ b/tests/src/python/test_qgssymbollayer_createsld.py
@@ -98,6 +98,12 @@ def testSimpleMarkerUnitDefault(self):
self.assertStrokeWidth(root, 2, 11)
self.assertStaticDisplacement(root, 18, 36)
+ # Check negative offset
+ symbol.setOffset(QPointF(-5, -10))
+ dom, root = self.symbolToSld(symbol)
+ # print("Simple marker unit mm: " + root.ownerDocument().toString())
+ self.assertStaticDisplacement(root, -18, -36)
+
def testSimpleMarkerUnitPixels(self):
symbol = QgsSimpleMarkerSymbolLayer(
QgsSimpleMarkerSymbolLayerBase.Shape.Star, color=QColor(255, 0, 0), strokeColor=QColor(0, 255, 0), size=10)