Tutorial #23
Latitude, Longitude, Altitude and Direction Background   2013-11-18


We all think we understand the concepts of Latitude, Longitude, Altitude and Direction but when we want to start working with geographic data there are issues of data representation and frames of reference that can lead to confusion and error.

This Background tutorial will describe some of these issues and how to handle these types of data.

Latitude and Longitude

Any point on the surface of the Earth can be defined with a Latitude and Longitude coordinate pair.

Latitude specifies the angular distance of a point from the Equator, represented as a number of degrees between -90 (at the South Pole) and +90 (at the North Pole).

Longitude specifies the angular distance of a point from a line, called the Primer Meridian, the runs from the North Pole to the South Pole through a defined, but arbitrary, point at the Royal Observatory, in Greenwich, London. Values for Longitude run from -180 degrees to +180 degrees, with negative values being West of the Primer Meridian.

Image 1 for this tutorial
Image credit

The problem with coordinates is that they can be written in more than one format.

This Wikipedia entry describes the variants and how to convert between them.

Degress, Minutes, Seconds (DMS) format splits each coordinate into three components, followed by a direction from either the equator or the meridian. There are 60 seconds in a minute and 60 minutes in a degree.

For example: 40°26′21″N 079°58′36″W

Decimalformat represents the minutes and seconds as a decimal fraction and makes the number neagtive for latitudes south of the equator and longitudes west of the meridian.

For example: 40.446195, -79.948862

For our purposes, we typically want the Decimal format. It is easier to perform calculations on and, importantly, Google Maps requires it.

Unfortunately, DMS format is widely used elsewhere. For example, the GPS metadata included in photographs, such as from the iPhone, is in DMS format.

Happily, conversion from DMS into Decimal is straightforward. Here is a chunk of Ruby code that does this:

# Convert Lat Lon in Deg, Min, Sec to Decimal format
# e.g 122 deg 25' 25.20" W -> 122.42366666
def dms_to_decimal(coordinate)
  decimal = 0.0
  if coordinate =~ /^(\d+)\s+deg\s+(\d+)\'\s+([\d\.]+)\"\s+(\S)\s*$/
    deg = $1.to_f
    min = $2.to_f
    sec = $3.to_f
    direction = $4
    decimal = deg + (min*60.0 + sec)/3600.0
    if direction == 'S' or direction == 'W'
      decimal = -decimal

Altitude / Elevation

To fully define a point on, above, or below the surface of the Earth we need to define the Altitude, otherwise known as the Elevation or Geodetic Height

In general use, we define Altitude as relative to Sea Level. As this is variable, we use a fixed reference point, or points, which vary between countries, but which all approximate the notion of sea level.

In GPS units, cameras and phones there are two ways to determine your current altitude.

In principle, the most accurate is a Barometric Sensor which measures the Air Pressure. The pressure is lower at higher altitudes. The problem with these is that their results vary according to the Weather. The same location will appear to be at different altitudes if you are in the midst of a low pressure weather system versus a high pressure system.

For accurate reading, you need to calibrate the sensor at a known altitude. For example, with a handheld GPS unit you might calibrate to the known altitude at the trailhead before hiking in the mountains.

I believe that Android phones use barometric sensors.

The other approach is to compute altitude from GPS data. At any given location a GPS receiver detects the location of multiple satellites and computes its Latitude and Longitude. It can also use those data to compute the altitude, but the accuracy of this is much lower than with Lat Lon.

When you move across the surface of the Earth, the angles you make to the satellites, high above you, will vary by relatively large values. But when you move vertically, the change in those angles is much less as you are moving towards or away from the satellites.

You don't need to calibrate GPS based altitude measurements but you should treat them with caution. You would not use them to fly a plane at night, for example...

The iPhone uses GPS data for altitude measurements.

There is a third method for determining altitude, but only for points on the surface of the Earth. Google Maps has captured Elevation data for points all over the Earth, including the bottom of the Sea, and they make this available through their Elevation API. You pass in a Lat Lon coordinate pair and it uses the elevations of nearby points in its database to interpolate the elevation of your point.

Google does not address the issue of accuracy in its documentation - that will depend on the density of nearby data points.

None of these methods is without issues, but for a lot of applications they will be 'good enough'.

Direction / Bearing

A Direction or Bearing, in common usage, is an orientation in the horizontal plane, relative to the Compass Points of North, South, East and West.

It is typically represented as an angle between 0 and 360, with North at 0, East at 90, etc.

The meaning of Direction is specific to each instance. It might refer to the direction that a camera was pointing when a photograph was taken or it might refer to the direction a vehicle was moving at a given time point.

The classic way to measure direction is a Magnetic Compass which aligns itself to the lines of magnetic flux that run North-South around the Earth.

Devices like the iPhone include a digital version of a magnetic compass called a magnetometer. It has no moving parts. If you look at the Compass application on an iPhone, or its equivalent on Android, etc., you should see that it acts like a regular compass.

But you will also see that, from time to time, it requires calibration. On the iPhone this involves moving the phone in a circular motion.

A big issue for conventional compasses is that the magnetic field varies quite dramatically in different locations. The location of the Magentic North Pole is not the same as the True North Pole and local variations in field mean that topographic maps always include the local Magnetic Declination, a correction that you must make to a compass reading to get a bearing relative to True North.

The compasses in mobile devices appear to handle this automatically. This may be using a database of corrections and looking up the appropriate value from your current location.

In the metadata of photographs taken on the iPhone you will find the bearing information listed like this:

GPSImgDirectionRef  True North
GPSImgDirection        268.3062731

This indicates that the value given has been corrected for local declination.

You should treat bearings from phones, or the iPhone at least, with some caution. In my experience, they are not very accurate. They are still useful but you would not want to use it to navigate across the moors of Northern England, for example!

As an alternative, if we are moving, we can also use the difference between Lat/Lon coordinate pairs measured, through GPS, at two times. Those define the direction we have moved in that time interval, although it says nothing about the actual path taken between those points. This is the approach used in car GPS systems, I believe, where you can assume the vehicles are in motion.


Location data is incredibly useful but it is not perfect. I hope this tutorial helps you use it approriately in your apps.

Share this tutorial

Comment on this Tutorial

comments powered by Disqus