Understand shapefiles in different projections (notably OSGB)
[potlatch2.git] / com / gradoservice / proj4as / proj / ProjMerc.as
1 /*******************************************************************************\r
2 NAME                            MERCATOR\r
3 \r
4 PURPOSE:        Transforms input longitude and latitude to Easting and\r
5                 Northing for the Mercator projection.  The\r
6                 longitude and latitude must be in radians.  The Easting\r
7                 and Northing values will be returned in meters.\r
8 \r
9 PROGRAMMER              DATE\r
10 ----------              ----\r
11 D. Steinwand, EROS      Nov, 1991\r
12 T. Mittan               Mar, 1993\r
13 \r
14 ALGORITHM REFERENCES\r
15 \r
16 1.  Snyder, John P., "Map Projections--A Working Manual", U.S. Geological\r
17     Survey Professional Paper 1395 (Supersedes USGS Bulletin 1532), United\r
18     State Government Printing Office, Washington D.C., 1987.\r
19 \r
20 2.  Snyder, John P. and Voxland, Philip M., "An Album of Map Projections",\r
21     U.S. Geological Survey Professional Paper 1453 , United State Government\r
22     Printing Office, Washington D.C., 1989.\r
23 *******************************************************************************/\r
24 \r
25 package com.gradoservice.proj4as.proj\r
26 {\r
27         import com.gradoservice.proj4as.ProjPoint;\r
28         import com.gradoservice.proj4as.ProjConstants;\r
29         import com.gradoservice.proj4as.Datum;\r
30                 \r
31         public class ProjMerc extends AbstractProjProjection\r
32         {\r
33                 public function ProjMerc(data:ProjParams)\r
34                 {\r
35                         super(data);\r
36                 }\r
37                 \r
38                   override public function init():void\r
39                   {\r
40                         //?this.temp = this.r_minor / this.r_major;\r
41                         //this.temp = this.b / this.a;\r
42                         //this.es = 1.0 - Math.sqrt(this.temp);\r
43                         //this.e = Math.sqrt( this.es );\r
44                         //?this.m1 = Math.cos(this.lat_origin) / (Math.sqrt( 1.0 - this.es * Math.sin(this.lat_origin) * Math.sin(this.lat_origin)));\r
45                         //this.m1 = Math.cos(0.0) / (Math.sqrt( 1.0 - this.es * Math.sin(0.0) * Math.sin(0.0)));\r
46                     if (this.lat_ts) {\r
47                       if (this.sphere) {\r
48                         this.k0 = Math.cos(this.lat_ts);\r
49                       } else {\r
50                         this.k0 = ProjConstants.msfnz(this.es, Math.sin(this.lat_ts), Math.cos(this.lat_ts));\r
51                       }\r
52                     }\r
53                   }\r
54                 \r
55                 /* Mercator forward equations--mapping lat,long to x,y\r
56                   --------------------------------------------------*/\r
57                 \r
58                   override public function forward(p:ProjPoint):ProjPoint\r
59                   {     \r
60                     //alert("ll2m coords : "+coords);\r
61                     var lon:Number = p.x;\r
62                     var lat:Number = p.y;\r
63                     // convert to radians\r
64                     if ( lat*ProjConstants.R2D > 90.0 && \r
65                           lat*ProjConstants.R2D < -90.0 && \r
66                           lon*ProjConstants.R2D > 180.0 && \r
67                           lon*ProjConstants.R2D < -180.0) {\r
68                                 trace("merc:forward: llInputOutOfRange: "+ lon +" : " + lat);\r
69                       return null;\r
70                     }\r
71                 \r
72                     var x:Number,y:Number;\r
73                     if(Math.abs( Math.abs(lat) - ProjConstants.HALF_PI)  <= ProjConstants.EPSLN) {\r
74                       trace("merc:forward: ll2mAtPoles");\r
75                       return null;\r
76                     } else {\r
77                       if (this.sphere) {\r
78                         x = this.x0 + this.a * this.k0 * ProjConstants.adjust_lon(lon - this.long0);\r
79                         y = this.y0 + this.a * this.k0 * Math.log(Math.tan(ProjConstants.FORTPI + 0.5*lat));\r
80                       } else {\r
81                         var sinphi:Number = Math.sin(lat);\r
82                         var ts:Number = ProjConstants.tsfnz(this.e,lat,sinphi);\r
83                         x = this.x0 + this.a * this.k0 * ProjConstants.adjust_lon(lon - this.long0);\r
84                         y = this.y0 - this.a * this.k0 * Math.log(ts);\r
85                       }\r
86                       p.x = x; \r
87                       p.y = y;\r
88                       return p;\r
89                     }\r
90                   }\r
91                 \r
92                 \r
93                   /* Mercator inverse equations--mapping x,y to lat/long\r
94                   --------------------------------------------------*/\r
95                   override public function inverse(p:ProjPoint):ProjPoint\r
96                   {\r
97                     var x:Number = p.x - this.x0;\r
98                     var y:Number = p.y - this.y0;\r
99                     var lon:Number,lat:Number;\r
100                 \r
101                     if (this.sphere) {\r
102                       lat = ProjConstants.HALF_PI - 2.0 * Math.atan(Math.exp(-y / this.a * this.k0));\r
103                     } else {\r
104                       var ts:Number = Math.exp(-y / (this.a * this.k0));\r
105                       lat = ProjConstants.phi2z(this.e,ts);\r
106                       if(lat == -9999) {\r
107                         trace("merc:inverse: lat = -9999");\r
108                         return null;\r
109                       }\r
110                     }\r
111                     lon = ProjConstants.adjust_lon(this.long0+ x / (this.a * this.k0));\r
112                 \r
113                     p.x = lon;\r
114                     p.y = lat;\r
115                     return p;\r
116                   }\r
117                 \r
118                 \r
119         }\r
120 }