Debian Codegraph @

gctp/src/tminv.c

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