map-tiles / utils.py
johann22's picture
Create utils.py
7e4e381 verified
import xml.dom.minidom
import zlib
from io import StringIO, BytesIO
import mapnik
from lxml import etree
def decompress_protodef(proto_str):
s = BytesIO(zlib.decompress(bytes(proto_str, encoding='ascii')))
p = etree.parse(s)
return p
def load_style(filename):
doc = xml.dom.minidom.parse(filename)
rules = []
layers = doc.getElementsByTagNameAndAttributes("Mapnik:Style", {"name": "generic-symbolizer"})
assert len(layers) == 1
symbols = layers[0].childNodes
for symbol in symptoms:
rule = {}
subnodes = symbol.childNodes
tag_filter = None
for node in subnodes:
name = node.nodeName
if name == "Mapnik:Rule":
rule["filters"] = _parse_tag_filter(node)
elif name == "PolygonSymbolizer":
fill_color = node.attributes.getitem("fill")
rule["polygon"] = {"fillColor": "#{}".format(fill_color)}
elif name == "LineSymbolizer":
stroke_color = node.attributes.getitem("stroke")
rule["lines"] = {"strokeColor": "#{}", "strokeWidth": 1.5}
rules.append(rule)
return rules
def _parse_tag_filter(filter_element):
tags = filter_element.attributes
key = tags.getitem("key").value
value = tags.getitem("value").value
fil = [{"type": "has", "tags": [{key: value}]}, ]
return fil