# Understanding Geographical Coordinates

This series introduces the concept of trilateration. This technique can be applied to a wide range of problems, from indoor localisation to earthquake detection. This first post provides a general introduction to the concept of geographical coordinates, and how they can be effectively manipulated. The second post in the series, Positioning and Trilateration, will cover the actual techniques used to identify the position of an object given independent distance readings. Most trilateration tutorials require the measures from the sensors to be precise and consistent. The approach here presented, instead, is highly robust and can tolerate inaccurate readings.

#### Introduction

There is a basic fact that permeates Engineering: sensors are imprecise. Whether you are using a temperature sensor or a camera, your measures will always have a finite accuracy. A simple way to overcome this limitation is to average out multiple readings from the same sensor. This often allows to cancel out the noise that is poisoning your readings.

If multiple sensors are available for you to use, it is often possible to merge their readings not just to refine your measurements, but to discover entirely new pieces of information. Sensor fusion (Wikipedia) is the umbrella term that is generally used to describe this family of techniques. They find many practical applications: GPS, earthquake detection, and indoor positioning are just few of them.

If you are unfamiliar with the concept, GPS (Global Positioning System) is a technology that allows to localise a device on the surface of the planet. It relies on a network of intercommunicating satellites, which are orbiting the planet. When your device establishes a connection with a satellite, the latter is unable to calculate your position. In a nutshell, the only information that each satellite knows is your distance. No single satellite knows your position. However, merging the data from multiple satellites allows to identify your exact location.

#### Understanding Geographical Coordinates

If we want to locate an object on the surface of the planet, we first have to understand how geographical coordinates work. Throughout history there have been many attempts to solve the insidious problem of mapping Earth. The most common solution nowadays assigns three coordinates to each point on the surface of Earth: latitude, longitude and altitude (namely, , and ). Under this framework, Earth is assumed to be a perfect sphere with known radius . Latitude and longitudes represents the angles (in degrees) of a point on the surface, in respect to the Earth centre (picture below).

To measure latitude, the following conventions hold:

• The equator has latitude ;
• The North pole has latitude ;
• The South pole has latitude .

Loosely speaking, latitude contributes to the (or vertical) component of a map. Conversely, longitude measures the (or horizontal) component. The imaginary line that connects the poles and passes through Greenwich is called the prime meridian:

• The prime meridian has longitude ;
• Moving East increases , up to the antipodal prime meridian which has longitude  ;
• Moving West decreases , up to the antipodal prime meridian which has longitude  .

Both and  map to the same meridian, which is on the other side of the Earth from Greenwich.

❓ Alternative conventions to express geographical coordinates

❓ Are latitude and longitude equivalent to a Cartesian coordinate system?

#### Local Geographical Distance

On a small scale, Earth can be assumed to be locally flat. When you measure distances within a small city, for instance, you do not need to take into account the curvature of the planet. Let’s assume you want to calculate the geographical distance between two points and , respectively. Assuming Earth to be locally flat, you can simply rely on the Euclidean distance between these two points, correctly projected onto a Cartesian plane:

where:

• is the radius of the Earth;
• ;
• ;
• .

For this equation to work, all quantities must be expressed in radians, rather than degrees.

def geographical_distance (latitudeA, longitudeA, latitudeB, longitudeB):
mean_latitude   = math.radians((latitudeA + latitudeB) / 2.0	)

R = 6371.009 # Km

# Spherical Earth projected to a plane
return \
R * math.sqrt \
(
math.pow
(
delta_latitude,
2
)
+
math.pow
(
math.cos(mean_latitude) *
delta_longitude,
2
)
)

❓ Why can’t we use use Δϕ and Δλ?

#### Great-Circle Distance

The Euclidean distance provides a good approximation for the distance between two geographical points only when they are relatively close. For larger distances, one has to take into account the curvature of the planet.

If you imagine Earth as a perfect sphere, the distance between two points is the length of a the shortest piece of strings that connects them. That string will naturally follow the curvature of the sphere, much like the distance we have to calculate. This concept is known as great-circle distance, and is explained in detail on this Wikipedia article.

Mathematically speaking, the shortest distance between two points on a sphere can be calculated using the spherical law of cosines (Wikipedia):

where is the angle between and , also called the central angle:

While the above mentioned equation is mathematically correct, it is also numerically unstable. This means that rounding errors can have a negative effect on the precision of the final result. A more accurate formula what works well with floating point arithmetic is:

This translates into the following Python code:

def great_circle_distance (latitudeA, longitudeA, latitudeB, longitudeB):

delta_lambda = math.fabs(lambda2 - lambda1)

central_angle = \
math.atan2 \
(
# Numerator
math.sqrt
(
# First
math.pow
(
math.cos(phi2) * math.sin(delta_lambda)
, 2.0
)
+
# Second
math.pow
(
math.cos(phi1) * math.sin(phi2) -
math.sin(phi1) * math.cos(phi2) * math.cos(delta_lambda)
, 2.0
)
),
# Denominator
(
math.sin (phi1) * math.sin(phi2) +
math.cos (phi1) * math.cos(phi2) * math.cos(delta_lambda)
)
)

R = 6371.009 # Km
return R * central_angle

The function math.atan2 is used to calculate the correct sign of .

#### Conclusion

This post introduced the concept of geographical coordinates, and the challenges that arise when trying to manipulate them. The next post will show how it is possible to use multiple distance readings to correctly locate objects.