Skip to content

Instantly share code, notes, and snippets.

@endarthur
Created June 8, 2020 03:42
Show Gist options
  • Save endarthur/2050e36a705f4fb7078d90917f684345 to your computer and use it in GitHub Desktop.
Save endarthur/2050e36a705f4fb7078d90917f684345 to your computer and use it in GitHub Desktop.
import xmltodict
def parse_categorized_qml(qml_fname):
with open(qml_fname, "rb") as f:
qml = xmltodict.parse(f)
column = qml["qgis"]["renderer-v2"]["@attr"]
symbols = {
symbol["@name"]:{
prop["@k"]:prop["@v"] for prop in symbol["layer"]["prop"]
}
for symbol in qml["qgis"]["renderer-v2"]["symbols"]["symbol"]
}
classes = {
category['@value']:symbols[category['@symbol']]
for category in qml["qgis"]["renderer-v2"]["categories"]["category"]
}
return column, classes
def build_style_callback(column, classes):
styles = {}
for cls, style in classes.items():
*color, opacity = [int(i) for i in style["outline_color"].split(",")]
*fillColor, fillOpacity = [int(i) for i in style["color"].split(",")]
styles[cls] = {
"color": f"#{color[0]:02x}{color[1]:02x}{color[2]:02x}",
"opacity": opacity/255,
"weight": float(style["outline_width"]),
"fillColor": f"#{fillColor[0]:02x}{fillColor[1]:02x}{fillColor[2]:02x}",
"fillOpacity": fillOpacity/255
}
def style_callback(feature):
value = feature["properties"][column]
return styles[value if value is not None else ""]
return style_callback
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment