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
- Part 1. Understanding Geographical Coordinates
- Part 2. Local Geographical Distance
- Part 3. Great-Circle Distance
- Conclusion

#### 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.

❓ What about the *altitude*?

Altitude in generally expressed in kilometre from the sea level, which is assumed to have . This series will not take into account altitude. This massively simplifies the resulting equations.

❓ Alternative conventions to express geographical coordinates

To make this notation even more complicated, there are multiple ways in which latitude and longitudes can be expressed.

Positive and negative coordinates are often expressed using North, South, East and West with the following conventions:

- Positive latitude is often expressed as North (i.e.: can be expressed as ;
- Negative latitude is often expressed as South (i.e.: can be expressed as ;
- Positive longitude is often expressed as East (i.e.: can be expressed as ;
- Negative longitude is often expressed as West (i.e.: can be expressed as .

Another way in which the angles can be expressed uses *degrees*, *minutes* and *seconds*. If you are interested in knowing more about this topic, Wikipedia has an interesting page on Geographic coordinate conversion.

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

Latitude and longitude *loosely* map to orthogonal coordinate system. If you have the map of a city, moving vertically changes your latitude, while moving horizontally changes your longitude. However, latitude and longitude do not map *one to one *to a Cartesian coordinate system. This is because the surface of the Earth is curved; while it is locally flat, its curvature becomes apparent on a larger scale.

#### 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): # Degrees to radians delta_latitude = math.radians(latitudeB - latitudeA) delta_longitude = math.radians(longitudeB - longitudeA) 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 Δλ?

You might have recognised that the equation used in this section uses the Pythagoras’s Theorem to calculate the distance between two points:

This real question is why do we need to use this equation:

instead of this one:

To answer this question, we first need to understand that even if latitude and longitude are both expressed in degrees, they are qualitatively different. Latitude refers to *parallels*, which (like the name suggests) always stay parallel to each others. Longitude, on the other hand, is based on *meridians* which intersects at the pole. Parallels and meridians are fundamentally different ways to measure the same thing.

#### 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): # Degrees to radians phi1 = math.radians(latitudeA) lambda1 = math.radians(longitudeA) phi2 = math.radians(latitudeB) lambda2 = math.radians(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 .

### 📚 Recommended Books

#### 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.

**Part 1. Understanding Geographical Coordinates**- Part 2. Positioning and Trilateration

## Leave a Reply