[mapguide-users] How to calculate distance

Scott McFarlane scott at geotropix.com
Tue Jun 19 13:34:05 EDT 2007


Here is a class written in VB.NET that accomplishes this by creating a
temporary UTM coordinate system based on the "from" point, converting both
points into that coordinate system, then doing a simple Pythagorean distance
calculation. This should work for points that are within 6 degrees of
longitude.

---------------------------------
Imports OSGeo.MapGuide

Public Class DistanceCalculator

	Private Const LL84 As String =
"GEOGCS[""LL84"",DATUM[""WGS84"",SPHEROID[""WGS84"",6378137.000,298.25722293
]],PRIMEM[""Greenwich"",0],UNIT[""Degree"",0.01745329251994]]"
	Private Const UTM_WKT_TEMPLATE As String =
"PROJCS[""UTM83-CUSTOM"",GEOGCS[""LL83"",DATUM[""NAD83"",SPHEROID[""GRS1980"
",6378137.000,298.25722210]],PRIMEM[""Greenwich"",0],UNIT[""Degree"",0.01745
3292519943295]],PROJECTION[""Transverse_Mercator""],PARAMETER[""false_eastin
g"",1640416.667],PARAMETER[""false_northing"",0.000],PARAMETER[""central_mer
idian"",{LON}],PARAMETER[""scale_factor"",0.9996],PARAMETER[""latitude_of_or
igin"",0.000],UNIT[""Foot_US"",0.30480060960122]]"
	Private _SourceX As Double
	Private _SourceY As Double
	Private _Transformer As MgCoordinateSystemTransform

	Public Sub New(ByVal SourceLat As Double, ByVal SourceLon As Double)
		With New MgCoordinateSystemFactory
			Dim CsSource As MgCoordinateSystem = .Create(LL84)
			Dim CsTarget As MgCoordinateSystem =
.Create(Replace(UTM_WKT_TEMPLATE, "{LON}", SourceLon.ToString()))
			_Transformer = New
MgCoordinateSystemTransform(CsSource, CsTarget)
			With _Transformer.Transform(SourceLon, SourceLat)
				_SourceX = .X
				_SourceY = .Y
			End With
		End With
	End Sub

	Public Function DistanceTo(ByVal Lat As Double, ByVal Lon As Double)
As Double
		With _Transformer.Transform(Lon, Lat)
			Dim dx As Double = Math.Abs(.X - _SourceX)
			Dim dy As Double = Math.Abs(.Y - _SourceY)
			' Return distance in miles
			Return Math.Sqrt((dx * dx) + (dy * dy)) / 5280.0
		End With
	End Function

End Class
----------------------------------

Hope this helps!

Scott

-----Original Message-----
From: mapguide-users-bounces at lists.osgeo.org
[mailto:mapguide-users-bounces at lists.osgeo.org] On Behalf Of Deepak Kumar
Sent: Tuesday, June 19, 2007 1:03 AM
To: mapguide-users at lists.osgeo.org
Subject: [mapguide-users] How to calculate distance

Hi,

How to calcualte distance between two points lat1, long1 and lat2, lon2.

Regards
Deepak Kumar
http://www.roseindia.net

_______________________________________________
mapguide-users mailing list
mapguide-users at lists.osgeo.org
http://lists.osgeo.org/mailman/listinfo/mapguide-users




More information about the mapguide-users mailing list