January 2008

SharpMap Distance Calutaions

For most of January I have been working with an open source GIS mapping component called SharpMap. It's a cool tool, and very powerful, but being open source it - to some extent, -suffers from a lack of clear examples, particularly for programmers new to this type of programing (geospatial) .

Map.jpg

Today I wanted to calculate the distance between to points on the earths surface. This is quite a difficult thing to do mathematically, but luckily for me in 1975 this fellow came up with a good answer. Even better is that this fine fellow wrote it up in a C# Class - so that saved me a few hours today ;-).

Anyway i thought it might be useful to show how I have put this into practice using VB.net. Enjoy!

Go to Mike Gavaghans website and down load his C# code. unzip it and copy the file called "Gavaghan.Geodesy.dll" from the "Dist" folder to your base folder for your solution. Then add a reference to it in your IDE. The following function takes 4 arguments and returns a distance between them in meters.

VBA:
  1. Imports System
  2. Imports Gavaghan.Geodesy
  3.  
  4.     Public Class VincentyGeodeticProblems
  5.  
  6.     '//Answers is in meters!
  7.     Shared Function Distance _
  8.         (ByVal Loc1_X As Double, ByVal Loc1_Y As Double, _
  9.     byval Loc2_X as Double, byval Loc2_Y as Double) _
  10.         as Double
  11.        
  12.        
  13.             ' instantiate the calculator
  14.             Dim geoCalc As New GeodeticCalculator()
  15.  
  16.             ' select a reference elllipsoid
  17.             Dim reference As Ellipsoid = Ellipsoid.WGS84
  18.  
  19.             ' set loc 1 coordinates
  20.             Dim Loc1 As GlobalCoordinates
  21.             Loc1 = New GlobalCoordinates(Loc1_X, Loc1_Y)
  22.  
  23.             ' set loc 2 coordinates
  24.             Dim Loc2 As GlobalCoordinates
  25.             Loc2 = New GlobalCoordinates(Loc2_X, Loc2_Y)
  26.  
  27.             ' calculate the geodetic curve
  28.             Dim geoCurve As GeodeticCurve = _
  29.             geoCalc.CalculateGeodeticCurve(reference, Loc1, Loc2)
  30.  
  31.                      return geoCurve.EllipsoidalDistance
  32.            
  33.         End function
  34.  
  35.     End Class

Then you can do all sorts of useful stuff!

Resolver Systems release Resolver v1

I've not really had a popper play around with this yet, despite begin in the beat program for quite a while. Now they have released the first full version. The good news is the non-commercial version is completely free! And to be fair the commercial version is only 50 quid!

Why not take a little look?