From c51bac877e7481a7aa2e1fe012e4d22c46d33aa1 Mon Sep 17 00:00:00 2001 From: Artem Pavlenko Date: Fri, 9 Apr 2010 18:46:56 +0000 Subject: [PATCH] + add map_xml upgrade script (mapnik->mapnik2) --- utils/upgrade_map_xml/upgrade_map_xml.py | 59 ++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100755 utils/upgrade_map_xml/upgrade_map_xml.py diff --git a/utils/upgrade_map_xml/upgrade_map_xml.py b/utils/upgrade_map_xml/upgrade_map_xml.py new file mode 100755 index 000000000..151649a57 --- /dev/null +++ b/utils/upgrade_map_xml/upgrade_map_xml.py @@ -0,0 +1,59 @@ +#!/usr/bin/env python +import os +import sys +from lxml import etree +from lxml import objectify + +def name2expr(sym): + name = sym.attrib['name'] + expression = '[%s]' % name + sym.attrib['name'] = expression + +def fixup_pointsym(sym): + if sym.attrib.get('width'): + sym.attrib.pop('width') + if sym.attrib.get('height'): + sym.attrib.pop('height') + #if sym.attrib.get('type'): + # sym.attrib.pop('type') + +def fixup_sym_attributes(sym): + attrib = {} + for css in sym.CssParameter: + key = css.attrib.get('name') + value = css.text + attrib[key]=value + sym.clear() # remove CssParameter children + for k,v in attrib.items(): # insert attributes + sym.attrib[k] = v + + +if __name__ == "__main__": + + if len(sys.argv) != 2: + print>>sys.stderr,'Usage: %s ' % sys.argv[0] + sys.exit(1) + + xml = sys.argv[1] + tree = objectify.parse(xml) + root = tree.getroot() + for style in root.Style: + if len(style.Rule): + for rule in style.Rule: + if hasattr(rule,'TextSymbolizer'): + for sym in rule.TextSymbolizer: + name2expr(sym) + if hasattr(rule,'ShieldSymbolizer'): + for sym in rule.ShieldSymbolizer: + name2expr(sym) + if hasattr(rule,'PointSymbolizer'): + for sym in rule.PointSymbolizer: + fixup_pointsym(sym) + if hasattr(rule,'LineSymbolizer'): + for sym in rule.LineSymbolizer: + fixup_sym_attributes(sym) + + + + print etree.tostring(tree,pretty_print=True,standalone=True) +