UK Postcodes – Distance Calculator

haversine

Challenge


Your challenge is to write a Python program that asks the end-user to enter two valid UK postcodes and in return displays the distance in miles or km between those two postcodes.

To complete this challenge you will first find the exact longitude and latitude of both the postcodes entered by the end user. The following text file contains a list of all the UK outer codes (first part of a postcode) with their exact longitude and latitude.


TextFile
UK Postcodes.csv

Once your program will have retrieved the longitude and latitude of both locations (postcodes) it will use the Haversine formula to calculate the exact distance between these two locations.

Haversine Formula


The Haversine formula is an equation important in navigation, giving great-circle distances between two points on a sphere from their longitudes and latitudes.

This formula will enable us to calculate the shortest distance over the earth’s surface – giving an ‘as-the-crow-flies’ distance between the two locations (ignoring any hills they fly over, of course!).

You can read more about the haversine formula on this wikipedia page.

To complete this challenge you will also need to investigate the use of trigonometric functions from the math library.

haversine-formula-1

haversine-formula-2

haversine-formula-3

Note that when completing this challenge, longitudes and latitudes are given in degrees, not radians.


Once completed you can check your distances online.

unlock-access

Solution...

The solution for this challenge is available to full members!
Find out how to become a member:
➤ Members' Area

Did you like this challenge?

Click on a star to rate it!

Average rating 5 / 5. Vote count: 5

No votes so far! Be the first to rate this post.

As you found this challenge interesting...

Follow us on social media!

Tagged with: , ,