Advertisement
Guest User

Untitled

a guest
Apr 24th, 2018
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.42 KB | None | 0 0
  1.     public class LambertConformalProjection
  2.     {
  3.         private double n;
  4.         private double F;
  5.         private double rho;
  6.         private double earthRadiusTimesF;
  7.         private double lon0Degrees;
  8.         private double lat0;
  9.         private double lon0;
  10.         private double par1;
  11.         private double par2;
  12.         private double falseEasting;
  13.         private double falseNorthing;
  14.  
  15.         public LambertConformalProjection(double centerPtLat, double standardLon, double trueLat1, double trueLat2)
  16.             : this(centerPtLat, standardLon, trueLat1, trueLat2, 0.0D, 0.0D)
  17.         {
  18.  
  19.         }
  20.  
  21.         public LambertConformalProjection(double centerPtLat, double standardLon, double trueLat1, double trueLat2, double falseEast, double falseNorth)
  22.         {
  23.             this.lat0 = this.ToRadians(centerPtLat);
  24.             this.lon0 = this.ToRadians(standardLon);
  25.             this.par1 = trueLat1;
  26.             this.par2 = trueLat2;
  27.             double d = 1.0D;
  28.             if (Double.IsNaN(falseEast))
  29.             {
  30.                 falseEast = 0.0D;
  31.             }
  32.  
  33.             if (Double.IsNaN(falseNorth))
  34.             {
  35.                 falseNorth = 0.0D;
  36.             }
  37.  
  38.             int i = (falseEast != 0.0D) || (falseNorth != 0.0D) ? 1 : 0;
  39.             if (i != 0)
  40.             {
  41.                 d = 1.0D;
  42.             }
  43.  
  44.             this.falseEasting = (d * falseEast);
  45.             this.falseNorthing = (d * falseNorth);
  46.             this.Precalculate();
  47.         }
  48.  
  49.         private double ToRadians(double degrees)
  50.         {
  51.             return degrees * Math.PI / 180.0;
  52.         }
  53.  
  54.         private double ToDegrees(double radians)
  55.         {
  56.             return radians * 180.0 / Math.PI;
  57.         }
  58.  
  59.         private void Precalculate()
  60.         {
  61.             if (Math.Abs(this.lat0 - 1.570796326794897D) < 1.0E-006D)
  62.             {
  63.                 throw new ArgumentException("LambertConformal lat0 = 90");
  64.             }
  65.  
  66.             if (Math.Abs(this.lat0 + 1.570796326794897D) < 1.0E-006D)
  67.             {
  68.                 throw new ArgumentException("LambertConformal lat0 = -90");
  69.             }
  70.  
  71.             if (Math.Abs(this.par1 - 90.0D) < 1.0E-006D)
  72.             {
  73.                 throw new ArgumentException("LambertConformal par1 = 90");
  74.             }
  75.  
  76.             if (Math.Abs(this.par1 + 90.0D) < 1.0E-006D)
  77.             {
  78.                 throw new ArgumentException("LambertConformal par1 = -90");
  79.             }
  80.  
  81.             if (Math.Abs(this.par2 - 90.0D) < 1.0E-006D)
  82.             {
  83.                 throw new ArgumentException("LambertConformal par2 = 90");
  84.             }
  85.  
  86.             if (Math.Abs(this.par2 + 90.0D) < 1.0E-006D)
  87.             {
  88.                 throw new ArgumentException("LambertConformal par2 = -90");
  89.             }
  90.  
  91.             double d1 = this.ToRadians(this.par1);
  92.             double d2 = this.ToRadians(this.par2);
  93.             double d3 = Math.Tan(0.7853981633974483D + d1 / 2.0D);
  94.             double d4 = Math.Tan(0.7853981633974483D + d2 / 2.0D);
  95.  
  96.             if (Math.Abs(this.par2 - this.par1) < 1.0E-006D)
  97.             {
  98.                 this.n = Math.Sin(d1);
  99.             }
  100.             else
  101.             {
  102.                 this.n = (Math.Log(Math.Cos(d1) / Math.Cos(d2)) / Math.Log(d4 / d3));
  103.             }
  104.  
  105.             double d5 = Math.Pow(d3, this.n);
  106.             this.F = (Math.Cos(d1) * d5 / this.n);
  107.             this.earthRadiusTimesF = (6371.2290000000003D * this.F) * 1000;
  108.             double d6 = Math.Pow(Math.Tan(0.7853981633974483D + this.lat0 / 2.0D), this.n);
  109.             this.rho = (6371.2290000000003D * 1000 * this.F / d6);
  110.             this.lon0Degrees = this.ToDegrees(this.lon0);
  111.         }
  112.  
  113.         public void LatLonToProj(double[] inLonLatOutXY)
  114.         {
  115.             var xy = this.LatLonToProj(new CoordinateLatLon() { Lon = inLonLatOutXY[0], Lat = inLonLatOutXY[1] });
  116.             inLonLatOutXY[0] = xy.X;
  117.             inLonLatOutXY[1] = xy.Y;
  118.         }
  119.  
  120.         public CoordinateXY LatLonToProj(double paramDouble1, double paramDouble2, CoordinateXY paramCoordinateXY)
  121.         {
  122.             double d3 = paramDouble1;
  123.             double d4 = paramDouble2;
  124.             d3 = this.ToRadians(d3);
  125.             double d5 = CoordinateLatLon.LonNormal(d4 - this.lon0Degrees);
  126.             double d6 = this.n * this.ToRadians(d5);
  127.             double d7 = Math.Pow(Math.Tan(0.7853981633974483D + d3 / 2.0D), this.n);
  128.             double d8 = this.earthRadiusTimesF / d7;
  129.             double d1 = d8 * Math.Sin(d6);
  130.             double d2 = this.rho - d8 * Math.Cos(d6);
  131.             paramCoordinateXY.X = ((float)(d1 + this.falseEasting));
  132.             paramCoordinateXY.Y = ((float)(d2 + this.falseNorthing));
  133.             return paramCoordinateXY;
  134.         }
  135.  
  136.         public CoordinateXY LatLonToProj(CoordinateLatLon paramCoordinateLatLon)
  137.         {
  138.             double d3 = paramCoordinateLatLon.Lat;
  139.             double d4 = paramCoordinateLatLon.Lon;
  140.             d3 = this.ToRadians(d3);
  141.             double d5 = CoordinateLatLon.LonNormal(d4 - this.lon0Degrees);
  142.             double d6 = this.n * this.ToRadians(d5);
  143.             double d7 = Math.Pow(Math.Tan(0.7853981633974483D + d3 / 2.0D), this.n);
  144.             double d8 = this.earthRadiusTimesF / d7;
  145.             double d1 = d8 * Math.Sin(d6);
  146.             double d2 = this.rho - d8 * Math.Cos(d6);
  147.             return new CoordinateXY() { X = (d1 + this.falseEasting), Y = (d2 + this.falseNorthing) };
  148.         }
  149.  
  150.         public CoordinateLatLon ProjToLatLon(CoordinateXY paramCoordinateXY)
  151.         {
  152.             CoordinateLatLon localCoordinateLatLon = new CoordinateLatLon();
  153.             return ProjToLatLon(localCoordinateLatLon, paramCoordinateXY.X, paramCoordinateXY.Y);
  154.         }
  155.  
  156.         public CoordinateLatLon ProjToLatLon(CoordinateLatLon paramCoordinateLatLon, double paramFloat1, double paramFloat2)
  157.         {
  158.             double d3 = paramFloat1 - this.falseEasting;
  159.             double d4 = paramFloat2 - this.falseNorthing;
  160.             double d5 = this.rho;
  161.  
  162.             if (this.n < 0.0D)
  163.             {
  164.                 d5 *= -1.0D;
  165.                 d3 *= -1.0D;
  166.                 d4 *= -1.0D;
  167.             }
  168.  
  169.             double d6 = d5 - d4;
  170.             double d7 = Math.Atan2(d3, d6);
  171.             double d8 = Math.Sqrt(d3 * d3 + d6 * d6);
  172.  
  173.             if (this.n < 0.0D)
  174.             {
  175.                 d8 *= -1.0D;
  176.             }
  177.  
  178.             double d2 = this.ToDegrees(d7 / this.n + this.lon0);
  179.             double d1;
  180.             if (Math.Abs(d8) < 1.0E-006D)
  181.             {
  182.                 d1 = this.n < 0.0D ? -90.0D : 90.0D;
  183.             }
  184.             else
  185.             {
  186.                 double d9 = Math.Pow(6371.2290000000003D * this.F / d8, 1.0D / this.n);
  187.                 d1 = this.ToDegrees(2.0D * Math.Atan(d9) - 1.570796326794897D);
  188.             }
  189.  
  190.             paramCoordinateLatLon.Lat = d1;
  191.             paramCoordinateLatLon.Lon = d2;
  192.             return paramCoordinateLatLon;
  193.         }
  194.     }
  195.  
  196.     public struct CoordinateXY
  197.     {
  198.         public double X;
  199.         public double Y;
  200.     }
  201.  
  202.     public struct CoordinateLatLon
  203.     {
  204.         public double Lat;
  205.         public double Lon;
  206.  
  207.         public static double LonNormal(double paramDouble)
  208.         {
  209.             if ((paramDouble < -180.0D) || (paramDouble > 180.0D))
  210.             {
  211.                 return Math.IEEERemainder(paramDouble, 360.0D);
  212.             }
  213.  
  214.             return paramDouble;
  215.         }
  216.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement