in Maths, Python, Science, Tutorial

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, \phi, \lambda and h). Under this framework, Earth is assumed to be a perfect sphere with known radius R. 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 \phi=0^{\circ};
  • The North pole has latitude \phi=90^{\circ};
  • The South pole has latitude \phi=-90^{\circ}.

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

  • The prime meridian has longitude \lambda=0^{\circ};
  • Moving East increases \lambda, up to the antipodal prime meridian which has longitude  \lambda=180^{\circ};
  • Moving West decreases \lambda, up to the antipodal prime meridian which has longitude  \lambda=-180^{\circ}.

Both \lambda=180^{\circ} and  \lambda=-180^{\circ} 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 h=0. 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.: \phi=+50^{\circ} can be expressed as 50^{\circ} N;
  • Negative latitude is often expressed as South (i.e.: \phi=-50^{\circ} can be expressed as 50^{\circ} S;
  • Positive longitude is often expressed as East (i.e.: \lambda=+30^{\circ} can be expressed as 30^{\circ} E;
  • Negative longitude is often expressed as West (i.e.: \lambda=-30^{\circ} can be expressed as 30^{\circ} W.

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 D between two points P=\left(\phi_1, \lambda_1\right) and Q=\left(\phi_2, \lambda_2\right), 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:

    \[D=R \cdot \sqrt{{\Delta \phi}^2 + \left(\cos\phi_m\Delta \lambda\right)^2 }\]

where:

  • R is the radius of the Earth;
  • \Delta \phi = \left| \phi_2 - \phi_1 \right|;
  • \Delta \lambda = \left|\lambda_2 - \lambda_1\right|;
  • \phi_m=\frac{\phi_1+\phi_2}{2}.

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:

    \[d=\sqrt{{\Delta x}^2 + {\Delta y}^2}\]

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

    \[D=R \cdot \sqrt{{\Delta \phi}^2 + \left(\cos\phi_m\Delta \lambda\right)^2 }\]

instead of this one:

    \[D=R \cdot \sqrt{{\Delta \phi}^2 + {\Delta \lambda}^2 }\]

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):

    \[D = R \Delta \sigma\]

where \Delta \sigma is the angle between P and Q, also called the central angle:

    \[\Delta \sigma = \arccos\left( \sin\phi_1 \cdot \sin\phi_2 + \cos\phi_1 \cdot \cos \phi_2 \cdot \cos \Delta \lambda \right)\]

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:

    \[\Delta \sigma = \arctan \frac{ \sqrt{ \left( \cos \phi_2 \cdot \sin\Delta\lambda \right)^2 + \left( \cos \phi_1 \cdot \sin \phi_2 - \sin \phi_1 \cdot \cos\phi_2 \cdot \cos \Delta\lambda \right) ^2 } }{ \sin \phi_1 \cdot \sin \phi_2 + \cos\phi_1 \cdot \cos\phi_2 \cdot \cos\Delta\lambda }\]

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 \Delta\sigma.

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.

💖 Support this blog

This website exists thanks to the contribution of patrons on Patreon. If you think these posts have either helped or inspired you, please consider supporting this blog.

Patreon Patreon_button
Twitter_logo

YouTube_logo
📧 Stay updated

You will be notified when a new tutorial is released!

📝 Licensing

You are free to use, adapt and build upon this tutorial for your own projects (even commercially) as long as you credit me.

You are not allowed to redistribute the content of this tutorial on other platforms, especially the parts that are only available on Patreon.

If the knowledge you have gained had a significant impact on your project, a mention in the credit would be very appreciated. ❤️🧔🏻

Write a Comment

Comment

Webmentions

  • Localisation and Trilateration - Alan Zucconi October 11, 2018

    […] If you are unfamiliar with the concepts of latitude and longitude, I suggest you read the first post in this series: Understanding Geographical Coordinates. […]

  • Geographical Coordinates for Programmers | StratCom October 11, 2018

    […] Error loading HTMLUnique Article […]