

The downside here is it provides no way to check distance, so you will have to do it yourself using the Longitude and Latitude returned. Results are in JSON which can be mapped to a Python dictionary using the json module. Postcodes.io has a nice API backed by a public data set. Option 2 - Do it yourself with postcodes.io Note: Google Maps API is subject to usage limits. The results from this will be based on travel (so driving distance), this may or may not be what you want. The link above is to the Distance Matrix API, which will help with working out distances between 2 points. The Google Maps API is feature packed and will provide you with a lot of options. The Google maps service at is of no use, as it's not designed for you to retrieve information like this. WikipediaĪ postcode by itself provides no useful information, so you are correct you need help from an external source. The main issue with finding a distance between 2 postcodes is that they aren't designed for it.įor the purposes of directing mail, the United Kingdom is divided by

In the particular case of Great Britain, only the outward codes are included in the GB dataset, the full dataset is also available as GB_full but it is currently not supported in pgeocode. This uses the GeoNames postal code dataset to get the GPS coordinates, then computes the Haversine (great circle) distance on those. State_name state_code county_name county_code community_name \Ġ England ENG Greater London 11609024 NaNĬommunity_code latitude longitude accuracy > nomi.query_postal_code()ġ EH53 GB Pumpherston, Mid Calder, East Calder, Oakbank More information about these postal codes, including latitude and longitude, can be queried with, > nomi = pgeocode.Nominatim('GB') Unlike the above response, it does not query a web API, and is therefore more suitable for processing large amounts of data, > import pgeocode The distance between postal codes can be obtained with the pgeocode library.
