Debian Codegraph @

gctp/src/tmfor.c

raw
1 /*******************************************************************************
2 NAME TRANSVERSE MERCATOR
3
4 PURPOSE: Transforms input longitude and latitude to Easting and
5 Northing for the Transverse Mercator projection. The
6 longitude and latitude must be in radians. The Easting
7 and Northing values will be returned in meters.
8
9 PROGRAMMER DATE REASON
10 ---------- ---- ------
11 D. Steinwand, EROS Nov, 1991
12 T. Mittan Mar, 1993
13 S. Nelson Feb, 1995 Divided tmfor.c into two files, one
14 for UTM (utmfor.c) and one for
15 TM (tmfor.c). This was a
16 necessary change to run forward
17 projection conversions for both UTM
18 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 /* Variables common to all subroutines in this code file
34 -----------------------------------------------------*/
35 static double r_major; /* major axis */
36 static double r_minor; /* minor axis */
37 static double scale_factor; /* scale factor */
38 static double lon_center; /* Center longitude (projection center) */
39 static double lat_origin; /* center latitude */
40 static double e0,e1,e2,e3; /* eccentricity constants */
41 static double es,esp; /* eccentricity constants */
42 static double ml0; /* small value m */
43 static double false_northing; /* y offset in meters */
44 static double false_easting; /* x offset in meters */
45 static double ind; /* spherical flag */
46
47
48 /* Initialize the Transverse Mercator (TM) projection
49 -------------------------------------------------*/
50 int tmforint(
51 double r_maj, /* major axis */
52 double r_min, /* minor axis */
53 double scale_fact, /* scale factor */
54 double center_lon, /* center longitude */
55 double center_lat, /* center latitude */
56 double false_east, /* x offset in meters */
57 double false_north) /* y offset in meters */
58 {
59 double temp; /* temporary variable */
60
61 /* Place parameters in static storage for common use
62 -------------------------------------------------*/
63 r_major = r_maj;
64 r_minor = r_min;
65 scale_factor = scale_fact;
66 lon_center = center_lon;
67 lat_origin = center_lat;
68 false_northing = false_north;
69 false_easting = false_east;
70
71 temp = r_minor / r_major;
72 es = 1.0 - SQUARE(temp);
73 e0 = e0fn(es);
74 e1 = e1fn(es);
75 e2 = e2fn(es);
76 e3 = e3fn(es);
77 ml0 = r_major * mlfn(e0, e1, e2, e3, lat_origin);
78 esp = es / (1.0 - es);
79
80 if (es < .00001)
81 ind = 1;
82 else
83 ind = 0;
84
85 /* Report parameters to the user
86 -----------------------------*/
87 ptitle("TRANSVERSE MERCATOR (TM)");
88 radius2(r_major, r_minor);
89 genrpt(scale_factor,"Scale Factor at C. Meridian: ");
90 cenlonmer(lon_center);
91 origin(lat_origin);
92 offsetp(false_easting,false_northing);
93 return(OK);
94 }
95
96 /* Transverse Mercator forward equations--mapping lat,long to x,y
97 Note: The algorithm for TM is exactly the same as UTM and therefore
98 if a change is implemented, also make the change to UTMFOR.c
99 --------------------------------------------------------------*/
100 int tmfor(
101 double lon, /* (I) Longitude */
102 double lat, /* (I) Latitude */
103 double *x, /* (O) X projection coordinate */
104 double *y) /* (O) Y projection coordinate */
105 {
106 double delta_lon; /* Delta longitude (Given longitude - center */
107 double sin_phi, cos_phi;/* sin and cos value */
108 double al, als; /* temporary values */
109 double b; /* temporary values */
110 double c, t, tq; /* temporary values */
111 double con, n, ml; /* cone constant, small m */
112
113 /* Forward equations
114 -----------------*/
115 delta_lon = adjust_lon(lon - lon_center);
116 tsincos(lat, &sin_phi, &cos_phi);
117
118 /* This part was in the fortran code and is for the spherical form
119 ----------------------------------------------------------------*/
120 if (ind != 0)
121 {
122 b = cos_phi * sin(delta_lon);
123 if ((fabs(fabs(b) - 1.0)) < .0000000001)
124 {
125 p_error("Point projects into infinity","tm-for");
126 return(93);
127 }
128 else
129 {
130 *x = .5 * r_major * scale_factor * log((1.0 + b)/(1.0 - b));
131 con = acos(cos_phi * cos(delta_lon)/sqrt(1.0 - b*b));
132 if (lat < 0)
133 con = - con;
134 *y = r_major * scale_factor * (con - lat_origin);
135 return(OK);
136 }
137 }
138
139 al = cos_phi * delta_lon;
140 als = SQUARE(al);
141 c = esp * SQUARE(cos_phi);
142 tq = tan(lat);
143 t = SQUARE(tq);
144 con = 1.0 - es * SQUARE(sin_phi);
145 n = r_major / sqrt(con);
146 ml = r_major * mlfn(e0, e1, e2, e3, lat);
147
148 *x = scale_factor * n * al * (1.0 + als / 6.0 * (1.0 - t + c + als / 20.0 *
149 (5.0 - 18.0 * t + SQUARE(t) + 72.0 * c - 58.0 * esp))) + false_easting;
150
151 *y = scale_factor * (ml - ml0 + n * tq * (als * (0.5 + als / 24.0 *
152 (5.0 - t + 9.0 * c + 4.0 * SQUARE(c) + als / 30.0 * (61.0 - 58.0 * t
153 + SQUARE(t) + 600.0 * c - 330.0 * esp))))) + false_northing;
154
155 return(OK);
156 }