1 package com.gradoservice.proj4as
3 import com.gradoservice.proj4as.proj.*;
5 public class ProjProjection
9 * Flag to indicate if initialization is complete for this Proj object
11 public var readyToUse:Boolean = false;
15 * The title to describe the projection
17 protected var projParams:ProjParams = new ProjParams();
19 static public const defs:Object = {
20 'EPSG:900913': "+title=Google Mercator EPSG:900913 +proj=merc +a=6378137 +b=6378137 +lat_ts=0.0 +lon_0=0.0 +x_0=0.0 +y_0=0 +k=1.0 +units=m +nadgrids=@null +no_defs",
21 'WGS84': "+title=long/lat:WGS84 +proj=longlat +ellps=WGS84 +datum=WGS84 +units=degrees",
22 'EPSG:4326': "+title=long/lat:WGS84 +proj=longlat +a=6378137.0 +b=6356752.31424518 +ellps=WGS84 +datum=WGS84 +units=degrees",
23 'EPSG:4269': "+title=long/lat:NAD83 +proj=longlat +a=6378137.0 +b=6356752.31414036 +ellps=GRS80 +datum=NAD83 +units=degrees",
24 'EPSG:32639': "+title=WGS 84 / UTM zone 39N +proj=utm +zone=39 +ellps=WGS84 +datum=WGS84 +units=m +no_defs",
25 'EPSG:27700': "+proj=tmerc +lat_0=49 +lon_0=-2 +k=0.9996012717 +x_0=400000 +y_0=-100000 +ellps=airy +datum=OSGB36 +units=m +no_defs"
30 protected var proj:IProjection;
32 public function get srsCode():String
34 return projParams.srsCode;
37 public function get srsProjNumber():String
39 return projParams.srsProjNumber;
42 public function get projName():String
44 return projParams.projName;
47 public function get datum():Datum
49 return projParams.datum;
52 public function get datumCode():String
54 return projParams.datumCode;
57 public function get from_greenwich():Number
59 return projParams.from_greenwich;
62 public function get to_meter():Number
64 return projParams.to_meter;
67 public function get a():Number
72 public function get b():Number
77 public function get ep2():Number
79 return projParams.ep2;
82 public function get es():Number
87 public function get datum_params():Array
89 return projParams.datum_params;
92 public function ProjProjection(srsCode:String)
94 this.projParams.srsCode = srsCode.toUpperCase();
96 if (this.projParams.srsCode.indexOf("EPSG") == 0) {
97 this.projParams.srsAuth = 'epsg';
98 this.projParams.srsProjNumber = this.projParams.srsCode.substring(5);
99 // DGR 2007-11-20 : authority IGNF
100 } else if (this.projParams.srsCode.indexOf("IGNF") == 0) {
101 this.projParams.srsAuth = 'IGNF';
102 this.projParams.srsProjNumber = this.projParams.srsCode.substring(5);
103 // DGR 2008-06-19 : pseudo-authority CRS for WMS
104 } else if (this.projParams.srsCode.indexOf("CRS") == 0) {
105 this.projParams.srsAuth = 'CRS';
106 this.projParams.srsProjNumber = this.projParams.srsCode.substring(4);
108 this.projParams.srsAuth = '';
109 this.projParams.srsProjNumber = this.projParams.srsCode;
111 this.loadProjDefinition();
114 private function loadProjDefinition():void
116 if (this.srsCode!=null && ProjProjection.defs[this.srsCode]!=null)
118 this.parseDef(ProjProjection.defs[this.projParams.srsCode]);
119 this.initTransforms();
123 protected function initTransforms():void
125 switch (this.projParams.projName)
127 case "aea": this.proj = new ProjAea(this.projParams); break;
128 case "aeqd": this.proj = new ProjAeqd(this.projParams); break;
129 case "eqc": this.proj = new ProjEqc(this.projParams); break;
130 case "eqdc": this.proj = new ProjEqdc(this.projParams); break;
131 case "equi": this.proj = new ProjEqui(this.projParams); break;
132 case "gauss": this.proj = new ProjGauss(this.projParams); break;
133 case "gstmerc": this.proj = new ProjGstmerc(this.projParams); break;
134 case "laea": this.proj = new ProjLaea(this.projParams); break;
135 case "lcc": this.proj = new ProjLcc(this.projParams); break;
136 case "longlat": this.proj = new ProjLonglat(this.projParams); break;
137 case "merc": this.proj = new ProjMerc(this.projParams); break;
138 case "mill": this.proj = new ProjMill(this.projParams); break;
139 case "moll": this.proj = new ProjMoll(this.projParams); break;
140 case "nzmg": this.proj = new ProjNzmg(this.projParams); break;
141 case "omerc": this.proj = new ProjOmerc(this.projParams); break;
142 case "ortho": this.proj = new ProjOrtho(this.projParams); break;
143 case "sinu": this.proj = new ProjSinu(this.projParams); break;
144 case "omerc": this.proj = new ProjOmerc(this.projParams); break;
145 case "stere": this.proj = new ProjStere(this.projParams); break;
146 case "sterea": this.proj = new ProjSterea(this.projParams); break;
147 case "tmerc": this.proj = new ProjTmerc(this.projParams); break;
148 case "utm": this.proj = new ProjUtm(this.projParams); break;
149 case "vandg": this.proj = new ProjVandg(this.projParams); break;
151 if (this.proj!=null) {
153 this.readyToUse = true;
157 private function parseDef(definition:String):void
159 var paramName:String = '';
160 var paramVal:String = '';
161 var paramArray:Array=definition.split("+");
162 for (var prop:int=0; prop<paramArray.length; prop++) {
163 var property:Array = paramArray[prop].split("=");
164 paramName = property[0].toLowerCase();
165 paramVal = property[1];
167 switch (paramName.replace(/\s/gi,"")) { // trim out spaces
168 case "": break; // throw away nameless parameter
169 case "title": this.projParams.title = paramVal; break;
170 case "proj": this.projParams.projName = paramVal.replace(/\s/gi,""); break;
171 case "units": this.projParams.units = paramVal.replace(/\s/gi,""); break;
172 case "datum": this.projParams.datumCode = paramVal.replace(/\s/gi,""); break;
173 case "nadgrids": this.projParams.nagrids = paramVal.replace(/\s/gi,""); break;
174 case "ellps": this.projParams.ellps = paramVal.replace(/\s/gi,""); break;
175 case "a": this.projParams.a = parseFloat(paramVal); break; // semi-major radius
176 case "b": this.projParams.b = parseFloat(paramVal); break; // semi-minor radius
178 case "rf": this.projParams.rf = parseFloat(paramVal); break; // inverse flattening rf= a/(a-b)
179 case "lat_0": this.projParams.lat0 = parseFloat(paramVal)*ProjConstants.D2R; break; // phi0, central latitude
180 case "lat_1": this.projParams.lat1 = parseFloat(paramVal)*ProjConstants.D2R; break; //standard parallel 1
181 case "lat_2": this.projParams.lat2 = parseFloat(paramVal)*ProjConstants.D2R; break; //standard parallel 2
182 case "lat_ts": this.projParams.lat_ts = parseFloat(paramVal)*ProjConstants.D2R; break; // used in merc and eqc
183 case "lon_0": this.projParams.long0 = parseFloat(paramVal)*ProjConstants.D2R; break; // lam0, central longitude
184 case "alpha": this.projParams.alpha = parseFloat(paramVal)*ProjConstants.D2R; break; //for somerc projection
185 case "lonc": this.projParams.longc = parseFloat(paramVal)*ProjConstants.D2R; break; //for somerc projection
186 case "x_0": this.projParams.x0 = parseFloat(paramVal); break; // false easting
187 case "y_0": this.projParams.y0 = parseFloat(paramVal); break; // false northing
188 case "k_0": this.projParams.k0 = parseFloat(paramVal); break; // projection scale factor
189 case "k": this.projParams.k0 = parseFloat(paramVal); break; // both forms returned
190 case "R_A": this.projParams.R_A = true; break; //Spheroid radius
191 case "zone": this.projParams.zone = parseInt(paramVal); break; // UTM Zone
192 case "south": this.projParams.utmSouth = true; break; // UTM north/south
193 case "towgs84":this.projParams.datum_params = paramVal.split(","); break;
194 case "to_meter": this.projParams.to_meter = parseFloat(paramVal); break; // cartesian scaling
195 case "from_greenwich": this.projParams.from_greenwich = parseFloat(paramVal)*ProjConstants.D2R; break;
196 // DGR 2008-07-09 : if pm is not a well-known prime meridian take
197 // the value instead of 0.0, then convert to radians
198 case "pm": paramVal = paramVal.replace(/\s/gi,"");
199 this.projParams.from_greenwich = ProjConstants.PrimeMeridian[paramVal] ?
200 ProjConstants.PrimeMeridian[paramVal] : parseFloat(paramVal);
201 this.projParams.from_greenwich *= ProjConstants.D2R;
203 case "no_defs": break;
204 default: trace("Unrecognized parameter: " + paramName); break;
207 this.deriveConstants();
211 private function deriveConstants():void
213 if (this.projParams.nagrids == '@null') this.projParams.datumCode = 'none';
214 if (this.projParams.datumCode && this.projParams.datumCode != 'none')
216 var datumDef:Object = ProjConstants.Datum[this.projParams.datumCode];
219 this.projParams.datum_params = datumDef.towgs84.split(',');
220 this.projParams.ellps = datumDef.ellipse;
221 this.projParams.datumName = datumDef.datumName ? datumDef.datumName : this.projParams.datumCode;
225 if (!this.projParams.a) { // do we have an ellipsoid?
226 var ellipse:Object = ProjConstants.Ellipsoid[this.projParams.ellps] ? ProjConstants.Ellipsoid[this.projParams.ellps] : ProjConstants.Ellipsoid['WGS84'];
227 extend(this.projParams, ellipse);
229 if (this.projParams.rf && !this.projParams.b) this.projParams.b = (1.0 - 1.0/this.projParams.rf) * this.projParams.a;
230 if (Math.abs(this.projParams.a - this.projParams.b)<ProjConstants.EPSLN) {
231 this.projParams.sphere = true;
232 this.projParams.b= this.projParams.a;
234 this.projParams.a2 = this.projParams.a * this.projParams.a; // used in geocentric
235 this.projParams.b2 = this.projParams.b * this.projParams.b; // used in geocentric
236 this.projParams.es = (this.projParams.a2-this.projParams.b2)/this.projParams.a2; // e ^ 2
237 this.projParams.e = Math.sqrt(this.projParams.es); // eccentricity
238 if (this.projParams.R_A) {
239 this.projParams.a *= 1. - this.projParams.es * (ProjConstants.SIXTH + this.projParams.es * (ProjConstants.RA4 + this.projParams.es * ProjConstants.RA6));
240 this.projParams.a2 = this.projParams.a * this.projParams.a;
241 this.projParams.b2 = this.projParams.b * this.projParams.b;
242 this.projParams.es = 0.;
244 this.projParams.ep2=(this.projParams.a2-this.projParams.b2)/this.projParams.b2; // used in geocentric
245 if (!this.projParams.k0) this.projParams.k0 = 1.0; //default value
247 this.projParams.datum = new Datum(this);
250 private function extend(destination:Object, source:Object):void
252 destination = destination || {};
254 for(var property:String in source) {
255 var value:Object = source[property];
257 destination[property] = value;
263 public function forward(p:ProjPoint):ProjPoint
267 return this.proj.forward(p);
272 public function inverse(p:ProjPoint):ProjPoint
276 return this.proj.inverse(p);