Debian Codegraph @

gctp/src/lamazinv.c

raw
1 /*******************************************************************************
2 NAME LAMBERT AZIMUTHAL EQUAL-AREA
3
4 PURPOSE: Transforms input Easting and Northing to longitude and
5 latitude for the Lambert Azimuthal Equal Area projection. The
6 Easting and Northing must be in meters. The longitude
7 and latitude values will be returned in radians.
8
9 PROGRAMMER DATE REASON
10 ---------- ---- ------
11 D. Steinwand, EROS March, 1991
12 S. Nelson,EROS Dec, 1993 changed asin() to asinz() because
13 NaN resulted expecting poles.
14 Abe Taaheri/Raytheon IIS Nov, 2013 Modified to support both
15 spherical and ellipsoidal
16 models of earth for
17 Lambert Azimuthal Equal Area
18 projection to support EASE-Grid2
19
20 This function was adapted from the Lambert Azimuthal Equal Area projection
21 code (FORTRAN) in the General Cartographic Transformation Package software
22 which is available from the U.S. Geological Survey National Mapping Division.
23
24 ALGORITHM REFERENCES
25
26 1. "New Equal-Area Map Projections for Noncircular Regions", John P. Snyder,
27 The American Cartographer, Vol 15, No. 4, October 1988, pp. 341-355.
28
29 2. Snyder, John P., "Map Projections--A Working Manual", U.S. Geological
30 Survey Professional Paper 1395 (Supersedes USGS Bulletin 1532), United
31 State Government Printing Office, Washington D.C., 1987.
32
33 3. "Software Documentation for GCTP General Cartographic Transformation
34 Package", U.S. Geological Survey National Mapping Division, May 1982.
35
36 4. "EASE-Grid 2.0: Incremental but Significant Improvements for Earth-Gridded Data
37 Sets", Mary J. Brodzik ?, Brendan Billingsley, Terry Haran , Bruce Raup and
38 Matthew H.Savoie, National Snow & Ice Data Center, Cooperative Institute of
39 Environmental Sciences, University of Colorado, 449 UCB, Boulder, CO 80309, USA;
40 E-Mails: [email protected] (B.B.); [email protected] (T.H.);
41 [email protected] (B.R.); [email protected] (M.H.S.)
42 *******************************************************************************/
43 #include <stdio.h>
44 #include "cproj.h"
45
46 /* Variables common to all subroutines in this code file
47 -----------------------------------------------------*/
48 static double r_major; /* major axis */
49 static double r_minor; /* minor axis */
50 static double lon_center; /* Center longitude (projection center) */
51 static double lat_center; /* Center latitude (projection center) */
52 static double R; /* Radius of the earth (sphere) */
53 static double sin_lat_o; /* Sine of the center latitude */
54 static double cos_lat_o; /* Cosine of the center latitude */
55 static double false_easting; /* x offset in meters */
56 static double false_northing; /* y offset in meters */
57 static double cosphi1; /* cos of latitude of true scale */
58 static double sinphi1; /* sin of latitude of true scale */
59 static double qp; /* q at 90 degrees. */
60 static double q1; /* q at lat_center degrees */
61 static double beta1, m1, Rq, D;
62 static double sin_beta1;
63 static double cos_beta1;
64 static long ind; /* spherical flag */
65 static double e, es, e_p4, e_p6;/* eccentricity constants */
66
67 /* Initialize the General Lambert Azimuthal Equal Area projection
68 --------------------------------------------------------------*/
69 int lamazinvint(
70 double r_maj, /* major axis */
71 double r_min, /* minor axis */
72 double center_long, /* (I) Center longitude */
73 double center_lat, /* (I) Center latitude */
74 double false_east, /* x offset in meters */
75 double false_north) /* y offset in meters */
76 {
77 /* Place parameters in static storage for common use
78 -------------------------------------------------*/
79 R = r_maj;
80 if(fabs(r_min) < EPSLN ) /* sphere */
81 {
82 r_major = r_maj;
83 r_minor = r_maj;
84 }
85 else /* sphere or ellipsoide */
86 {
87 r_major = r_maj;
88 r_minor = r_min;
89 }
90
91 lon_center = center_long;
92 lat_center = center_lat;
93 false_easting = false_east;
94 false_northing = false_north;
95 tsincos(center_lat, &sin_lat_o, &cos_lat_o);
96 sinphi1 = sin_lat_o;
97 cosphi1 = cos_lat_o;
98
99 es = 1.0 - SQUARE(r_minor / r_major);
100 e = sqrt(es);
101 e_p4 = es * es;
102 e_p6 = e_p4 *es;
103
104 if(e < 0.00001)
105 {
106 ind = 1; /* sphere */
107 qp = 2.0;
108 q1 = 2.0;
109 }
110 else
111 {
112 ind = 0; /* ellipsoid */
113 qp = (1.0 - es)* (((1.0/(1.0 - es))-(1.0/(2.0*e))*log((1.0 - e)/(1.0 + e))));
114
115 if((fabs(lat_center - HALF_PI) <= EPSLN ) || (fabs(lat_center + HALF_PI) <= EPSLN ))
116 {
117 /* no other constants needed for LA with North and South Aspects lat_center = 90 or -90*/
118 }
119 else
120 {
121 tsincos(lat_center, &sinphi1, &cosphi1);
122 q1 = (1.0 - es) * ((sinphi1 / (1.0 - es * sinphi1 * sinphi1))
123 - (1.0 / (2.0 * e)) *
124 log((1.0 - e * sinphi1)/(1.0 + e * sinphi1)));
125 Rq = r_major * sqrt(qp/2.0);
126 if(fabs(q1) >= fabs(qp))
127 {
128 beta1 = HALF_PI * (fabs(q1/qp)/(q1/qp));
129 }
130 else
131 {
132 beta1 = asinz(q1/qp);
133 }
134 tsincos(beta1, &sin_beta1, &cos_beta1);
135 m1 = cosphi1 / sqrt(1.0 - es * sinphi1 * sinphi1);
136 D = (r_major * m1)/ (Rq * cos_beta1);
137 }
138 }
139
140 /* Report parameters to the user
141 -----------------------------*/
142 ptitle("LAMBERT AZIMUTHAL EQUAL-AREA");
143 radius2(r_major, r_minor);
144 cenlon(center_long);
145 cenlat(center_lat);
146 offsetp(false_easting,false_northing);
147 return(OK);
148 }
149
150 /* Lambert Azimuthal Equal Area inverse equations--mapping x,y to lat,long
151 -----------------------------------------------------------------------*/
152 int lamazinv(
153 double x, /* (I) X projection coordinate */
154 double y, /* (I) Y projection coordinate */
155 double *lon, /* (O) Longitude */
156 double *lat) /* (O) Latitude */
157 {
158 double Rh;
159 double z; /* Great circle dist from proj center to given point */
160 double sin_z; /* Sine of z */
161 double cos_z; /* Cosine of z */
162 double sin_ce; /* Sine of ce */
163 double cos_ce; /* Cosine of ce */
164 double temp; /* Re-used temporary variable */
165 double ce;
166 double arg;
167 double rho, q, beta;
168 double q_test;
169 double phi, cos_phi, sin_phi,delta_phi;
170 double esin_phi, one_m_essin2_phi;
171 double one_m_es, one_over_2e;
172 /* double epsilon = 1e-6. Used in azimuthal_equal_area.c code */
173 int it_max = 35;
174 int i;
175
176 /* Inverse equations
177 -----------------*/
178 x -= false_easting;
179 y -= false_northing;
180
181
182 if( ind != 0) /* sphere */
183 {
184 Rh = sqrt(x * x + y * y);
185 temp = Rh / (2.0 * R);
186 if (temp > 1)
187 {
188 p_error("Input data error", "lamaz-inverse");
189 return(115);
190 }
191 z = 2.0 * asinz(temp);
192 tsincos(z, &sin_z, &cos_z);
193 *lon = lon_center;
194 if (fabs(Rh) > EPSLN)
195 {
196 *lat = asinz(sin_lat_o * cos_z + cos_lat_o * sin_z * y / Rh);
197 temp = fabs(lat_center) - HALF_PI;
198 if (fabs(temp) > EPSLN)
199 {
200 temp = cos_z - sin_lat_o * sin(*lat);
201 if(temp!=0.0)*lon=adjust_lon(lon_center+atan2(x*sin_z*cos_lat_o,temp*Rh));
202 }
203 else if (lat_center < 0.0) *lon = adjust_lon(lon_center - atan2(-x, y));
204 else *lon = adjust_lon(lon_center + atan2(x, -y));
205 }
206 else *lat = lat_center;
207 }
208 else /* ellipsoid */
209 {
210 if (fabs(fabs(lat_center) - HALF_PI) <= EPSLN) /* if Northern or Southern Polar Aspect */
211 {
212 rho = sqrt(x * x + y * y);
213 ce = rho / r_major;
214 arg = 1.0 - (rho * rho)/(qp *(r_major * r_major));
215 beta = sign(lat_center) * asinz(arg);
216 q = sign(lat_center) * (qp - ce * ce);
217 *lon = adjust_lon(lon_center + atan2(x, sign(lat_center) * (-y)));
218 }
219 else
220 {
221 rho = sqrt(((x*x)/(D*D))+(D*D*y*y));
222 ce = 2.0 * asinz(rho / (2.0 *Rq));
223 tsincos(ce, &sin_ce, &cos_ce);
224 *lon = adjust_lon(lon_center +
225 atan2( x * sin_ce, (D * rho * cos_beta1 * cos_ce -
226 D * D * y * sin_beta1 * sin_ce)));
227
228 if(fabs(rho) <= EPSLN)
229 {
230 q = qp * sin_beta1;
231 }
232 else
233 {
234 q = qp *((cos_ce * sin_beta1) + ( D * y * sin_ce * cos_beta1 / rho));
235 }
236 }
237
238 /* calculate phi by itteration using equation 3-16, Snyder 1987, p188 */
239
240 q_test = 1.0 - (1.0 - es)/(2.0 * e) * log((1.0 - e) / (1.0 + e));
241
242 if (fabs(fabs(q) - fabs(q_test)) < EPSLN)
243 {
244 *lat = sign(q) * PI / 2;
245 }
246 else
247 {
248 phi = asinz(q / 2.0);
249 one_m_es = 1.0 - es;
250 one_over_2e = 1.0 / (2.0 * e);
251
252 for (i = 0; i < it_max; i++)
253 {
254 tsincos(phi, &sin_phi, &cos_phi);
255 if (cos_phi < EPSLN)
256 {
257 phi = sign(q) * PI / 2;
258 break;
259 }
260 esin_phi = e * sin_phi;
261 one_m_essin2_phi = 1.0 - esin_phi * esin_phi;
262 delta_phi = one_m_essin2_phi * one_m_essin2_phi / (2.0 * cos_phi) *
263 (q / one_m_es - sin_phi / one_m_essin2_phi + one_over_2e *
264 log((1.0 - esin_phi) / (1.0 + esin_phi)));
265 phi += delta_phi;
266 if (fabs(delta_phi) < EPSLN)
267 break;
268 }
269 *lat = phi;
270 }
271
272 /* Approximate calculation of lat using series
273
274 if (fabs(lat_center - HALF_PI) <= EPSLN) // if Norther Polar Aspect
275 {
276 *lat = beta +((((es / 3.0) + ((31.0/180.0) * e_p4)+
277 ((517.0/5040.0) * e_p6)) * sin(2.0*beta))+
278 ((((23.0/360.0) * e_p4)+
279 ((251.0/3780.0) * e_p6)) * sin(4.0*beta))+
280 (((761.0/45360.0) * e_p6) * sin(6.0*beta)));
281 *lon = adjust_lon(lon_center + atan2(x, -y));
282 }
283 else if (fabs(lat_center + HALF_PI) <= EPSLN) // if Souther Polar Aspect
284 {
285 *lat = beta +((((es / 3.0) + ((31.0/180.0) * e_p4)+
286 ((517.0/5040.0) * e_p6)) * sin(2.0*beta))+
287 ((((23.0/360.0) * e_p4)+
288 ((251.0/3780.0) * e_p6)) * sin(4.0*beta))+
289 (((761.0/45360.0) * e_p6) * sin(6.0*beta)));
290 *lon = adjust_lon(lon_center + atan2(x, y));
291 }
292 else // with any other latitude of origin
293 {
294 rho = sqrt(((x*x)/(D*D))+(D*D*y*y));
295 if(fabs(rho) <= EPSLN)
296 {
297 ce = 0.0;
298 q = qp * sin_beta1;
299 beta = beta1;
300 *lon = adjust_lon(lon_center);
301 *lat = beta +((((es / 3.0) + ((31.0/180.0) * e_p4)+
302 ((517.0/5040.0) * e_p6)) * sin(2.0*beta))+
303 ((((23.0/360.0) * e_p4)+
304 ((251.0/3780.0) * e_p6)) * sin(4.0*beta))+
305 (((761.0/45360.0) * e_p6) * sin(6.0*beta)));
306 }
307 else
308 {
309 ce = 2.0 * asinz(rho / (2.0 *Rq));
310 tsincos(ce, &sin_ce, &cos_ce);
311 q = qp *((cos_ce * sin_beta1) + ( D * y * sin_ce * cos_beta1 / rho));
312 beta = asinz(q/qp);
313
314 *lon = adjust_lon(lon_center +
315 atan2( x * sin_ce, (D * rho * cos_beta1 * cos_ce -
316 D * D * y * sin_beta1 * sin_ce)));
317 *lat = beta +((((es / 3.0) + ((31.0/180.0) * e_p4)+
318 ((517.0/5040.0) * e_p6)) * sin(2.0*beta))+
319 ((((23.0/360.0) * e_p4)+
320 ((251.0/3780.0) * e_p6)) * sin(4.0*beta))+
321 (((761.0/45360.0) * e_p6) * sin(6.0*beta)));
322 }
323 }
324 */
325 }
326
327 return(OK);
328 }