Understanding UK Postcodes and Using Them in Geospatial Systems

Understanding UK Postcodes and Using Them in Geospatial Systems

Contributed By:-

Dan Winchester_founder_ThingsToDoWithData

Postcodes are very powerful within UK data – if you know the postcode of a geographical entity, you can geocode it, reasonably accurately, and using open and reliable datasets.

Postcodes are very powerful within UK data – if you know the postcode of a geographical entity, you can geocode it, reasonably accurately, and using open and reliable datasets.

This article explains how to understand the format of UK postcodes, and how to use them practically for geocoding both in grid systems and using latitude/longitude.

Format

Let’s start with an example:

SW1A 0AA

This is the postcode of The Palace of Westminster, the principle building within the UKHouses of Parliament.

A single building may have sole use of a postcode, or a postcode may be shared amongst a number of nearby buildings (for example, 50 houses on a section of the same street).

The postcode consists of two halves, separated by a space:

  1. The outcode: SW1A
  2. The incode: 0AA

The outcode and incode (also known as outward code and inward code respectively) were designed to assist the Royal Mail in a routing mail.

More interesting from a geospatial point of view, the postcode can be divided up into four geographical areas, each increasingly precise:

  1. Postcode area
  2. Postcode district
  3. Postcode sector
  4. Unit postcode

Working out which part is which can be confusing because both the postcode area and postcode district contain optional characters.

The below graphic illustrates the format:

postcode_format

Source: https://www.getthedata.com/postcode

In the case of our example postcode, SW1A 0AA, the breakdown is as follows:

UK postcodes

To add to the confusion, postcodes maybe supplied without the delimiting space – the space adds readability rather than uniqueness. Sometimes postcodes are supplied in a seven or eight character fixed-width format, with zero, one, two, or three spaces separating the outcode and the incode depending on whether the outcode is two, three, or four characters. In such cases, the final three characters will always be the incode.

How to parse a UK postcode

Given a full postcode, it is possible to parse that postcode into its four geographical component parts (postcode area, postcode district, postcode sector and unit postcode).

At first sight, this can seem rather daunting, but there are three rules which help:

  1. The outcode will always contain a number. The one or two letters in front of the number form the postcode area.
  2. The incode always starts with a single number followed by two letters. This initial single number is combined with the outcode to form the postcode sector.
  3. As the outcode contains optional characters, but the incode does not, it can help to treat the incode first when parsing the postcode. This is particularly useful where the postcode is supplied without the delimiting space character.

There are four possible combinations of letters and numbers for outcode, where A is alpha (letter) and N is numeric:

  • AN
  • AAN
  • AANN
  • AANA

UK postcode_2

Note that, where there is no space character delimiting the outcode and incode, trying to parse a postcode from the start can introduce ambiguity as the same sequence of characters match both an outcode and a postcode sector.

For example, both postcodes below start with a sequence of characters which potentially matches two different outcodes, RG1, and RG12:

UK postcodes_3

In the first example, “RG1” is the outcode and “RG1 2” is the postcode sector.

In the second example, “RG12” is the outcode.

By starting from the end of the postcode, and determining the incode first, we are left with the correct outcode.

The below regular expression will parse a postcode, delimited with zero, one or more spaces, into the four geographical component parts:

/^(((([A-Z][A-Z]{0,1})[0-9][A-Z0-9]{0,1}) {0,}[0-9])[A-Z]{2})$/

This matches, in order:

  • Unit Postcode
  • Postcode Sector
  • Postcode District
  • Postcode Area

It is also possible to lookup the postcode area, postcode district, and postcode sector using a lookup file such as Open Postcode Geo:

https://www.getthedata.com/open-postcode-geo

Geography of a postcode

Typically each geographical component within the postcode is contained within the parent.

In other words:

  • The unit postcode is a subarea within the postcode sector.
  • The postcode sector is a subarea within the postcode district.
  • The postcode district is a subarea within the postcode area.

For example, for postcode WC2B 4AB:

  • WC2B 4AB is a subarea within WC2B 4.
  • WC2B 4 is a subarea within WC2B.
  • WC2B is a subarea within WC.

The below graphic illustrates how this looks on a map:

postcode_map

Source: https://www.getthedata.com/postcode

Using postcodes in geospatial systems

The most straightforward task is geocoding a full postcode, or indeed geocoding any entity where you know the postcode for the location of that entity.

This can be achieved by looking up the postcode in one of a number of open data files, namely Code-Point Open, ONS Postcode Directory, and Open Postcode GIS. Each file is slightly different:

Code-Point Open

This file is maintained by the UK Ordnance Survey.

  • The postcode field is in seven character fixed-width format, which means outcode and incode are delimited by either zero, one, or two spaces.
  • Easting and northing are provided, but not latitude and longitude.
  • The data is delivered in a separate file for each postcode area.
  • Registration is required.

You can download it here:

https://www.ordnancesurvey.co.uk/business-and-government/products/code-point-open.html

ONS Postcode Directory

This file is maintained by the UK Office for National Statistics.

  • The postcode field is provided in three formats: Seven-character fixed-width; eight character fixed-width; space delimited.
  • Easting, northing, latitude, and longitude are provided, along with a host of other fields related to UK national statistics.
  • No registration required.

You can download it here (look for ONSPD_<month>_<year>_<format>.zip):

https://geoportal.statistics.gov.uk/geoportal/catalog/content/filelist.page?pos=7

Open Postcode Geo

  • This file is derived from the ONS Postcode Directory, and is maintained by com.
  • The postcode field is provided in four formats: Space-delimited; no space;seven character fixed-width; eight character fixed-width.
  • Easting, northing, latitude, and longitude are provided.
  • Postcode area, postcode district, and postcode sector are provided.
  • All non-geo fields are removed.
  • MySQL dump or CSV.
  • No registration required.

You can download it here:

https://www.getthedata.com/open-postcode-geo

Polygons

If you want polygons, e.g. for GIS applications, you can get these down to postcode level from Ordnance Survey. Unfortunately, this data is neither open nor free of charge.

For more details see this page on the Ordnance Survey website:

https://www.ordnancesurvey.co.uk/business-and-government/products/code-point-with-polygons.html

If you only require polygons for postcode sector, postcode district, or postcode area, then a number of attempts have been made to approximate polygons from open data and release on an open (or relatively open) basis.

Here are a couple of examples:

http://geolytix.co.uk/geodata/

http://www.opendoorlogistics.com/data/

Categories: GIS

About Author

GIS Resources

GIS Resources is an initiative of Spatial Media and Services Enterprises with the purpose that everyone can enrich their knowledge and develop competitiveness. GIS Resources is a global platform, for latest and high-quality information source for the geospatial industry, brings you the latest insights into the developments in geospatial science and technology.

Comments

  1. Biplob Rakhal
    Biplob Rakhal 4 July, 2016, 16:02

    it is interesting to know about post code. In Nepal, some municipalities has developed similar kind of mapping techniques to identify the individual house number based on metric system. Personally I was involved in mapping the GIS database of two municipalities in which the individual house is coded by Municipalty name/Sector name/ Streetcode/housenumber eg.
    Bharatpur/East/1001/21
    BH/E/1001/21
    BHE100121

    Reply this comment
  2. Jonathan O'Grady
    Jonathan O'Grady 20 May, 2017, 21:56

    Dan,

    This is really helpful, thank you very much for sharing!

    I had one quick question for you. I’m using your code above to parse postcode districts, but I’m finding that while the expression:

    (([A-Z][A-Z]{0,1})[0-9][A-Z0-9]{0,1})

    Succeeds in picking up postcode such as:

    E8
    E9
    EC1A
    EC1N
    EC1V

    It is failing to pick up postcode districts such as:

    AB 1

    I’m not an expert in regular expressions, but if you had time to point me in the right direction I’d be very grateful!

    Keep up the great work!

    Best wishes,

    Jonathan

    Reply this comment

Write a Comment

Your e-mail address will not be published.
Required fields are marked*

This site uses Akismet to reduce spam. Learn how your comment data is processed.