Skip to content

Instantly share code, notes, and snippets.

@techbelly
Created March 31, 2011 11:08
Show Gist options
  • Save techbelly/896185 to your computer and use it in GitHub Desktop.
Save techbelly/896185 to your computer and use it in GitHub Desktop.
Maps loose county names to mapit area ids
import json
def mappit_county_matcher(counties_json):
source_counties = json.loads(counties_json)
counties_map = {}
for code in source_counties.values():
name,type_name,type_id,mapit_id = code[u'name'],code[u'type_name'],code[u'type'],code[u'id']
counties_map[name] = {
'name':name,
'type_name':type_name,
'type_id':type_id,
'mapit_id':mapit_id,
}
custom_mappings = {
"Bradford Metropolitan District Council":"Bradford City Council",
"City of London":"City of London Corporation",
"Royal Borough of Kensington and Chelsea":"Kensington and Chelsea Borough Council",
"Kingston-upon-Hull City Council":"Hull City Council",
"Oadby & Wigston District Council":"Oadby and Wigston Borough Council",
"South Buckinghamshire District Council":"South Bucks District Council",
"St Albans District Council":"St Albans City Council",
"Wakefield Metropolitan District Council":"Wakefield City Council",
"London Borough of Westminster":"Westminster City Council",
"Royal Borough of Windsor and Maidenhead":"Windsor and Maidenhead Council",
"Edinburgh City Council":"City of Edinburgh Council",
"Middlesbrough Borough Council":"Middlesbrough Council",
"Rhondda Cynon Taff County Borough Council":"Rhondda Cynon Taf Council",
"Newcastle-Under-Lyme District Council":"Newcastle-under-Lyme Borough Council",
"Northumberland Council (Unitary)":"Northumberland County Council",
"Bedford Council (Unitary)":"Bedford Borough Council",
}
def map(local_county):
if counties_map.has_key(local_county):
return ("exact_match",counties_map[local_county])
if custom_mappings.has_key(local_county):
local_county = custom_mappings[local_county]
options = []
if local_county.startswith('London Borough of '):
options.append(local_county.replace('London Borough of ','') + " Borough Council")
options.append((local_county.replace('London Borough of ','') + " Borough Council").replace('&','and'))
if local_county.startswith('Royal Borough of '):
options.append(local_county.replace('London Borough of ','') + " Borough Council")
options.append(local_county.replace(" Metropolitan",''))
options.append(local_county.replace(" Metropolitan District",''))
options.append(local_county.replace(" County Borough",""))
options.append(local_county.replace(" District",""))
options.append(local_county.replace(" County",""))
options.append(local_county.replace(" City",""))
options.append(local_county.replace("City and Borough ",""))
options.append(local_county.replace(" (Unitary)",""))
options.append(local_county.replace(" & "," and "))
options.append(local_county.replace("-"," "))
options.append(local_county.replace(" Council"," Borough Council"))
options.append(local_county.replace(" Council"," District Council"))
options.append(local_county.replace(" Council"," County Council"))
options.append(local_county.replace(" Council"," City Council"))
options.append(local_county+" Council")
if local_county.startswith('Borough of '):
options.append(local_county.replace('Borough of ','')+" Borough Council")
for option in options:
if counties_map.has_key(option):
return("fuzzy_match",counties_map[option])
break
else:
return None
return map
# councils data from: http://mapit.mysociety.org/areas/CTY,DIS,LBO,LGD,UTA,MTD,COI
mapper = mappit_county_matcher(open("/Users/ben/Desktop/counties.json").read())
# example
councils = ["Borough of Tower Hamlets", "Trafford Metropolitan Borough Council", "Tunbridge Wells Borough Council", "Uttlesford District Council", "Vale of White Horse District Council"]
print [(c,mapper(c)) for c in councils]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment