Permalink
Browse files

Minimal version of de4d7df from https://github.com/jennyd/mapit polic…

…e-legislation branch, to try new many-many setup.
  • Loading branch information...
1 parent dd49486 commit 94baa20d0f4a5aa397d0eff610e2951c12594621 @dracos dracos committed Sep 27, 2012
View
352 data/UK/police_areas_england_wales.py
@@ -0,0 +1,352 @@
+# The description of each police area here is taken from Schedule 1 to the
+# Police Act 1996, amended in two cases by statutory instruments:
+#
+# http://www.legislation.gov.uk/ukpga/1996/16/schedule/1
+#
+# The legislation may be used under the terms of the Open Government Licence:
+#
+# http://www.nationalarchives.gov.uk/doc/open-government-licence/
+#
+# Where the name of a district in the description does not match the lookup
+# used, the reason for the discrepancy is given along with an official source,
+# if possible. Many references for subsequent amendments and name changes were
+# found in citations on Wikipedia.
+
+police_areas = (
+ # North West Somerset was renamed as North Somerset in either 1996 or 2005:
+ # http://www.legislation.gov.uk/uksi/1995/493/article/4/made
+ # http://www.n-somerset.gov.uk/cairo/docs/doc10520.htm
+ # ...depending on whether or not the change was legally valid the first time:
+ # http://www.n-somerset.gov.uk/cairo/docs/doc10395.htm
+ # (section 3.5, Technical Issues from the 1996 Re-organisation)
+ ("Avon and Somerset",
+ {"description": """The county of Somerset and the non-metropolitan
+ districts of Bath and North East Somerset, Bristol,
+ North West Somerset and South Gloucestershire""",
+ "lookup": (("CTY", "Somerset"),
+ ("UTA", "Bath and North East Somerset"),
+ ("UTA", "Bristol"),
+ ("UTA", "North Somerset"),
+ ("UTA", "South Gloucestershire"))}),
+ # Bedfordshire is amended by S.I. 2009/119 art. 3:
+ # http://www.legislation.gov.uk/uksi/2009/119/article/3/made
+ # 'Bedford ' has a trailing space to prevent it also matching 'Bedfordshire'
+ ("Bedfordshire",
+ {"description": """The non-metropolitan districts of Bedford,
+ Central Bedfordshire and Luton""",
+ "lookup": (("UTA", "Bedford "),
+ ("UTA", "Central Bedfordshire"),
+ ("UTA", "Luton"))}),
+ ("Cambridgeshire",
+ {"description": """The county of Cambridgeshire and the
+ non-metropolitan district of Peterborough""",
+ "lookup": (("CTY", "Cambridgeshire"),
+ ("UTA", "Peterborough"))}),
+ # Cheshire is amended by S.I. 2009/119 art. 6:
+ # http://www.legislation.gov.uk/uksi/2009/119/article/6/made
+ ("Cheshire",
+ {"description": """The non-metropolitan districts of Cheshire East,
+ Cheshire West and Chester, Halton and Warrington""",
+ "lookup": (("UTA", "Cheshire East"),
+ ("UTA", "Cheshire West and Chester"),
+ ("UTA", "Halton"),
+ ("UTA", "Warrington"))}),
+ # FIXME Find the City of London Police Act 1839, which defines the force
+ # area:
+ # http://www.cityoflondon.gov.uk/about-the-city/who-we-are/police-authority/Pages/default.aspx
+ # Inner and Middle Temples are excluded from the Metropolitan police area
+ # along with the City of London. They appear to be within the City of London
+ # police area in practice, although I haven't been able to find any
+ # legislative basis for that.
+ ("City of London",
+ {"description": """
+ """,
+ # override name is 'City of London Corporation'
+ "lookup": (("LBO", "City and County of the City of London"),)}),
+ ("Cleveland",
+ {"description": """The non-metropolitan districts of Hartlepool,
+ Middlesbrough, Redcar and Cleveland and Stockton-on-Tees""",
+ "lookup": (("UTA", "Hartlepool"),
+ ("UTA", "Middlesbrough"),
+ ("UTA", "Redcar and Cleveland"),
+ ("UTA", "Stockton-on-Tees"))}),
+ ("Cumbria",
+ {"description": """The county of Cumbria""",
+ "lookup": (("CTY", "Cumbria"),)}),
+ ("Derbyshire",
+ {"description": """The county of Derbyshire and the non-metropolitan
+ district of Derby""",
+ "lookup": (("CTY", "Derbyshire"),
+ ("UTA", "Derby"))}),
+ # Cornwall County Council was made a unitary authority in 2009 by S.I. 2008/491:
+ # http://www.legislation.gov.uk/uksi/2008/491/introduction/made
+ ("Devon and Cornwall",
+ {"description": """The counties of Devon and Cornwall, the
+ non-metropolitan districts of Plymouth and Torbay and the
+ Isles of Scilly""",
+ "lookup": (("CTY", "Devon"),
+ ("UTA", "Cornwall"),
+ ("UTA", "Plymouth"),
+ ("UTA", "Torbay"),
+ ("COI", "Isles of Scilly"))}),
+ ("Dorset",
+ {"description": """The county of Dorset and the non-metropolitan
+ districts of Bournemouth and Poole""",
+ "lookup": (("CTY", "Dorset"),
+ ("UTA", "Bournemouth"),
+ ("UTA", "Poole"))}),
+ # Durham County Council was made a unitary authority in 2009 by S.I. 2008/493:
+ # http://www.legislation.gov.uk/uksi/2008/493/introduction/made
+ ("Durham",
+ {"description": """The county of Durham and the non-metropolitan
+ district of Darlington""",
+ "lookup": (("UTA", "County Durham"),
+ ("UTA", "Darlington"))}),
+ # Local authorities in Wales are all unitary authorities:
+ ("Dyfed Powys",
+ {"description": """The counties of Ceredigion, Carmarthenshire,
+ Pembrokeshire and Powys""",
+ "lookup": (("UTA", "Ceredigion"),
+ ("UTA", "Carmarthenshire"),
+ ("UTA", "Pembrokeshire"),
+ ("UTA", "Powys"))}),
+ ("Essex",
+ {"description": """The county of Essex and the non-metropolitan
+ districts of Southend-on-Sea and Thurrock""",
+ "lookup": (("CTY", "Essex"),
+ ("UTA", "Southend-on-Sea"),
+ ("UTA", "Thurrock"))}),
+ ("Gloucestershire",
+ {"description": """The county of Gloucestershire""",
+ "lookup": (("CTY", "Gloucestershire"),)}),
+ ("Greater Manchester",
+ {"description": """The metropolitan districts of Bolton, Bury,
+ Manchester, Oldham, Rochdale, Salford, Stockport, Tameside,
+ Trafford and Wigan""",
+ "lookup": (("MTD", "Bolton"),
+ ("MTD", "Bury"),
+ ("MTD", "Manchester"),
+ ("MTD", "Oldham"),
+ ("MTD", "Rochdale"),
+ ("MTD", "Salford"),
+ ("MTD", "Stockport"),
+ ("MTD", "Tameside"),
+ ("MTD", "Trafford"),
+ ("MTD", "Wigan"))}),
+ ("Gwent",
+ {"description": """The county of Monmouthshire and the county boroughs
+ of Blaenau Gwent, Caerphilly, Newport and Torfaen""",
+ "lookup": (("UTA", "Monmouthshire"),
+ ("UTA", "Blaenau Gwent"),
+ ("UTA", "Caerphilly"),
+ ("UTA", "Newport"),
+ ("UTA", "Torfaen"))}),
+ # The Isle of Wight Council was made a unitary authority in 1995 by
+ # S.I. 1994/1210:
+ # http://www.legislation.gov.uk/uksi/1994/1210/article/3/made
+ ("Hampshire",
+ {"description": """The counties of Hampshire and Isle of Wight and the
+ non-metropolitan districts of Portsmouth and Southampton""",
+ "lookup": (("CTY", "Hampshire"),
+ ("UTA", "Isle of Wight"),
+ ("UTA", "Portsmouth"),
+ ("UTA", "Southampton"))}),
+ ("Hertfordshire",
+ {"description": """The county of Hertfordshire""",
+ "lookup": (("CTY", "Hertfordshire"),)}),
+ ("Humberside",
+ {"description": """The non-metropolitan districts of the East Riding of
+ Yorkshire, Kingston upon Hull, North East Lincolnshire and
+ North Lincolnshire""",
+ "lookup": (("UTA", "East Riding of Yorkshire"),
+ ("UTA", "Kingston upon Hull"),
+ ("UTA", "North East Lincolnshire"),
+ ("UTA", "North Lincolnshire"))}),
+ # Medway Towns is still officially referred to as such rather than just
+ # 'Medway', e.g.:
+ # http://www.legislation.gov.uk/uksi/2012/1803/schedule/crossheading/local-government/made
+ # However, the council calls itself 'Medway Council':
+ # http://www.medway.gov.uk/councilanddemocracy/aboutus.aspx
+ ("Kent",
+ {"description": """The county of Kent and the non-metropolitan district
+ of Medway Towns""",
+ "lookup": (("CTY", "Kent"),
+ ("UTA", "Medway"))}),
+ ("Lancashire",
+ {"description": """The county of Lancashire and the non-metropolitan
+ districts of Blackburn with Darwen and Blackpool""",
+ "lookup": (("CTY", "Lancashire"),
+ ("UTA", "Blackburn with Darwen"),
+ ("UTA", "Blackpool"))}),
+ ("Leicestershire",
+ {"description": """The county of Leicestershire and the non-metropolitan
+ districts of Leicester and Rutland""",
+ "lookup": (("CTY", "Leicestershire"),
+ ("UTA", "Leicester"),
+ ("UTA", "Rutland"))}),
+ ("Lincolnshire",
+ {"description": """The county of Lincolnshire""",
+ "lookup": (("CTY", "Lincolnshire"),)}),
+ ("Merseyside",
+ {"description": """The metropolitan districts of Knowsley, Liverpool,
+ St. Helens, Sefton and Wirral""",
+ "lookup": (("MTD", "Knowsley"),
+ ("MTD", "Liverpool"),
+ ("MTD", "St. Helens"),
+ ("MTD", "Sefton"),
+ ("MTD", "Wirral"))}),
+ # The Greater London Authority Act 1999 amends the force area:
+ # http://www.legislation.gov.uk/ukpga/1999/29/section/323
+ # Inner and Middle Temples appear to be within the City of London police
+ # area in practice (although I haven't been able to find any legislative
+ # basis for that)
+ ("Metropolitan",
+ {"description": """Greater London, excluding the City of London,
+ the Inner Temple and the Middle Temple""",
+ # This lookup is the empty list because we're treating this area as a
+ # special case, and creating a unionagg of all London boroughs
+ # excluding the City of London:
+ "lookup": []}),
+ ("Norfolk",
+ {"description": """The county of Norfolk""",
+ "lookup": (("CTY", "Norfolk"),)}),
+ ("Northamptonshire",
+ {"description": """The county of Northamptonshire""",
+ "lookup": (("CTY", "Northamptonshire"),)}),
+ # Northumberland County Council was made a unitary authority in 2009 by
+ # S.I. 2008/494:
+ # http://www.legislation.gov.uk/uksi/2008/494/introduction/made
+ ("Northumbria",
+ {"description": """The county of Northumberland and the metropolitan
+ districts of Gateshead, Newcastle upon Tyne, North Tyneside,
+ South Tyneside and Sunderland""",
+ "lookup": (("UTA", "Northumberland"),
+ ("MTD", "Gateshead"),
+ ("MTD", "Newcastle upon Tyne"),
+ ("MTD", "North Tyneside"),
+ ("MTD", "South Tyneside"),
+ ("MTD", "Sunderland"))}),
+ ("North Wales",
+ {"description": """The counties of the Isle of Anglesey, Gwynedd,
+ Denbighshire and Flintshire and the county boroughs of Conwy
+ and Wrexham""",
+ "lookup": (("UTA", "Isle of Anglesey"),
+ ("UTA", "Gwynedd"),
+ ("UTA", "Denbighshire"),
+ ("UTA", "Flintshire"),
+ ("UTA", "Conwy"),
+ ("UTA", "Wrexham"))}),
+ # 'York ' has a trailing space to prevent it also matching 'Yorkshire'
+ ("North Yorkshire",
+ {"description": """The county of North Yorkshire and the
+ non-metropolitan district of York""",
+ "lookup": (("CTY", "North Yorkshire"),
+ ("UTA", "York "))}),
+ ("Nottinghamshire",
+ {"description": """The county of Nottinghamshire and the
+ non-metropolitan district of Nottingham""",
+ "lookup": (("CTY", "Nottinghamshire"),
+ ("UTA", "Nottingham"))}),
+ # Rhondda, Cynon and Taff were merged in 1996 by the Local Government
+ # (Wales) Act 1994 to form Rhondda Cynon Taf:
+ # http://www.legislation.gov.uk/ukpga/1994/19/schedule/1
+ ("South Wales",
+ {"description": """The counties of Cardiff and Swansea and the county
+ boroughs of Bridgend, Merthyr Tydfil, Neath Port Talbot,
+ Rhondda, Cynon, Taff and the Vale of Glamorgan""",
+ "lookup": (("UTA", "Cardiff"),
+ ("UTA", "Swansea"),
+ ("UTA", "Bridgend"),
+ ("UTA", "Merthyr Tydfil"),
+ ("UTA", "Neath Port Talbot"),
+ ("UTA", "Rhondda Cynon Taf"),
+ ("UTA", "Vale of Glamorgan"))}),
+ ("South Yorkshire",
+ {"description": """The metropolitan districts of Barnsley, Doncaster,
+ Rotherham and Sheffield""",
+ "lookup": (("MTD", "Barnsley"),
+ ("MTD", "Doncaster"),
+ ("MTD", "Rotherham"),
+ ("MTD", "Sheffield"))}),
+ ("Staffordshire",
+ {"description": """The county of Staffordshire and the
+ non-metropolitan district of Stoke-on-Trent""",
+ "lookup": (("CTY", "Staffordshire"),
+ ("UTA", "Stoke-on-Trent"))}),
+ ("Suffolk",
+ {"description": """The county of Suffolk""",
+ "lookup": (("CTY", "Suffolk"),)}),
+ ("Surrey",
+ {"description": """The county of Surrey""",
+ "lookup": (("CTY", "Surrey"),)}),
+ ("Sussex",
+ {"description": """The counties of East Sussex and West Sussex and the
+ non-metropolitan district of Brighton and Hove""",
+ "lookup": (("CTY", "East Sussex"),
+ ("CTY", "West Sussex"),
+ ("UTA", "Brighton and Hove"))}),
+ # Berkshire County Council was dissolved in 1998 by S.I. 1996/1879 and its
+ # functions transferred to six of its district councils as unitary authorities:
+ # http://www.legislation.gov.uk/uksi/1996/1879/article/3/made
+ ("Thames Valley",
+ {"description": """The counties of Berkshire, Buckinghamshire and
+ Oxfordshire and the non-metropolitan district of
+ Milton Keynes""",
+ "lookup": (("UTA", "Bracknell Forest"),
+ ("UTA", "Reading"),
+ ("UTA", "Slough"),
+ ("UTA", "West Berkshire"),
+ ("UTA", "Windsor and Maidenhead"),
+ ("UTA", "Wokingham"),
+ ("CTY", "Buckinghamshire"),
+ ("CTY", "Oxfordshire"),
+ ("UTA", "Milton Keynes"))}),
+ ("Warwickshire",
+ {"description": """The county of Warwickshire""",
+ "lookup": (("CTY", "Warwickshire"),)}),
+ # Shropshire County Council was made a unitary authority in 2009 by
+ # S.I. 2008/492:
+ # http://www.legislation.gov.uk/uksi/2008/492/introduction/made
+ # The Wrekin was renamed "Telford and Wrekin" in 1998, when the district
+ # became a unitary authority. The only legislative reference I can find to
+ # this change is in the footnote to S.I. 2002/2373 art.3:
+ # http://www.legislation.gov.uk/uksi/2002/2373/article/3/made
+ ("West Mercia",
+ {"description": """The counties of Shropshire and Worcestershire and
+ the non-metropolitan districts of Herefordshire and
+ The Wrekin""",
+ "lookup": (("UTA", "Shropshire"),
+ ("CTY", "Worcestershire"),
+ ("UTA", "Herefordshire"),
+ ("UTA", "Telford and Wrekin"))}),
+ ("West Midlands",
+ {"description": """The metropolitan districts of Birmingham, Coventry,
+ Dudley, Sandwell, Solihull, Walsall and Wolverhampton""",
+ "lookup": (("MTD", "Birmingham"),
+ ("MTD", "Coventry"),
+ ("MTD", "Dudley"),
+ ("MTD", "Sandwell"),
+ ("MTD", "Solihull"),
+ ("MTD", "Walsall"),
+ ("MTD", "Wolverhampton"))}),
+ ("West Yorkshire",
+ {"description": """The metropolitan districts of Bradford, Calderdale,
+ Kirklees, Leeds and Wakefield""",
+ "lookup": (("MTD", "Bradford"),
+ ("MTD", "Calderdale"),
+ ("MTD", "Kirklees"),
+ ("MTD", "Leeds"),
+ ("MTD", "Wakefield"))}),
+ # Wiltshire County Council was made a unitary authority in 2009 by
+ # S.I. 2008/490:
+ # http://www.legislation.gov.uk/uksi/2008/490/introduction/made
+ # Thamesdown was renamed as "Swindon" in 1997, shortly after it became a
+ # unitary authority; I can't find any other source for this date:
+ # http://en.wikipedia.org/wiki/Thamesdown
+ ("Wiltshire",
+ {"description": """The county of Wiltshire and the non-metropolitan
+ district of Thamesdown""",
+ "lookup": (("UTA", "Wiltshire"),
+ ("UTA", "Swindon"))})
+)
+
View
2 mapit/fixtures/uk.json
@@ -52,6 +52,8 @@
{ "pk": 39, "model": "mapit.type", "fields": { "code": "OMG", "description": "Middle Layer Super Output Area (Generalised)" } },
{ "pk": 40, "model": "mapit.type", "fields": { "code": "OUA", "description": "Output Area" } },
+ { "pk": 41, "model": "mapit.type", "fields": { "code": "PFL", "description": "Police force (from legislation)" } },
+
{ "pk": 1, "model": "mapit.codetype", "fields": { "code": "ons", "description": "SNAC" } },
{ "pk": 2, "model": "mapit.codetype", "fields": { "code": "gss", "description": "GSS (SNAC replacement)" } },
{ "pk": 3, "model": "mapit.codetype", "fields": { "code": "unit_id", "description": "Boundary-Line (OS Admin Area ID)" } },
View
105 mapit/management/commands/mapit_UK_create_police_force_areas.py
@@ -0,0 +1,105 @@
+# This script is used after Boundary-Line has been imported to create police
+# force areas in England and Wales from the other administrative areas which
+# they cover.
+
+import os, sys
+sys.path.append(os.path.dirname(__file__) + '/../../../data/UK')
+
+from optparse import make_option
+from django.core.management.base import NoArgsCommand
+
+from mapit.models import Area, Geometry, Generation, Country, Type
+from police_areas_england_wales import police_areas
+
+class Command(NoArgsCommand):
+ help = 'Create police force areas from existing Boundary-Line areas'
+ option_list = NoArgsCommand.option_list + (
+ make_option(
+ '--commit',
+ action='store_true',
+ dest='commit',
+ help='Actually update the database'
+ ),
+ )
+
+ def handle_noargs(self, **options):
+ current_generation = Generation.objects.current()
+ new_generation = Generation.objects.new()
+ if not new_generation:
+ raise Exception, "No new generation to be used for import!"
+
+ force_type_code = 'PFL'
+ force_area_type = Type.objects.get(code=force_type_code)
+
+ england = Country.objects.get(code='E')
+ wales = Country.objects.get(code='W')
+
+ welsh_forces = ('dyfed-powys', 'gwent', 'north-wales', 'south-wales')
+
+ for police_area in police_areas:
+ force_name = police_area[0]
+ force_code = force_name.lower().replace(' ', '-')
+ country = wales if (force_code in welsh_forces) else england
+
+ try:
+ force = Area.objects.get(codes__code=force_code,
+ type=force_area_type)
+ print "Area matched, %s" % force
+ except Area.DoesNotExist:
+ force = Area(
+ # If committing, name will be overwritten by the
+ # force.names.update_or_create:
+ name = force_name,
+ type = force_area_type,
+ country = country,
+ generation_low = new_generation,
+ generation_high = new_generation,
+ )
+ print "New area: %s %s" % (force_type_code, force_name)
+
+ # check that we are not about to skip a generation
+ if force.generation_high and current_generation and force.generation_high.id < current_generation.id:
+ raise Exception, "Area %s found, but not in current generation %s" % (force_code, current_generation)
+
+ force.generation_high = new_generation
+ if options['commit']:
+ print ' saving force'
+ force.save()
+
+ # Create the Metropolitan Police Service area by unioning all the
+ # London boroughs except the City of London, to avoid having to list
+ # every borough in police_areas:
+ if force_code == 'metropolitan':
+ force_geoms = Geometry.objects.filter(areas__type__code="LBO").exclude(areas__names__name__contains="City of London")
+ else:
+ force_geoms = []
+ # For all other forces, look for an Area matching each named
+ # area in the descriptions, and union their polygons to create a
+ # geometry for the force area:
+ lookup = police_area[1]['lookup']
+ for a in lookup:
+ lookup_type_code, lookup_name = a
+ try:
+ area = Area.objects.get(type__code=lookup_type_code,
+ names__name__contains=lookup_name)
+ areas = area.polygons.all()
+ force_geoms.extend(areas)
+ except Area.DoesNotExist:
+ print a, 'not found'
+ except Area.MultipleObjectsReturned:
+ print 'More than one area found matching', a
+ for match in Area.objects.filter(type__code=lookup_type_code,
+ names__name__contains=lookup_name):
+ print ' ', match
+ print ' No polygons from these areas were used.'
+ if len(force_geoms):
+ print ' Geometry successfully assembled'
+ if options['commit']:
+ force.polygons.clear()
+ for polygon in force_geoms:
+ force.polygons.add(polygon)
+ else:
+ print ' (not saving geometry)'
+ else:
+ print ' No geometry created for', force_name
+
View
3 mapit/templates/mapit/api/areas.html
@@ -23,7 +23,8 @@
SPC (Scottish Parliament constituency), SPE (Scottish Parliament region),
UTA (Unitary authority), UTE (Unitary authority electoral division),
UTW (Unitary authority ward), WAC (Welsh Assembly constituency),
- WAE (Welsh Assembly region), WMC (UK Parliamentary constituency)</small>.
+ WAE (Welsh Assembly region), WMC (UK Parliamentary constituency),
+ PFL (Police area)</small>.
{% endif %}
{% if country == 'NO' %}
<small><a href="{% url mapit_index %}areas/NKO.html">NKO</a> (kommune),

0 comments on commit 94baa20

Please sign in to comment.