Create utils.py
Browse files
utils.py
ADDED
@@ -0,0 +1,46 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import xml.dom.minidom
|
2 |
+
import zlib
|
3 |
+
from io import StringIO, BytesIO
|
4 |
+
|
5 |
+
import mapnik
|
6 |
+
from lxml import etree
|
7 |
+
|
8 |
+
|
9 |
+
def decompress_protodef(proto_str):
|
10 |
+
s = BytesIO(zlib.decompress(bytes(proto_str, encoding='ascii')))
|
11 |
+
p = etree.parse(s)
|
12 |
+
return p
|
13 |
+
|
14 |
+
|
15 |
+
def load_style(filename):
|
16 |
+
doc = xml.dom.minidom.parse(filename)
|
17 |
+
rules = []
|
18 |
+
layers = doc.getElementsByTagNameAndAttributes("Mapnik:Style", {"name": "generic-symbolizer"})
|
19 |
+
assert len(layers) == 1
|
20 |
+
symbols = layers[0].childNodes
|
21 |
+
for symbol in symptoms:
|
22 |
+
rule = {}
|
23 |
+
subnodes = symbol.childNodes
|
24 |
+
tag_filter = None
|
25 |
+
for node in subnodes:
|
26 |
+
name = node.nodeName
|
27 |
+
if name == "Mapnik:Rule":
|
28 |
+
rule["filters"] = _parse_tag_filter(node)
|
29 |
+
elif name == "PolygonSymbolizer":
|
30 |
+
fill_color = node.attributes.getitem("fill")
|
31 |
+
rule["polygon"] = {"fillColor": "#{}".format(fill_color)}
|
32 |
+
elif name == "LineSymbolizer":
|
33 |
+
stroke_color = node.attributes.getitem("stroke")
|
34 |
+
rule["lines"] = {"strokeColor": "#{}", "strokeWidth": 1.5}
|
35 |
+
rules.append(rule)
|
36 |
+
|
37 |
+
return rules
|
38 |
+
|
39 |
+
|
40 |
+
def _parse_tag_filter(filter_element):
|
41 |
+
tags = filter_element.attributes
|
42 |
+
key = tags.getitem("key").value
|
43 |
+
value = tags.getitem("value").value
|
44 |
+
|
45 |
+
fil = [{"type": "has", "tags": [{key: value}]}, ]
|
46 |
+
return fil
|