Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More SVG marker capabilities #1005

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
40 changes: 27 additions & 13 deletions qgis2web/leafletStyleScripts.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os
import re
import shutil
from qgis.core import (QgsSingleSymbolRenderer,
QgsCategorizedSymbolRenderer,
Expand Down Expand Up @@ -62,7 +63,8 @@ def getLayerStyle(layer, sln, interactivity, markerFolder,
(styleCode, markerType, useMapUnits,
pattern) = getSymbolAsStyle(cat.symbol(), markerFolder,
layer_alpha, interactivity, sln,
sl, useMapUnits, feedback)
sl, useMapUnits, feedback,
cat.value())
patterns += pattern
if (cat.value() is not None and cat.value() != ""):
style += """
Expand Down Expand Up @@ -167,7 +169,7 @@ def getLayerStyle(layer, sln, interactivity, markerFolder,


def getSymbolAsStyle(symbol, markerFolder, layer_transparency, interactivity,
sln, sl, useMapUnits, feedback):
sln, sl, useMapUnits, feedback, label=''):
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had to add a label param to getSymbolAsStyle() so that the category label could be used in the derivative SVG file name.

interactive = str(interactivity).lower()
markerType = None
pattern = ""
Expand Down Expand Up @@ -221,25 +223,37 @@ def getSymbolAsStyle(symbol, markerFolder, layer_transparency, interactivity,
rot += ") * 0.0174533"
else:
rot = str(sl.angle() * 0.0174533)
safeLabel = '' if not label else '_' + re.sub(r'[\W_]+', '', str(label))
style = """
rotationAngle: %s,
rotationOrigin: 'center center',
icon: %s""" % (rot, getIcon("markers/" + sln + ".svg", svgSize))
icon: %s""" % (rot, getIcon("markers/" + sln + safeLabel + ".svg", svgSize))
markerType = "marker"

# save a colorized svg in the markers folder
# Save a colorized svg in the markers folder
# replacing "param(...)" with actual values from QGIS
# and renaming to safe layer name
pColor = getRGBAColor(props["color"], alpha).strip("'"))
pOutline = getRGBAColor(props["outline_color"], alpha).strip("'"))
# and renaming to safe layer name.
#
# Note that svg attributes with params sometimes also have default values
# like: stroke-width="param(outline-width) 1"
# but we need to replace the whole attribute value.
pColor = getRGBAColor(props["color"], alpha)
pOutline = getRGBAColor(props["outline_color"], alpha)
with open(sl.path()) as f:
s = f.read()
s = s.replace('param(fill)', pColor)
s = s.replace('param(fill-opacity)', '1')
s = s.replace('param(outline)', pOutline)
s = s.replace('param(outline-width)', props["outline_width"])
s = s.replace('param(outline-opacity)', '1')
with open(os.path.join(markerFolder, sln + ".svg"), 'w') as f:
# adjust outline width to account for svg symbol size
viewboxSize = float(re.search('viewBox="([^"]*)"', s)
.group(1).split(' ')[-1])
outlinefactor = viewboxSize / sl.size()
pOutlineWidth = '"{}"'.format(float(props["outline_width"]) * outlinefactor)
s = re.sub('"param\(fill\)[^"]*"', pColor, s)
s = re.sub('"param\(fill-opacity\)[^"]*"', '"1"', s)
s = re.sub('"param\(outline\)[^"]*"', pOutline, s)
s = re.sub('"param\(outline-width\)[^"]*"', pOutlineWidth, s)
s = re.sub('"param\(outline-opacity\)[^"]*"', '"1"', s)
safeLabel = '' if not label else '_' + re.sub(r'[\W_]+', '', str(label))
markerPath = os.path.join(markerFolder, sln + safeLabel + ".svg")
with open(markerPath, 'w') as f:
f.write(s)
elif isinstance(sl, QgsSimpleLineSymbolLayer):
color = getRGBAColor(props["line_color"], alpha)
Expand Down