2010-04-09 20:46:56 +02:00
|
|
|
#!/usr/bin/env python
|
|
|
|
import os
|
2010-08-26 22:42:50 +02:00
|
|
|
import re
|
2010-04-09 20:46:56 +02:00
|
|
|
import sys
|
2010-08-21 00:20:45 +02:00
|
|
|
import tempfile
|
2010-08-26 22:42:50 +02:00
|
|
|
|
|
|
|
try:
|
|
|
|
import lxml.etree as etree
|
|
|
|
except ImportError:
|
|
|
|
try:
|
|
|
|
import xml.etree.ElementTree as etree
|
|
|
|
except ImportError:
|
|
|
|
import elementtree.ElementTree as etree
|
2010-04-09 20:46:56 +02:00
|
|
|
|
2010-08-21 00:20:45 +02:00
|
|
|
def indent(elem, level=0):
|
|
|
|
""" http://infix.se/2007/02/06/gentlemen-indent-your-xml
|
|
|
|
"""
|
|
|
|
i = "\n" + level*" "
|
|
|
|
if len(elem):
|
|
|
|
if not elem.text or not elem.text.strip():
|
|
|
|
elem.text = i + " "
|
|
|
|
for e in elem:
|
|
|
|
indent(e, level+1)
|
|
|
|
if not e.tail or not e.tail.strip():
|
|
|
|
e.tail = i + " "
|
|
|
|
if not e.tail or not e.tail.strip():
|
|
|
|
e.tail = i
|
|
|
|
else:
|
|
|
|
if level and (not elem.tail or not elem.tail.strip()):
|
|
|
|
elem.tail = i
|
|
|
|
|
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-08-26 22:42:50 +02:00
|
|
|
|
|
|
|
def handle_attr_changes(sym):
|
|
|
|
# http://www.w3schools.com/css/pr_text_text-transform.asp
|
|
|
|
# http://code.google.com/p/mapnik-utils/issues/detail?id=32&colspec=ID%20Type%20Status%20Priority%20Component%20Owner%20Summary
|
|
|
|
text_transform = sym.attrib.get('text_convert')
|
|
|
|
# note: css supports text-transform:capitalize but Mapnik does not (yet)
|
|
|
|
t_ = {'tolower':'uppercase','toupper':'lowercase','none':'none'}
|
|
|
|
if text_transform:
|
|
|
|
new = t_.get(text_transform)
|
|
|
|
if new:
|
|
|
|
sym.attrib['text_transform'] = new
|
|
|
|
|
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-08-26 22:42:50 +02:00
|
|
|
#if not sym.find('CssParameter'):
|
|
|
|
# return
|
2010-04-09 20:46:56 +02:00
|
|
|
attrib = {}
|
2010-08-16 21:14:21 +02: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-08-26 22:42:50 +02:00
|
|
|
for css in sym.findall('CssParameter'):
|
2010-04-09 20:46:56 +02:00
|
|
|
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-06-29 10:59:50 +02:00
|
|
|
#required parameters:
|
|
|
|
# map_xml_file: outdated stylesheet file
|
|
|
|
# output_file: new stylesheet file
|
|
|
|
|
|
|
|
if len(sys.argv) != 3:
|
2010-08-21 00:20:45 +02:00
|
|
|
sys.stderr.write('Usage: %s <map_xml_file> <output_file>\n' % os.path.basename(sys.argv[0]))
|
2010-04-09 20:46:56 +02:00
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
xml = sys.argv[1]
|
2010-08-26 22:42:50 +02:00
|
|
|
tree = etree.parse(xml)
|
2010-08-26 22:45:08 +02:00
|
|
|
if hasattr(tree,'xinclude')
|
|
|
|
tree.xinclude()
|
2010-04-09 20:46:56 +02:00
|
|
|
root = tree.getroot()
|
2010-07-19 14:01:34 +02:00
|
|
|
|
|
|
|
# rename 'bgcolor' to 'background-color'
|
|
|
|
if root.attrib.get('bgcolor'):
|
|
|
|
root.attrib['background-color'] = root.attrib.get('bgcolor')
|
|
|
|
root.attrib.pop('bgcolor')
|
|
|
|
|
2010-08-26 22:42:50 +02:00
|
|
|
styles = root.findall('Style')
|
|
|
|
if not len(styles):
|
2010-08-21 00:20:45 +02:00
|
|
|
sys.stderr.write('### Warning, no styles encountered and nothing able to be upgraded!\n')
|
2010-08-26 22:42:50 +02:00
|
|
|
else:
|
|
|
|
for style in styles:
|
|
|
|
for rule in style.findall('Rule'):
|
|
|
|
for sym in rule.findall('TextSymbolizer') or []:
|
|
|
|
name2expr(sym)
|
|
|
|
#handle_attr_changes(sym)
|
|
|
|
for sym in rule.findall('ShieldSymbolizer') or []:
|
|
|
|
name2expr(sym)
|
|
|
|
for sym in rule.findall('PointSymbolizer') or []:
|
|
|
|
fixup_pointsym(sym)
|
|
|
|
for sym in rule.findall('LineSymbolizer') or []:
|
|
|
|
fixup_sym_attributes(sym)
|
|
|
|
for sym in rule.findall('PolygonSymbolizer') or []:
|
|
|
|
fixup_sym_attributes(sym)
|
|
|
|
for sym in rule.findall('RasterSymbolizer') or []:
|
|
|
|
fixup_sym_attributes(sym)
|
|
|
|
for sym in rule.findall('BuildingSymbolizer') or []:
|
|
|
|
fixup_sym_attributes(sym)
|
2010-06-29 10:59:50 +02:00
|
|
|
|
2010-08-21 00:20:45 +02:00
|
|
|
updated_xml = etree.tostring(tree)
|
|
|
|
(handle, path) = tempfile.mkstemp(suffix='.xml', prefix='mapnik-')
|
|
|
|
os.close(handle)
|
|
|
|
open(path,'w').write(updated_xml)
|
|
|
|
indented = etree.parse(path)
|
|
|
|
indent(indented.getroot())
|
|
|
|
output_file = open(sys.argv[2], 'w')
|
|
|
|
output_file.write(etree.tostring(indented))
|
|
|
|
output_file.close()
|