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.)
  • 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 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.

10 thoughts on “Constituency maps in under a minute”

  1. Thanks for highlighting this Simon. I’ve been waiting for an easy-to-use political mapping tool for ages. Do you know if you can highlight groups of county or district council wards on the same map?

  2. Ed, using the full Google Maps API you should be able use multiple wards, and using the MaPit API you could make it pretty simple just requiring the Parent ID for the county / district.
    As it’s been years since I’ve had a play with Google Maps might give it a try tonight.

  3. “Note – these will produce nasty-looking data files, rather than pretty HTML lists.” – yes, it could be improved in this regard. Here, have an HTML lookup by type: 🙂 But I don’t think I have the time to do them all; perhaps someone will come along and patch the code for me 🙂

    Ed: You can add multiple KML overlays to a Google Map using their API quite simply: – this might do what you want.

  4. I’ve figured out how to display an entire council district, such as Rugby Borough Council, but I want to be able to display all the wards in that district, in different colours, side by side. My technical knowledge is pretty limited on this front so any help would be gratefully received!

  5. The trouble is Matthew, there’s ‘quite simply’ for you, and ‘quite simply’ for the rest of us! 🙂

    KML is basically just XML… so you might get away with downloading the individual files for each ward, then manually merging them, putting them on a web server somewhere, then ‘searching’ Google Maps for the URL of the merged file.

    This example on Google’s KML Reference page seems to show how you can then define a set of styles for the various polygons.

    This is probably enough to bring a ‘proper’ developer out in a rash.

  6. Thank you all for your assistance. This thread is very close to going way over my head! Ryan makes it appear ridiculously easy. Any chance you can package this is a format I can adapt easily?

  7. In theory using this should build a map for Rugby
    however it’s not filling in the boundarys, but clicking on the map will bring up a popup for the ward (might be too many KML files).

    Also I’m struggling to fill the wards in with other colours, from what I can see the Google API doesn’t allow you to edit the KML data after it’s generated.
    A possibility would be to store a local copy which had the colours edited in to them, but this wouldn’t work as a dynamic option across the country.

  8. Total novice, tried this and the Northern Ireland constituencies don’t work (I;m just substituting the ‘id’ number in the .kml link and pasting into google maps

  9. No, I’m afraid we don’t have boundary data for Northern Ireland, only Great Britain, so any request for a KML file for a Northern Ireland boundary returns 404 “No polygons found”. The postcode lookups for Northern Ireland are done using ONS point data.

Comments are closed.