Debian Codegraph @

gctp/src/isinusfor.c

raw
1 /******************************************************************************
2 NAME ISINUSFOR.C
3
4 PURPOSE: Integerized Sinusoidal Library Functions - library routines to
5 perform mapping to and from the Integerized Sinusoidal. These
6 functions perform the mapping from geographic coordinates
7 (longitude/latitude) to the map projection coordinates (x/y).
8
9 PROGRAMMER DATE REASON
10 ---------- ---- ------
11 Robert Wolfe (STX) 1-2-97 Initial version.
12 Raj Gejjagaraguppe (ARC) 1-21-97 Modified and added code to make
13 this work with GCTP software.
14
15
16 Usage Notes:
17 1. The following functions are available:
18
19 isinusforinit - Initialize integerized sinusoidal transformations
20 isinusfor - Forward mapping; converts geographic coordinates
21 (longitude/latitude) to map projection
22 coordinates (x/y)
23
24 2. Since there are discontinuities at the top and bottom of each zone
25 within the integerized sinusoidal grid care should be taken when
26 mapping points near the edges of the zones. Also, care should be taken
27 near the discontinuity at the most eastward and westward portions of
28 the projection (180 degrees from the central meridian).
29
30 3. Latitudes are expected to in the range [-'HALFPI' to 'HALFPI'].
31
32 4. Longitudes are expected to be in the range [-'TWOPI' to 'TWOPI'].
33
34 5. The justify flag is used to indicate what to do with zones with an
35 odd number of columns. If it has a value of 0 or 1 it indicates the
36 extra column is on the right (zero) or left (one) of the projection
37 y axis. If the flag is set to 2 the columns are calculated so there
38 are always an even number of column in each zone.
39
40 6. The origin is located at the equator which is at the bottom of the first
41 zone above the equator and the top of the first zone below the equator.
42
43 7. These routines were designed as an GCTP (General Cartographic
44 Transformation Package) interface to the 'isin' library.
45
46 *****************************************************************************/
47
48 /* #define NO_OUTPUT */ /* if defined, error messages are not written */
49 /* #define CHECK_EDGE */ /* if defined, statistics are gathered on how
50 * close the column calculations are to being
51 * dependent on the machine precision */
52
53
54 #include <stdio.h>
55 #include <stdlib.h>
56 #include <limits.h>
57 #include <math.h>
58
59 #ifndef NO_OUTPUT
60 #include <stdio.h>
61 #endif
62
63 #include "isin.h"
64
65 #ifndef M_PI
66 #define PI 3.141592653589793 /* Circular constant */
67 #else
68 #define PI M_PI
69 #endif
70 #define TWOPI (2.0 * PI)
71 #define HALFPI (PI / 2.0)
72 #define TWOPI_INV (1.0 / (2.0 * PI))
73
74 #define NROW_MAX (360 * 3600) /* Maximum number of rows (zones) */
75 #define NZONE_MAX (360 * 3600) /* Maximum number of longitudinal zones */
76 #define EPS_SPHERE (1.0e-10) /* Minimum sphere radius */
77 #define EPS_CNVT 0.01 /* Doubles must be within this of an integer to be
78 * valid */
79 #define ISIN_KEY 212589603 /* Key to verify correct data structure */
80
81
82 /* Local error handling routine */
83
84 static void error(const char *routine, const char *text) {
85 #ifndef NO_OUTPUT
86 fprintf(stderr, " error (isinusfor.c/%s : %s\n", routine, text);
87 #endif
88 /* exit(EXIT_FAILURE); */
89 }
90
91 /* Error Messages */
92
93 static error_t ISIN_BADALLOC = { -3, "memory allocation"};
94 static error_t ISIN_BADPARAM = { -4, "invalid parameter"};
95 static error_t ISIN_BADHANDLE = { -5, "invalid handle"};
96 static error_t ISIN_BADKEY = { -6, "invalid key"};
97
98
99 /* Local data structure for 'Isin' library */
100
101 static Isin_t *isin = NULL;
102
103 /* Functions */
104
105
106 int isinusforinit(double sphere, double lon_cen_mer, double false_east,
107 double false_north, double dzone, double djustify)
108 /*
109 !C******************************************************************************
110 !Description: isinusforinit (initialize mapping) initializes the integerized
111 sinusoidal transformations.
112
113 !Input Parameters:
114 sphere radius (meters)
115 longitude of central meridian (radians)
116 easting at projection origin (meters)
117 northing at projection origin (meters)
118 number of longitudinal zones
119 justify flag; flag to indicate what to do with
120 rows with an odd number of columns:
121 0.0 - indicates the extra column is on the right
122 of the projection y axis;
123 1.0 - indicates the extra column is on the left
124 of the projection y axis;
125 2.0 - calculate an even number of columns
126
127 !Output Parameters:
128 (none)
129
130 !Team Unique Header:
131
132 ! Usage Notes:
133 1. The sphere radius must not be smaller than 'EPS_SPHERE'.
134 2. The longitude must be in the range [-'TWOPI' to 'TWOPI'].
135 3. The number of longitudinal zones must be a positive multiple of two
136 and no more than 'NZONE_MAX'.
137 4. The number of longitudinal zones and the justify flag must be within
138 'EPS_CNVT' of an integer.
139
140 !END****************************************************************************
141 */
142 {
143 long nzone; /* Number of longitudinal zones */
144 int ijustify; /* Justify flag (see above) */
145 int istat; /* Status returned from 'Isin' functions */
146
147 /* Check to see if this data set was already initialized; if it was,
148 * free the data structure so it can be re-used */
149
150 if (isin != NULL) {
151 istat = Isin_for_free(isin);
152 if (istat != ISIN_SUCCESS) {
153 error("isinusforinit", "bad return from Isin_for_free");
154 return ISIN_ERROR;
155 }
156 }
157
158 /* Check the input parameters */
159
160 if (sphere <= 0.0) {
161 error("isinusforinit", "bad parameter; sphere radius invalid");
162 return ISIN_ERROR;
163 }
164
165 if (lon_cen_mer < -TWOPI || lon_cen_mer > TWOPI) {
166 error("isinusforinit", "bad parameter; longitude of central meridian invalid");
167 return ISIN_ERROR;
168 }
169
170 if (dzone < (2.0 - EPS_CNVT) ||
171 dzone > ((double) NZONE_MAX + EPS_CNVT)) {
172 error("isinusforinit", "bad parameter; nzone out of range");
173 return ISIN_ERROR;
174 }
175
176 nzone = (long) (dzone + EPS_CNVT);
177 if (fabs(dzone - nzone) > EPS_CNVT) {
178 error("isinusforinit", "bad parameter; nzone not near an integer value");
179 return ISIN_ERROR;
180 }
181
182 if ((nzone % 2) != 0) {
183 error("isinusforinit", "bad parameter; nzone not multiple of two");
184 return ISIN_ERROR;
185 }
186
187 if (djustify < -EPS_CNVT ||
188 djustify > (2.0 + EPS_CNVT)) {
189 error("isinusforinit", "bad parameter; ijustify out of range");
190 return ISIN_ERROR;
191 }
192
193 ijustify = djustify + EPS_CNVT;
194 if (fabs(djustify - ijustify) > EPS_CNVT) {
195 error("isinusforinit", "bad parameter; ijustify not near an integer value");
196 return ISIN_ERROR;
197 }
198
199 /* Initialize the projection */
200
201 isin = Isin_for_init(sphere, lon_cen_mer, false_east, false_north,
202 nzone, ijustify);
203 if (isin == NULL) {
204 error("Isin_for_init", "bad return from Isin_for_init");
205 return ISIN_ERROR;
206 }
207
208 return ISIN_SUCCESS;
209 }
210
211
212 Isin_t *Isin_for_init(double sphere,
213 double lon_cen_mer,
214 double false_east, double false_north,
215 long nrow, int ijustify)
216 /*
217 !C******************************************************************************
218 !Description: Isin_for_init (initialize mapping) initializes the integerized
219 sinusoidal transformations by calculating constants and a short-cut
220 lookup table.
221
222 !Input Parameters:
223 sphere sphere radius (user's units)
224 lon_cen_mer longitude of central meridian (radians)
225 false_east easting at projection origin (user's units)
226 false_north northing at projection origin (user's units)
227 nrow number of rows (longitudinal zones)
228 ijustify justify flag; flag to indicate what to do with rows with an
229 odd number of columns;
230 0 = indicates the extra column is on the right
231 of the projection y axis;
232 1 = indicates the extra column is on the left
233 of the projection y axis;
234 2 = calculate an even number of columns
235
236 !Output Parameters:
237 (returns) a handle for this instance of the integerized sinusoidal
238 projection or NULL for error
239
240 !Team Unique Header:
241
242 ! Usage Notes:
243 1. The sphere radius must not be smaller than 'EPS_SPHERE'.
244 2. The longitude must be in the range [-'TWOPI' to 'TWOPI'].
245 3. The number of rows must be a multiple of two and no more than 'NROW_MAX'.
246
247 !END****************************************************************************
248 */
249 {
250 Isin_t *thisIsin; /* 'isin' data structure */
251 Isin_row_t *row; /* current row data structure */
252 long irow; /* row (zone) index */
253 double clat; /* central latitude of the row */
254 long ncol_cen; /* number of columns in the central row of the grid
255 * (at the equator) */
256
257 #ifdef CHECK_EDGE
258 double dcol; /* delta column (normalized by number of columns) */
259 double dcol_min, /* minimum delta column */
260 double log2_dcol_min; /* log base 2 of minimum delta column */
261
262 dcol_min = 1.0;
263 #endif
264
265 /* Check input parameters */
266
267 if (sphere < EPS_SPHERE)
268 {Isin_error(&ISIN_BADPARAM, "Isin_for_init"); return NULL;}
269
270 if (lon_cen_mer < -TWOPI || lon_cen_mer > TWOPI)
271 {Isin_error(&ISIN_BADPARAM, "Isin_for_init"); return NULL;}
272 if (lon_cen_mer < PI) lon_cen_mer += TWOPI;
273 if (lon_cen_mer >= PI) lon_cen_mer -= TWOPI;
274
275 if (nrow < 2 || nrow > NROW_MAX)
276 {Isin_error(&ISIN_BADPARAM, "Isin_for_init"); return NULL;}
277 if ((nrow % 2) != 0)
278 {Isin_error(&ISIN_BADPARAM, "Isin_for_init"); return NULL;}
279
280 if (ijustify < 0 || ijustify > 2)
281 {Isin_error(&ISIN_BADPARAM, "Isin_for_init"); return NULL;}
282
283 /* Allocate 'isin' data structure */
284
285 thisIsin = (Isin_t *) malloc(sizeof(Isin_t));
286 if (thisIsin == NULL)
287 {Isin_error(&ISIN_BADALLOC, "Isin_for_init"); return NULL;}
288
289 /* Initialize data structure */
290
291 thisIsin->key = (long) NULL;
292 thisIsin->false_east = false_east;
293 thisIsin->false_north = false_north;
294 thisIsin->sphere = sphere;
295 thisIsin->sphere_inv = 1.0 / sphere;
296 thisIsin->ang_size_inv = ((double) nrow) / PI;
297 thisIsin->nrow = nrow;
298 thisIsin->nrow_half = nrow / 2;
299 thisIsin->lon_cen_mer = lon_cen_mer;
300 thisIsin->ref_lon = lon_cen_mer - PI;
301 if (thisIsin->ref_lon < -PI) thisIsin->ref_lon += TWOPI;
302 thisIsin->ijustify = ijustify;
303
304 /* Allocate space for information about each row */
305
306 thisIsin->row = (Isin_row_t *) malloc(thisIsin->nrow_half * sizeof(Isin_row_t));
307 if (thisIsin->row == NULL) {
308 free(thisIsin);
309 Isin_error(&ISIN_BADALLOC, "Isin_for_init");
310 return NULL;
311 }
312
313 /* Do calculations for each row; calculations are only done for half
314 * the rows because of the symmetry between the rows above the
315 * equator and the ones below */
316
317 row = thisIsin->row;
318 for (irow = 0; irow < thisIsin->nrow_half; irow++, row++) {
319
320 /* Calculate latitude at center of row */
321
322 clat = HALFPI * (1.0 - ((double) irow + 0.5) / thisIsin->nrow_half);
323
324 /* Calculate number of columns per row */
325
326 if (ijustify < 2)
327 row->ncol = (long) ((2.0 * cos(clat) * nrow) + 0.5);
328 else { /* make the number of columns even */
329 row->ncol = (long) ((cos(clat) * nrow) + 0.5);
330 row->ncol *= 2;
331 }
332
333 #ifdef CHECK_EDGE
334 /* Check to be sure the are no less then three columns per row and that
335 * there are exactly three columns at the poles */
336 if (ijustify < 2) {
337 if (row->ncol < 3 || (irow == 0 && row->ncol != 3))
338 printf(" irow = %d ncol = %d\n", irow, row->ncol);
339 } else {
340 if (row->ncol < 6 || (irow == 0 && row->ncol != 6))
341 printf(" irow = %d ncol = %d\n", irow, row->ncol);
342 }
343 #endif
344
345 /* Must have at least one column */
346
347 if (row->ncol < 1) row->ncol = 1;
348
349 #ifdef CHECK_EDGE
350
351 /* Calculate the minimum delta column (normalized by the number of
352 * columns in the row) */
353
354 if (ijustify < 2)
355 dcol = fabs((2.0 * cos(clat) * nrow) + 0.5 - row->ncol);
356 else
357 dcol = 2.0 * fabs((cos(clat) * nrow) + 0.5 - (row->ncol / 2));
358 dcol = dcol / row->ncol;
359 if (dcol < dcol_min) dcol_min = dcol;
360
361 if (ijustify < 2) {
362 dcol = fabs((2.0 * cos(clat) * nrow) + 0.5 - (row->ncol + 1));
363 dcol = dcol / (row->ncol + 1);
364 } else {
365 dcol = 2.0 * fabs((cos(clat) * nrow) + 0.5 - ((row->ncol / 2) + 1));
366 dcol = dcol / (row->ncol + 2);
367 }
368 if (dcol < dcol_min) dcol_min = dcol;
369 #endif
370
371 /* Save the inverse of the number of columns */
372
373 row->ncol_inv = 1.0 / ((double) row->ncol);
374
375 /* Calculate the column number of the column whose left edge touches the
376 * central meridian */
377 if (ijustify == 1)
378 row->icol_cen = (row->ncol + 1) / 2;
379 else
380 row->icol_cen = row->ncol / 2;
381
382 } /* for (irow... */
383
384 /* Get the number of columns at the equator */
385 ncol_cen = thisIsin->row[thisIsin->nrow_half - 1].ncol;
386
387 #ifdef CHECK_EDGE
388
389 /* Print the minimum delta column and its base 2 log */
390
391 log2_dcol_min = log(dcol_min) / log(2.0);
392 printf(" dcol_min = %g log2_dcol_min = %g\n", dcol_min, log2_dcol_min);
393
394 /* Check to be sure the number of columns at the equator is twice the
395 * number of rows */
396 if (ncol_cen != nrow * 2)
397 printf(" ncol_cen = %d nrow = %d\n", ncol_cen, nrow);
398 #endif
399
400 /* Calculate the distance at the equator between
401 * the centers of two columns (and the inverse) */
402
403 thisIsin->col_dist = (TWOPI * sphere) / ncol_cen;
404 thisIsin->col_dist_inv = ncol_cen / (TWOPI * sphere);
405
406 /* Give the data structure a valid key */
407
408 thisIsin->key = ISIN_KEY;
409
410 /* All done */
411 return thisIsin;
412 }
413
414
415
416
417 int isinusfor(double lon, double lat, double *x, double *y)
418 /*
419 !C******************************************************************************
420 !Description: isinusfor (forward mapping) converts geographic
421 coordinates ('lon', 'lat') to map projection coordinates ('x', 'y').
422
423 !Input Parameters:
424 lon longitude (radians)
425 lat latitude (radians)
426
427 !Output Parameters:
428 x easting in map projection (same units as 'sphere')
429 y northing in map projection (same units as 'sphere')
430
431 !Team Unique Header:
432
433 ! Usage Notes:
434 1. 'isinusforinit' must have been previously called.
435 2. The longitude must be in the range [-'TWOPI' to 'TWOPI'].
436 3. The latitude must be in the range [-'HALFPI' to 'HALFPI'].
437
438 !END****************************************************************************
439 */
440 {
441 int istat; /* Status returned from 'Isin_fwd' function */
442
443 /* Call 'Isin_fwd' */
444
445 istat = Isin_fwd(isin, lon, lat, x, y);
446 if (istat != ISIN_SUCCESS) {
447 error("isinusfor", "bad return from Isin_fwd");
448 return ISIN_ERROR;
449 }
450
451 return ISIN_SUCCESS;
452 }
453
454
455
456
457
458
459 int Isin_fwd(const Isin_t *thisIsin,
460 double lon, double lat,
461 double *x, double *y)
462 /*
463 !C******************************************************************************
464 !Description: Isin_fwd (forward mapping) converts geographic
465 coordinates ('lon', 'lat') to map projection coordinates ('x', 'y').
466
467 !Input Parameters:
468 thisIsin handle for this instance of the integerized sinusoidal
469 projection
470 lon longitude (radians)
471 lat latitude (radians)
472
473 !Output Parameters:
474 x easting in map projection (same units as 'sphere')
475 y northing in map projection (same units as 'sphere')
476 (returns) status:
477 ISIN_SUCCESS - normal return
478 ISIN_ERANGE - longitude or latitude not in range
479 ISIN_ERROR - error return
480
481 !Team Unique Header:
482
483 ! Usage Notes:
484 1. 'Isin_for_init' must have been previously called for the handle.
485 2. The longitude must be in the range [-'TWOPI' to 'TWOPI'].
486 3. The latitude must be in the range [-'HALFPI' to 'HALFPI'].
487
488 !END****************************************************************************
489 */
490 {
491 double row, col; /* Row (zone) and column; column is relative to
492 * central; 0.5 is the center of a row or column */
493 double flon; /* Fractional longitude (multiples of PI) */
494 long irow; /* Integer row (zone) number */
495
496 /* Check input parameters */
497
498 *x = 0.0;
499 *y = 0.0;
500
501 if (thisIsin == NULL) return Isin_error(&ISIN_BADHANDLE, "Isin_fwd");
502 if (thisIsin->key != ISIN_KEY) return Isin_error(&ISIN_BADKEY, "Isin_fwd");
503 if (lon < -TWOPI || lon > TWOPI) return ISIN_ERANGE;
504 if (lat < -HALFPI || lat > HALFPI) return ISIN_ERANGE;
505
506 /* Northing */
507
508 *y = thisIsin->false_north + (lat * thisIsin->sphere);
509
510 /* Integer row number */
511
512 row = (HALFPI - lat) * thisIsin->ang_size_inv;
513 irow = (long) row;
514 if (irow >= thisIsin->nrow_half) irow = (thisIsin->nrow - 1) - irow;
515 if (irow < 0) irow = 0;
516
517 /* Fractional longitude */
518
519 flon = (lon - thisIsin->ref_lon) * TWOPI_INV;
520 if (flon < 0.0) flon += (1 - (long) flon);
521 if (flon > 1.0) flon -= (long) flon;
522
523 /* Column number (relative to center) */
524
525 col = (thisIsin->row[irow].ncol * flon) - thisIsin->row[irow].icol_cen;
526
527 /* Easting */
528
529 *x = thisIsin->false_east + (thisIsin->col_dist * col);
530
531 return ISIN_SUCCESS;
532 }
533
534
535
536 int Isin_for_free(Isin_t *thisIsin)
537 /*
538 !C******************************************************************************
539 !Description: Isin_for_free (free) deallocates the 'isin' data structure and
540 array memory.
541
542 !Input Parameters:
543 thisIsin handle for this instance of the integerized sinusoidal
544 projection
545 !Output Parameters:
546 (returns) status:
547 ISIN_SUCCESS - normal return
548 ISIN_ERROR - error return
549
550 !Team Unique Header:
551
552 ! Usage Notes:
553 1. 'Isin_for_init' must have been previously called for the handle.
554
555 !END****************************************************************************
556 */
557 {
558 if (thisIsin == NULL) return Isin_error(&ISIN_BADHANDLE, "Isin_for_free");
559 if (thisIsin->key != ISIN_KEY) return Isin_error(&ISIN_BADKEY, "Isin_for_free");
560
561 /* Set the key to NULL */
562 thisIsin->key = (long) NULL;
563
564 /* Free the memory */
565 free(thisIsin->row);
566 thisIsin->row = NULL;
567 free(thisIsin);
568 thisIsin = NULL;
569
570 return ISIN_SUCCESS;
571 }
572
573
574 static int Isin_error(const error_t *err, const char *routine)
575 /*
576 !C******************************************************************************
577
578 !Description: Private function to handle errors.
579
580 !Input Parameters:
581 err Error structure
582 routine String containing name of routine where error occurred
583
584 !Output Parameters:
585 (returns) status:
586 ISIN_ERROR - normal return
587
588 !Team Unique Header:
589
590 !Usage Notes: (none)
591
592 !END****************************************************************************
593 */
594 {
595 #ifndef NO_OUTPUT
596 fprintf(stderr, " error (isinusfor.c/%s) : (%i) %s\n", routine, err->num, err->str);
597 #endif
598
599 return ISIN_ERROR;
600 }