Debian Codegraph @

gctp/src/stplninv.c

raw
1 /*******************************************************************************
2 NAME STATE PLANE
3
4 PURPOSE: Transforms input Easting and Northing to longitude and
5 latitude for the State Plane 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
10 ---------- ----
11 T. Mittan Apr, 1993
12
13 ALGORITHM REFERENCES
14
15 1. Snyder, John P., "Map Projections--A Working Manual", U.S. Geological
16 Survey Professional Paper 1395 (Supersedes USGS Bulletin 1532), United
17 State Government Printing Office, Washington D.C., 1987.
18
19 2. Snyder, John P. and Voxland, Philip M., "An Album of Map Projections",
20 U.S. Geological Survey Professional Paper 1453 , United State Government
21 Printing Office, Washington D.C., 1989.
22 *******************************************************************************/
23 #include <stdio.h>
24 #include "cproj.h"
25
26 static int id;
27 static long inzone = 0;
28 static long nad27[134] = {101,102,5010,5300,201,202,203,301,302,401,402,403,
29 404,405,406,407,501,502,503,600,700,901,902,903,1001,1002,
30 5101,5102,5103,5104,5105,1101,1102,1103,1201,1202,1301,1302,
31 1401,1402,1501,1502,1601,1602,1701,1702,1703,1801,1802,1900,
32 2001,2002,2101,2102,2103,2111,2112,2113,2201,2202,2203,2301,
33 2302,2401,2402,2403,2501,2502,2503,2601,2602,2701,2702,2703,
34 2800,2900,3001,3002,3003,3101,3102,3103,3104,3200,3301,3302,
35 3401,3402,3501,3502,3601,3602,3701,3702,3800,3901,3902,4001,
36 4002,4100,4201,4202,4203,4204,4205,4301,4302,4303,4400,4501,
37 4502,4601,4602,4701,4702,4801,4802,4803,4901,4902,4903,4904,
38 5001,5002,5003,5004,5005,5006,5007,5008,5009,5201,5202,5400};
39
40 static long nad83[134] = {101,102,5010,5300,201,202,203,301,302,401,402,403,
41 404,405,406,0000,501,502,503,600,700,901,902,903,1001,1002,
42 5101,5102,5103,5104,5105,1101,1102,1103,1201,1202,1301,1302,
43 1401,1402,1501,1502,1601,1602,1701,1702,1703,1801,1802,1900,
44 2001,2002,2101,2102,2103,2111,2112,2113,2201,2202,2203,2301,
45 2302,2401,2402,2403,2500,0000,0000,2600,0000,2701,2702,2703,
46 2800,2900,3001,3002,3003,3101,3102,3103,3104,3200,3301,3302,
47 3401,3402,3501,3502,3601,3602,3701,3702,3800,3900,0000,4001,
48 4002,4100,4201,4202,4203,4204,4205,4301,4302,4303,4400,4501,
49 4502,4601,4602,4701,4702,4801,4802,4803,4901,4902,4903,4904,
50 5001,5002,5003,5004,5005,5006,5007,5008,5009,5200,0000,5400};
51 /* Initialize the State Plane projection
52 ------------------------------------*/
53 int stplninvint(
54 long zone,
55 long sphere,
56 char *fn27,
57 char *fn83)
58 {
59 long ind;
60 long i;
61 long nadval;
62 double table[9];
63 char pname[33];
64 char buf[100];
65 double r_maj,r_min,scale_fact,center_lon;
66 double center_lat,false_east,false_north;
67 double azimuth,lat_orig,lon_orig,lon1,lat1,lon2,lat2;
68 long mode,iflg;
69 FILE *ptr;
70 #ifndef BIG_ENDIAN
71 char swap[16];
72 char* swapPtr;
73 long j;
74 #endif
75
76 ind = -1;
77 if (inzone != zone)
78 inzone = zone;
79 else
80 return(OK);
81 if (zone > 0)
82 {
83 if (sphere == 0)
84 {
85 for (i = 0; i < 134; i++)
86 {
87 if (zone == nad27[i])
88 {
89 ind = i;
90 break;
91 }
92 }
93 }
94 else
95 if (sphere == 8)
96 {
97 for (i = 0; i < 134; i++)
98 {
99 if (zone == nad83[i])
100 {
101 ind = i;
102 break;
103 }
104 }
105 }
106 }
107 if (ind == -1)
108 {
109 sprintf(buf,"Illegal zone #%4ld for spheroid #%4ld",zone,sphere);
110 p_error(buf,"state-init");
111 return(21);
112 }
113
114 if (sphere == 0)
115 ptr = fopen(fn27,"r");
116 else
117 ptr = fopen(fn83,"r");
118 if (ptr == NULL)
119 {
120 p_error("Error opening State Plane parameter file","state-inv");
121 return(22);
122 }
123 fseek(ptr,ind * 432, 0);
124 ftell(ptr);
125 fread(pname,sizeof(char),32,ptr);
126 fread(&id,sizeof(int),1,ptr);
127 fread(table,sizeof(double),9,ptr);
128 fclose(ptr);
129
130 #ifndef BIG_ENDIAN
131 /* swap bytes as necessary */
132 swapPtr = (char*) (&id);
133 for (i=0;i<sizeof(int);i++)
134 {
135 swap[i] = swapPtr[sizeof(int) - 1 - i];
136 }
137 id = *((int*)swap);
138
139 for (i=0;i<9;i++)
140 {
141 swapPtr = (char*) (table+i);
142 for (j=0;j<sizeof(double);j++)
143 {
144 swap[j] = swapPtr[sizeof(double) - 1 - j];
145 }
146 table[i] = *((double*)swap);
147 }
148 #endif
149
150 if (id <= 0)
151 {
152 sprintf(buf,"Illegal zone #%4ld for spheroid #%4ld",zone,sphere);
153 p_error(buf,"state-init");
154 return(21);
155 }
156
157 /* Report parameters to the user
158 -----------------------------*/
159 ptitle("STATE PLANE");
160 genrpt_long(zone,"Zone: ");
161 if (sphere == 0)
162 nadval = 27;
163 else
164 nadval = 83;
165 genrpt_long(nadval,"Datum: NAD");
166
167 /* initialize proper projection
168 -----------------------------*/
169 r_maj = table[0];
170 r_min = (sqrt(1.0 - table[1])) * table[0];
171
172 if (id == 1)
173 {
174 scale_fact = table[3];
175 center_lon = paksz(pakcz(table[2]),&iflg) * D2R;
176 if (iflg != 0)
177 return(iflg);
178 center_lat = paksz(pakcz(table[6]),&iflg) * D2R;
179 if (iflg != 0)
180 return(iflg);
181 false_east = table[7];
182 false_north = table[8];
183 tminvint(r_maj,r_min,scale_fact,center_lon,center_lat,false_east,
184 false_north);
185 }
186 else
187 if (id == 2)
188 {
189 lat1 = paksz(pakcz(table[5]),&iflg) * D2R;
190 if (iflg != 0)
191 return(iflg);
192 lat2 = paksz(pakcz(table[4]),&iflg) * D2R;
193 if (iflg != 0)
194 return(iflg);
195 center_lon = paksz(pakcz(table[2]),&iflg) * D2R;
196 if (iflg != 0)
197 return(iflg);
198 center_lat = paksz(pakcz(table[6]),&iflg) * D2R;
199 if (iflg != 0)
200 return(iflg);
201 false_east = table[7];
202 false_north = table[8];
203 lamccinvint(r_maj,r_min,lat1,lat2,center_lon,center_lat,false_east,
204 false_north);
205 }
206 else
207 if (id == 3)
208 {
209 center_lon = paksz(pakcz(table[2]),&iflg) * D2R;
210 if (iflg != 0)
211 return(iflg);
212 center_lat = paksz(pakcz(table[3]),&iflg) * D2R;
213 if (iflg != 0)
214 return(iflg);
215 false_east = table[4];
216 false_north = table[5];
217 polyinvint(r_maj,r_min,center_lon,center_lat,false_east,false_north);
218 }
219 else
220 if (id == 4)
221 {
222 scale_fact = table[3];
223 azimuth = paksz(pakcz(table[5]),&iflg) * D2R;
224 if (iflg != 0)
225 return(iflg);
226 lon_orig = paksz(pakcz(table[2]),&iflg) * D2R;
227 if (iflg != 0)
228 return(iflg);
229 lat_orig = paksz(pakcz(table[6]),&iflg) * D2R;
230 if (iflg != 0)
231 return(iflg);
232 false_east = table[7];
233 false_north = table[8];
234 mode = 1;
235 lon1 = 0.0;
236 lon2 = 0.0;
237 lat1 = 0.0; /* for mode > 0 setting lat1 and lat2 to zero do
238 make any difference in omerinvint */
239 lat2 = 0.0;
240 omerinvint(r_maj,r_min,scale_fact,azimuth,lon_orig,lat_orig,false_east,
241 false_north,lon1,lat1,lon2,lat2,mode);
242 }
243
244 return(OK);
245 }
246
247
248 /* State Plane inverse equations--mapping x,y to lat/long
249 -----------------------------------------------------*/
250 int stplninv(
251 double x, /* (O) X projection coordinate */
252 double y, /* (O) Y projection coordinate */
253 double *lon, /* (I) Longitude */
254 double *lat) /* (I) Latitude */
255 {
256 long iflg;
257
258 /* Inverse equations
259 -----------------*/
260 if (id == 1)
261 {
262 if ((iflg = tminv(x, y, lon, lat)) != 0)
263 return(iflg);
264 }
265 else
266 if (id == 2)
267 {
268 if ((iflg = lamccinv(x, y, lon, lat)) != 0)
269 return(iflg);
270 }
271 else
272 if (id == 3)
273 {
274 if ((iflg = polyinv(x, y, lon, lat)) != 0)
275 return(iflg);
276 }
277 else
278 if (id == 4)
279 {
280 if ((iflg = omerinv(x, y, lon, lat)) != 0)
281 return(iflg);
282 }
283
284 return(OK);
285 }