Python Geopy – Find farthest location

In this post we will be creating a Python script that will find the farthest location to a given reference location. To do this we will be using the Geopy library which is a Python client for several popular geocoding web services. To install the library run the following in command line.

pip install geopy

For this example each of our locations will be given in latitude and longitude co-ordinates. Our reference location will be the bank of England as seen below.

Note: For more on the Python Geopy library click here.

Bank of England co-ordinates – ‘51.51413225,-0.08892476721255456’

Our list of other locations can be seen below.

Tower of London co-ordinates – ‘51.50800374,-0.07685069’

Canterbury Cathedral – ‘51.2794458,1.0813207’

Royal Botanic Gardens, Kew – ‘51.478340599999996,-0.29689810014827533’

See the sample of Python code below which will find and return the farthest location to our reference and return this to output.


from geopy.distance import great_circle

ref_location = (51.51413225,-0.08892476721255456)

other_locations = [
    (51.50800374,-0.07685069),
    (51.2794458,1.0813207),
    (51.478340599999996,-0.29689810014827533)
]

farthest_location = max(other_locations, key=lambda x: great_circle(ref_location, x).miles)

print("Farthest  location:", farthest_location)

The above would return the following as output. This means that the Canterbury Cathedral is the farthest to our reference location i.e. The Bank of England.

Farthest location: (51.2794458, 1.0813207)
>>> 

Take a look at some of our other content around the Python programming language here. Also find some more posts on the Geopy library by clicking here.

Leave a Reply