Tuesday 2 March 2010

Find by Postcode C#.NET: UK Parliamentary Constituencies

Task 1: Update a contact database and find the UK Parliamentary Constituencies (for the 2010 General Election) within which each contact's postcode falls.

This had been achieved before with a button on the form that opened a web page with the postcode in the URL... the user was expected to look for the information and then close the browser window and select the item from the list.

After some searching I found that the best API was from:

http://www.theyworkforyou.com/api/

You can register for an API Key from that address. This gives you access to a number of functions:

getConstituency: Searches for a Constituency by Postcode (current or future 2010)
getConsituencies: Returns a list of Constituencies
getPerson: Returns main details for a Person
getMP: Returns details for an MP
getMPInfo: Returns extra info on a person
getMPsInfo: Returns extra info on one or more people
getMPs: Returns a list of MPs (by postcode or constituency)
getLord: Returns main details for a Lord
getLords: Returns a list of Lords
getMLA: Returns main details for an MLA (Member of the Legislative Assembly (N Eire))
getMLAs: Returns a list of MLAs
getMSP: Returns main details for an MSP (Member of the Scottish Parliament)
getMSPs: Returns list of MSPs
getGeometry: Returns centre and bounding box of constituencies
getCommittee: Returns members of Select Committee
getDebates: Returns Debates (Commons, Westminster, Lords)
getWrans: Returns Written Answers
getWMS: Returns Written Ministerial Statements
getHansard: Returns any of the above
getComments: Returns comments

Here we need getConstituency only...

So I build a simple C#.NET program... add a datagrid... and show my data... to which i've added a new field to take the new constituency name. The following link is to the function's description page. The code is quick and dirty... GTD coding... a single pass, get what it can, and then it's done... you could put a Try..Catch around the ReadXML code... no errors occurred at all... so once the data's downloaded, on to the next phase.


private void goButton_Click(object sender, EventArgs e)
{
if (!(this.contactsDGView.CurrentRow == null))
{
string postcode =
this.contactsDGView.CurrentRow.Cells["Postcode"].Value.ToString();
postcode = postcode.Trim().Replace(" ", "+");
DataSet ds = new DataSet();
ds.ReadXml("http://www.theyworkforyou.com/api/getConstituency?" +
"key=<INSERT-API-KEY-HERE>&postcode=" + postcode +
"&future=1&output=xml");
if (ds.Tables[0].Columns.Contains("error")) { return; }
else
{
this.contactsDGView.CurrentRow.Cells["PConstituency"].Value =
ds.Tables[0].Rows[0]["name"].ToString();
}

}
}

[The update of the table in the DataGrid is done elsewhere.]

It took a few minutes to fill in all the constituencies... of course not all postcodes were valid... so they took a while to fix...

TheyWorkForYou.com have a fair-use policy... and the usage has to be declared when applying for an API... I have just discovered the UK eGov site for Office of National Statistics has a free data exchange program and I might be able to get this data from there instead...

I will have to go there for Wards and Councils...

No comments:

Post a Comment