Debian Codegraph @

gctp/src/gctp.c

raw
1 /*******************************************************************************
2 NAME GCTP
3
4 VERSION PROGRAMMER DATE
5 ------- ---------- ----
6 T. Mittan 2-26-93 Conversion from FORTRAN to C
7 S. Nelson 12-14-93 Added assignments to inunit and
8 outunit for State Plane purposes.
9 c.1.0 S. Nelson 9-15-94 Added outdatum parameter call.
10 c.1.1 S. Nelson 11-94 Modified code so that UTM can accept
11 any spheroid code. Changed State
12 Plane legislated distance units,
13 for NAD83, to be consistant with
14 FORTRAN version of GCTP. Unit codes
15 are specified by state laws as of
16 2/1/92.
17 DaW 22-10-97 Changed all "return;" to "return(0);".
18 The SGI 64-bit platform complained about
19 no return values.
20
21 ALGORITHM REFERENCES
22
23 1. Snyder, John P., "Map Projections--A Working Manual", U.S. Geological
24 Survey Professional Paper 1395 (Supersedes USGS Bulletin 1532), United
25 State Government Printing Office, Washington D.C., 1987.
26
27 2. Snyder, John P. and Voxland, Philip M., "An Album of Map Projections",
28 U.S. Geological Survey Professional Paper 1453 , United State Government
29 Printing Office, Washington D.C., 1989.
30 *******************************************************************************/
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include "cproj.h"
34 #include "proj.h"
35
36
37 /* Function prototypes */
38
39 int init(long, long, char*, char*);
40 long untfz(long, long, double*);
41 void close_file();
42 int for_init(int, int, double*, int, char*, char*, int*,
43 long (*f[])(double, double, double*, double*));
44 int inv_init(int, int, double*, int, char*, char*, int*,
45 long (*i[])(double, double, double*, double*));
46
47
48 #define TRUE 1
49 #define FALSE 0
50
51 static long iter = 0; /* First time flag */
52 static long inpj[MAXPROJ + 1]; /* input projection array */
53 static long indat[MAXPROJ + 1]; /* input dataum array */
54 static long inzn[MAXPROJ + 1]; /* input zone array */
55 static double pdin[MAXPROJ + 1][15]; /* input projection parm array */
56 static long outpj[MAXPROJ + 1]; /* output projection array */
57 static long outdat[MAXPROJ + 1]; /* output dataum array */
58 static long outzn[MAXPROJ + 1]; /* output zone array */
59 static double pdout[MAXPROJ + 1][15]; /* output projection parm array */
60 static long (*for_trans[MAXPROJ + 1])();/* forward function pointer array*/
61 static long (*inv_trans[MAXPROJ + 1])();/* inverse function pointer array*/
62
63 /* Table of unit codes as specified by state
64 laws as of 2/1/92 for NAD 1983 State Plane
65 projection, 1 = U.S. Survey Feet, 2 = Meters,
66 5 = International Feet */
67
68 static long NADUT[134] = {1, 5, 1, 1, 5, 1, 1, 1, 1, 2, 1, 1, 1, 2, 1, 1, 2, 2,
69 1, 1, 5, 2, 1, 2, 5, 1, 2, 2, 2, 1, 1, 1, 5, 2, 1, 5,
70 2, 2, 5, 2, 1, 1, 5, 2, 2, 1, 2, 1, 2, 2, 1, 2, 2, 2};
71 /*
72 static long NAD83[134] = {101,102,5010,5300,201,202,203,301,302,401,402,403,
73 404,405,406,0000,501,502,503,600,700,901,902,903,1001,1002,
74 5101,5102,5103,5104,5105,1101,1102,1103,1201,1202,1301,1302,
75 1401,1402,1501,1502,1601,1602,1701,1702,1703,1801,1802,1900,
76 2001,2002,2101,2102,2103,2111,2112,2113,2201,2202,2203,2301,
77 2302,2401,2402,2403,2500,0000,0000,2600,0000,2701,2702,2703,
78 2800,2900,3001,3002,3003,3101,3102,3103,3104,3200,3301,3302,
79 3401,3402,3501,3502,3601,3602,3701,3702,3800,3900,0000,4001,
80 4002,4100,4201,4202,4203,4204,4205,4301,4302,4303,4400,4501,
81 4502,4601,4602,4701,4702,4801,4802,4803,4901,4902,4903,4904,
82 5001,5002,5003,5004,5005,5006,5007,5008,5009,5200,0000,5400};
83 */
84 int gctp(incoor,insys,inzone,inparm,inunit,indatum,ipr,efile,jpr,pfile,outcoor,
85 outsys,outzone,outparm,outunit,outdatum,fn27,fn83,iflg)
86
87 double *incoor; /* input coordinates */
88 long *insys; /* input projection code */
89 long *inzone; /* input zone number */
90 double *inparm; /* input projection parameter array */
91 long *inunit; /* input units */
92 long *indatum; /* input datum */
93 long *ipr; /* printout flag for error messages. 0=screen, 1=file,
94 2=both*/
95 char *efile; /* error file name */
96 long *jpr; /* printout flag for projection parameters 0=screen,
97 1=file, 2 = both*/
98 char *pfile; /* error file name */
99 double *outcoor; /* output coordinates */
100 long *outdatum; /* output datum */
101 long *outsys; /* output projection code */
102 long *outzone; /* output zone */
103 double *outparm; /* output projection array */
104 long *outunit; /* output units */
105 char fn27[]; /* file name of NAD 1927 parameter file */
106 char fn83[]; /* file name of NAD 1983 parameter file */
107 long *iflg; /* error flag */
108 {
109 double x; /* x coordinate */
110 double y; /* y coordinate */
111 double factor; /* conversion factor */
112 double lon; /* longitude */
113 double lat; /* latitude */
114 /*double temp; */ /* dummy variable */
115 double pakr2dm();
116 long i,j; /* loop counters */
117 long ininit_flag; /* input initilization flag */
118 long outinit_flag; /* output initilization flag */
119 /*long ind;*/ /* temporary var used to find state plane zone */
120 long unit; /* temporary unit variable */
121 double dummy[15]; /* temporary projection array */
122 int iflgval=0;
123 /* setup initilization flags and output message flags
124 ---------------------------------------------------*/
125 ininit_flag = FALSE;
126 outinit_flag = FALSE;
127 *iflg = 0;
128
129 *iflg = init(*ipr,*jpr,efile,pfile);
130 if ((int)*iflg != 0)
131 return(0);
132
133 /* check to see if initilization is required
134 only the first 13 projection parameters are currently used.
135 If more are added the loop should be increased.
136 ---------------------------------------------------------*/
137 if (iter == 0)
138 {
139 for (i = 0; i < MAXPROJ + 1; i++)
140 {
141 inpj[i] = 0;
142 indat[i] = 0;
143 inzn[i] = 0;
144 outpj[i] = 0;
145 outdat[i] = 0;
146 outzn[i] = 0;
147 for (j = 0; j < 15; j++)
148 {
149 pdin[i][j] = 0.0;
150 pdout[i][j] = 0.0;
151 }
152 }
153 ininit_flag = TRUE;
154 outinit_flag = TRUE;
155 iter = 1;
156 }
157 else
158 {
159 if ((int)*insys != GEO)
160 {
161 if ((inzn[(int)*insys] != *inzone) || (indat[(int)*insys] != *indatum) ||
162 (inpj[(int)*insys] != (int)*insys) || ((int)*insys == 2))
163 {
164 ininit_flag = TRUE;
165 }
166 else
167 for (i = 0; i < 13; i++)
168 if (pdin[(int)*insys][i] != inparm[i])
169 {
170 ininit_flag = TRUE;
171 break;
172 }
173 }
174 if ((int)*outsys != GEO)
175 {
176 if ((outzn[(int)*outsys] != *outzone) || (outdat[(int)*outsys] != *outdatum) ||
177 (outpj[(int)*outsys] != *outsys) || ((int)*outsys == 2))
178 {
179 outinit_flag = TRUE;
180 }
181 else
182 for (i = 0; i < 13; i++)
183 if (pdout[(int)*outsys][i] != outparm[i])
184 {
185 outinit_flag = TRUE;
186 break;
187 }
188 }
189 }
190
191 /* Check input and output projection numbers
192 ------------------------------------------*/
193 if (((int)*insys < 0) || ((int)*insys > MAXPROJ))
194 {
195 p_error("Insys is illegal","GCTP-INPUT");
196 *iflg = 1;
197 return(0);
198 }
199 if (((int)*outsys < 0) || ((int)*outsys > MAXPROJ))
200 {
201 p_error("Outsys is illegal","GCTP-OUTPUT");
202 *iflg = 2;
203 return(0);
204 }
205
206 /* find the correct conversion factor for units
207 ---------------------------------------------*/
208 unit = *inunit;
209
210 /* use legislated unit table for State Plane
211 -------------------------------------------*/
212 if (((int)*indatum == 0) && ((int)*insys == 2) && ((int)*inunit == 6))
213 unit = 1;
214 if (((int)*indatum == 8) && ((int)*insys == 2) && ((int)*inunit == 6))
215 unit = NADUT[(*inzone)/100];
216
217 /* find the factor unit conversions
218 --------------------------------*/
219 if ((int)*insys == GEO)
220 *iflg = untfz(unit,0,&factor);
221 else
222 *iflg = untfz(unit,2,&factor);
223 if ((int)*insys == SPCS)
224 *inunit = unit;
225 if ((int)*iflg != 0)
226 {
227 close_file();
228 return(0);
229 }
230
231 x = incoor[0] * factor;
232 y = incoor[1] * factor;
233
234 /* Initialize inverse transformation
235 ----------------------------------*/
236 if ((int)ininit_flag)
237 {
238 inpj[(int)*insys] = *insys;
239 indat[(int)*insys] = *indatum;
240 inzn[(int)*insys] = *inzone;
241
242 for (i = 0;i < 15; i++)
243 pdin[(int)*insys][i] = inparm[i];
244
245 if ((int)*insys == 1)
246 {
247
248 for( i = 2; i < 15; i++)
249 dummy[i] = inparm[i];
250
251 dummy[0] = 0;
252 dummy[1] = 0;
253 if (((int)*inzone != 0) || (inparm[0] == 0.0))
254 {
255 dummy[0] = 1.0e6 * (double)(6 * (int)*inzone -183);
256 if ( (int)*inzone >= 0)
257 dummy[1] = 4.0e7;
258 else
259 dummy[1] = -4.0e7;
260 }
261 else
262 {
263 dummy[0] = inparm[0];
264 dummy[1] = inparm[1];
265 }
266 inv_init((int)*insys,(int)*inzone,dummy,(int)*indatum,fn27,fn83,&iflgval,inv_trans);
267 }
268 else
269 inv_init((int)*insys,(int)*inzone,inparm,(int)*indatum,fn27,fn83,&iflgval,inv_trans);
270 *iflg=(long)iflgval;
271
272 if ((int)*iflg != 0)
273 {
274 close_file();
275 return(0);
276 }
277 }
278
279 /* Do actual transformations
280 --------------------------*/
281
282 /* Inverse transformations
283 ------------------------*/
284 if ((int)*insys == GEO)
285 {
286 lon = x;
287 lat = y;
288 }
289 else
290 if ((*iflg = inv_trans[(int)*insys](x, y, &lon, &lat)) != 0)
291 {
292 close_file();
293 return(0);
294 }
295
296 /* DATUM conversion should go here
297 --------------------------------*/
298
299 /*
300 The datum conversion facilities should go here
301 */
302
303 /* Initialize forward transformation
304 ----------------------------------*/
305 if ((int)outinit_flag)
306 {
307 outpj[(int)*outsys] = *outsys;
308 outdat[(int)*outsys] = *outdatum;
309 outzn[(int)*outsys] = *outzone;
310 for (i = 0;i < 15; i++)
311 pdout[(int)*outsys][i] = outparm[i];
312 if ((int)*outsys == 1)
313 {
314 for (i = 2; i < 15; i++)
315 dummy[i] = outparm[i];
316 dummy[0] = 0;
317 dummy[1] = 0;
318 if (outparm[0] == 0.0)
319 {
320 dummy[0] = pakr2dm(lon);
321 dummy[1] = pakr2dm(lat);
322 }
323 else
324 {
325 dummy[0] = outparm[0];
326 dummy[1] = outparm[1];
327 }
328 for_init((int)*outsys,(int)*outzone,dummy,(int)*outdatum,fn27,fn83,&iflgval,for_trans);
329 }
330 else
331 for_init((int)*outsys,(int)*outzone,outparm,(int)*outdatum,fn27,fn83,&iflgval,for_trans);
332
333 *iflg=(long)iflgval;
334
335 if ((int)*iflg != 0)
336 {
337 close_file();
338 return(0);
339 }
340 }
341
342 /* Forward transformations
343 ------------------------*/
344 if ((int)*outsys == GEO)
345 {
346 outcoor[0] = lon;
347 outcoor[1] = lat;
348 }
349 else
350 if ((*iflg = for_trans[(int)*outsys](lon, lat, &outcoor[0], &outcoor[1])) != 0)
351 {
352 close_file();
353 return(0);
354 }
355
356 /* find the correct conversion factor for units
357 ---------------------------------------------*/
358 unit = *outunit;
359 /* use legislated unit table
360 ----------------------------*/
361 if (((int)*outdatum == 0) && ((int)*outsys == 2) && ((int)*outunit == 6))
362 unit = 1;
363 if (((int)*outdatum == 8) && ((int)*outsys == 2) && ((int)*outunit == 6))
364 unit = NADUT[(*outzone)/100];
365
366 if ((int)*outsys == GEO)
367 *iflg = untfz(0,unit,&factor);
368 else
369 *iflg = untfz(2,unit,&factor);
370
371 if ((int)*outsys == SPCS)
372 *outunit = unit;
373
374 outcoor[0] *= factor;
375 outcoor[1] *= factor;
376 close_file();
377 return(0);
378 }