Skip to main content

A tool to look up UK postcode eastings, northings, and convert them to latitude and longitude.

Project description

Postcode Lookup Tool

This repository contains a simple Python package for looking up UK postcodes to obtain their easting/northing coordinates and corresponding latitude/longitude values. The package reads data from CSV files based on the postcode prefix and uses this data to perform lookups efficiently.

Features

  • Lookup eastings and northings for a given UK postcode.
  • Convert the easting and northing coordinates to latitude and longitude.
  • Efficient caching using functools.lru_cache to improve performance for repeated postcode prefix lookups.

Installation

You can install this package directly from PyPI using pip:

pip install uklookup

Usage

Command Line Usage

Once installed, you can use the package directly from the command line to look up the easting, northing, latitude, and longitude for a UK postcode:

python -m uklookup "SW1A 1AA"

Example output:

Postcode: SW1A 1AA
Easting: 530046, Northing: 179386
Latitude: 51.501009, Longitude: -0.141588

Programmatic Usage

You can also import the package and use it in your own Python scripts.

Example:

from uklookup import lookup_postcode_lat_long

postcode = "SW1A 1AA"
lat_long = lookup_postcode_lat_long(postcode)

if lat_long:
    lat, long = lat_long
    print(f"Latitude: {lat}, Longitude: {long}")
else:
    print("Postcode not found.")

Functions

lookup_postcode(postcode: str) -> Optional[Tuple[int, int]]

Looks up the easting and northing for a given UK postcode. Returns a tuple of integers (easting, northing) if found, or None if the postcode is not in the dataset.

lookup_postcode_lat_long(postcode: str) -> Optional[Tuple[float, float]]

Looks up the latitude and longitude for a given UK postcode. Returns a tuple of floats (latitude, longitude) if found, or None if the postcode is not in the dataset.

Dependencies

  • numpy
  • convertbng (for converting eastings/northings to latitudes/longitudes)

License

This project is licensed under the MIT License.

Acknowledgements

This project uses data from the Code-Point Open dataset provided by Ordnance Survey. The data is available under the Open Government Licence v3.0.

Other Projects

A much more comprehensive postcode lookup tool is available at postcodes.io. This project is a simple alternative for basic postcode lookups in an offline setting.

pgeocode looks like a much more advanced, multi-country, higher detail version of this tool. So if you are looking for a high power tool in offline setting check that out.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

uklookup-0.0.2.tar.gz (13.9 MB view details)

Uploaded Source

Built Distribution

uklookup-0.0.2-py3-none-any.whl (13.9 MB view details)

Uploaded Python 3

File details

Details for the file uklookup-0.0.2.tar.gz.

File metadata

  • Download URL: uklookup-0.0.2.tar.gz
  • Upload date:
  • Size: 13.9 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.1.1 CPython/3.12.7

File hashes

Hashes for uklookup-0.0.2.tar.gz
Algorithm Hash digest
SHA256 8e6bddf6f5f24a7974ea9af3e8fcb421e3b20e909fbb3b117f1a15ae0c070ecb
MD5 b871ce25187a73efdb4c19cdebd6c6df
BLAKE2b-256 e0e65dc0b008bb614c268a02a78a5ca5288451b0e76f1997be41f1424894fa7b

See more details on using hashes here.

File details

Details for the file uklookup-0.0.2-py3-none-any.whl.

File metadata

  • Download URL: uklookup-0.0.2-py3-none-any.whl
  • Upload date:
  • Size: 13.9 MB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.1.1 CPython/3.12.7

File hashes

Hashes for uklookup-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 2b0217fe66c5c4cf5bb29702aa901e7c83921cfac6dea4c3cd875b52c29236a4
MD5 9b362c0e22c5e329d6f6f8907375225f
BLAKE2b-256 14e2e1899388d5b091b2c60f4612c3ce4fea2cc80c476a8280005c14b48f1484

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page