Omniscope Custom Blocks › Preparation/Geo/UK-Postcode-Tools

UK Postcode Tools

Validates, standardises and extracts UK postcodes and splits them into area, district and sector with area name and country, offline.

Preparation › Geo Python visokio-agents

Open the demo in Omniscope Download demo.ioz Sample data: customer-addresses.csv Source on GitHub

Items tagged visokio-agents are community examples built by Visokio's agents, to extend Omniscope's core blocks and views with popular, specialist and more unusual use cases, and to show what anyone, people or agents, can build on Omniscope. They are tested by automated tests and were run on a real Omniscope server. They were not reviewed by Omniscope's core team, so they are not on a par with the core blocks and views, which have years of manual, functional and unit tests behind them. About

Omniscope custom block · Preparation

Valid, consistently formatted UK postcodes with area, district, sector, area name and country, even when they are buried in a free-text address. Offline, nothing to subscribe to.

Python · pandas · nothing extra to install

The problem it solves

Postcodes are the key to UK geography: territories, delivery zones, catchments, demographics and maps all hang off them. But CRM and order data hold them in every format (ls15hd, M3 2EG, YO1 OAB with a letter O), sometimes only inside the address line, and some are simply invalid.

UK Postcode Tools validates every value against the official format and the list of postcode areas, standardises it, explains what is wrong when it fails, and splits it into the parts analysts join and aggregate on.

How it works

What goes in

Customer addresses

24 synthetic customers, postcodes typed in mixed formats; one missing, three invalid.

Customer ID Address Postcode
C001 12 Larch Row, Leeds LS1 5HD ls15hd
C002 Flat 3, 48 Bramble Street, Manchester, M3 2EG M3 2EG
C003 7 Thistle Court Edinburgh EH2 2PR EH2 2PR
C004 Unit 4 Heron Business Park, Cardiff CF10 5AF CF105AF
C005 27 Willow Street, Bristol BS1 4D
C006 5 Linen Yard Belfast BT1 5AB bt1 5ab
C007 The Old Croft, Lerwick, Shetland ZE1 0AA ZE1 0AA
C008 14 Ashgrove Lane Glasgow G1 3DX G1 3DX
C009 La Petite Rue, St Peter Port GY1 1AA GY1 1AA
C010 99 Marigold Street London W3 7TN W3 7TN

24 rows in the sample file.

The options

Option Demo setting What it does
Postcode or address field Postcode
Find the postcode inside the text Off Tick when the field is a full address rather than just a postcode.
Prefix for new fields Postcode New fields are ' Valid', ' Area' and so on.

What comes out

Postcodes

Customer ID Postcode Postcode Formatted Postcode Valid Postcode Problem Postcode District Postcode Sector Postcode Area Name Postcode Country
C001 ls15hd LS1 5HD Yes LS1 LS1 5 Leeds England
C002 M3 2EG M3 2EG Yes M3 M3 2 Manchester England
C003 EH2 2PR EH2 2PR Yes EH2 EH2 2 Edinburgh Scotland
C004 CF105AF CF10 5AF Yes CF10 CF10 5 Cardiff Wales
C005 BS1 4D No Inward code (last three characters) not valid
C006 bt1 5ab BT1 5AB Yes BT1 BT1 5 Belfast Northern Ireland
C007 ZE1 0AA ZE1 0AA Yes ZE1 ZE1 0 Lerwick (Shetland) Scotland
C008 G1 3DX G1 3DX Yes G1 G1 3 Glasgow Scotland
C009 GY1 1AA GY1 1AA Yes GY1 GY1 1 Guernsey Channel Islands
C010 W3 7TN W3 7TN Yes W3 W3 7 London W England
C011 NW5 2RX NW5 2RX Yes NW5 NW5 2 London NW England
C012 PL1 2LR PL1 2LR Yes PL1 PL1 2 Plymouth England
C013 CH12HU CH1 2HU Yes CH1 CH1 2 Chester England / Wales (cross-border)
C014 No Missing
C015 NR1 1EF NR1 1EF Yes NR1 NR1 1 Norwich England
C016 SW9 8LP SW9 8LP Yes SW9 SW9 8 London SW England
C017 TD1 1BX TD1 1BX Yes TD1 TD1 1 Galashiels Scotland / England (cross-border)
C018 SA1 5LB SA1 5LB Yes SA1 SA1 5 Swansea Wales
C019 YO1 OAB YO1 0AB Yes YO1 YO1 0 York England
C020 QQ1 1AA No Letters or digits not allowed in this position
C021 IM1 1AA IM1 1AA Yes IM1 IM1 1 Isle of Man Isle of Man
C022 SY3 8LA SY3 8LA Yes SY3 SY3 8 Shrewsbury England / Wales (cross-border)
C023 AB101AB AB10 1AB Yes AB10 AB10 1 Aberdeen Scotland
C024 S1 2HE S1 2HE Yes S1 S1 2 Sheffield England

Area summary

Postcode Country Postcode Area Postcode Area Name Records Share
Channel Islands GY Guernsey 1 0.042
England LS Leeds 1 0.042
England M Manchester 1 0.042
England NR Norwich 1 0.042
England NW London NW 1 0.042
England PL Plymouth 1 0.042
England S Sheffield 1 0.042
England SW London SW 1 0.042
England W London W 1 0.042
England YO York 1 0.042
England / Wales (cross-border) CH Chester 1 0.042
England / Wales (cross-border) SY Shrewsbury 1 0.042

Extracting from the address text instead

Address Postcode Formatted Postcode Valid Postcode Area Name
12 Larch Row, Leeds LS1 5HD LS1 5HD Yes Leeds
Flat 3, 48 Bramble Street, Manchester, M3 2EG M3 2EG Yes Manchester
7 Thistle Court Edinburgh EH2 2PR EH2 2PR Yes Edinburgh
Unit 4 Heron Business Park, Cardiff CF10 5AF CF10 5AF Yes Cardiff
27 Willow Street, Bristol No
5 Linen Yard Belfast BT1 5AB BT1 5AB Yes Belfast
Valid postcodes by country (synthetic data).
Valid postcodes by country (synthetic data).

Valid postcodes by country (synthetic data).

Where it pays off

Retail and e-commerce: Delivery and catchment analysis

Standardise customer postcodes, then aggregate by district or sector for catchment maps, delivery zones and local marketing.

Insurance: Rating by postcode sector

Clean and split postcodes to sector level to join risk factors and rating tables reliably.

Public sector and charities: Service reach by nation and area

Report users by country and postcode area, and catch invalid postcodes at the point of entry.

Data quality and CRM: Address hygiene

A repeatable postcode check in every data load, with the problem per record for the data steward.

Use cases describe typical scenarios and are illustrative; the sample data is synthetic.

Why do this in Omniscope

How it fits into an Omniscope workflow

CRM / orders → Data Cleansing → UK Postcode Tools → Join (postcode lookup or territories) → Report

Run after Data Cleansing and before joins on postcode, such as to a territory table, to the ONS Postcode Directory for coordinates, or to demographic data.

Route invalid records (Postcode Valid = No) to a review table or back to the source system.

The standardised postcodes feed the territory or ONS joins directly in the workflow, so the cleaned values and the report filters stay consistent from the CRM load to the final view.

In one step

Offline format and area validation, standardisation and splitting in one block; a full address-existence check still needs a lookup file or service.

Try it

  1. Open the demo project (demo.iox on the demo server, or import demo.ioz from this folder): customer addresses with postcodes typed every possible way.
  2. The question: can we join these to our territory or demographic tables as they are? Not reliably.
  3. Open UK Postcode Tools: choose the Postcode field. Run it.
  4. Look at the output: 21 of 24 valid and standardised, the letter-O slip fixed, invalid ones with a reason, plus district, sector, area name and country.
  5. Switch to the Address field with extraction ticked: the postcode is found inside the free text.

Running in Omniscope

Omniscope workflow
Omniscope workflow

The demo project's workflow: the sample data, the block, and a Validate data check on its output.

Language and libraries

Python. It uses pandas, plus the Python standard library (re).

pandas and numpy are part of Omniscope's Docker Python image, and the block was run in both host mode and Docker mode on an Omniscope server, with the same results. It needs nothing else installed.

Tested

Run on a real Omniscope server:

The block is also covered by automated tests (normal data, empty input, wrong types, large input).

Reference

Fields it reads

NameTypeWhat it does
Postcode or address fieldField

Options

NameTypeWhat it does
Find the postcode inside the textYes / noTick when the field is a full address rather than just a postcode.
Replace the field with the formatted postcodeYes / noOtherwise a new field is added. Ignored when extracting from text.
Prefix for new fieldsTextNew fields are '<prefix> Valid', '<prefix> Area' and so on.

Outputs

OutputWhat it holds
PostcodesThe input with formatted postcode, validity, problem, area, district, sector, area name and country.
AreasRecords per country and postcode area, plus counts per problem for invalid postcodes.