Pastie now auto-senses if line-wrap is a bad or good idea. Feedback?
## mark a section (Learn more)
import getopt, sys, csv from BeautifulSoup import BeautifulSoup def colorize(paths,legend,colors,data): path_style = 'fill-opacity:1;fill:' for p in paths: if p['id'].startswith('RU-'): try: rate = data[p['id']] except: continue i = len(colors)-1 color_class = 0 for lv in legend: if rate > lv: color_class = i color = colors[color_class] p['style'] = path_style + color continue i -= 1 def main(): try: opts, args = getopt.getopt(sys.argv[1:], "vd:c:l:i:o:", ["data-file=", "colors-file=", "legend-file", "input-file=", "output-file="] ) except getopt.GetoptError: print str(err) sys.exit(2) verbose = False datafile = "" colorsfile = "" legendfile = "" inputfile = "" outputfile = "" for o, a in opts: if o == "-v": verbose = True elif o in ("-d", "--data-file"): datafile = a elif o in ("-c", "--colors-file"): colorsfile = a elif o in ("-l", "--legend-file"): legendfile = a elif o in ("-i", "--input-file"): inputfile = a elif o in ("-o", "--output-file"): outputfile = a else: assert False, "unhandled option" if datafile == "": print "No input file specified" sys.exit(2) if colorsfile == "": print "No colors file specified" sys.exit(2) if legendfile == "": print "No legend file specified" sys.exit(2) if inputfile == "": print "No input file specified" sys.exit(2) if outputfile == "": print "No output file specified" sys.exit(2) # read the color scheme f = file(colorsfile, "r") colors = list() for line in f.readlines(): colors.extend(line.split()) f.close() #read the legend values l = file(legendfile, "r") legend = list() for line in l.readlines(): legend.extend(map(float,line.split())) l.close() #read data data = {} csvReader = csv.reader(open(datafile), delimiter=",") for row in csvReader: fips = row[0] data[fips] = float( row[1] ) #read SVG map svg = open(inputfile, 'r').read() soup = BeautifulSoup(svg) paths = soup.findAll('path') gs = soup.findAll('g') #print "%s: %s" % ('Colors', colors) #print "%s: %s" % ('Legend', legend) colorize(paths,legend,colors,data) colorize(gs,legend,colors,data) outsvg = open(outputfile, 'wt') outsvg.write(soup.prettify()) outsvg.close() if __name__ == "__main__": main()
This paste will be private.
From the Design Piracy series on my blog: