Skip to content

Instantly share code, notes, and snippets.

@robflaherty
Created January 15, 2012 16:39
Show Gist options
  • Star 18 You must be signed in to star a gist
  • Fork 7 You must be signed in to fork a gist
  • Save robflaherty/1616367 to your computer and use it in GitHub Desktop.
Save robflaherty/1616367 to your computer and use it in GitHub Desktop.
Extract SVG paths and convert to JSON for use with Raphael.js
from xml.dom import minidom
import json
config = {
'svg_file' : 'map.svg',
'js_file' : 'map.js',
'js_var' : 'svgMap'
}
svg = minidom.parse(config['svg_file'])
paths = svg.getElementsByTagName('path')
items = {}
for node in paths:
if node.getAttributeNode('id'):
path_id = str(node.getAttributeNode('id').nodeValue)
path = str(node.getAttributeNode('d').nodeValue)
items[path_id] = path
json = json.dumps(items, indent=2)
f = open(config['js_file'], 'w')
f.write('var %s = ' % config['js_var'])
f.write(json)
f.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment