index.html
<!DOCTYPE html>
<html>
<head>
<title>Czech presidential elections 2013, 2nd round: Zeman vs. Schwarzenberg</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="viewport" content="initial-scale=1.0, user-scalable=no"/>
<script src="//code.jquery.com/jquery-1.8.2.min.js"></script>
<script>
L_PREFER_CANVAS = true;
</script>
<link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.0.2/css/bootstrap.min.css">
<link rel="stylesheet" href="//cdn.leafletjs.com/leaflet-0.6.4/leaflet.css" />
<script src="//cdn.leafletjs.com/leaflet-0.6.4/leaflet.js"></script>
<style type="text/css">
html, body, #map {
width: 100%;
height: 100%;
margin: 0;
padding: 0;
}
</style>
</head>
<body>
<div class="navbar navbar-inverse navbar-fixed-top" role="navigation">
<div class="container">
<div class="navbar-header">
<a class="navbar-brand" href="#">Czech presidential elections 2013, 2nd round: Zeman vs. Schwarzenberg</a>
</div>
</div>
</div>
<div style="position:fixed;top:50px;z-index:1000;">
<div class="alert alert-info" >
Look inside cities and towns! Each bubble represents one polling station.<br/>
The <strong>size</strong> of bubbles represents number of voters, the <strong>color</strong> represents the winner and the <strong>oppacity</strong> the margin of victory. <span style="color:#b00">Zeman</span> vs. <span style="color:#b0b">Schwarzenberg</span><br/>
<strong><em>There are still some missing data that are shown only aggregated, notably Ústí n.L., part of Brno, Praha 1 and 3, Plzeň 1, Opava.</em></strong>
</div>
</div>
<div id="map" style="margin-top:40px;"></div>
<script type="text/javascript">
var map = L.map('map',{zoomControl: false}).setView([50,15], 8);
map.addControl( L.control.zoom({position: 'topright'}) );
L.tileLayer('//{s}.www.toolserver.org/tiles/bw-mapnik/{z}/{x}/{y}.png', {
attribution: '© <a href="//osm.org/copyright">OpenStreetMap</a> contributors'
}).addTo(map);
$.getJSON( "cz_president_2013_okrsky_2_ring.json", function (data) {
$.each(data, function (index, value) {
circle = L.circle([value.coordinates[1], value.coordinates[0]], Math.sqrt((parseInt(value.population.p6)+parseInt(value.population.p9))*150), {
color: class2color(value.classname),
fillColor: class2color(value.classname),
fillOpacity: 2*(Math.max(parseInt(value.population.p6),parseInt(value.population.p9)) / (parseInt(value.population.p6)+parseInt(value.population.p9))-0.5),
weight: 0.1,
className: value.classname,
}).addTo(map);
perc1 = Math.round(Math.max(parseInt(value.population.p6),parseInt(value.population.p9)) / (parseInt(value.population.p6)+parseInt(value.population.p9)) * 100);
perc2 = 100 - perc1;
circle.bindPopup(value.name + "<br>" + value.winner + " vyhrál " + perc1 + " % vs. " + perc2 + " % <br>(" +Math.max(value.population.p6,value.population.p9) + " : " + Math.min(value.population.p6,value.population.p9) + " hlasů)");
});
});
function class2color(className) {
if (className == 'zeman') return "#b00";
if (className == 'schwarzenberg') return "#b0b";
else return "#fff";
}
</script>
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-8592359-13', 'ocks.org');
ga('send', 'pageview');
</script>
</body>
</html>
csv2json.py
import json
import csv
import string
data = []
i = 0
with open("okrsky_geo_adj.csv","r") as fin:
finreader = csv.reader(fin)
for row in finreader:
if i>0:
if ((row[2] == "Orlová") and (row[4] == '9')):
print row
if (int(row[22]) >= int(row[23])):
winner = "Zeman"
else:
winner = "Schwarzenberg"
item = {
'id': string.join([row[3],row[4]],sep="-"),
'coordinates': [row[25],row[26]],
'population': {'p6': row[22], 'p9': row[23]},
'name': string.join([row[2],row[4]],sep="-"),
'winner': winner,
'classname': winner.lower()
}
if ((row[2] == "Orlová") and (row[4] == '9')):
print item
data.append(item)
i = i + 1
with open('cz_president_2013_okrsky_2_ring.json', 'w') as outfile:
json.dump(data, outfile)
print i
okrsky_join.py
import csv
o2nado = {}
m = 0
with open("centroids.csv","r") as fcentroids:
fcreader = csv.reader(fcentroids, delimiter="\t")
for row in fcreader:
if (m>0):
if row[9] != '':
o2nado[row[9]] = row[8]
else:
o2nado[row[8]] = row[8]
m = m + 1
okrsky = {}
m = {}
with open("president_2013_okrsky.csv","r") as fin:
finreader = csv.reader(fin)
for row in finreader:
try:
okrsky[row[3]]
except:
okrsky[row[3]] = {row[4]: {'original': row}}
else:
okrsky[row[3]][row[4]] = {'original': row}
try:
o2nado[row[3]]
except:
m[row[3]] = row[3]
else:
okrsky[row[3]][row[4]]['original'] = okrsky[row[3]][row[4]]['original'] + [o2nado[row[3]]]
print m
i = 0
k = 0
with open("centroids.csv","r") as fcentroids:
fcreader = csv.reader(fcentroids, delimiter="\t")
for row in fcreader:
try:
okrsky[row[8]][row[7]]
except:
try:
okrsky[row[9]][row[7]]
except:
i = i + 1
else:
okrsky[row[9]][row[7]]['geography'] = [row[0],row[1]]
else:
okrsky[row[8]][row[7]]['geography'] = [row[0],row[1]]
print k
print i
j = 0
writer = csv.writer(open('okrsky_geo.csv', 'wb'))
for key0 in okrsky:
for key1 in okrsky[key0]:
try: okrsky[key0][key1]['geography']
except:
vals = okrsky[key0][key1]['original']
else:
vals = okrsky[key0][key1]['original'] + okrsky[key0][key1]['geography']
writer.writerow(vals)