Spherical coordinates define a point in 3D space (may be generalized to higher dimensions) by using a radial distance from the origin and angles from reference axes. It is a higher-dimensional equivalent to 2D polar coordinates.
Conventions
There are several conventions for representing spherical coordinates. Each is discussed here.
Latitude
Latitude angles are measured from an equatorial plane passing through the origin (canonically the xy-plane). This type of measurement is used typically in geospatial contexts.
Given spherical coordinates with radius, longitude, and latitude , the corresponding point in Cartesian coordinates is given by:
This convention will be used for other areas of this page unless otherwise specified.
Colatitude
Colatitude angles are measured from the z-axis. This convention is seldom used outside the realm of mathematics. Even so, it is used in conjunction with other conventions.
Given spherical coordinates with radius, longitude, and colatitude , the corresponding point in Cartesian coordinates is given by:
Angle Between Vectors
Proof. Note that and . The dot product is given by:
Where and are the Cartesian coordinates of and , respectively. Equating both definitions and solving for gives:
Substituting the above formulae for these coordinates into the alternate dot product definition gives:
Note the use of the subtractive trigonometric identity for cosine. Since cosine is an even function (i.e. , the order of subtraction does not matter; only the difference between and matters.
quod erat demonstrandum
Computational Formula
In situations where floating point rounding errors pose a challenge, the following alternate formula may be used (from wikipedia:Great-circle distance):
Below is a Python implementation (using the NumPy library) of this formula.
import numpy as np
def angle_between(lng1, lat1, lng2, lat2):
s1 = np.sin(lat1)
c1 = np.cos(lat1)
s2 = np.sin(lat2)
c2 = np.cos(lat2)
dlng = lng2 - lng1
c2cdlng = c2 * np.cos(dlng)
return np.arctan2(
np.sqrt((c2*np.sin(dlng))**2 + (c1*s2 - s1*c2cdlng)**2),
s1*s2 + c1*c2cdlng
)