IEBC (Independent Electoral and Boundaries Commission) 'DATA'

My first post for 2016 'Happy Year',yeah someone hasn't be coding that much,with the Kenyan elections just around i decided to dig the web looking for election data and stumble upon the IEBC | Elections portal.
An API for Election data as needed i registered for the API but no reply ,wrote a couple of emails ,tweets still no reply 'but' i found a workaround the API works just fine with the sample token.

e.g 
http://api.iebc.or.ke/constituency/?token=afd3877583a07e5b77e447332bb98a80 end point to all 290 consituency.
http://api.iebc.or.ke/ward/?token=afd3877583a07e5b77e447332bb98a80 end point to  1450 wards.

The boundaries are in geojson, i used this example at gis.stackexchange to convert geojson files to shapefiles,but my preferred file format was SVG,to convert the shapefiles to SVG i used ArcMap i had to add every shapefile individually time consuming process but was needed to confirm all constituencies and wards and then exported them to an SVG format.

Javafx supports SVG and the whole idea of treating a county or county assembly ward as an individual node made it more practical to use SVG.
for Counties and Constituencies i copied the d content of the SVG path how to do it in Inkscape and used the county/constitunecy names as Enum.

public enum Constituency {

   MANDERA_SOUTH("path content");

   private final String path;
    
    Constituency(String path){
        this.path=path;
    }

}
for county assembly wards 1450 wards i could not simply copy all the contents so i decided to parse the SVG file using the Jsoup library. this a snapshot of my wards SVG file.

SVG file snapshot
CAW.svg


Code sample 
      Group root=new Group();

        File input = new File("CAW.svg");
        
        Document doc = Jsoup.parse(input, "UTF-8", "http://example.com/");
        
        Elements content = doc.getElementsByTag("g");

        for(Element el:content){
            
            Elements path=el.getElementsByAttributeValueMatching("id","\\w{1,}[_]\\d{1,}");

            Elements path_clip= el.getElementsByAttribute("clip-path");

            for(Element _el:path_clip){
                
                SVGPath svg=new SVGPath();
                svg.setContent(_el.attr("d"));
                root.getChildren().add(svg);
          }

        }

Results
Kenya County Assembly Wards


All 290 constituency were accounted for but county assembly wards this 6 were missing :-

Name             County Assembly Code
Riwo                        641
Kasemeni                50
Elwak North           209
Embobut_embulot  734
Lelan                       735
Ngei                        1448

The IEBC doesn't have all election results for all wards but the endpoint looks like this

http://api.iebc.or.ke/results/ward/6C03B688-9D05-4592-A0B3-55B25BBFC05A/?post=4&election=GEK2013&token=afd3877583a07e5b77e447332bb98a80

Endpoint to SOUTH IMENTI county assembly ward results.Look below for the final result of the available county assembly ward results vizualisation.


All the SVG files can be found here

Results 
2013 General Election Presidential result by county color coded by party

Query counties with more than 5 constituencies.

counties with more than 5 constituencies.
Available results of county assembly wards

900+ results CAW color coded by party

Still waiting for the complete results ,but they were some issues with the IEBC portal data like some party color string were wrong,

 #fdcbo7 instead #fdcb07 had to create a ternary operator that looks like so

  String color=party.getColor().equals("#fdcbo7") ? "#fdcb07" : party.getColor();                              
Its good effort that IEBC is making available this kind of data to the public.

Comments

  1. Good piece of info. However I would like to implement this API in a web platform that runs on Java: i.e jsp. How would I go about it?

    ReplyDelete
  2. You can as well use QGIS to convert geojson to shapefile or SVG formats. Check qgis.org. At the same time you can use the new kid on the block, LeafletJS to do the styling and displaying your map as a web map. All these are OpenSource software/library

    ReplyDelete

Post a Comment

Popular Posts