100 lines
5.0 KiB
C#
100 lines
5.0 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
|
|
namespace GODATA.Models.Calcuation
|
|
{
|
|
class DistanceCalc
|
|
{
|
|
/// <summary>
|
|
/// take lat/long in double datatype and return decimal list of x,y,z, coordinates respectively
|
|
/// </summary>
|
|
/// <param name="latitude"></param>
|
|
/// <param name="longitude"></param>
|
|
/// <returns></returns>
|
|
public Dictionary<string, double> GetCoordinates(double latitude, double longitude)
|
|
{
|
|
//Dictionary<string, decimal> Coordinates= new Dictionary<string,decimal>();
|
|
Dictionary<string, double> Coordinates = new Dictionary<string, double>();
|
|
//decimal XCoordinate,YCoordinate,ZCoordinate;
|
|
//XCoordinate = Convert.ToDecimal(6371000 * Convert.ToDouble((Math.Cos(latitude)) * (Math.Cos(longitude))));
|
|
//YCoordinate = Convert.ToDecimal(6371000 * Convert.ToDouble((Math.Cos(latitude)) * (Math.Sin(longitude))));
|
|
//ZCoordinate = Convert.ToDecimal(6371000 * Convert.ToDouble((Math.Sin(latitude))));
|
|
latitude = Math.PI * latitude / 180;
|
|
longitude = Math.PI * longitude / 180;
|
|
double xCoordinate = 6371000d * Convert.ToDouble((Math.Cos(latitude)) * (Math.Cos(longitude)));
|
|
double yCoordinate = 6371000d * Convert.ToDouble((Math.Cos(latitude)) * (Math.Sin(longitude)));
|
|
double zCoordinate = 6371000d * Convert.ToDouble((Math.Sin(latitude)));
|
|
|
|
Coordinates.Add("XCoordinate", xCoordinate);
|
|
Coordinates.Add("YCoordinate", yCoordinate);
|
|
Coordinates.Add("ZCoordinate", zCoordinate);
|
|
return Coordinates;
|
|
}
|
|
|
|
/// <summary>
|
|
/// This method simply return unit distance of the vehicle's current position from geofence boundry
|
|
///
|
|
/// </summary>
|
|
/// <param name="IXCoordinate">I point X coordinate </param>
|
|
/// <param name="IYCoordinate">I point Y coordinate</param>
|
|
/// <param name="JXCoordinate">J point X coordinate</param>
|
|
/// <param name="JYCoordinate">J point Y coordinate</param>
|
|
/// <param name="XCoordinate">Current position X coordinate</param>
|
|
/// <param name="YCoordinate">Current position Y coordinate</param>
|
|
/// <returns></returns>
|
|
public decimal CalcDistance(double IXCoordinate, double IYCoordinate, double JXCoordinate, double JYCoordinate, double XCoordinate, double YCoordinate)
|
|
{
|
|
try
|
|
{
|
|
double _constantA, _constantB;
|
|
double _slopA, _slopB;
|
|
double _tempexprA, _tempexprB;
|
|
double _intersectX, _intersectY;
|
|
_slopA = (JYCoordinate - IYCoordinate) / (JXCoordinate - IXCoordinate);
|
|
_constantA = IYCoordinate - (_slopA * IXCoordinate);
|
|
_slopB = -(1 / _slopA);
|
|
_constantB = YCoordinate - (_slopB * XCoordinate);
|
|
_tempexprA = (_slopA) - (_slopB);
|
|
_tempexprB = _constantA - (_constantB);
|
|
if (_tempexprB == 0)
|
|
{
|
|
_intersectX = 0;
|
|
}
|
|
else
|
|
{
|
|
_intersectX = -(_tempexprA / _tempexprB);
|
|
}
|
|
_intersectY = (_slopB * _intersectX) + (_constantB);
|
|
return Convert.ToDecimal(Math.Sqrt(Math.Pow(Convert.ToDouble(_intersectX - XCoordinate), 2) + Math.Pow(Convert.ToDouble(_intersectY - YCoordinate), 2)));
|
|
}
|
|
catch (Exception ex) { throw new Exception(ex.Message); }
|
|
}
|
|
|
|
public double DistanceInKM(double[] latitude, double[] longitude)
|
|
{
|
|
try
|
|
{
|
|
double distance = 0.0F;
|
|
double lat1, lng1, lat2, lng2;
|
|
double Radius = 6371;
|
|
double LatDistance, LngDistance;
|
|
for (int i = 0; i < latitude.Length - 1; i++)
|
|
{
|
|
lat1 = latitude[i] * Math.PI / 180;
|
|
lat2 = latitude[i + 1] * Math.PI / 180;
|
|
lng1 = longitude[i] * Math.PI / 180;
|
|
lng2 = longitude[i + 1] * Math.PI / 180;
|
|
LatDistance = lat2 - lat1;
|
|
LngDistance = lng2 - lng1;
|
|
var a = Math.Sin(LatDistance / 2) * Math.Sin(LatDistance / 2) + Math.Cos(lat1) * Math.Cos(lat2) * Math.Sin(LngDistance / 2) * Math.Sin(LngDistance / 2);
|
|
var c = 2 * Math.Atan2(Math.Sqrt(a), Math.Sqrt(1 - a));
|
|
distance = distance + Radius * c;
|
|
}
|
|
return distance;
|
|
}
|
|
catch (Exception ex) { throw new Exception(ex.Message); }
|
|
}
|
|
|
|
}
|
|
}
|