using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace WebSamples.Utilities.Geo
{
    public class GeoConversionHelper
    {
        #region Constants

        //-- Linear projection constants

        // Latitude constants
        const double c_LatLengthAverage = 111132.92;
        const double c_LatConstant2 = -559.82;
        const double c_LatConstant3 = 1.175;
        const double c_LatConstant4 = -0.0023;

        // Longitude constants
        const double c_LngLengthAverage = 111412.84;
        const double c_LngConstant2 = -93.5;
        const double c_LngConstant3 = 0.118;

        #endregion

        #region Utilities methods

        public double Deg2rad(double val)
        {
            return (val * ((2.0 * Math.PI) / 360));
        }

        public double GetLatLengthFromLatitude(double latitude)
        {
            double latLength = 0;

            latitude = Deg2rad(latitude);

            latLength = c_LatLengthAverage + (c_LatConstant2 * Math.Cos(2 * latitude))
                                         + (c_LatConstant3 * Math.Cos(4 * latitude))
                                         + (c_LatConstant4 * Math.Cos(6 * latitude));

            return latLength;
        }

        public double GetLngLengthFromLatitude(double latitude)
        {
            double lngLength = 0;

            latitude = Deg2rad(latitude);

            lngLength = (c_LngLengthAverage * Math.Cos(latitude))
                      + (c_LngConstant2 * Math.Cos(3 * latitude))
                      + (c_LngConstant3 * Math.Cos(5 * latitude));

            return lngLength;
        }

        #endregion
    }
}
