Understand shapefiles in different projections (notably OSGB)
[potlatch2.git] / com / gradoservice / proj4as / proj / ProjAeqd.as
1 package com.gradoservice.proj4as.proj\r
2 {\r
3         import com.gradoservice.proj4as.ProjPoint;\r
4         import com.gradoservice.proj4as.ProjConstants;\r
5         import com.gradoservice.proj4as.Datum;\r
6                 \r
7         public class ProjAeqd extends AbstractProjProjection\r
8         {\r
9                 public function ProjAeqd(data:ProjParams)\r
10                 {\r
11                         super(data);\r
12                 }\r
13                 \r
14                 override public function init():void \r
15                 {\r
16         this.sin_p12=Math.sin(this.lat0)\r
17         this.cos_p12=Math.cos(this.lat0)\r
18                 }\r
19 \r
20                 override public function forward(p:ProjPoint):ProjPoint\r
21                 {\r
22         var lon:Number=p.x;\r
23         var lat:Number=p.y;\r
24         var ksp:Number;\r
25 \r
26         var sinphi:Number=Math.sin(p.y);\r
27         var cosphi:Number=Math.cos(p.y); \r
28         var dlon:Number = ProjConstants.adjust_lon(lon - this.long0);\r
29         var coslon:Number = Math.cos(dlon);\r
30         var g:Number = this.sin_p12 * sinphi + this.cos_p12 * cosphi * coslon;\r
31         if (Math.abs(Math.abs(g) - 1.0) < ProjConstants.EPSLN) {\r
32                 ksp = 1.0;\r
33                 if (g < 0.0) {\r
34                 trace("aeqd:Fwd:PointError");\r
35                 return null;\r
36                 }\r
37         } else {\r
38         var z:Number = Math.acos(g);\r
39         ksp = z/Math.sin(z);\r
40         }\r
41         p.x = this.x0 + this.a * ksp * cosphi * Math.sin(dlon);\r
42         p.y = this.y0 + this.a * ksp * (this.cos_p12 * sinphi - this.sin_p12 * cosphi * coslon);\r
43         return p;\r
44                 }\r
45 \r
46                 override public function inverse(p:ProjPoint):ProjPoint \r
47                 {\r
48         p.x -= this.x0;\r
49         p.y -= this.y0;\r
50         \r
51         var rh:Number = Math.sqrt(p.x * p.x + p.y *p.y);\r
52         if (rh > (2.0 * ProjConstants.HALF_PI * this.a)) \r
53         {\r
54                 trace("aeqdInvDataError");\r
55                 return null;\r
56         }\r
57         var z:Number = rh / this.a;\r
58 \r
59     var sinz:Number=Math.sin(z)\r
60     var cosz:Number=Math.cos(z)\r
61 \r
62     var lon:Number = this.long0;\r
63     var lat:Number;\r
64     if (Math.abs(rh) <= ProjConstants.EPSLN) {\r
65       lat = this.lat0;\r
66     } else {\r
67       lat = ProjConstants.asinz(cosz * this.sin_p12 + (p.y * sinz * this.cos_p12) / rh);\r
68       var con:Number = Math.abs(this.lat0) - ProjConstants.HALF_PI;\r
69       if (Math.abs(con) <= ProjConstants.EPSLN) {\r
70         if (lat0 >= 0.0) {\r
71           lon = ProjConstants.adjust_lon(this.long0 + Math.atan2(p.x , -p.y));\r
72         } else {\r
73           lon = ProjConstants.adjust_lon(this.long0 - Math.atan2(-p.x , p.y));\r
74         }\r
75       } else {\r
76         con = cosz - this.sin_p12 * Math.sin(lat);\r
77         if ((Math.abs(con) < ProjConstants.EPSLN) && (Math.abs(p.x) < ProjConstants.EPSLN)) {\r
78            //no-op, just keep the lon value as is\r
79         } else {\r
80           var temp:Number = Math.atan2((p.x * sinz * this.cos_p12), (con * rh));\r
81           lon = ProjConstants.adjust_lon(this.long0 + Math.atan2((p.x * sinz * this.cos_p12), (con * rh)));\r
82         }\r
83       }\r
84     }\r
85 \r
86     p.x = lon;\r
87     p.y = lat;\r
88     return p;\r
89   } \r
90                 \r
91                 \r
92         }\r
93 }