File size: 1,396 Bytes
7e4e381
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
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