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?

❓ 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 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 Δλ?

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.

Comments

3 responses to “Understanding Geographical Coordinates”

  1. pongthana avatar

    Hi. I don’t understand that how to calculate position with three-Dimension-triangulation after center point of sphere.
    Reference: https://www.researchgate.net/publication/312060737_Three-dimensional_positioning_system_using_Bluetooth_low-energy_beacons

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

  3. […] Error loading HTMLUnique Article […]

Leave a Reply

Your email address will not be published. Required fields are marked *