Skip to content

Commit

Permalink
[labeling] Fix SLD export of negative offset
Browse files Browse the repository at this point in the history
Fix #58862
  • Loading branch information
elpaso authored and nyalldawson committed Oct 2, 2024
1 parent fae8e82 commit 85ccffe
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/core/labeling/qgsvectorlayerlabeling.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand Down
50 changes: 47 additions & 3 deletions tests/src/python/test_qgsserver_wms.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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('<se:DisplacementX>-36</se:DisplacementX>', body)
self.assertIn('<se:DisplacementY>-18</se:DisplacementY>', body)


if __name__ == '__main__':
unittest.main()
6 changes: 6 additions & 0 deletions tests/src/python/test_qgssymbollayer_createsld.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 85ccffe

Please sign in to comment.