## Introduction

This project provides a solution to manage coordinates at both memory and persistent storage (XML or database), complying with the ISO 6709 Standard (Annex H - Text string representation), in concordance with the World Wide Web Consortium's LatitudeLongitudeAltitude workgroup.

## In-memory storage

A geodesic coordinate has two main components: latitude (a.k.a. "y") and longitude (a.k.a. "x"). Both latitude and longitude are expressed in sexagesimal degrees and decimals, according to ISO standards, with the following constraints:
• Positive values for North and East hemispheres
• Negative values for South and West hemispheres
• Latitude values from -90.0 to +90.0
• Longitude values from -180 to +180.0
With these constraints, it can concluded that coordinates could be stored in a pair of 32-bit single precision data type, known as float in C# and C++ (see the addendum "Why use float?" at the end of this page). But, there are more decisions pending... latitude and longitude are usually expressed in degrees, minutes, and seconds. A minute is 1/60 of a degree, and a second is 1/3600. So, if latitude or longitude are stored in degrees unit, they will need to be converted from 1/60 and 1/3600 fractions, to entire values, and back again to degrees and decimal; this will cause a cumulative loss of precision.

After many years working with coordinates, I concluded the best unit to store coordinates is not degrees but "seconds of degree". This way, all coordinate elements (degrees, minutes, and seconds) will reside on the integral portion of values, with the exception of decimal of seconds, avoiding the annoying loss of precision. The first approach to implement a coordinate class would be then:

```class Coordinate
{
float latitude;
float longitude;
}
```
Here is an example on how to store a coordinate as explained above:
• Latitude: 05º 13' 01.3" S
• Longitude: 080º 37' 49.5" W
• In degrees and decimals: { -(5 + 13/60 + 1/3600), -(80 + 37/60 + 49/3600) } equals { -5.2169..., -80.6303... }
• Optimized storage: { -(5*3600 + 13*60 + 1.3), -(80*3600 + 37*60 + 49.5) } equals { -18781.3, -290269.5 }

## XML serialization

The .NET Framework provides facilities to easily store and retrieve data in XML format; by associating an XML serializer object with the class above, the result will be like:

```<Coordinate>
<Latitude>-18781.3</Latitude>
<Longitude>-290269.5</Longitude>
</Coordinate>
```
This is barely 90 - 100 bytes per coordinate, including spacing characters. Just figure out a 100,000 node geographic file. This will require nearly 10 MB of file storage! Here is where ISO comes to the rescue. Reading the ISO 6709 explanation, the proper representation for the previous sample data would be, among others:
• -05.2169-080.6303/ expressed in pure degrees, or
• -051301.3-0803749.5/ expressed in degrees, minutes, and seconds

So, overriding the default serializer for our Coordinate class to use the second version, a result like this shall be expected:

```<Coordinate>-051301.3-0803749.5/</Coordinate>
```
At first glance, it is noticeable a storage saving of more than 50%. Moreover, besides the fact that the class stores data in-memory conveniently in "seconds of degree", XML storage is expressed in degrees, minutes, and seconds, so the data can interpreted in the XML file by direct reading, without conversion formulas: the best of both worlds.

## Coordinate class implementation

The first part of the Coordinate class implementation has the following fields and properties declarations:

```[Serializable()]
public class Coordinate : ICloneable, IXmlSerializable, IFormattable
{
#region Private fields
// Expressed in seconds of degree, positive values for north
private float latitude;
// Expressed in seconds of degree, positive values for east
private float longitude;

#endregion

#region Constructors
public Coordinate()
{
Latitude = Longitude = 0.0f;
}
public Coordinate(float lat, float lon)
{
Latitude = lat;
Longitude = lon;
}
#endregion

#region Properties
public float Latitude
{
set
{
latitude = value * 3600.0f;
}
get
{
return latitude / 3600.0f;  // return degrees
}
}
public float Longitude
{
set
{
longitude = value * 3600.0f;
}
get
{
return longitude / 3600.0f;  // return degrees
}
}
#endregion
```
Notice how latitude and longitude fields have been hidden from external usage and serialization. There are a couple of properties instead, Latitude and Longitude (with lead uppercase character); they can be accessed by the user, and return values as degrees and decimals, hiding our underlying storage format (seconds of degrees). Also, two constructors have been implemented: a default constructor, which sets lat/lon as zeroes, and a custom constructor which receive lat/lon as degrees and decimal with proper sign.

Following with code, there are public methods, to either set and get values in a friendly way:

```#region Public methods
// Multi-argument setters
public void SetD(float latDeg, float lonDeg) {...}
public void SetDM(float latDeg, float latMin, bool north, float lonDeg, float lonMin,
bool east) {...}
public void SetDMS(float latDeg, float latMin, float latSec, bool north, float lonDeg,
float lonMin, float lonSec, bool east) {...}

// Multi-argument getters
public void GetD(out float latDeg, out float lonDeg) {...}
public void GetDM(out float latDeg, out float latMin, out bool north, out float lonDeg,
out float lonMin, out bool east) {...}
public void GetDMS(out float latDeg, out float latMin, out float latSec, out bool north,
out float lonDeg, out float lonMin, out float lonSec, out bool east) {...}

// Distance in meters
public float Distance(Coordinate other) {...}

// Parsing method
public void ParseIsoString(string isoStr) {...}
#endregion
```
The first version of the setter, SetD, just needs two arguments expressed in degrees and decimal with proper sign for the hemisphere. It is equivalent to setting the Latitude and Longitude properties independently. The following two overrides will need minutes and hemisphere explicitly, and seconds optionally. In this case, the degrees arguments should not contain the hemisphere sign. The getter has a one-by one correspondence with the setters.

Finally, there is a method to calculate the distance against other coordinate. It is implemented using the classical Haversine formula (Web references inside the source code). The result is expressed in meters, the ISO unit for distances. More methods can be added for other calculations.

## String formatting

The following methods in the code implement some fundamental overrides:

```#region Overrides
public override string ToString() {...}
public override bool Equals(object obj) {...}
public override int GetHashCode() {...}
#endregion
```
The default implementation of Coordinate.ToString() will return a string with the coordinate expressed in degrees, minutes, and seconds. There will be more implementations of this method. Equals() compares both the latitude and longitude values for equality, and GetHashCode() returns a hash value based on the lat/lon values. The following code implements the IFormattable interface, allowing to display coordinates in different fashions:

```#region IFormattable Members
// Not really IFormattable member
public string ToString(string format) {...}
// ToString version with formatting
public string ToString(string format, IFormatProvider formatProvider) {...}
#endregion
```
As the IFormattable.ToString() method accepts a second argument that is unused in this implementation (formatProvider), an abbreviated override has been added with just an argument: the format string. Valid format strings are the following, with a little example:
• "D": 05.2169ºS 080.6303ºW
• "DM": 05º13.02'S 080º37.82'W
• "DMS": 05º13'01.3"S 080º37'49.5"W (default)
• "ISO": -051301.3-0803749.5/

Any other formatting string will produce an exception. Calling any version with an empty or null formatting string will output the default version. One of the best features of the IFormattable interface is that formatting string can be embedded inside a more complex formatting case; for example:

```string s = string.Format("Our sample coordinate is: {0:DM}\r\n.", someCoord);
```
Finally, there is the IXmlSerializable implementation. It overrides the default XML formatting, as explained earlier. Here is the abbreviated source code:

```#region IXmlSerializable Members
System.Xml.Schema.XmlSchema IXmlSerializable.GetSchema() {...}
void IXmlSerializable.WriteXml(XmlWriter writer) {...}
#endregion
```
It is not required to invoke these functions directly, they will be invoked by any serialization function, as depicted in the test source code below. WriteXml() will produce XML code for storing the latitude and longitude in ISO format with the pattern: "±DDMMSS.S±DDDMMSS.S/". ReadXml(), countersense, will parse a coordinate stored with any valid ISO format, even those including the height or depth, not considered for this implementation. An incorrect format will produce an exception.

## Coordinate collections

Once using the Coordinate class, it will be noticeable another source of space wasting; consider the following example:

```<CoordinateList>
<Coordinate>+010000.0+0010000.0/</Coordinate>
<Coordinate>-022400.0+0013000.0/</Coordinate>
<Coordinate>-034200.0+0021200.0/</Coordinate>
<Coordinate>-020000.0-0003000.0/</Coordinate>
</CoordinateList>
```
A real-world GIS application will need hundreds of <Coordinate> entries for each polygon. So, the ISO standard comes again to the rescue by storing the data in a more compact fashion:

```<CoordinateList>+010000.0+0010000.0/-022400.0+0013000.0/-034200.0+0021200.0/-020000.0-0003000.0/</CoordinateList>
```
Once again, storage saving of more than 50% is achieved. Implementing the CoordinateList class is quiet easy by deriving a generic List<Coordinate> collection and override the serialization methods from the IXmlSerializable interface. Here is the template:

```[Serializable()]
public class CoordinateList : List<Coordinate>, IXmlSerializable
{
#region Constructors
public CoordinateList() {...}
#endregion

#region Overrides
public override string ToString() {...}
#endregion

#region Public methods
public void ParseIsoString(string isoStr) {...}
#endregion

#region System.Xml.Schema.IXmlSerializable Members
XmlSchema IXmlSerializable.GetSchema() {...}
void IXmlSerializable.WriteXml(XmlWriter writer) {...}
#endregion
}
```

## Database storage/retrieving

A single coordinate or a coordinate collection can be stored in any text field type into a database. For storage, the query string can be prepared using the proper ToString() method, as in the following example:

```// Single point example
string query = string.Format("INSERT INTO PointMarks SET ID={0}, LOCATION='{1:ISO}'", someID, coord);

// Coordinate list example
string query = string.Format("INSERT INTO Boundaries SET ID={0}, POLYGON='{1}'", someID, coord_list);
```
Notice that when inserting a single coordinate, the "ISO" formatting should be used, since the Coordinate.ToString() method has many formatting options, as explained earlier.

To retrieve a single coordinate or coordinate collection, just create a new object and invoke the ParseIsoString() method, passing the string retrieved from the database, like in the following example. A try/catch block also will be recommendable, to avoid an unexpected exception.

```// Single point example
Coordinate coord = new Coordinate();
coord.ParseIsoString(iso);

// Coordinate list example
CoordinateList coord_list = new CoordinateList();
coord_list.ParseIsoString(iso);
```

## The sample code

The supplied sample code in the Program.Main() method will do some little tasks to demonstrate the Coordinate and CoordinateList classes functionality.
```static void Main()
{
// Create a new coordinate with some values
Coordinate c = new Coordinate();
c.SetDMS(00, 10, 20.8f, true, 30, 40, 50.9f, false);

// Create a new coordinate list with
CoordinateList cl = new CoordinateList();

// Show formatting capabilities
MessageBox.Show(string.Format(
"String Formatting:\r\nD: {0:D}\r\nDM: {0:DM}\r\nDMS: {0:DMS}\r\nISO: {0:ISO}", c), "Formatting example");

// Serialize coordinates to a string object
System.Text.StringBuilder sb = new System.Text.StringBuilder();
System.IO.StringWriter sw = new System.IO.StringWriter(sb);
string part1, part2;

// Single coordinate
System.Xml.Serialization.XmlSerializer xs = new System.Xml.Serialization.XmlSerializer(typeof(Coordinate));
xs.Serialize(sw, c);
sw.Flush();
part1 = sb.ToString();

// Coordinate list
sb.Length = 0;
System.Xml.Serialization.XmlSerializer xsl = new System.Xml.Serialization.XmlSerializer(typeof(CoordinateList));
xsl.Serialize(sw, cl);
sw.Flush();
part2 = sb.ToString();
sw.Close();

// Show serialized data
MessageBox.Show(string.Format("Single coordinate:\r\n{0}\r\n\r\nCoordinate list:\r\n{1}", part1, part2),
"Serialization example");

// Deserialize single coordinate from a string object
Coordinate c1 = (Coordinate)xs.Deserialize(sr);
sr.Close();

// Deserialize coordinate list from a string object
CoordinateList cl1 = (CoordinateList)xsl.Deserialize(sr);
sr.Close();

// Show properties of deserialized data
string message = string.Format(
"Single coordinate:\r\n{0}\r\nLatitude: {1}°\r\nLongitude: {2}°\r\n\r\nCoordinate list:\r\n",
c1, c1.Latitude, c1.Longitude);
foreach (Coordinate coord in cl1)
message += coord.ToString() + "\r\n";

MessageBox.Show(message, "Deserialization example");
}
```

## Why use float?

There are many opinions and questions about about why to use the float data type, instead of the Int32, double, or decimal data types. Here are some reasons about the use of float against Int32 (expressed in milliseconds of degree or other scales):
• There is no meaningful advantage in storing size with an Int32, since float is 32-bits too.
• When calculating coordinates components (D/M/S), convertion to floating point is needed anyway.
• When painting, it has to be converted to floating point.
• Also, float conversion will be needed to store in ISO 6709 format.
• If expressed in milliseconds, the code will be less legible due to continuous multiplication/division by 1000.

About float against decimal, which is four times larger than float (128 bits), the main disadvantage, besides the obvious space consumption, is performance. Calculations with decimal are 20-30 times slower than with float. This is due to the fact that decimal is stored as base-10 by microprocessor, not as base-2 like all other data types, forcing it to do several convertions to perform any math calculation.

About float against double, there are concerns about precision that deserves a bigger explanation. Let's mention first the advantages:
• The double data type (64 bits) needs twice the space as float (32 bits).
• Half the space and half the time to load in memory.
• Precision is enough, when compared to GPS precision (5-10 meters).

About precision, it is important to be aware of the internals of the float data type, as explained in Wikipedia http://en.wikipedia.org/wiki/IEEE_754. It reserves 8 bits for the exponent and 23 bits for the mantissa. This means that a float value can contain 223 different values, besides the exponent. Now, let's go to the most pessimistic calculation:

A longitude with a maximum value of 180 degrees at the Equator that has 60 nautical miles per minute, when stored in a float variable, will have a precision of:

```180 * 60 * 1,852 meters / 2^23 values = 20,001,600 / 8,388,608 = about 2.4 meters
```

But, the longitude magnitude will vary according to the latitude, with the equation decreasing up to zero at the poles. The precision will increase when the latitude is greater, according to the following chart:

The case of latitude is different. It doesn't vary in any zone of the earth, with a constant magnitude of 60 nautical miles per minute of degree, and a maximum value of 90 in the poles. The most pessimistic calculation for the latitude (near to the poles) will be:

```90 * 60 * 1,852 meters / 2^23 values = 10,000,800 / 8,388,608 = about 1.2 meters
```

The following chart depicts the precision of latitude stored in a float variable, according to the latitude value:

So, it can be concluded that in practical terms, the average precision value will be lower than 1 meter. This is far more precise than a GPS output, and suitable for most GIS applications up to street level.

## Class enhancements

There are a lot of features that can be added to this fundamental class, the following ones can be included in the future releases:
• Altitude/depth element
• NMEA sentences parsing
• UTM system translation
• More geodesic calculations

## Disclaimer

The code/library supplied is totally free for either personal or commercial use, with no warranties. Please support the sustaining and improvement of it with a little donation (absolutely voluntarily).

Last edited Jul 30, 2009 at 2:14 PM by jaime_olivares, version 10