SharpMap

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]
Imports System
Imports Gavaghan.Geodesy

Public Class VincentyGeodeticProblems

‘//Answers is in meters!
Shared Function Distance _
(ByVal Loc1_X As Double, ByVal Loc1_Y As Double, _
byval Loc2_X as Double, byval Loc2_Y as Double) _
as Double

‘ instantiate the calculator
Dim geoCalc As New GeodeticCalculator()

‘ select a reference elllipsoid
Dim reference As Ellipsoid = Ellipsoid.WGS84

‘ set loc 1 coordinates
Dim Loc1 As GlobalCoordinates
Loc1 = New GlobalCoordinates(Loc1_X, Loc1_Y)

‘ set loc 2 coordinates
Dim Loc2 As GlobalCoordinates
Loc2 = New GlobalCoordinates(Loc2_X, Loc2_Y)

‘ calculate the geodetic curve
Dim geoCurve As GeodeticCurve = _
geoCalc.CalculateGeodeticCurve(reference, Loc1, Loc2)

return geoCurve.EllipsoidalDistance

End function

End Class
[/VBA]

Then you can do all sorts of useful stuff!