Skip to content

Commit

Permalink
Merge pull request #169 from MIERUNE/fix/hairline
Browse files Browse the repository at this point in the history
Handle hairline
  • Loading branch information
Kanahiro authored Mar 5, 2024
2 parents eeac579 + bbc23ff commit d43b7fb
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 13 deletions.
7 changes: 3 additions & 4 deletions translator/vector/symbol/fill.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from qgis.core import QgsSymbolLayer
from plugx_utils import convert_to_point
from PyQt5.QtCore import Qt
from translator.vector.symbol.utils import to_rgba
from translator.vector.symbol.utils import to_rgba, get_stroke_width_pt
from translator.vector.symbol.penstyle import get_penstyle_from


Expand Down Expand Up @@ -37,7 +36,7 @@ def get_polygon_symbol_data(
"color": to_rgba(symbol_layer.fillColor()),
"brushstyle": _get_brushstyle_from(symbol_layer.brushStyle()),
"outline_color": to_rgba(symbol_layer.strokeColor()),
"outline_width": convert_to_point(
"outline_width": get_stroke_width_pt(
symbol_layer.strokeWidth(), symbol_layer.strokeWidthUnit()
),
"outline_penstyle": get_penstyle_from(symbol_layer),
Expand Down Expand Up @@ -84,7 +83,7 @@ def get_polygon_symbol_data(
"type": "svg",
"color": to_rgba(symbol_layer.color()),
"outline_color": to_rgba(symbol_layer.svgStrokeColor()),
"outline_width": convert_to_point(
"outline_width": get_stroke_width_pt(
symbol_layer.svgStrokeWidth(), symbol_layer.svgStrokeWidthUnit()
),
"level": symbol_layer.renderingPass(),
Expand Down
22 changes: 16 additions & 6 deletions translator/vector/symbol/line.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from plugx_utils import convert_to_point

from .marker import get_point_symbol_data
from translator.vector.symbol.utils import to_rgba
from translator.vector.symbol.utils import to_rgba, get_stroke_width_pt
from translator.vector.symbol.penstyle import get_penstyle_from


Expand All @@ -12,7 +12,9 @@ def get_line_symbol_data(symbol_layer: QgsSymbolLayer, symbol_opacity: float) ->
"type": "simple",
"color": to_rgba(symbol_layer.color()),
"penstyle": get_penstyle_from(symbol_layer),
"width": convert_to_point(symbol_layer.width(), symbol_layer.widthUnit()),
"width": get_stroke_width_pt(
symbol_layer.width(), symbol_layer.widthUnit()
),
"level": symbol_layer.renderingPass(),
"opacity": symbol_opacity,
}
Expand All @@ -22,7 +24,9 @@ def get_line_symbol_data(symbol_layer: QgsSymbolLayer, symbol_opacity: float) ->
symbol_layer_dict = {
"type": "interpolated",
"color": to_rgba(symbol_layer.color()),
"width": convert_to_point(symbol_layer.width(), symbol_layer.widthUnit()),
"width": get_stroke_width_pt(
symbol_layer.width(), symbol_layer.widthUnit()
),
"level": symbol_layer.renderingPass(),
"opacity": symbol_opacity,
}
Expand Down Expand Up @@ -50,23 +54,29 @@ def get_line_symbol_data(symbol_layer: QgsSymbolLayer, symbol_opacity: float) ->
# TODO: implement
symbol_layer_dict = {
"type": "raster",
"width": convert_to_point(symbol_layer.width(), symbol_layer.widthUnit()),
"width": get_stroke_width_pt(
symbol_layer.width(), symbol_layer.widthUnit()
),
"level": symbol_layer.renderingPass(),
"opacity": symbol_opacity,
}
elif symbol_layer.layerType() == "Lineburst":
# TODO: implement
symbol_layer_dict = {
"type": "lineburst",
"width": convert_to_point(symbol_layer.width(), symbol_layer.widthUnit()),
"width": get_stroke_width_pt(
symbol_layer.width(), symbol_layer.widthUnit()
),
"level": symbol_layer.renderingPass(),
"opacity": symbol_opacity,
}
elif symbol_layer.layerType() == "ArrowLine":
# TODO: implement
symbol_layer_dict = {
"type": "arrow",
"width": convert_to_point(symbol_layer.width(), symbol_layer.widthUnit()),
"width": get_stroke_width_pt(
symbol_layer.width(), symbol_layer.widthUnit()
),
"level": symbol_layer.renderingPass(),
"opacity": symbol_opacity,
}
Expand Down
4 changes: 2 additions & 2 deletions translator/vector/symbol/marker.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from typing import Union, Tuple

from plugx_utils import convert_to_point
from translator.vector.symbol.utils import get_asset_name, to_rgba
from translator.vector.symbol.utils import get_asset_name, to_rgba, get_stroke_width_pt
from translator.vector.symbol.penstyle import get_penstyle_from


Expand Down Expand Up @@ -174,7 +174,7 @@ def get_point_symbol_data(
"size": convert_to_point(symbol_layer.size(), symbol_layer.sizeUnit()),
"color": to_rgba(symbol_layer.color()),
"outline_color": to_rgba(symbol_layer.strokeColor()),
"outline_width": convert_to_point(
"outline_width": get_stroke_width_pt(
symbol_layer.strokeWidth(), symbol_layer.strokeWidthUnit()
),
"outline_penstyle": get_penstyle_from(symbol_layer),
Expand Down
17 changes: 16 additions & 1 deletion translator/vector/symbol/utils.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import os

from qgis.core import QgsSymbolLayer
from qgis.core import QgsSymbolLayer, QgsUnitTypes
from PyQt5.QtGui import QColor
from plugx_utils import convert_to_point


def get_asset_dir(output_dir: str):
Expand All @@ -19,3 +20,17 @@ def to_rgba(color: QColor):
alpha = hex(int(color.alphaF() * 255))[2:].zfill(2)

return f"{color.name()}{alpha}"


def get_stroke_width_pt(defined_width: float, unit: QgsUnitTypes.RenderUnit) -> float:
"""get line width in points, considering hairline or not
symbol layer stroke width is set as 0 when hairline
to be render as 0.5px (0.375pt) as defined in QGIS code
https://github.com/qgis/QGIS/blob/e7f5b6f286b6cbaeaad5e73d69fad1448018c7e0/
tests/src/python/test_qgssymbollayer_createsld.py#L234
"""
if defined_width == 0:
hairline_px_size = 0.5
return convert_to_point(hairline_px_size, QgsUnitTypes.RenderPixels)
else:
return convert_to_point(defined_width, unit)

0 comments on commit d43b7fb

Please sign in to comment.