Skip to content

Instantly share code, notes, and snippets.

@jezhiggins
Forked from pezholio/mapit.js
Last active May 11, 2017 12:31
Show Gist options
  • Save jezhiggins/592c5483011c8e610e6ee7a8cad8464e to your computer and use it in GitHub Desktop.
Save jezhiggins/592c5483011c8e610e6ee7a8cad8464e to your computer and use it in GitHub Desktop.
Getting parliamentary constituencies from MapIt
const Client = require('node-rest-client').Client;
const postcode = 'SW1A1AA'; // Or whatever postcode you're looking for
const uri = `https://mapit.mysociety.org/postcode/${postcode}`;
const getConstituency = uri => new Promise((resolve, error) =>
{
const client = new Client();
client.get(uri, (data, response) => {
constituencyCode = data.shortcuts.WMC;
constituency = data.areas[constituencyCode]
resolve(constituency);
});
});
getConstituency(uri).then(data => console.log(data));
// { parent_area: null, generation_high: 30, all_names: {}, id: 65759, codes: { gss: 'E14000639', unit_id: '25040' }, name: 'Cities of London and Westminster', country: 'E', type_name: 'UK Parliament constituency', generation_low: 13, country_name: 'England', type: 'WMC' }
// This assumes you have node-rest-client (https://www.npmjs.com/package/node-rest-client) installed and available in your NODE_PATH
var Client = require('node-rest-client').Client;
var postcode = 'SW1A1AA'; // Or whatever postcode you're looking for
var uri = 'https://mapit.mysociety.org/postcode/' + postcode;
var getConstituency = function(uri, callback) {
var client = new Client();
client.get(uri, function(data, response) {
constituencyCode = data.shortcuts.WMC;
constituency = data.areas[constituencyCode]
callback(constituency);
return;
});
};
getConstituency(uri, function(data) {
console.log(data);
// { parent_area: null, generation_high: 30, all_names: {}, id: 65759, codes: { gss: 'E14000639', unit_id: '25040' }, name: 'Cities of London and Westminster', country: 'E', type_name: 'UK Parliament constituency', generation_low: 13, country_name: 'England', type: 'WMC' }
});
<?php
$postcode = 'SW1A1AA'; # Or whatever postcode you're looking for
$api_key = ''; # specify your API key here
$uri = "https://mapit.mysociety.org/postcode/$postcode?api_key=$api_key";
$json = json_decode(file_get_contents($uri), true);
$constituency_code = $json['shortcuts']['WMC'];
# => 65759
$constituency = $json['areas'][$constituency_code];
# { ["parent_area"]=> NULL ["generation_high"]=> int(30) ["all_names"]=> array(0) { ["id"]=> int(65759) ["codes"]=> array(2) { ["gss"]=> string(9) "E14000639" ["unit_id"]=> string(5) "25040" } ["name"]=> string(32) "Cities of London and Westminster" ["country"]=> string(1) "E" ["type_name"]=> string(26) "UK Parliament constituency" ["generation_low"]=> int(13) ["country_name"]=> string(7) "England" ["type"]=> string(3) "WMC" }
?>
import requests # pip install requests
postcode = 'SW1A1AA' # Or whatever postcode you're looking for
uri = "https://mapit.mysociety.org/postcode/{0}".format(postcode)
response = requests.get(uri)
response.raise_for_status()
json = response.json()
constituency_code = str(json['shortcuts']['WMC'])
# u'65759'
constituency = json['areas'][constituency_code]
# {u'codes': {u'gss': u'E14000639', u'unit_id': u'25040'}, u'name': u'Cities of London and Westminster', u'country': u'E', u'type_name': u'UK Parliament constituency', u'parent_area': None, u'generation_high': 30, u'all_names': {}, u'generation_low': 13, u'country_name': u'England', u'type': u'WMC', u'id': 65759}
require 'json'
require 'net/http'
postcode = 'SW1A1AA' # Or whatever postcode you're looking for
api_key = '' # Specify your API key here
uri = URI("https://mapit.mysociety.org/postcode/#{postcode}?api_key=#{api_key}")
response = Net::HTTP.get(uri)
json = JSON.parse(response)
constituency_code = json['shortcuts']['WMC'].to_s
#=> "65759"
constituency = json['areas'][constituency_code]
#=> {"parent_area"=>nil, "generation_high"=>30, "all_names"=>{}, "id"=>65759, "codes"=>{"gss"=>"E14000639", "unit_id"=>"25040"}, "name"=>"Cities of London and Westminster", "country"=>"E", "type_name"=>"UK Parliament constituency", "generation_low"=>13, "country_name"=>"England", "type"=>"WMC"}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment