Skip to content

Instantly share code, notes, and snippets.

@tecoholic
Created December 20, 2012 09:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tecoholic/4344168 to your computer and use it in GitHub Desktop.
Save tecoholic/4344168 to your computer and use it in GitHub Desktop.
The script converts the old QGIS symbol XML structure using the @ notation for sub-symbols into a nested XML DOM structure for proper import in newer versions of QGIS (i.e >=1.9)
"""
/***************************************************************************
symbol_nesting.py
-------------------
begin : 20-12-2012
copyright : (C) 2012 by Arunmozhi
email : aruntheguy at gmail dot com
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
The script cconverts the sold symbols XML structure using the @ notation
to the new multiple nested XML structure.
"""
import sys
import codecs
from xml.dom.minidom import parse
from xml.dom.minidom import getDOMImplementation
def convertXMLStructure( filepath ):
''' The function converts the old XML into new XML with nesting'''
indom = parse( filepath )
impli = getDOMImplementation()
outdom = impli.createDocument( None, "qgis_style", None )
outsymdom = outdom.createElement("symbols")
outdom.documentElement.setAttribute("version", "0")
outdom.documentElement.appendChild( outsymdom )
symbols = indom.getElementsByTagName( "symbol" )
for symbol in symbols:
symbol_name = symbol.getAttribute("name")
if '@' not in symbol_name:
# Skip the sub-symbols in first iteration
outsymdom.appendChild( symbol )
outsymbols = outsymdom.getElementsByTagName( "symbol" )
# Now fix the sub-symbols
for symbol in symbols:
symbol_name = symbol.getAttribute("name")
if '@' in symbol_name:
parts = symbol_name.split('@')
parent_name = parts[1]
layerno = int(parts[2])
# get the parent symbol and insert the sub-symbol in correct layer
for sym in outsymbols:
sym_name = sym.getAttribute("name")
if sym_name == parent_name:
sym.getElementsByTagName( "layer" )[ layerno ].appendChild( symbol )
rampdom = indom.getElementsByTagName("colorramps")[0]
outdom.documentElement.appendChild(rampdom)
opf = codecs.open( filepath+"new.xml", mode="w", encoding="utf-8")
opf.write( outdom.toxml() )
if __name__ == "__main__":
if len( sys.argv ) == 2:
convertXMLStructure( sys.argv[1] )
else:
print "Usage: python symbol_nesting.py style_filename.xml"\
"Output: A new file with name: style_filename.xmlnew.xml"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment