Wikiproyecto:Comunidad Valenciana/generamapas.py
Apariencia
# -*- coding: utf-8 -*-
import codecs
import sys
import os
import re
import xml.dom.minidom as minidom
import xml.dom.ext
re_fill=re.compile(ur"fill:#[\da-fA-F]{6}")
re_muni=re.compile(ur"#muni-(.*)")
def get_a_document(name="sample.svg"):
return minidom.parse(name)
def usage():
print "Usage: %s -m basemap.svg" % os.path.basename(sys.argv[0])
def main():
args = sys.argv[1:]
if "-h" in args or "--help" in args:
usage()
sys.exit(2)
if "-m" in args or "--map" in args:
if len(args) is not 2:
usage()
sys.exit(2)
mapname = args[1]
doc = get_a_document(mapname)
dirname = mapname[:-4]
if not os.path.isdir("./" + dirname + "/"):
os.mkdir("./" + dirname + "/")
paths=doc.getElementsByTagName("path")
labels=[]
for path in paths:
if "inkscape:label" in path.attributes:
label=path.attributes[u"inkscape:label"].value
muni=re_muni.search(label)
if muni:
muniname= muni.group(1)
newmap=muniname+u"-"+dirname.decode("utf-8")+u".svg"
style=path.attributes[u"style"].value
newstyle=re_fill.sub("fill:#ff0000",style)
path.attributes[u"style"].value=newstyle
newmap=re.sub(" ","_",newmap)
print "Writing "+(dirname+"/"+newmap.encode("utf-8"))
file_object = open(dirname+"/"+newmap.encode("utf-8"), "w")
xml.dom.ext.PrettyPrint(doc, file_object)
file_object.close()
path.attributes[u"style"].value=style
doc.unlink()
main()