2010-04-09 18:46:56 +00:00
|
|
|
#!/usr/bin/env python
|
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
from lxml import etree
|
|
|
|
from lxml import objectify
|
2010-04-09 18:47:04 +00:00
|
|
|
import re
|
2010-04-09 18:46:56 +00:00
|
|
|
|
|
|
|
def name2expr(sym):
|
|
|
|
name = sym.attrib['name']
|
2010-04-09 18:47:04 +00:00
|
|
|
if re.match('^\[.*\]$',name) is None:
|
|
|
|
print>>sys.stderr,"Fixing %s" % name
|
|
|
|
expression = '[%s]' % name
|
|
|
|
sym.attrib['name'] = expression
|
|
|
|
|
2010-04-09 18:46:56 +00: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 19:46:05 +00:00
|
|
|
if sym.attrib.get('type'):
|
|
|
|
sym.attrib.pop('type')
|
2010-04-09 18:46:56 +00:00
|
|
|
|
|
|
|
def fixup_sym_attributes(sym):
|
2010-04-09 18:47:04 +00:00
|
|
|
if not hasattr(sym,'CssParameter'):
|
|
|
|
return
|
2010-04-09 18:46:56 +00:00
|
|
|
attrib = {}
|
2010-08-16 19:14:21 +00:00
|
|
|
metawriter = sym.attrib.get('meta-writer')
|
|
|
|
if metawriter:
|
|
|
|
attrib['meta-writer'] = metawriter
|
|
|
|
metaoutput = sym.attrib.get('meta-output')
|
|
|
|
if metaoutput:
|
|
|
|
attrib['meta-output'] = metaoutput
|
|
|
|
|
2010-04-09 18:46:56 +00:00
|
|
|
for css in sym.CssParameter:
|
|
|
|
key = css.attrib.get('name')
|
|
|
|
value = css.text
|
2010-06-02 12:53:13 +00:00
|
|
|
attrib[key] = value
|
2010-04-09 18:47:04 +00:00
|
|
|
sym.clear() # remove CssParameter elements
|
|
|
|
for k,v in attrib.items(): # insert attributes instead
|
2010-04-09 18:46:56 +00:00
|
|
|
sym.attrib[k] = v
|
|
|
|
|
2010-06-02 12:53:13 +00:00
|
|
|
|
2010-04-09 18:46:56 +00:00
|
|
|
if __name__ == "__main__":
|
2010-04-09 18:47:04 +00:00
|
|
|
|
2010-06-29 08:59:50 +00:00
|
|
|
#required parameters:
|
|
|
|
# map_xml_file: outdated stylesheet file
|
|
|
|
# output_file: new stylesheet file
|
|
|
|
|
|
|
|
if len(sys.argv) != 3:
|
|
|
|
print>>sys.stderr,'Usage: %s <map_xml_file> <output_file>' % sys.argv[0]
|
2010-04-09 18:46:56 +00:00
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
xml = sys.argv[1]
|
|
|
|
tree = objectify.parse(xml)
|
2010-08-10 06:01:16 +00:00
|
|
|
tree.xinclude()
|
2010-04-09 18:46:56 +00:00
|
|
|
root = tree.getroot()
|
2010-07-19 12:01:34 +00:00
|
|
|
|
|
|
|
# rename 'bgcolor' to 'background-color'
|
|
|
|
if root.attrib.get('bgcolor'):
|
|
|
|
root.attrib['background-color'] = root.attrib.get('bgcolor')
|
|
|
|
root.attrib.pop('bgcolor')
|
|
|
|
|
2010-04-09 18:46:56 +00:00
|
|
|
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 18:47:04 +00:00
|
|
|
if hasattr(rule,'LineSymbolizer') :
|
2010-04-09 18:46:56 +00:00
|
|
|
for sym in rule.LineSymbolizer:
|
|
|
|
fixup_sym_attributes(sym)
|
2010-04-09 18:47:04 +00:00
|
|
|
if hasattr(rule,'PolygonSymbolizer') :
|
|
|
|
for sym in rule.PolygonSymbolizer:
|
|
|
|
fixup_sym_attributes(sym)
|
2010-06-02 12:53:13 +00: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 18:46:56 +00:00
|
|
|
|
2010-06-29 08:59:50 +00:00
|
|
|
updated_xml = etree.tostring(tree,pretty_print=True,standalone=True)
|
2010-04-09 18:46:56 +00:00
|
|
|
|
2010-06-29 08:59:50 +00:00
|
|
|
output_file = open(sys.argv[2], 'w')
|
|
|
|
|
|
|
|
output_file.write(updated_xml)
|
|
|
|
|
|
|
|
output_file.close()
|