Debian Codegraph @

gctp/src/utminv.c

raw
1 /*******************************************************************************
2 NAME UNIVERSAL TRANSVERSE MERCATOR
3
4 PURPOSE: Transforms input Easting and Northing to longitude and
5 latitude for the Universal Transverse Mercator projection.
6 The 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 Nov, 1991
12 T. Mittan Mar, 1993
13 S. Nelson Feb, 1995 Divided tminv.c into two files, one
14 for UTM (utminv.c) and one for
15 TM (tminv.c). This was a necessary
16 change to run inverse projection
17 conversions for both UTM and TM
18 in the same process.
19
20 ALGORITHM REFERENCES
21
22 1. Snyder, John P., "Map Projections--A Working Manual", U.S. Geological
23 Survey Professional Paper 1395 (Supersedes USGS Bulletin 1532), United
24 State Government Printing Office, Washington D.C., 1987.
25
26 2. Snyder, John P. and Voxland, Philip M., "An Album of Map Projections",
27 U.S. Geological Survey Professional Paper 1453 , United State Government
28 Printing Office, Washington D.C., 1989.
29 *******************************************************************************/
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include "cproj.h"
33
34
35 /* Variables common to all subroutines in this code file
36 -----------------------------------------------------*/
37 static double r_major; /* major axis */
38 static double r_minor; /* minor axis */
39 static double scale_factor; /* scale factor */
40 static double lon_center; /* Center longitude (projection center) */
41 static double lat_origin; /* center latitude */
42 static double e0,e1,e2,e3; /* eccentricity constants */
43 static double es,esp; /* eccentricity constants */
44 static double ml0; /* small value m */
45 static double false_northing; /* y offset in meters */
46 static double false_easting; /* x offset in meters */
47 static long ind; /* sphere flag value */
48
49
50 /* Initialize the Universal Transverse Mercator (UTM) projection
51 -------------------------------------------------------------*/
52 int utminvint(
53 double r_maj, /* major axis */
54 double r_min, /* minor axis */
55 double scale_fact, /* scale factor */
56 long zone) /* zone number */
57 {
58 double temp; /* temprorary variables */
59
60 if ((labs(zone) < 1) || (labs(zone) > 60))
61 {
62 p_error("Illegal zone number","utm-invint");
63 return(11);
64 }
65 r_major = r_maj;
66 r_minor = r_min;
67 scale_factor = scale_fact;
68 lat_origin = 0.0;
69 lon_center = ((6 * labs(zone)) - 183) * D2R;
70 false_easting = 500000.0;
71 false_northing = (zone < 0) ? 10000000.0 : 0.0;
72
73 temp = r_minor / r_major;
74 es = 1.0 - SQUARE(temp);
75 e0 = e0fn(es);
76 e1 = e1fn(es);
77 e2 = e2fn(es);
78 e3 = e3fn(es);
79 ml0 = r_major * mlfn(e0, e1, e2, e3, lat_origin);
80 esp = es / (1.0 - es);
81
82 if (es < .00001)
83 ind = 1;
84 else
85 ind = 0;
86
87 /* Report parameters to the user
88 -----------------------------*/
89 ptitle("UNIVERSAL TRANSVERSE MERCATOR (UTM)");
90 genrpt_long(zone, "Zone: ");
91 radius2(r_major, r_minor);
92 genrpt(scale_factor,"Scale Factor at C. Meridian: ");
93 cenlonmer(lon_center);
94 return(OK);
95 }
96
97 /* Universal Transverse Mercator inverse equations--mapping x,y to lat,long
98 Note: The algorithm for UTM is exactly the same as TM and therefore
99 if a change is implemented, also make the change to TMINV.c
100 -----------------------------------------------------------------------*/
101 int utminv(
102 double x, /* (I) X projection coordinate */
103 double y, /* (I) Y projection coordinate */
104 double *lon, /* (O) Longitude */
105 double *lat) /* (O) Latitude */
106 {
107 double con,phi; /* temporary angles */
108 double delta_phi; /* difference between longitudes */
109 long i; /* counter variable */
110 double sin_phi, cos_phi, tan_phi; /* sin cos and tangent values */
111 double c, cs, t, ts, n, r, d, ds; /* temporary variables */
112 double f, h, g, temp; /* temporary variables */
113 long max_iter = 6; /* maximun number of iterations */
114
115 /* fortran code for spherical form
116 --------------------------------*/
117 if (ind != 0)
118 {
119 f = exp(x/(r_major * scale_factor));
120 g = .5 * (f - 1/f);
121 temp = lat_origin + y/(r_major * scale_factor);
122 h = cos(temp);
123 con = sqrt((1.0 - h * h)/(1.0 + g * g));
124 *lat = asinz(con);
125 if (temp < 0)
126 *lat = -*lat;
127 if ((g == 0) && (h == 0))
128 {
129 *lon = lon_center;
130 return(OK);
131 }
132 else
133 {
134 *lon = adjust_lon(atan2(g,h) + lon_center);
135 return(OK);
136 }
137 }
138
139 /* Inverse equations
140 -----------------*/
141 x = x - false_easting;
142 y = y - false_northing;
143
144 con = (ml0 + y / scale_factor) / r_major;
145 phi = con;
146 for (i=0;;i++)
147 {
148 delta_phi=((con + e1 * sin(2.0*phi) - e2 * sin(4.0*phi) + e3 * sin(6.0*phi))
149 / e0) - phi;
150 /*
151 delta_phi = ((con + e1 * sin(2.0*phi) - e2 * sin(4.0*phi)) / e0) - phi;
152 */
153 phi += delta_phi;
154 if (fabs(delta_phi) <= EPSLN) break;
155 if (i >= max_iter)
156 {
157 p_error("Latitude failed to converge","UTM-INVERSE");
158 return(95);
159 }
160 }
161 if (fabs(phi) < HALF_PI)
162 {
163 tsincos(phi, &sin_phi, &cos_phi);
164 tan_phi = tan(phi);
165 c = esp * SQUARE(cos_phi);
166 cs = SQUARE(c);
167 t = SQUARE(tan_phi);
168 ts = SQUARE(t);
169 con = 1.0 - es * SQUARE(sin_phi);
170 n = r_major / sqrt(con);
171 r = n * (1.0 - es) / con;
172 d = x / (n * scale_factor);
173 ds = SQUARE(d);
174 *lat = phi - (n * tan_phi * ds / r) * (0.5 - ds / 24.0 * (5.0 + 3.0 * t +
175 10.0 * c - 4.0 * cs - 9.0 * esp - ds / 30.0 * (61.0 + 90.0 * t +
176 298.0 * c + 45.0 * ts - 252.0 * esp - 3.0 * cs)));
177 *lon = adjust_lon(lon_center + (d * (1.0 - ds / 6.0 * (1.0 + 2.0 * t +
178 c - ds / 20.0 * (5.0 - 2.0 * c + 28.0 * t - 3.0 * cs + 8.0 * esp +
179 24.0 * ts))) / cos_phi));
180 }
181 else
182 {
183 *lat = HALF_PI * sign(y);
184 *lon = lon_center;
185 }
186 return(OK);
187 }