Notes on Address Lookup with Royal Mail PAF and the ONS Postcode Database

At work we use the Royal Mail Postcode Address File to provide address information. The file comes in CSV format with a 200-odd page “Programmer’s Guide” which contains, among other things, the fairly extensive rules (and exceptions!) for constructing addresses from the database fields.

Based on these rules I developed the PafUtils PHP library. The library is fully tested and also includes some “fixes” for situations we’ve encountered with incorrectly formatted addresses.

One requirement we have is to include the “postal county” in addresses, while this information is included in the Royal Mail PAF alias file (at least for now), we found it didn’t always appear to be complete. To fix this I read in and combine the PAF with data from the ONS postcode directory

For one of our backend systems we wanted to determine addresses within a given radius of a location. While it is now included, the Royal Mail PAF didn’t originally include coordinates. However the ONS PD does include this information. I use the PHP-Coord package for dealing with coordinates.

For points within a radius of a given coordinate, you use the Haversine formula. You might want to check out this StackOverflow post for some pointers on handling this.