Skip to content

Instantly share code, notes, and snippets.

@jj0hns0n
Created April 4, 2014 19:59
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 jj0hns0n/9982061 to your computer and use it in GitHub Desktop.
Save jj0hns0n/9982061 to your computer and use it in GitHub Desktop.
import xml.etree.ElementTree as ET
from xml.dom import minidom
namespaces = {
'sld': 'http://www.opengis.net/sld',
'ogc': 'http://www.opengis.net/ogc',
'gml':'http://www.opengis.net/gml'
}
for prefix, uri in namespaces.iteritems():
ET.register_namespace(prefix, uri)
filenames = ['SLD1.txt', 'SLD2.txt']
with open('ConcatenatedSLDs.sld', 'w') as outfile:
out_root = ET.Element("sld:StyledLayerDescriptor")
nl = ET.Element("sld:NamedLayer")
out_root.append(nl)
name = ET.Element("sld:Name")
name.text = "Some Style Name Here"
nl.append(name)
us = ET.Element("sld:UserStyle")
nl.append(us)
title = ET.Element("sld:Title")
title.text = "Some Title Here"
us_name = ET.Element("sld:Name")
us_name.text = "Some Name Here"
us.append(title)
us.append(us_name)
fts = ET.Element("sld:FeatureTypeStyle")
us.append(fts)
fts_name = ET.Element("sld:Name")
fts_name.text = "Some Feature Type Style Name"
fts.append(fts_name)
for fname in filenames:
with open(fname) as infile:
root = ET.fromstring(infile.read())
rules = root.findall('./sld:NamedLayer/sld:UserStyle/sld:FeatureTypeStyle/sld:Rule', namespaces=namespaces)
for rule in rules:
fts.append(rule)
output = ET.tostring(out_root)
reparsed = minidom.parseString(output)
outfile.write(reparsed.toprettyxml(indent=" "))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment