Skip to content

Instantly share code, notes, and snippets.

@vikjam
Last active October 10, 2015 22:18
Show Gist options
  • Save vikjam/3759108 to your computer and use it in GitHub Desktop.
Save vikjam/3759108 to your computer and use it in GitHub Desktop.
Quick replacement of colored pins by school attributes
#!/usr/local/bin/env python
"""
Assign colors to each school's pin
"""
from BeautifulSoup import BeautifulSoup
# Hash to store style values
styles = {}
# Load schools
top50map = open('Top 50 Alabama LEP Schools.kml', 'r').read()
# Load US map SVG into Beautiful Soup
soup = BeautifulSoup(top50map)
g = open( 'top50v2.kml', 'w')
g.write( soup.prettify() )
# Find all schools and their associated style
places = soup.findAll('placemark')
for p in places:
percentvalue = int(p.findAll('description')[0].text.split('>')[1].split('%<')[0].strip())
styleid = p.findAll('styleurl')[0].text.strip().replace('#', '')
styles[styleid] = percentvalue
styles_css = soup.findAll('style')
for s in styles_css:
style_id = s['id']
if styles[style_id] > 59:
s.findAll('href')[0].string = 'https://dl.dropbox.com/u/1501412/60percent.png'
elif styles[style_id] > 49:
s.findAll('href')[0].string = 'https://dl.dropbox.com/u/1501412/50percent.png'
elif styles[style_id] > 39:
s.findAll('href')[0].string = 'https://dl.dropbox.com/u/1501412/40percent.png'
elif styles[style_id] > 29:
s.findAll('href')[0].string = 'https://dl.dropbox.com/u/1501412/30percent.png'
elif styles[style_id] > 19:
s.findAll('href')[0].string = 'https://dl.dropbox.com/u/1501412/20percent.png'
elif styles[style_id] > 9:
s.findAll('href')[0].string = 'https://dl.dropbox.com/u/1501412/10percent.png'
else:
s.findAll('href')[0].string = 'https://dl.dropbox.com/u/1501412/0percent.png'
f = open( 'top50.kml', 'w')
f.write( soup.prettify() )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment