block by michalskop 8379520

CZ Senate Elections; 2014 (Winners in Polling Station)

Full Screen

index.html

<!DOCTYPE html>
<html>
  <head>
    <title>Senate Elections 2014</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>
	  // see //leafletjs.com/reference.html
	  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" />
	 <!--[if lte IE 8]>
		 <link rel="stylesheet" href="//cdn.leafletjs.com/leaflet-0.6.4/leaflet.ie.css" />
	 <![endif]-->
    <script src="//cdn.leafletjs.com/leaflet-0.6.4/leaflet.js"></script>
    <!--<script type="text/javascript" src="//mbostock.github.com/d3/d3.js?1.29.1"></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="#">Senate Elections 2014</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.
      </div>

    </div>
    <div id="map" style="margin-top:40px;"></div>
    <script type="text/javascript">
		// Create the map
		var map = L.map('map',{zoomControl: false}).setView([49.1,17.77], 11);
		map.addControl( L.control.zoom({position: 'topright'}) );
		
		// add an OpenStreetMap tile layer
		// also see //wiki.openstreetmap.org/wiki/Tiles
		//L.tileLayer('//{s}.tile.osm.org/{z}/{x}/{y}.png', {
		L.tileLayer('//{s}.www.toolserver.org/tiles/bw-mapnik/{z}/{x}/{y}.png', {
			attribution: '&copy; <a href="//osm.org/copyright">OpenStreetMap</a> contributors'
		}).addTo(map);
		
		//add circles
		//$.getJSON( "cz_president_2013_both_2_ring.json", function (data) {
		$.getJSON( "zlin.json", function (data) {
		    $.each(data, function (index, value) {
		        sum = 0;
		        descr = '';
		        $.each(value['person'], function (i,d) {
		          sum = sum + parseInt(d['population']);
		          descr += d['name'] + ", " + d['party'] + " (" + d['population'] + ")<br>";
		        })
				circle = L.circle(
				  [value.coordinates[0], value.coordinates[1]],
				   Math.sqrt(sum*400), 
				  {
					color: values2color(value['winner']),
					fillColor: values2color(value['winner']),
					//fillOpacity: 2*(Math.max(parseInt(value.population.p6),parseInt(value.population.p9)) / (parseInt(value.population.p6)+parseInt(value.population.p9))-0.5),
					fillOpacity: .8,
					weight: 0.1,
					//className: value.classname,	//this does not seem to work, so hacked by class2color
				}).addTo(map);
				
				
				circle.bindPopup("<strong>" + value.name + "</strong><br>" + "Vyhrál/a <strong>" + value.winner + '</strong><br>' + descr);
		  });
		});
		
		function values2color(w) {
          if (w == 'Talaš Pavel MUDr.') return '#0ff';
          if (w == 'Juřenčáková Jana Ing.') return '#444';
          if (w == 'Rafaja Radomír') return '#800';
          if (w == 'Maděra Luděk Ing.') return '#000';
          if (w == 'Lukáš Libor') return '#008';
          if (w == 'Kunčar Patrik Bc.') return '#fedc35';
          if (w == 'Zdráhalová Lapčíková Zuzana Mgr.') return '#808';
          if (w == 'Remeš Jiří') return '#080';
          if (w == 'Kovaříková Milena Mgr.') return '#f54200';
		}
	
	</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.php

<?php
//join election results with list of all stations and output lat,lng,results into json
// we want to get such structure (similar):
// {"name": "\u00dast\u00ed nad Labem-0", "winner": "Zeman", "coordinates": ["14.036975", "50.660316"], "id": "554804-0", "population": {"p9": "16025", "p6": "21011", etc. }, "party" : {"p1": "Úsvit",  etc. }, "names": ["p1": "Talaš ..", etc.}},

$handle_results = fopen('zlin_stations.csv','r');
$handle_geo = fopen('okrsky_geo_adj.csv','r');
$handle_out = fopen('zlin.json','w');

// read results
$results = array();
while (($row = fgetcsv($handle_results, 10000, ",")) !== FALSE) {
  $results[] = $row;
}

// read geo
$geo = array();
while (($row = fgetcsv($handle_geo, 10000, ",")) !== FALSE) {
  // we want to have access to data through code and station number:
  $key1 = $row[3];
  $key2 = $row[4];
  $geo[$key1][$key2] = $row;
}

//print_r($geo['592731']);//die();



$data = array();
// for each results find geo:
foreach ($results as $row) {
  // $row[0] ~ code, $row[2] ~ station
  // e.g., $geo['592013']['1'] (it stores the first row of the zlin_stations.csv)
  // we need to check if we have the geo
  if (isset($geo[$row[0]]) and isset($geo[$row[0]][$row[2]])) { 
    $data[$row[0]][$row[2]]['name'] = $row[1];  //we put it there several times, but we dont care...
    $data[$row[0]][$row[2]]['id'] = $row[0] . '-' . $row[2]; // the same here and for the coordinates
    $data[$row[0]][$row[2]]['person']['p'.$row[3]]['population'] = $row[8];
    $data[$row[0]][$row[2]]['person']['p'.$row[3]]['party'] = $row[5];
    $data[$row[0]][$row[2]]['person']['p'.$row[3]]['name'] = $row[4];
    $data[$row[0]][$row[2]]['coordinates'] = array(
      $geo[$row[0]][$row[2]][26],
      $geo[$row[0]][$row[2]][25]
    );
  }
  //print_r($data);die();
}

// we want to find the winner 
foreach ($data as $rkey => $row) {
  foreach ($row as $skey => $station) {
    $winner = '';
    $n = 0;
    $i = 1;
    //print_r($station);
    foreach ($station['person'] as $person) {
      $nvotes = $person['population'];
      print_r ($nvotes);
      if ($nvotes > $n) {
        $winner = $person['name'];
        $winneri = $i;
        $n = $nvotes;
      }
      $i++;
    }
    $data[$rkey][$skey]['winner'] = $winner;
    $data[$rkey][$skey]['winneri'] = $winneri;
  }
}

//we need to reorder the array, for the correct output
$out = array();
foreach ($data as $row) {
  foreach ($row as $station) {
    $out[] = $station;
  }
}

fwrite($handle_out, json_encode($out));
fclose($handle_out);



?>

disticts_list.csv

592013,Babice,se2111?xjazyk=CZ&xdatum=20140110&xobvod=80&xobec=592013
592021,Bánov,se2111?xjazyk=CZ&xdatum=20140110&xobvod=80&xobec=592021&xokrsek=1,1
592021,Bánov,se2111?xjazyk=CZ&xdatum=20140110&xobvod=80&xobec=592021&xokrsek=2,2
588318,Bělov,se2111?xjazyk=CZ&xdatum=20140110&xobvod=80&xobec=588318
592030,Bílovice,se2111?xjazyk=CZ&xdatum=20140110&xobvod=80&xobec=592030&xokrsek=1,1
592030,Bílovice,se2111?xjazyk=CZ&xdatum=20140110&xobvod=80&xobec=592030&xokrsek=2,2
585076,Biskupice,se2111?xjazyk=CZ&xdatum=20140110&xobvod=80&xobec=585076
557102,"Bohuslavice nad Vláří",se2111?xjazyk=CZ&xdatum=20140110&xobvod=80&xobec=557102
585092,"Bohuslavice u Zlína",se2111?xjazyk=CZ&xdatum=20140110&xobvod=80&xobec=585092
592048,Bojkovice,se2111?xjazyk=CZ&xdatum=20140110&xobvod=80&xobec=592048&xokrsek=1,1
592048,Bojkovice,se2111?xjazyk=CZ&xdatum=20140110&xobvod=80&xobec=592048&xokrsek=2,2
592048,Bojkovice,se2111?xjazyk=CZ&xdatum=20140110&xobvod=80&xobec=592048&xokrsek=3,3
592048,Bojkovice,se2111?xjazyk=CZ&xdatum=20140110&xobvod=80&xobec=592048&xokrsek=4,4
592048,Bojkovice,se2111?xjazyk=CZ&xdatum=20140110&xobvod=80&xobec=592048&xokrsek=5,5
592048,Bojkovice,se2111?xjazyk=CZ&xdatum=20140110&xobvod=80&xobec=592048&xokrsek=6,6
585114,Brumov-Bylnice,se2111?xjazyk=CZ&xdatum=20140110&xobvod=80&xobec=585114&xokrsek=1,1
585114,Brumov-Bylnice,se2111?xjazyk=CZ&xdatum=20140110&xobvod=80&xobec=585114&xokrsek=2,2
585114,Brumov-Bylnice,se2111?xjazyk=CZ&xdatum=20140110&xobvod=80&xobec=585114&xokrsek=3,3
585114,Brumov-Bylnice,se2111?xjazyk=CZ&xdatum=20140110&xobvod=80&xobec=585114&xokrsek=4,4
585114,Brumov-Bylnice,se2111?xjazyk=CZ&xdatum=20140110&xobvod=80&xobec=585114&xokrsek=5,5
538744,Březnice,se2111?xjazyk=CZ&xdatum=20140110&xobvod=80&xobec=538744
592081,Březolupy,se2111?xjazyk=CZ&xdatum=20140110&xobvod=80&xobec=592081&xokrsek=1,1
592081,Březolupy,se2111?xjazyk=CZ&xdatum=20140110&xobvod=80&xobec=592081&xokrsek=2,2
585149,Březůvky,se2111?xjazyk=CZ&xdatum=20140110&xobvod=80&xobec=585149
592111,"Bystřice pod Lopeníkem",se2111?xjazyk=CZ&xdatum=20140110&xobvod=80&xobec=592111
592137,Částkov,se2111?xjazyk=CZ&xdatum=20140110&xobvod=80&xobec=592137
585165,Dobrkovice,se2111?xjazyk=CZ&xdatum=20140110&xobvod=80&xobec=585165
585173,"Dolní Lhota",se2111?xjazyk=CZ&xdatum=20140110&xobvod=80&xobec=585173
585181,Doubravy,se2111?xjazyk=CZ&xdatum=20140110&xobvod=80&xobec=585181
592153,Drslavice,se2111?xjazyk=CZ&xdatum=20140110&xobvod=80&xobec=592153
585220,Halenkovice,se2111?xjazyk=CZ&xdatum=20140110&xobvod=80&xobec=585220
585238,Haluzice,se2111?xjazyk=CZ&xdatum=20140110&xobvod=80&xobec=585238
585246,"Horní Lhota",se2111?xjazyk=CZ&xdatum=20140110&xobvod=80&xobec=585246
550736,Hostětín,se2111?xjazyk=CZ&xdatum=20140110&xobvod=80&xobec=550736
592200,Hradčovice,se2111?xjazyk=CZ&xdatum=20140110&xobvod=80&xobec=592200&xokrsek=1,1
592200,Hradčovice,se2111?xjazyk=CZ&xdatum=20140110&xobvod=80&xobec=592200&xokrsek=2,2
585271,"Hřivínův Újezd",se2111?xjazyk=CZ&xdatum=20140110&xobvod=80&xobec=585271
592234,Jankovice,se2111?xjazyk=CZ&xdatum=20140110&xobvod=80&xobec=592234
585319,Jestřabí,se2111?xjazyk=CZ&xdatum=20140110&xobvod=80&xobec=585319
585327,Kaňovice,se2111?xjazyk=CZ&xdatum=20140110&xobvod=80&xobec=585327
587052,Karlovice,se2111?xjazyk=CZ&xdatum=20140110&xobvod=80&xobec=587052
592251,Kelníky,se2111?xjazyk=CZ&xdatum=20140110&xobvod=80&xobec=592251
549436,Komárov,se2111?xjazyk=CZ&xdatum=20140110&xobvod=80&xobec=549436
592277,Komňa,se2111?xjazyk=CZ&xdatum=20140110&xobvod=80&xobec=592277
592307,Košíky,se2111?xjazyk=CZ&xdatum=20140110&xobvod=80&xobec=592307
592323,Kudlovice,se2111?xjazyk=CZ&xdatum=20140110&xobvod=80&xobec=592323
573434,Lhota,se2111?xjazyk=CZ&xdatum=20140110&xobvod=80&xobec=573434
586871,Lipová,se2111?xjazyk=CZ&xdatum=20140110&xobvod=80&xobec=586871
592340,Lopeník,se2111?xjazyk=CZ&xdatum=20140110&xobvod=80&xobec=592340
585432,Loučka,se2111?xjazyk=CZ&xdatum=20140110&xobvod=80&xobec=585432
585441,Ludkovice,se2111?xjazyk=CZ&xdatum=20140110&xobvod=80&xobec=585441
585459,Luhačovice,se2111?xjazyk=CZ&xdatum=20140110&xobvod=80&xobec=585459&xokrsek=1,1
585459,Luhačovice,se2111?xjazyk=CZ&xdatum=20140110&xobvod=80&xobec=585459&xokrsek=2,2
585459,Luhačovice,se2111?xjazyk=CZ&xdatum=20140110&xobvod=80&xobec=585459&xokrsek=3,3
585459,Luhačovice,se2111?xjazyk=CZ&xdatum=20140110&xobvod=80&xobec=585459&xokrsek=4,4
585459,Luhačovice,se2111?xjazyk=CZ&xdatum=20140110&xobvod=80&xobec=585459&xokrsek=5,5
585459,Luhačovice,se2111?xjazyk=CZ&xdatum=20140110&xobvod=80&xobec=585459&xokrsek=6,6
585459,Luhačovice,se2111?xjazyk=CZ&xdatum=20140110&xobvod=80&xobec=585459&xokrsek=7,7
585513,Napajedla,se2111?xjazyk=CZ&xdatum=20140110&xobvod=80&xobec=585513&xokrsek=1,1
585513,Napajedla,se2111?xjazyk=CZ&xdatum=20140110&xobvod=80&xobec=585513&xokrsek=2,2
585513,Napajedla,se2111?xjazyk=CZ&xdatum=20140110&xobvod=80&xobec=585513&xokrsek=3,3
585513,Napajedla,se2111?xjazyk=CZ&xdatum=20140110&xobvod=80&xobec=585513&xokrsek=4,4
585513,Napajedla,se2111?xjazyk=CZ&xdatum=20140110&xobvod=80&xobec=585513&xokrsek=5,5
585513,Napajedla,se2111?xjazyk=CZ&xdatum=20140110&xobvod=80&xobec=585513&xokrsek=6,6
592404,Nedachlebice,se2111?xjazyk=CZ&xdatum=20140110&xobvod=80&xobec=592404
592421,Nezdenice,se2111?xjazyk=CZ&xdatum=20140110&xobvod=80&xobec=592421
549444,Oldřichovice,se2111?xjazyk=CZ&xdatum=20140110&xobvod=80&xobec=549444
585599,Otrokovice,se2111?xjazyk=CZ&xdatum=20140110&xobvod=80&xobec=585599&xokrsek=1,1
585599,Otrokovice,se2111?xjazyk=CZ&xdatum=20140110&xobvod=80&xobec=585599&xokrsek=2,2
585599,Otrokovice,se2111?xjazyk=CZ&xdatum=20140110&xobvod=80&xobec=585599&xokrsek=3,3
585599,Otrokovice,se2111?xjazyk=CZ&xdatum=20140110&xobvod=80&xobec=585599&xokrsek=4,4
585599,Otrokovice,se2111?xjazyk=CZ&xdatum=20140110&xobvod=80&xobec=585599&xokrsek=5,5
585599,Otrokovice,se2111?xjazyk=CZ&xdatum=20140110&xobvod=80&xobec=585599&xokrsek=6,6
585599,Otrokovice,se2111?xjazyk=CZ&xdatum=20140110&xobvod=80&xobec=585599&xokrsek=7,7
585599,Otrokovice,se2111?xjazyk=CZ&xdatum=20140110&xobvod=80&xobec=585599&xokrsek=8,8
585599,Otrokovice,se2111?xjazyk=CZ&xdatum=20140110&xobvod=80&xobec=585599&xokrsek=9,9
585599,Otrokovice,se2111?xjazyk=CZ&xdatum=20140110&xobvod=80&xobec=585599&xokrsek=10,10
585599,Otrokovice,se2111?xjazyk=CZ&xdatum=20140110&xobvod=80&xobec=585599&xokrsek=11,11
585599,Otrokovice,se2111?xjazyk=CZ&xdatum=20140110&xobvod=80&xobec=585599&xokrsek=12,12
585599,Otrokovice,se2111?xjazyk=CZ&xdatum=20140110&xobvod=80&xobec=585599&xokrsek=13,13
592480,Pašovice,se2111?xjazyk=CZ&xdatum=20140110&xobvod=80&xobec=592480
556874,Petrůvka,se2111?xjazyk=CZ&xdatum=20140110&xobvod=80&xobec=556874
592498,Pitín,se2111?xjazyk=CZ&xdatum=20140110&xobvod=80&xobec=592498
534811,Podhradí,se2111?xjazyk=CZ&xdatum=20140110&xobvod=80&xobec=534811
549461,Pohořelice,se2111?xjazyk=CZ&xdatum=20140110&xobvod=80&xobec=549461
549401,Pozlovice,se2111?xjazyk=CZ&xdatum=20140110&xobvod=80&xobec=549401
592536,Prakšice,se2111?xjazyk=CZ&xdatum=20140110&xobvod=80&xobec=592536
585661,Provodov,se2111?xjazyk=CZ&xdatum=20140110&xobvod=80&xobec=585661
556980,Rokytnice,se2111?xjazyk=CZ&xdatum=20140110&xobvod=80&xobec=556980&xokrsek=1,1
556980,Rokytnice,se2111?xjazyk=CZ&xdatum=20140110&xobvod=80&xobec=556980&xokrsek=2,2
592552,Rudice,se2111?xjazyk=CZ&xdatum=20140110&xobvod=80&xobec=592552
586919,Rudimov,se2111?xjazyk=CZ&xdatum=20140110&xobvod=80&xobec=586919
585734,Sehradice,se2111?xjazyk=CZ&xdatum=20140110&xobvod=80&xobec=585734
585751,Slavičín,se2111?xjazyk=CZ&xdatum=20140110&xobvod=80&xobec=585751&xokrsek=1,1
585751,Slavičín,se2111?xjazyk=CZ&xdatum=20140110&xobvod=80&xobec=585751&xokrsek=2,2
585751,Slavičín,se2111?xjazyk=CZ&xdatum=20140110&xobvod=80&xobec=585751&xokrsek=3,3
585751,Slavičín,se2111?xjazyk=CZ&xdatum=20140110&xobvod=80&xobec=585751&xokrsek=4,4
585751,Slavičín,se2111?xjazyk=CZ&xdatum=20140110&xobvod=80&xobec=585751&xokrsek=5,5
585751,Slavičín,se2111?xjazyk=CZ&xdatum=20140110&xobvod=80&xobec=585751&xokrsek=6,6
585751,Slavičín,se2111?xjazyk=CZ&xdatum=20140110&xobvod=80&xobec=585751&xokrsek=7,7
585751,Slavičín,se2111?xjazyk=CZ&xdatum=20140110&xobvod=80&xobec=585751&xokrsek=8,8
585769,Slopné,se2111?xjazyk=CZ&xdatum=20140110&xobvod=80&xobec=585769
585793,Spytihněv,se2111?xjazyk=CZ&xdatum=20140110&xobvod=80&xobec=585793
592609,"Starý Hrozenkov",se2111?xjazyk=CZ&xdatum=20140110&xobvod=80&xobec=592609
592650,Sušice,se2111?xjazyk=CZ&xdatum=20140110&xobvod=80&xobec=592650
592668,Svárov,se2111?xjazyk=CZ&xdatum=20140110&xobvod=80&xobec=592668
585807,Šanov,se2111?xjazyk=CZ&xdatum=20140110&xobvod=80&xobec=585807
585815,Šarovy,se2111?xjazyk=CZ&xdatum=20140110&xobvod=80&xobec=585815
585831,"Štítná nad Vláří-Popov",se2111?xjazyk=CZ&xdatum=20140110&xobvod=80&xobec=585831&xokrsek=1,1
585831,"Štítná nad Vláří-Popov",se2111?xjazyk=CZ&xdatum=20140110&xobvod=80&xobec=585831&xokrsek=2,2
592676,Šumice,se2111?xjazyk=CZ&xdatum=20140110&xobvod=80&xobec=592676&xokrsek=1,1
592676,Šumice,se2111?xjazyk=CZ&xdatum=20140110&xobvod=80&xobec=592676&xokrsek=2,2
549649,Tečovice,se2111?xjazyk=CZ&xdatum=20140110&xobvod=80&xobec=549649
585858,Tlumačov,se2111?xjazyk=CZ&xdatum=20140110&xobvod=80&xobec=585858&xokrsek=1,1
585858,Tlumačov,se2111?xjazyk=CZ&xdatum=20140110&xobvod=80&xobec=585858&xokrsek=2,2
592692,Topolná,se2111?xjazyk=CZ&xdatum=20140110&xobvod=80&xobec=592692
592706,Traplice,se2111?xjazyk=CZ&xdatum=20140110&xobvod=80&xobec=592706
592731,"Uherský Brod",se2111?xjazyk=CZ&xdatum=20140110&xobvod=80&xobec=592731&xokrsek=1,1
592731,"Uherský Brod",se2111?xjazyk=CZ&xdatum=20140110&xobvod=80&xobec=592731&xokrsek=2,2
592731,"Uherský Brod",se2111?xjazyk=CZ&xdatum=20140110&xobvod=80&xobec=592731&xokrsek=3,3
592731,"Uherský Brod",se2111?xjazyk=CZ&xdatum=20140110&xobvod=80&xobec=592731&xokrsek=4,4
592731,"Uherský Brod",se2111?xjazyk=CZ&xdatum=20140110&xobvod=80&xobec=592731&xokrsek=5,5
592731,"Uherský Brod",se2111?xjazyk=CZ&xdatum=20140110&xobvod=80&xobec=592731&xokrsek=6,6
592731,"Uherský Brod",se2111?xjazyk=CZ&xdatum=20140110&xobvod=80&xobec=592731&xokrsek=7,7
592731,"Uherský Brod",se2111?xjazyk=CZ&xdatum=20140110&xobvod=80&xobec=592731&xokrsek=8,8
592731,"Uherský Brod",se2111?xjazyk=CZ&xdatum=20140110&xobvod=80&xobec=592731&xokrsek=9,9
592731,"Uherský Brod",se2111?xjazyk=CZ&xdatum=20140110&xobvod=80&xobec=592731&xokrsek=10,10
592731,"Uherský Brod",se2111?xjazyk=CZ&xdatum=20140110&xobvod=80&xobec=592731&xokrsek=11,11
592731,"Uherský Brod",se2111?xjazyk=CZ&xdatum=20140110&xobvod=80&xobec=592731&xokrsek=12,12
592731,"Uherský Brod",se2111?xjazyk=CZ&xdatum=20140110&xobvod=80&xobec=592731&xokrsek=13,13
592731,"Uherský Brod",se2111?xjazyk=CZ&xdatum=20140110&xobvod=80&xobec=592731&xokrsek=14,14
592773,Vápenice,se2111?xjazyk=CZ&xdatum=20140110&xobvod=80&xobec=592773
585912,"Velký Ořechov",se2111?xjazyk=CZ&xdatum=20140110&xobvod=80&xobec=585912
585955,Vlachovice,se2111?xjazyk=CZ&xdatum=20140110&xobvod=80&xobec=585955&xokrsek=1,1
585955,Vlachovice,se2111?xjazyk=CZ&xdatum=20140110&xobvod=80&xobec=585955&xokrsek=2,2
592838,Vyškovec,se2111?xjazyk=CZ&xdatum=20140110&xobvod=80&xobec=592838
592846,Záhorovice,se2111?xjazyk=CZ&xdatum=20140110&xobvod=80&xobec=592846
592854,Zlámanec,se2111?xjazyk=CZ&xdatum=20140110&xobvod=80&xobec=592854
592871,Žítková,se2111?xjazyk=CZ&xdatum=20140110&xobvod=80&xobec=592871
586013,Žlutava,se2111?xjazyk=CZ&xdatum=20140110&xobvod=80&xobec=586013
586013,Žlutava,se2111?xjazyk=CZ&xdatum=20140110&xobvod=80&xobec=586013
586013,Žlutava,se2111?xjazyk=CZ&xdatum=20140110&xobvod=80&xobec=586013

scraper.php

<?php

require 'simple_html_dom.php';

$handle_out = fopen('zlin_stations.csv','w');
$handle_in = fopen('disticts_list.csv','r');


 while (($row = fgetcsv($handle_in, 1000, ",")) !== FALSE) {
  $d = row2data($row);
  foreach ($d as $r) {
    fputcsv($handle_out,$r);
  }
}

function row2data ($filerow) {

    $url = "http://volby.cz/pls/senat/" . $filerow[2];

    //download
    $html = file_get_contents($url);
    // change encoding
    $html = iconv('ISO-8859-2','UTF-8//TRANSLIT', $html);
    // change name in html
    $html = str_replace("ISO-8859-2","UTF-8",$html);
    // change nbsp to nothing in numbers
    //$html = str_replace("&nbsp;","",$html);

    /*
    $dom = new simple_html_dom();
    $dom->load($html);
    $name = trim($dom->find('h2',0)->plaintext);
    ->innertext
    ->outertext
    */

    // load it into dom
    $dom = new simple_html_dom();
    $dom->load($html);

    //1st table:
    /*$table = $dom->find('table',0);
    //echo $table->outertext;

    $tr = $table->find('tr',2);

    $tds = $tr->find('td');

    $overall_data = array();
    foreach ($tds as $td) {
      $number = $td->plaintext;
      $number = str_replace(",",".",$number);
      $overall_data[] = trim(str_replace("&nbsp;","",$number));
    }*/
    //print_r($overall_data);

    // 2nd table

    $table = $dom->find('table',1);

    $trs = $table->find('tr');

    array_shift($trs);
    array_shift($trs);

    $data = array();
    foreach($trs as $tr) {
      $tds = $tr->find('td');
      $i = 0;
      $row = array();
      //we want to store town name and code and polling station number as well:
      $row[] = $filerow[0];
      $row[] = $filerow[1];
      //if there is number of polling station, put it there, otherwise put '1' as the number
      if (isset($filerow[3])) {
        $row[] = $filerow[3];
      } else {
        $row[] = 1;
      }
      foreach($tds as $td) {
        $value = $td->plaintext;
        if (($i >= 1) and ($i <= 4)) {
          $value = str_replace('&nbsp;',' ',$value); 
        } else {
          $value = str_replace('&nbsp;','',$value);
          $value = str_replace(',','.',$value);
        }
        //get rid of the '+'
        if ($i == 0) {
          $value = trim($value,'+');
        }
        $row[] = trim($value);
        $i++; // the same as: $i = $i + 1; 
      }
      $data[] = $row;
    }

    return $data;
}

?>

scraper_first.php

<?php

/*
 get links to all polling stations and save them to csv
 the problem is that some towns have 1 polling station and some of them more polling stations, see:
 http://volby.cz/pls/senat/se2111?xjazyk=CZ&xdatum=20140110&xobvod=80&xobec=592013
 vs.
 http://volby.cz/pls/senat/se211?xjazyk=CZ&xdatum=20140110&xobvod=80&xobec=585599
*/

// library for scraping, we need it
require 'simple_html_dom.php';

// open csv file for results
$handle = fopen('disticts_list.csv','w');

// url of the main page
$url = "http://volby.cz/pls/senat/se21?xjazyk=CZ&xdatum=20140110&xobvod=80";
// download the main page
$html = file_get_contents($url);
// change encoding
$html = iconv('ISO-8859-2','UTF-8//TRANSLIT', $html);
// change name of encoding in html, just in case we would like to save it
$html = str_replace("ISO-8859-2","UTF-8",$html);

// load the html into dom (object)
$dom = new simple_html_dom();
$dom->load($html);

// we will make list of data, let's declare it (and set the values to empty array)
$list = array();

// there are up to 3 tables with results on volby.cz (now, exactly 3), lets get them to an array (of objects)
$tables = $dom->find('table');

// for each table do:
foreach ($tables as $table) {
  //get all rows ~ <tr>
  $trs = $table->find('tr');
  // delete first two rows from the array, they are header
  array_shift($trs);
  array_shift($trs);
  // we will store the data about one row in $item, let's declare it (and set the values to empty array)
  $item = array();
  //for each row do:
  foreach ($trs as $tr) {
    //get all cells ~ <td>
    //echo $tr->outertext;
    $tds = $tr->find('td');
    // get name
    $name = $tds[1]->plaintext;
    // some rows are empty, we dont want them
    if ($name != '-') {
      // get code
      $code = $tds[0]->plaintext;
      // get link to results, we may use more '->'
      $link = $tds[2]->find('a',0)->href;
      // the link has '&amp;' instead of '&', change it:
      $link = str_replace('&amp;','&',$link);
      // save to item
      $item = array(
        'code' => $code,
        'name' => $name,
        'link' => $link
      );
      //add item to the main array of data
      $list[] = $item;
    }
  }
}

// we try all the results and see, if they are with 1 polling station or more
// we could save the htmls locally for future faster loading, but they are not so many, so it is not so important

// we will save all the polling station into 'stations'
$stations = array();

// for each town (saved in list) do:
foreach ($list as $row) {
  // we recognize (for example) that there are more polling stations from the link as it starts 'se211?' in such case, and the one with 1 polling station with 'se2111?'
  // we use function strpos php.net/function.strpos
  // if there is the string '2111' in the html, it's position is >0
  if (strpos($row['link'],'2111') > 0) { //it is 1 polling station
    //save the row into the csv file
    fputcsv($handle,$row);
  } else { //there are more polling stations

    // get the html
    $url = "http://volby.cz/pls/senat/" . $row['link'];
    $html = file_get_contents($url);
    $html = iconv('ISO-8859-2','UTF-8//TRANSLIT', $html);
    $html = str_replace("ISO-8859-2","UTF-8",$html);
 
    // we need to get the names (numbers of all the polling stations)
    // load the html into dom (object)
    $dom = new simple_html_dom();
    $dom->load($html);
    echo $row['name'] . "<br/>\n";
    echo $row['link'] . "<br/>\n";
    // get the 1st table
    $table = $dom->find('table',0);
    // all rows, just in case there are more 
    // actually, there is an error in volby.cz's html, thereis missing '</tr>' tag in the table
    // it sucks, because we cannot do:
    // $trs = $table->find('tr');
    // so lets access the <td> directly
    // get all cells
    $tds = $table->find('td');
    // extract from each cell:
    foreach ($tds as $td) {
        $item = $row; //it is part of $list, remember, we have the name of town and code there
        $item['link'] = str_replace('&amp;','&',$td->find('a',0)->href); //but put the right link
        $item['station'] = $td->find('a',0)->plaintext; //and the name (number) of the station
        // save it to csv file
        fputcsv($handle,$item);
    }
  }
}
fclose($handle);
?>