Debian Codegraph @

gctp/src/cproj.c

raw
1 /*******************************************************************************
2 NAME Projection support routines listed below.
3
4 PURPOSE: The following functions are included in CPROJ.C.
5
6 SINCOS: Calculates the sine and cosine.
7 ASINZ: Eliminates roundoff errors.
8 MSFNZ: Computes the constant small m for Oblique Equal Area.
9 QSFNZ: Computes the constant small q for Oblique Equal Area.
10 PHI1Z: Computes phi1 for Albers Conical Equal-Area.
11 PHI2Z: Computes the latitude angle, phi2, for Lambert
12 Conformal Conic and Polar Stereographic.
13 PHI3Z: Computes the latitude, phi3, for Equidistant Conic.
14 PHI4Z: Computes the latitude, phi4, for Polyconic.
15 PAKCZ: Converts a 2 digit alternate packed DMS format to
16 standard packed DMS format.
17 PAKR2DM: Converts radians to 3 digit packed DMS format.
18 TSFNZ: Computes the small t for Lambert Conformal Conic and
19 Polar Stereographic.
20 SIGN: Returns the sign of an argument.
21 ADJUST_LON: Adjusts a longitude angle to range -180 to 180.
22 E0FN, E1FN, E2FN, E3FN:
23 Computes the constants e0,e1,e2,and e3 for
24 calculating the distance along a meridian.
25 E4FN: Computes e4 used for Polar Stereographic.
26 MLFN: Computes M, the distance along a meridian.
27 CALC_UTM_ZONE: Calculates the UTM zone number.
28
29 PROGRAMMER DATE REASON
30 ---------- ---- ------
31 D. Steinwand, EROS July, 1991 Initial development
32 T. Mittan, EROS May, 1993 Modified from Fortran GCTP for C GCTP
33 S. Nelson, EROS June, 1993 Added inline comments
34 S. Nelson, EROS Nov, 1993 Added loop counter in ADJUST_LON
35
36 *******************************************************************************/
37 #include <stdio.h>
38 #include "cproj.h"
39
40 #define MAX_VAL 4
41 #define MAXLONG 2147483647.
42 #define DBLLONG 4.61168601e18
43
44 /* Function to calculate the sine and cosine in one call. Some computer
45 systems have implemented this function, resulting in a faster implementation
46 than calling each function separately. It is provided here for those
47 computer systems which don`t implement this function
48 ----------------------------------------------------*/
49
50 /*#ifndef sun */
51 void tsincos(double val, double *sin_val, double *cos_val)
52 { *sin_val = sin(val); *cos_val = cos(val); return; }
53 /* #endif */
54
55
56
57 /* Function to eliminate roundoff errors in asin
58 ----------------------------------------------*/
59 double asinz (double con)
60 {
61 if (fabs(con) > 1.0)
62 {
63 if (con > 1.0)
64 con = 1.0;
65 else
66 con = -1.0;
67 }
68 return(asin(con));
69 }
70
71 /* Function to compute the constant small m which is the radius of
72 a parallel of latitude, phi, divided by the semimajor axis.
73 ---------------------------------------------------------------*/
74 double msfnz (
75 double eccent,
76 double sinphi,
77 double cosphi)
78 {
79 double con;
80
81 con = eccent * sinphi;
82 return((cosphi / (sqrt (1.0 - con * con))));
83 }
84
85 /* Function to compute constant small q which is the radius of a
86 parallel of latitude, phi, divided by the semimajor axis.
87 ------------------------------------------------------------*/
88 double qsfnz (
89 double eccent,
90 double sinphi,
91 double cosphi)
92 {
93 double con;
94
95 if (eccent > 1.0e-7)
96 {
97 con = eccent * sinphi;
98 return (( 1.0- eccent * eccent) * (sinphi /(1.0 - con * con) - (.5/eccent)*
99 log((1.0 - con)/(1.0 + con))));
100 }
101 else
102 return(2.0 * sinphi);
103 }
104
105 /* Function to compute phi1, the latitude for the inverse of the
106 Albers Conical Equal-Area projection.
107 -------------------------------------------*/
108 double phi1z (
109 double eccent, /* Eccentricity angle in radians */
110 double qs, /* Angle in radians */
111 long *flag) /* Error flag number */
112 {
113 double eccnts;
114 double dphi;
115 double con;
116 double com;
117 double sinpi;
118 double cospi;
119 double phi;
120 long i;
121
122 phi = asinz(.5 * qs);
123 if (eccent < EPSLN)
124 return(phi);
125 eccnts = eccent * eccent;
126 for (i = 1; i <= 25; i++)
127 {
128 tsincos(phi,&sinpi,&cospi);
129 con = eccent * sinpi;
130 com = 1.0 - con * con;
131 dphi = .5 * com * com / cospi * (qs / (1.0 - eccnts) - sinpi / com +
132 .5 / eccent * log ((1.0 - con) / (1.0 + con)));
133 phi = phi + dphi;
134 if (fabs(dphi) <= 1e-7)
135 return(phi);
136 }
137 p_error ("Convergence error","phi1z-conv");
138 *flag = 001;
139 return(ERROR);
140 }
141
142 /* Function to compute the latitude angle, phi2, for the inverse of the
143 Lambert Conformal Conic and Polar Stereographic projections.
144 ----------------------------------------------------------------*/
145 double phi2z(
146 double eccent, /* Spheroid eccentricity */
147 double ts, /* Constant value t */
148 long *flag) /* Error flag number */
149
150 {
151 double eccnth;
152 double phi;
153 double con;
154 double dphi;
155 double sinpi;
156 long i;
157
158 *flag = 0;
159 eccnth = .5 * eccent;
160 phi = HALF_PI - 2 * atan(ts);
161 for (i = 0; i <= 15; i++)
162 {
163 sinpi = sin(phi);
164 con = eccent * sinpi;
165 dphi = HALF_PI - 2 * atan(ts *(pow(((1.0 - con)/(1.0 + con)),eccnth))) -
166 phi;
167 phi += dphi;
168 if (fabs(dphi) <= .0000000001)
169 return(phi);
170 }
171 p_error ("Convergence error","phi2z-conv");
172 *flag = 002;
173 return(002);
174 }
175
176 /* Function to compute latitude, phi3, for the inverse of the Equidistant
177 Conic projection.
178 -----------------------------------------------------------------*/
179 double phi3z(
180 double ml, /* Constant */
181 double e0, /* Constant */
182 double e1, /* Constant */
183 double e2, /* Constant */
184 double e3, /* Constant */
185 long *flag) /* Error flag number */
186
187 {
188 double phi;
189 double dphi;
190 long i;
191
192 phi = ml;
193 for (i = 0; i < 15; i++)
194 {
195 dphi = (ml + e1 * sin(2.0 * phi) - e2 * sin(4.0 * phi) + e3 * sin(6.0 * phi))
196 / e0 - phi;
197 phi += dphi;
198 if (fabs(dphi) <= .0000000001)
199 {
200 *flag = 0;
201 return(phi);
202 }
203 }
204 p_error("Latitude failed to converge after 15 iterations","PHI3Z-CONV");
205 *flag = 3;
206 return(3);
207 }
208
209 /* Function to compute, phi4, the latitude for the inverse of the
210 Polyconic projection.
211 ------------------------------------------------------------*/
212 double phi4z (
213 double eccent, /* Spheroid eccentricity squared */
214 double e0,
215 double e1,
216 double e2,
217 double e3,
218 double a,
219 double b,
220 double *c,
221 double *phi)
222 {
223 double sinphi;
224 double sin2ph;
225 double tanphi;
226 double ml;
227 double mlp;
228 double con1;
229 double con2;
230 double con3;
231 double dphi;
232 long i;
233
234 *phi = a;
235 for (i = 1; i <= 15; i++)
236 {
237 sinphi = sin(*phi);
238 tanphi = tan(*phi);
239 *c = tanphi * sqrt (1.0 - eccent * sinphi * sinphi);
240 sin2ph = sin (2.0 * *phi);
241 /*
242 ml = e0 * *phi - e1 * sin2ph + e2 * sin (4.0 * *phi);
243 mlp = e0 - 2.0 * e1 * cos (2.0 * *phi) + 4.0 * e2 *
244 cos (4.0 * *phi);
245 */
246 ml = e0 * *phi - e1 * sin2ph + e2 * sin (4.0 * *phi) - e3 *
247 sin (6.0 * *phi);
248 mlp = e0 - 2.0 * e1 * cos (2.0 * *phi) + 4.0 * e2 *
249 cos (4.0 * *phi) - 6.0 * e3 * cos (6.0 * *phi);
250 con1 = 2.0 * ml + *c * (ml * ml + b) - 2.0 * a * (*c * ml + 1.0);
251 con2 = eccent * sin2ph * (ml * ml + b - 2.0 * a * ml) / (2.0 * *c);
252 con3 = 2.0 * (a - ml) * (*c * mlp - 2.0 / sin2ph) - 2.0 * mlp;
253 dphi = con1 / (con2 + con3);
254 *phi += dphi;
255 if (fabs(dphi) <= .0000000001 )
256 return(OK);
257 }
258 p_error("Lattitude failed to converge","phi4z-conv");
259 return(004);
260 }
261
262 /* Function to convert 2 digit alternate packed DMS format (+/-)DDDMMSS.SSS
263 to 3 digit standard packed DMS format (+/-)DDDMMMSSS.SSS.
264 -----------------------------------------------------------------*/
265 double pakcz(
266 double pak) /* Angle in alternate packed DMS format */
267 {
268 double con;
269 double secs;
270 long degs,mins;
271 char sgna;
272
273 sgna = ' ';
274 if (pak < 0.0)
275 sgna = '-';
276 con = fabs (pak);
277 degs = (long) ((con / 10000.0) + .001);
278 con = con - degs * 10000;
279 mins = (long) ((con / 100.0) + .001);
280 secs = con - mins * 100;
281 con = (double) (degs) * 1000000.0 + (double) (mins) * 1000.0 + secs;
282 if (sgna == '-')
283 con = - con;
284 return(con);
285 }
286
287 /* Function to convert radians to 3 digit packed DMS format (+/-)DDDMMMSSS.SSS
288 ----------------------------------------------------------------------------*/
289 double pakr2dm(
290 double pak) /* Angle in radians */
291 {
292 double con;
293 double secs;
294 long degs,mins;
295 char sgna;
296
297 sgna = ' ';
298 pak *= R2D;
299 if (pak < 0.0)
300 sgna = '-';
301 con = fabs (pak);
302 degs = (long) (con);
303 con = (con - degs) * 60;
304 mins = (long) con;
305 secs = (con - mins) * 60;
306 con = (double) (degs) * 1000000.0 + (double) (mins) * 1000.0 + secs;
307 if (sgna == '-')
308 con = - con;
309 return(con);
310 }
311
312 /* Function to compute the constant small t for use in the forward
313 computations in the Lambert Conformal Conic and the Polar
314 Stereographic projections.
315 --------------------------------------------------------------*/
316 double tsfnz(
317 double eccent, /* Eccentricity of the spheroid */
318 double phi, /* Latitude phi */
319 double sinphi) /* Sine of the latitude */
320 {
321 double con;
322 double com;
323
324 con = eccent * sinphi;
325 com = .5 * eccent;
326 con = pow(((1.0 - con) / (1.0 + con)),com);
327 return (tan(.5 * (HALF_PI - phi))/con);
328 }
329
330
331 /* Function to return the sign of an argument
332 ------------------------------------------*/
333 int sign(double x) { if (x < 0.0) return(-1); else return(1);}
334
335 /* Function to adjust a longitude angle to range from -180 to 180 radians
336 added if statments
337 -----------------------------------------------------------------------*/
338 double adjust_lon(
339 double x) /* Angle in radians */
340 {
341 long count = 0;
342 for(;;)
343 {
344 if (fabs(x)<=PI)
345 break;
346 else
347 if (((long) fabs(x / PI)) < 2)
348 x = x-(sign(x) *TWO_PI);
349 else
350 if (((long) fabs(x / TWO_PI)) < MAXLONG)
351 {
352 x = x-(((long)(x / TWO_PI))*TWO_PI);
353 }
354 else
355 if (((long) fabs(x / (MAXLONG * TWO_PI))) < MAXLONG)
356 {
357 x = x-(((long)(x / (MAXLONG * TWO_PI))) * (TWO_PI * MAXLONG));
358 }
359 else
360 if (((long) fabs(x / (DBLLONG * TWO_PI))) < MAXLONG)
361 {
362 x = x-(((long)(x / (DBLLONG * TWO_PI))) * (TWO_PI * DBLLONG));
363 }
364 else
365 x = x-(sign(x) *TWO_PI);
366 count++;
367 if (count > MAX_VAL)
368 break;
369 }
370
371 return(x);
372 }
373
374 /* Functions to compute the constants e0, e1, e2, and e3 which are used
375 in a series for calculating the distance along a meridian. The
376 input x represents the eccentricity squared.
377 ----------------------------------------------------------------*/
378 double e0fn(double x){return(1.0-0.25*x*(1.0+x/16.0*(3.0+1.25*x)));}
379 double e1fn(double x){return(0.375*x*(1.0+0.25*x*(1.0+0.46875*x)));}
380 double e2fn(double x){return(0.05859375*x*x*(1.0+0.75*x));}
381 double e3fn(double x){return(x*x*x*(35.0/3072.0));}
382
383 /* Function to compute the constant e4 from the input of the eccentricity
384 of the spheroid, x. This constant is used in the Polar Stereographic
385 projection.
386 --------------------------------------------------------------------*/
387 double e4fn(double x)
388 {
389 double con;
390 double com;
391 con = 1.0 + x;
392 com = 1.0 - x;
393 return (sqrt((pow(con,con))*(pow(com,com))));
394 }
395
396 /* Function computes the value of M which is the distance along a meridian
397 from the Equator to latitude phi.
398 ------------------------------------------------*/
399 double mlfn(double e0, double e1, double e2, double e3, double phi){
400 return(e0*phi-e1*sin(2.0*phi)+e2*sin(4.0*phi)-e3*sin(6.0*phi));}
401
402 /* Function to calculate UTM zone number--NOTE Longitude entered in DEGREES!!!
403 ---------------------------------------------------------------------------*/
404 long calc_utm_zone(double lon) { return((long)(((lon + 180.0) / 6.0) + 1.0)); }