Convert from kilometers, (km), to decimal degrees

user965586 picture user965586 · Aug 9, 2013 · Viewed 7k times · Source

Using scipy.KDTree to do some quick nearest neighbour searches. I'm using KDTree.query_ball_point(pnt, r=some_distance) to do the search.

As my points are lat,long the radius value to search (some_distance) has to be in decimal degrees (I believe). If I wanted to make this accessible to a user, I would expect that distance to be given in Kilometres, meters, miles etc.

What is the best way, with python libs, to convert a distance in km to a decimal degrees value? I'm using numpy, scipy and played a bit with PySAL.

Help appreciated, Louis

Answer

Steve Barnes picture Steve Barnes · Aug 9, 2013

Classic Calculation from here:

Distance

This uses the ‘haversine’ formula to calculate the great-circle distance between two points – that is, the shortest distance over the earth’s surface – giving an ‘as-the-crow-flies’ distance between the points (ignoring any hills, of course!).

Haversine formula:

a = sin²(Δφ/2) + cos(φ1).cos(φ2).sin²(Δλ/2)
c = 2.atan2(√a, √(1−a))
d = R.c

where φ is latitude, λ is longitude, R is earth’s radius (mean radius = 6,371km) note that angles need to be in radians to pass to trig functions!

You can of course do a very rough and ready approximation from the definitions of Nautical Mile and kilometre:

The nautical mile (symbol M, NM or nmi) is a unit of length that is about one minute of arc of latitude measured along any meridian (at sea level), or about one minute of arc of longitude at the equator. By international agreement it has been set at 1,852 metres exactly (about 6,076 feet).