2010-04-09 20:46:56 +02:00
|
|
|
#!/usr/bin/env python
|
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
from lxml import etree
|
|
|
|
from lxml import objectify
|
2010-04-09 20:47:04 +02:00
|
|
|
import re
|
2010-04-09 20:46:56 +02:00
|
|
|
|
|
|
|
def name2expr(sym):
|
|
|
|
name = sym.attrib['name']
|
2010-04-09 20:47:04 +02:00
|
|
|
if re.match('^\[.*\]$',name) is None:
|
|
|
|
print>>sys.stderr,"Fixing %s" % name
|
|
|
|
expression = '[%s]' % name
|
|
|
|
sym.attrib['name'] = expression
|
|
|
|
|
2010-04-09 20:46:56 +02:00
|
|
|
def fixup_pointsym(sym):
|
|
|
|
if sym.attrib.get('width'):
|
|
|
|
sym.attrib.pop('width')
|
|
|
|
if sym.attrib.get('height'):
|
|
|
|
sym.attrib.pop('height')
|
2010-05-07 21:46:05 +02:00
|
|
|
if sym.attrib.get('type'):
|
|
|
|
sym.attrib.pop('type')
|
2010-04-09 20:46:56 +02:00
|
|
|
|
|
|
|
def fixup_sym_attributes(sym):
|
2010-04-09 20:47:04 +02:00
|
|
|
if not hasattr(sym,'CssParameter'):
|
|
|
|
return
|
2010-04-09 20:46:56 +02:00
|
|
|
attrib = {}
|
|
|
|
for css in sym.CssParameter:
|
|
|
|
key = css.attrib.get('name')
|
|
|
|
value = css.text
|
2010-06-02 14:53:13 +02:00
|
|
|
attrib[key] = value
|
2010-04-09 20:47:04 +02:00
|
|
|
sym.clear() # remove CssParameter elements
|
|
|
|
for k,v in attrib.items(): # insert attributes instead
|
2010-04-09 20:46:56 +02:00
|
|
|
sym.attrib[k] = v
|
|
|
|
|
2010-06-02 14:53:13 +02:00
|
|
|
|
2010-04-09 20:46:56 +02:00
|
|
|
if __name__ == "__main__":
|
2010-04-09 20:47:04 +02:00
|
|
|
|
2010-04-09 20:46:56 +02:00
|
|
|
if len(sys.argv) != 2:
|
|
|
|
print>>sys.stderr,'Usage: %s <map_xml_file>' % 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)
|
2010-04-09 20:47:04 +02:00
|
|
|
if hasattr(rule,'LineSymbolizer') :
|
2010-04-09 20:46:56 +02:00
|
|
|
for sym in rule.LineSymbolizer:
|
|
|
|
fixup_sym_attributes(sym)
|
2010-04-09 20:47:04 +02:00
|
|
|
if hasattr(rule,'PolygonSymbolizer') :
|
|
|
|
for sym in rule.PolygonSymbolizer:
|
|
|
|
fixup_sym_attributes(sym)
|
2010-06-02 14:53:13 +02:00
|
|
|
if hasattr(rule,'RasterSymbolizer') :
|
|
|
|
for sym in rule.RasterSymbolizer:
|
|
|
|
fixup_sym_attributes(sym)
|
|
|
|
if hasattr(rule,'BuildingSymbolizer') :
|
|
|
|
for sym in rule.BuildingSymbolizer:
|
|
|
|
fixup_sym_attributes(sym)
|
2010-04-09 20:46:56 +02:00
|
|
|
|
|
|
|
print etree.tostring(tree,pretty_print=True,standalone=True)
|
|
|
|
|