Understand shapefiles in different projections (notably OSGB)
[potlatch2.git] / com / gradoservice / proj4as / proj / ProjMoll.as
1 /*******************************************************************************\r
2 NAME                            MOLLWEIDE\r
3 \r
4 PURPOSE:        Transforms input longitude and latitude to Easting and\r
5                 Northing for the MOllweide 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      May, 1991;  Updated Sept, 1992; Updated Feb, 1993\r
12 S. Nelson, EDC          Jun, 2993;      Made corrections in precision and\r
13                                         number of iterations.\r
14 \r
15 ALGORITHM REFERENCES\r
16 \r
17 1.  Snyder, John P. and Voxland, Philip M., "An Album of Map Projections",\r
18     U.S. Geological Survey Professional Paper 1453 , United State Government\r
19     Printing Office, Washington D.C., 1989.\r
20 \r
21 2.  Snyder, John P., "Map Projections--A Working Manual", U.S. Geological\r
22     Survey Professional Paper 1395 (Supersedes USGS Bulletin 1532), United\r
23     State Government Printing Office, Washington D.C., 1987.\r
24 *******************************************************************************/\r
25 \r
26 \r
27 package com.gradoservice.proj4as.proj\r
28 {\r
29         import com.gradoservice.proj4as.ProjPoint;\r
30         import com.gradoservice.proj4as.ProjConstants;\r
31         import com.gradoservice.proj4as.Datum;\r
32                 \r
33         public class ProjMoll extends AbstractProjProjection\r
34         {\r
35                 public function ProjMoll(data:ProjParams)\r
36                 {\r
37                         super(data);\r
38                 }\r
39                 \r
40                   override public function init():void\r
41                   {\r
42                 //no-op\r
43                   }\r
44 \r
45   /* Mollweide forward equations--mapping lat,long to x,y\r
46     ----------------------------------------------------*/\r
47                   override public function forward(p:ProjPoint):ProjPoint\r
48                   {\r
49                     /* Forward equations\r
50                       -----------------*/\r
51                     var lon:Number=p.x;\r
52                     var lat:Number=p.y;\r
53                 \r
54                     var delta_lon:Number = ProjConstants.adjust_lon(lon - this.long0);\r
55                     var theta:Number = lat;\r
56                     var con:Number = ProjConstants.PI * Math.sin(lat);\r
57                 \r
58                     /* Iterate using the Newton-Raphson method to find theta\r
59                       -----------------------------------------------------*/\r
60                     for (var i:int=0;;i++) {\r
61                        var delta_theta:Number = -(theta + Math.sin(theta) - con)/ (1.0 + Math.cos(theta));\r
62                        theta += delta_theta;\r
63                        if (Math.abs(delta_theta) < ProjConstants.EPSLN) break;\r
64                        if (i >= 50) {\r
65                           trace("moll:Fwd:IterationError");\r
66                          //return(241);\r
67                        }\r
68                     }\r
69                     theta /= 2.0;\r
70                 \r
71                     /* If the latitude is 90 deg, force the x coordinate to be "0 + false easting"\r
72                        this is done here because of precision problems with "cos(theta)"\r
73                        --------------------------------------------------------------------------*/\r
74                     if (ProjConstants.PI/2 - Math.abs(lat) < ProjConstants.EPSLN) delta_lon =0;\r
75                     var x:Number = 0.900316316158 * this.a * delta_lon * Math.cos(theta) + this.x0;\r
76                     var y:Number = 1.4142135623731 * this.a * Math.sin(theta) + this.y0;\r
77                 \r
78                     p.x=x;\r
79                     p.y=y;\r
80                     return p;\r
81                   }\r
82                 \r
83                  override public function inverse(p:ProjPoint):ProjPoint\r
84                  {\r
85                     var theta:Number;\r
86                     var arg:Number;\r
87                 \r
88                     /* Inverse equations\r
89                       -----------------*/\r
90                     p.x-= this.x0;\r
91                     //~ p.y -= this.y0;\r
92                     arg = p.y /  (1.4142135623731 * this.a);\r
93                 \r
94                     /* Because of division by zero problems, 'arg' can not be 1.0.  Therefore\r
95                        a number very close to one is used instead.\r
96                        -------------------------------------------------------------------*/\r
97                     if(Math.abs(arg) > 0.999999999999) arg=0.999999999999;\r
98                     theta =Math.asin(arg);\r
99                     var lon:Number = ProjConstants.adjust_lon(this.long0 + (p.x / (0.900316316158 * this.a * Math.cos(theta))));\r
100                     if(lon < (-ProjConstants.PI)) lon= -ProjConstants.PI;\r
101                     if(lon > ProjConstants.PI) lon= ProjConstants.PI;\r
102                     arg = (2.0 * theta + Math.sin(2.0 * theta)) / ProjConstants.PI;\r
103                     if(Math.abs(arg) > 1.0)arg=1.0;\r
104                     var lat:Number = Math.asin(arg);\r
105                     //return(OK);\r
106                 \r
107                     p.x=lon;\r
108                     p.y=lat;\r
109                     return p;\r
110                   }             \r
111                 \r
112         }\r
113 }