# Cosine distance with latitude and longitude

I have several features I'd like to use for computing cosine similarity between rows in a data set. However, two of them are latitude and longitude.

Apart from the fact that it's not the "correct" way to measure the distance between points on the surface of the Earth, is there any pressing reason I can't use them along with other features to compute cosine similarity between two rows of a data set?

• How do you propose to compute the distance between, say, the nearby points at $(-179.99, 0)$ and $(+179.99, 0)$? Or between $(0, 89.99)$ and $(180, 89.99)$? A quick and dirty method is to convert (lon, lat) to spherical coordinates: then you'll be fine (at least once you decide what spherical radius is appropriate). – whuber Oct 21 '16 at 21:25
• @whuber great point, and clever solution! might as well post that as an answer (by the way, 6,371 km should be fine for what i'm doing) – shadowtalker Oct 21 '16 at 21:41
• 6371 will be fine if all your other variables are comparable to distances in kilometers. As I'm sure you know, cosine similarity can change dramatically when individual variables are rescaled. What is likely, though, is that you would want to use a common spherical radius to rescale the three Cartesian coordinates simultaneously, rather than independently rescaling them. – whuber Oct 21 '16 at 21:43

Because latitude and longitude are circular coordinates, some care is needed.

A simple solution is to convert them to geocentric Cartesian coordinates. For most purposes the usual conversion from spherical to Cartesian coordinates works just fine. A highly accurate calculation is included in my post at https://gis.stackexchange.com/a/34534/664; the key code is this:

ellipsoidToCartesian[{lon_, lat_}, {a_,b_}] :=
{a Cos[lat] Cos[lon], a Cos[lat] Sin[lon], b Sin[lat]};
cartesianToEllipsoid[{x_, y_, z_}, {a_,b_}] :=
{ArcTan[x, y], ArcTan[Norm[{x, y}]/a, z/b]};


(This is written in Mathematica. It serves as pseudocode for implementation in other environments, but pay attention to the order of arguments to ArcTan.)

The values of a and b are the planet's semi-axes. For modern Earth coordinate systems, such as WGS84, $a = 6\,378\,137.0$ and $b \approx 6\,356\,752.314\,245$ meters. When adopting a spherical approximation, use the Authalic radius of $6\,371\,007.2$ meters--but feel free to rescale this radius if you wish to adjust the relative weight of your coordinates within the overall analysis.

If you also have height or depth data coordinates relative to the planet's surface, refer to that post for details.