/// <summary> /// Returns the distance between two points... /// </summary> /// <param name="currentPosition"></param> /// <param name="currentDestination"></param> /// <returns></returns> public static double GetDistance(UtmCoordinate currentPosition, UtmCoordinate currentDestination) { double dEast, dNorth; //Check to see if within the same UTM zone if (currentPosition.Zone == currentDestination.Zone) { dEast = currentPosition.Easting - currentDestination.Easting; dNorth = currentPosition.Northing - currentDestination.Northing; return(Math.Sqrt(dEast * dEast + dNorth * dNorth)); } else { //Convert to UTM back to latitude and longitude LatLongCoordinate CurrentPositionLatLong = UtmToLL(RefEllipse, currentPosition); LatLongCoordinate DestinationPositionLatLong = UtmToLL(RefEllipse, currentDestination); //Work out distance double Distance = DistanceBetweenLatLongPoints(RefEllipse, CurrentPositionLatLong, DestinationPositionLatLong); //and return the result return(Distance); } }
internal static double DistanceBetweenLatLongPointsHaversine(int referenceEllipsoid, LatLongCoordinate origin, LatLongCoordinate destination) { //Convert the latitude long decimal degrees to radians and apply the formula //use the proper ellipsoid to get raidus of the earth double EquatorialRadius = EllipseCollection[referenceEllipsoid].EquatorialRadius; double OriginLatAsRadians = origin.Latitude * (Math.PI / 180.0); double OriginLongAsRadians = origin.Longitude * (Math.PI / 180.0); double DestinationLatAsRadians = destination.Latitude * (Math.PI / 180.0); double DestinationLongAsRadians = destination.Longitude * (Math.PI / 180.0); double ChangeLat = DestinationLatAsRadians - OriginLatAsRadians; double ChangeLong = DestinationLongAsRadians - OriginLongAsRadians; double a = Math.Pow(Math.Sin(ChangeLat / 2.0), 2.0) + Math.Cos(OriginLatAsRadians) * Math.Cos(DestinationLatAsRadians) * Math.Pow(Math.Sin(ChangeLong / 2.0), 2.0); double c = 2.0 * Math.Atan2(Math.Sqrt(a), Math.Sqrt((1 - a))); double Distance = EquatorialRadius * c; return Distance; }
public static LatLongCoordinate UtmToLL(int referenceEllipsoid, double utmNorthing, double utmEasting, int zone, char zoneIdentifier) { //converts Utm coords to latitude/long. Equations from USGS Bulletin 1532 //East Longitudes are positive, West longitudes are negative. //North latitudes are positive, South latitudes are negative //latitude and longitude are in decimal degrees. //Written by Chuck Gantz- [email protected] LatLongCoordinate CoordinateSet = new LatLongCoordinate(); double Lat, Long; double k0 = 0.9996; double a = EllipseCollection[referenceEllipsoid].EquatorialRadius; double eccSquared = EllipseCollection[referenceEllipsoid].EccentricitySquared; double eccPrimeSquared; double e1 = (1-Math.Sqrt(1-eccSquared))/(1+Math.Sqrt(1-eccSquared)); double N1, T1, C1, R1, D, M; double LongOrigin; double mu, /*phi1,*/ phi1Rad; double x, y; int ZoneNumber; //int NorthernHemisphere; //1 for northern hemispher, 0 for southern x = utmEasting - 500000.0; //remove 500,000 meter offset for longitude y = utmNorthing; ZoneNumber = zone; if ((zoneIdentifier - 'N') >= 0) { //NorthernHemisphere = 1;//point is in northern hemisphere } else { // NorthernHemisphere = 0;//point is in southern hemisphere y -= 10000000.0;//remove 10,000,000 meter offset used for southern hemisphere } LongOrigin = (ZoneNumber - 1)*6 - 180 + 3; //+3 puts origin in middle of zone eccPrimeSquared = (eccSquared)/(1-eccSquared); M = y / k0; mu = M/(a*(1-eccSquared/4-3*eccSquared*eccSquared/64-5*eccSquared*eccSquared*eccSquared/256)); phi1Rad = mu + (3*e1/2-27*e1*e1*e1/32)*Math.Sin(2*mu) + (21*e1*e1/16-55*e1*e1*e1*e1/32)*Math.Sin(4*mu) +(151*e1*e1*e1/96)*Math.Sin(6*mu); //phi1 = phi1Rad*(180.0 / Math.PI); N1 = a/Math.Sqrt(1-eccSquared*Math.Sin(phi1Rad)*Math.Sin(phi1Rad)); T1 = Math.Tan(phi1Rad)*Math.Tan(phi1Rad); C1 = eccPrimeSquared*Math.Cos(phi1Rad)*Math.Cos(phi1Rad); R1 = a*(1-eccSquared)/Math.Pow(1-eccSquared*Math.Sin(phi1Rad)*Math.Sin(phi1Rad), 1.5); D = x/(N1*k0); Lat = phi1Rad - (N1*Math.Tan(phi1Rad)/R1)*(D*D/2-(5+3*T1+10*C1-4*C1*C1-9*eccPrimeSquared)*D*D*D*D/24 +(61+90*T1+298*C1+45*T1*T1-252*eccPrimeSquared-3*C1*C1)*D*D*D*D*D*D/720); Lat = Lat * (180.0 / Math.PI); Long = (D-(1+2*T1+C1)*D*D*D/6+(5-2*C1+28*T1-3*C1*C1+8*eccPrimeSquared+24*T1*T1) *D*D*D*D*D/120)/Math.Cos(phi1Rad); Long = LongOrigin + Long * (180.0 / Math.PI); CoordinateSet.Latitude = Lat; CoordinateSet.Longitude = Long; return CoordinateSet; }
/// <summary> /// returns the distance in meters between 2 lat/long double-precision points /// </summary> /// <param name="referenceEllipsoid"></param> /// <param name="origin"></param> /// <param name="destination"></param> /// <returns></returns> public static double DistanceBetweenLatLongPoints(int referenceEllipsoid, LatLongCoordinate origin, LatLongCoordinate destination) { return DistanceBetweenLatLongPoints(referenceEllipsoid, origin.Latitude, origin.Longitude, destination.Latitude, destination.Longitude); }
public static LatLongCoordinate UtmToLL(int referenceEllipsoid, double utmNorthing, double utmEasting, int zone, char zoneIdentifier) { //converts Utm coords to latitude/long. Equations from USGS Bulletin 1532 //East Longitudes are positive, West longitudes are negative. //North latitudes are positive, South latitudes are negative //latitude and longitude are in decimal degrees. //Written by Chuck Gantz- [email protected] LatLongCoordinate CoordinateSet = new LatLongCoordinate(); double Lat, Long; double k0 = 0.9996; double a = EllipseCollection[referenceEllipsoid].EquatorialRadius; double eccSquared = EllipseCollection[referenceEllipsoid].EccentricitySquared; double eccPrimeSquared; double e1 = (1 - Math.Sqrt(1 - eccSquared)) / (1 + Math.Sqrt(1 - eccSquared)); double N1, T1, C1, R1, D, M; double LongOrigin; double mu, /*phi1,*/ phi1Rad; double x, y; int ZoneNumber; //int NorthernHemisphere; //1 for northern hemispher, 0 for southern x = utmEasting - 500000.0; //remove 500,000 meter offset for longitude y = utmNorthing; ZoneNumber = zone; if ((zoneIdentifier - 'N') >= 0) { //NorthernHemisphere = 1;//point is in northern hemisphere } else { // NorthernHemisphere = 0;//point is in southern hemisphere y -= 10000000.0;//remove 10,000,000 meter offset used for southern hemisphere } LongOrigin = (ZoneNumber - 1) * 6 - 180 + 3; //+3 puts origin in middle of zone eccPrimeSquared = (eccSquared) / (1 - eccSquared); M = y / k0; mu = M / (a * (1 - eccSquared / 4 - 3 * eccSquared * eccSquared / 64 - 5 * eccSquared * eccSquared * eccSquared / 256)); phi1Rad = mu + (3 * e1 / 2 - 27 * e1 * e1 * e1 / 32) * Math.Sin(2 * mu) + (21 * e1 * e1 / 16 - 55 * e1 * e1 * e1 * e1 / 32) * Math.Sin(4 * mu) + (151 * e1 * e1 * e1 / 96) * Math.Sin(6 * mu); //phi1 = phi1Rad*(180.0 / Math.PI); N1 = a / Math.Sqrt(1 - eccSquared * Math.Sin(phi1Rad) * Math.Sin(phi1Rad)); T1 = Math.Tan(phi1Rad) * Math.Tan(phi1Rad); C1 = eccPrimeSquared * Math.Cos(phi1Rad) * Math.Cos(phi1Rad); R1 = a * (1 - eccSquared) / Math.Pow(1 - eccSquared * Math.Sin(phi1Rad) * Math.Sin(phi1Rad), 1.5); D = x / (N1 * k0); Lat = phi1Rad - (N1 * Math.Tan(phi1Rad) / R1) * (D * D / 2 - (5 + 3 * T1 + 10 * C1 - 4 * C1 * C1 - 9 * eccPrimeSquared) * D * D * D * D / 24 + (61 + 90 * T1 + 298 * C1 + 45 * T1 * T1 - 252 * eccPrimeSquared - 3 * C1 * C1) * D * D * D * D * D * D / 720); Lat = Lat * (180.0 / Math.PI); Long = (D - (1 + 2 * T1 + C1) * D * D * D / 6 + (5 - 2 * C1 + 28 * T1 - 3 * C1 * C1 + 8 * eccPrimeSquared + 24 * T1 * T1) * D * D * D * D * D / 120) / Math.Cos(phi1Rad); Long = LongOrigin + Long * (180.0 / Math.PI); CoordinateSet.Latitude = Lat; CoordinateSet.Longitude = Long; return(CoordinateSet); }
/// <summary> /// returns the distance in meters between 2 lat/long double-precision points /// </summary> /// <param name="referenceEllipsoid"></param> /// <param name="origin"></param> /// <param name="destination"></param> /// <returns></returns> public static double DistanceBetweenLatLongPoints(int referenceEllipsoid, LatLongCoordinate origin, LatLongCoordinate destination) { return(DistanceBetweenLatLongPoints(referenceEllipsoid, origin.Latitude, origin.Longitude, destination.Latitude, destination.Longitude)); }
/// <summary> /// Returns distance in meters between two lat/long coordinates using Haversine formula. More accurate but slower. /// </summary> /// <param name="referenceEllipsoid"></param> /// <param name="origin"></param> /// <param name="destination"></param> /// <returns></returns> public static double DistanceBetweenLatLongPointsHaversine(int referenceEllipsoid, LatLongCoordinate origin, LatLongCoordinate destination) { return(DistanceBetweenLatLongPointsHaversine(referenceEllipsoid, origin.Latitude, origin.Longitude, destination.Latitude, destination.Longitude)); ////Convert the latitude long decimal degrees to radians and apply the formula ////use the proper ellipsoid to get raidus of the earth //double EquatorialRadius = EllipseCollection[referenceEllipsoid].EquatorialRadius; //double OriginLatAsRadians = origin.Latitude * (Math.PI / 180.0); //double OriginLongAsRadians = origin.Longitude * (Math.PI / 180.0); //double DestinationLatAsRadians = destination.Latitude * (Math.PI / 180.0); //double DestinationLongAsRadians = destination.Longitude * (Math.PI / 180.0); //double ChangeLat = DestinationLatAsRadians - OriginLatAsRadians; //double ChangeLong = DestinationLongAsRadians - OriginLongAsRadians; //double a = Math.Pow(Math.Sin(ChangeLat / 2.0), 2.0) + // Math.Cos(OriginLatAsRadians) * Math.Cos(DestinationLatAsRadians) * // Math.Pow(Math.Sin(ChangeLong / 2.0), 2.0); //double c = 2.0 * Math.Atan2(Math.Sqrt(a), Math.Sqrt((1 - a))); //double Distance = EquatorialRadius * c; //return Distance; }