Archive for the 'C#' Category

January
16th 2010
A Geographic Distance Class in C#

Posted under C# & GIS

The .NET Framework is pretty feature rich, helping developers put applications together quickly. A glaring omission, however, is support for anything to do with geography. I recently had a need to deal with distances between points in GPS files, calculated using the haversine formula, and came up with an elegant solution.

I modeled my class, Distance, on the TimeSpan struct, with a few important changes. This should make the programming model as familiar as possible. Just as a TimeSpan converts the same value ( say, 36 hours ) between different formats ( 1.5 days; 2,160 minutes, etc ), a Distance object converts between formats like feet, meters, and miles.

If this isn’t clear, a short code sample should help. The full code can be found at Distance.cs.

Distance d = new Distance();

d.Miles = 1;
Debug.Assert(d.Yards == 1760);
Debug.Assert(d.Feet == 5280);
Debug.Assert(d.Inches == 63360);

Continue Reading »

No Comments »