Constituency maps in under a minute

Opening up geographic data is beginning to bear fruit. MySociety’s Matthew Somerville has just unveiled MaPit, ‘our database and web service that maps postcodes and points to current or past administrative area information and polygons for all the United Kingdom.’ What that means in practice is, postcode lookups and boundary data are now just a URL away.
(Quick update: actually, not for all the United Kingdom as it turns out – the following method doesn’t work for N Ireland. See Matthew’s comment below.)
Here’s a quick example, as much for my own future reference as anyone else’s. Let’s say you wanted to generate a map of a given MP’s constituency – say Lynne Featherstone in Hornsey & Wood Green:

  • You need to find the appropriate reference number for the constituency: either by browsing the list of all constituencies, or searching for places whose names begin with Hornsey. Note – these will produce nasty-looking data files, rather than pretty HTML lists. Hunt through the code, and you’ll find:
    “65883”: {“codes”: {“unit_id”: “25044”}, “name”: “Hornsey and Wood Green”, “country”: “E”, “type_name”: “UK Parliament constituency”, “parent_area”: null, “generation_high”: 13, “generation_low”: 13, “country_name”: “England”, “type”: “WMC”, “id”: 65883}
  • The ‘id’ is the number you need – in this case, 65883. The MySociety API now lets you call the geometry of that area, in – among others – Google Earth’s KML format, using the following URL. (Don’t worry about the ‘4326’ here: it’s a reference to the coordinate system being used, and won’t change in this context.)
    http://mapit.mysociety.org/area/4326/65883.kml
  • Conveniently, Google Maps lets you enter a KML file’s URL as a search query, and it will draw it on a map. Even more conveniently, if you add ‘output=embed’ as a search parameter, it strips away everything but the map itself. So here’s an embedded map of Lynne’s constituency, pulled into an <iframe>. Look at the source code, to see how easy it is.

Boundary data generated by MaPit.mysociety.org which contains Ordnance Survey data © Crown copyright and database right 2010; Royal Mail data © Royal Mail copyright and database right 2010 (Code-Point Open); National Statistics data © Crown copyright and database right 2010 (NSPD Open).
And thankfully, it bears a close resemblance to this map on Lynne’s own website, which took me considerably longer to churn out.

Putting Google geo-location to the Twitter test

Google’s javascript API has an exciting, and somewhat underreported little feature built in: each time a call is initiated, it attempts to establish where the browser is physically located – and reports back a town, ‘region’ (county) and country. I was wondering if it was accurate enough to be used to ‘personalise’ a website automatically: so I ran a quick experiment among my Twitter following.
I set up a quick test page on puffbox.com, which included a call to the Google API, and asked people to leave a comment as to whether or not the response was accurate. Within an hour I’d had 30 responses, from all around the UK.
The results revealed that the function is sometimes bang-on, sometimes blocked, sometimes curious, and sometimes plain wrong… occasionally by hundreds of miles. I can forgive the occasional placing of towns in the wrong county; but several people in the north of England, using the same ISP also located up north, were getting responses of ‘London’. So my conclusion, disappointingly, is that it’s not really good enough to make meaningful use of.
A wasted effort? Hardly. It actually saves me the effort of building something reliant on the geo function, only to discover it’s useless for large numbers of people. And it’s a nice case study for the value of Twitter: a crowd of good folk and true, located all over the country, from whom I could ask a 5-second favour… with a good expectation of getting responses. Thanks, team.