-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmgrs.cpp
1365 lines (1297 loc) · 44.1 KB
/
mgrs.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (c) 1994-2009 Georgia Tech Research Corporation, Atlanta, GA
// This file is part of FalconView(tm).
// FalconView(tm) is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// FalconView(tm) is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
// You should have received a copy of the GNU Lesser General Public License
// along with FalconView(tm). If not, see <http://www.gnu.org/licenses/>.
// FalconView(tm) is a trademark of Georgia Tech Research Corporation.
/***************************************************************************/
/* RSC IDENTIFIER: MGRS
*
* ABSTRACT
*
* This component converts between geodetic coordinates (latitude and
* longitude) and Military Grid Reference System (MGRS) coordinates.
*
* ERROR HANDLING
*
* This component checks parameters for valid values. If an invalid value
* is found, the error code is combined with the current error code using
* the bitwise or. This combining allows multiple error codes to be
* returned. The possible error codes are:
*
* MGRS_NO_ERROR : No errors occurred in function
* MGRS_LAT_ERROR : Latitude outside of valid range
* (-90 to 90 degrees)
* MGRS_LON_ERROR : Longitude outside of valid range
* (-180 to 360 degrees)
* MGRS_STR_ERROR : An MGRS string error: string too long,
* too short, or badly formed
* MGRS_PRECISION_ERROR : The precision must be between 0 and 5
* inclusive.
* MGRS_A_ERROR : Semi-major axis less than or equal to zero
* MGRS_INV_F_ERROR : Inverse flattening outside of valid range
* (250 to 350)
* MGRS_EASTING_ERROR : Easting outside of valid range
* (100,000 to 900,000 meters for UTM)
* (0 to 4,000,000 meters for UPS)
* MGRS_NORTHING_ERROR : Northing outside of valid range
* (0 to 10,000,000 meters for UTM)
* (0 to 4,000,000 meters for UPS)
* MGRS_ZONE_ERROR : Zone outside of valid range (1 to 60)
* MGRS_HEMISPHERE_ERROR : Invalid hemisphere ('N' or 'S')
*
* REUSE NOTES
*
* MGRS is intended for reuse by any application that does conversions
* between geodetic coordinates and MGRS coordinates.
*
* REFERENCES
*
* Further information on MGRS can be found in the Reuse Manual.
*
* MGRS originated from : U.S. Army Topographic Engineering Center
* Geospatial Information Division
* 7701 Telegraph Road
* Alexandria, VA 22310-3864
*
* LICENSES
*
* None apply to this component.
*
* RESTRICTIONS
*
*
* ENVIRONMENT
*
* MGRS was tested and certified in the following environments:
*
* 1. Solaris 2.5 with GCC version 2.8.1
* 2. Windows 95 with MS Visual C++ version 6
*
* MODIFICATIONS
*
* Date Description
* ---- -----------
* 16-11-94 Original Code
* 15-09-99 Reengineered upper layers
*
*/
/***************************************************************************/
/*
* INCLUDES
*/
#include <ctype.h>
#include <math.h>
#include <stdio.h>
#include <string.h>
#include "ups.h"
#include "utm.h"
#include "mgrs.h"
/*
* ctype.h - Standard C character handling library
* math.h - Standard C math library
* stdio.h - Standard C input/output library
* string.h - Standard C string handling library
* ups.h - Universal Polar Stereographic (UPS) projection
* utm.h - Universal Transverse Mercator (UTM) projection
* mgrs.h - function prototype error checking
*/
/***************************************************************************/
/*
* Linux Build Details
*/
#ifndef _WIN32
#define sprintf_s snprintf
#define sscanf_s sscanf
static int strcpy_s(char* dst, size_t dstSize, const char* src)
{
strcpy(dst, src);
return 0;
}
static int strncpy_s(char* dst, size_t dstSize, const char* src, size_t srcSize)
{
strcpy(dst, src);
return 0;
}
#endif // _WIN32
/***************************************************************************/
/*
* GLOBAL DECLARATIONS
*/
#define DEGRAD 0.017453292519943295 /* PI/180 */
#define R3 0.052359877559829890 /* RADIANS FOR 3 DEGREES */
#define R8 0.139626340159546400 /* RADIANS FOR 8 DEGREES */
#define R9 0.157079632679489700 /* RADIANS FOR 9 DEGREES */
#define R21 0.366519142918809200 /* RADIANS FOR 21 DEGREES */
#define R33 0.575958653158128800 /* RADIANS FOR 33 DEGREES */
#define R56 0.977384381116824600 /* RADIANS FOR 56 DEGREES */
#define R64 1.117010721276371000 /* RADIANS FOR 64 DEGREES */
#define R72 1.256637061435917000 /* RADIANS FOR 72 DEGREES */
#define R80 1.396263401595464000 /* RADIANS FOR 80 DEGREES */
#define UPS_SOUTH 3 /* UPS COORDINATE IN SOUTHERN HEMISPHERE */
#define UPS_NORTH 2 /* UPS COORDINATE IN NORTHERN HEMISPHERE */
#define UTM 1 /* UTM COORDINATE */
#define ALBET "ABCDEFGHIJKLMNOPQRSTUVWXYZ" /* ALPHABET */
#define LETTER_A 0 /* ARRAY INDEX FOR LETTER A */
#define LETTER_B 1 /* ARRAY INDEX FOR LETTER B */
#define LETTER_C 2 /* ARRAY INDEX FOR LETTER C */
#define LETTER_D 3 /* ARRAY INDEX FOR LETTER D */
#define LETTER_E 4 /* ARRAY INDEX FOR LETTER E */
#define LETTER_H 7 /* ARRAY INDEX FOR LETTER H */
#define LETTER_I 8 /* ARRAY INDEX FOR LETTER I */
#define LETTER_J 9 /* ARRAY INDEX FOR LETTER J */
#define LETTER_L 11 /* ARRAY INDEX FOR LETTER L */
#define LETTER_M 12 /* ARRAY INDEX FOR LETTER M */
#define LETTER_N 13 /* ARRAY INDEX FOR LETTER N */
#define LETTER_O 14 /* ARRAY INDEX FOR LETTER O */
#define LETTER_P 15 /* ARRAY INDEX FOR LETTER P */
#define LETTER_Q 16 /* ARRAY INDEX FOR LETTER Q */
#define LETTER_R 17 /* ARRAY INDEX FOR LETTER R */
#define LETTER_S 18 /* ARRAY INDEX FOR LETTER S */
#define LETTER_U 20 /* ARRAY INDEX FOR LETTER U */
#define LETTER_V 21 /* ARRAY INDEX FOR LETTER V */
#define LETTER_W 22 /* ARRAY INDEX FOR LETTER W */
#define LETTER_X 23 /* ARRAY INDEX FOR LETTER X */
#define LETTER_Y 24 /* ARRAY INDEX FOR LETTER Y */
#define LETTER_Z 25 /* ARRAY INDEX FOR LETTER Z */
#define RND1 0.1e0 /* ROUND TO NEAREST .1 */
#define RND5 0.5e0 /* ROUND TO NEAREST .5 */
#define EOLN '\0' /* END OF STRING CHARACTER */
#define BLANK ' ' /* BLANK CHARACTER */
#define MGRS_LETTERS 3 /* NUMBER OF LETTERS IN MGRS */
#define NUM_OFFSET 48 /* USED TO CONVERT NUMBERS TO LETTERS */
#define ONEHT 100000.e0 /* ONE HUNDRED THOUSAND */
#define TWOMIL 2000000.e0 /* TWO MILLION */
#define EIGHT 800000.e0 /* EIGHT HUNDRED THOUSAND */
#define ONE3HT 1300000.e0 /* ONE MILLION THREE HUNDRED THOUSAND */
#define ZERO 0.e0 /* ZERO */
#define TEN 10.e0 /* TEN */
#define TRUE 1 /* CONSTANT VALUE FOR TRUE VALUE */
#define FALSE 0 /* CONSTANT VALUE FOR FALSE VALUE */
#ifndef PI
#define PI 3.14159265358979323e0 /* PI */
#endif
#define PI_OVER_2 (PI / 2.0e0)
#define NUM "01234567890" /* NUMBERS */
#define MAXALBET 25 /* LAST INDEX IN ALPHABET ARRAY(0-25) */
#define MAXNUMBER 10 /* LAST INDEX IN NUMBER ARRAY(0-9) */
#define MGRS_ZONE_AND_LETTERS 5 /* NUM. OF CHARS. IN ZONE AND LETTERS */
#define MGRS_MINIMUM 9 /* MINIMUM NUMBER OF CHARS FOR MGRS */
#define MGRS_MAXIMUM 15 /* MAXIMUM NUMBER OF CHARS FOR MGRS */
#define MIN_EASTING 100000
#define MAX_EASTING 900000
#define MIN_NORTHING 0
#define MAX_NORTHING 10000000
#define MAX_PRECISION 5 /* Maximum precision of easting & northing */
#define MIN_UTM_LAT ( (-80 * PI) / 180.0 ) /* -80 degrees in radians */
#define MAX_UTM_LAT ( (84 * PI) / 180.0 ) /* 84 degrees in radians */
#define MIN_EAST_NORTH 0
#define MAX_EAST_NORTH 4000000
/* Ellipsoid parameters, default to WGS 84 */
double MGRS_a = 6378137.0; /* Semi-major axis of ellipsoid in meters */
double MGRS_f = 1 / 298.257223563; /* Flattening of ellipsoid */
double MGRS_recpf = 298.257223563;
char MGRS_Ellipsoid_Code[3] = {'W','E',0};
const char* CLARKE_1866 = "CC";
const char* CLARKE_1880 = "CD";
const char* BESSEL_1841 = "BR";
/*
* CLARKE_1866 : Ellipsoid code for CLARKE_1866
* CLARKE_1880 : Ellipsoid code for CLARKE_1880
* BESSEL_1841 : Ellipsoid code for BESSEL_1841
*/
/***************************************************************************/
/*
* FUNCTIONS
*/
void UTMSET(long izone,
long* ltrlow,
long* ltrhi,
double *fnltr)
{ /* BEGIN UTMSET */
/*
* izone : Zone number
* ltrlow : 2nd letter low number
* ltrhi : 2nd letter high number
* fnltr : False northing
*/
long iset; /* Set number (1-6) based on UTM zone number */
long igroup; /* Group number (1-2) based on ellipsoid code and zone */
iset = 1;
while (((izone - iset) / 6) * 6 + iset != izone)
{
iset = iset + 1;
if (iset > 6)
{
return;
}
}
igroup = 1;
if (!strcmp(MGRS_Ellipsoid_Code, CLARKE_1866) || !strcmp(MGRS_Ellipsoid_Code, CLARKE_1880) || !strcmp(MGRS_Ellipsoid_Code, BESSEL_1841))
{
igroup = 2;
}
else if (!strcmp(MGRS_Ellipsoid_Code, CLARKE_1866) && (izone >= 47) && (izone <= 55))
{
igroup = 1;
}
if ((iset == 1) || (iset == 4))
{
*ltrlow = LETTER_A;
*ltrhi = LETTER_H;
}
else if ((iset == 2) || (iset == 5))
{
*ltrlow = LETTER_J;
*ltrhi = LETTER_R;
}
else if ((iset == 3) || (iset == 6))
{
*ltrlow = LETTER_S;
*ltrhi = LETTER_Z;
}
if (igroup == 1)
{
*fnltr = ZERO;
if ((iset % 2) == 0)
{
*fnltr = 1500000.e0;
}
}
else if (igroup == 2)
{
*fnltr = 1000000.0e0;
if ((iset % 2) == 0)
{
*fnltr = 500000.e0;
}
}
return;
} /* END OF UTMSET */
void UTMLIM(long* n,
double sphi,
long izone,
double *spsou,
double *spnor,
double *sleast,
double *slwest)
{ /* BEGIN UTMLIM */
/*
* n : 1st letter number for MGRS
* sphi : Latitude in radians
* izone : Zone number
* spsou : South latitude limit
* spnor : North latitude limit
* sleast : East longitude limit
* slwest : West longitude limit
*/
double slcm; /* Central meridian - Longitude of origin */
double temp; /* Temporary variable */
long icm; /* Central meridian */
long isphi; /* South latitude limit */
if (*n <= LETTER_A)
{
temp = ((sphi + R80) / (R8)) + 2;
temp = temp + .00000001;
*n = (long)temp;
if (*n > LETTER_H)
{
*n = *n + 1;
}
if (*n > LETTER_N)
{
*n = *n + 1;
}
if (*n >= LETTER_Y)
{
*n = LETTER_X;
}
if ((*n == LETTER_M) && (sphi == ZERO))
{
*n = LETTER_N;
}
isphi = (*n - 3) * 8 - 80;
}
else
{
isphi = (*n - 3) * 8 - 80;
*n = *n - 1;
}
if (*n > LETTER_H)
{
isphi = isphi - 8;
}
if (*n > LETTER_N)
{
isphi = isphi - 8;
}
*spsou = (double)(isphi)* DEGRAD;
*spnor = *spsou + R8;
if (*n == LETTER_X)
{
*spnor = *spsou + 12.e0 * DEGRAD;
}
icm = izone * 6 - 183;
slcm = (double)icm * DEGRAD;
*sleast = slcm + R3;
*slwest = slcm - R3;
if ((izone < 31) || (izone > 37))
{
return;
}
if (*n < LETTER_V)
{
return;
}
if ((*n == LETTER_V) && (izone == 31))
{
*sleast = R3;
}
if ((*n == LETTER_V) && (izone == 32))
{
*slwest = R3;
}
if (*n < LETTER_X)
{
return;
}
if (izone == 31)
{
*sleast = R9;
}
if (izone == 33)
{
*slwest = R9;
*sleast = R21;
}
if (izone == 35)
{
*slwest = R21;
*sleast = R33;
}
if (izone == 37)
{
*slwest = R33;
}
return;
} /* END OF UTMLIM */
void UTMMGRS(long izone,
long* ltrnum,
double sphi,
double x,
double y)
{ /* BEGIN UTMMGRS */
/*
* izone : Zone number.
* ltrnum : Values of letters in the MGRS coordinate.
* sphi : Latitude in radians.
* x : Easting.
* y : Northing.
*
* UTMMGRS CALLS THE FOLLOWING ROUTINES:
*
* GPTUTM
* UTMLIM
* UTMSET
*/
double fnltr; /* False northing for 3rd letter */
double slcm; /* Central meridian - longitude of origin */
double sleast; /* Longitude east limit - UTM */
double slwest; /* Longitude west limit -UTM */
double spnor; /* MGRS north latitude limits based on 1st letter */
double spsou; /* MGRS south latitude limits based on 1st letter */
double xltr; /* Easting used to derive 2nd letter of MGRS */
double yltr; /* Northing used to derive 3rd letter of MGRS */
long ltrlow; /* 2nd letter range - low number */
long ltrhi; /* 2nd letter range - high number */
char hemisphere;
UTMSET(izone, <rlow, <rhi, &fnltr);
ltrnum[0] = LETTER_A;
UTMLIM(<rnum[0], sphi, izone, &spsou, &spnor, &sleast, &slwest);
slcm = (double)(izone * 6 - 183) * DEGRAD;
/*
GPTUTM(a, recf, spsou, slcm, &izone, &yltr, &xltr, (long)1);
*/
Set_UTM_Parameters(MGRS_a, MGRS_f, izone);
Convert_Geodetic_To_UTM(spsou, slcm, &izone, &hemisphere, &xltr, &yltr);
yltr = (double)((long)(y + RND5));
if (((double)((long)(yltr + RND5))) == ((double)((long)(1.e7 + RND5))))
{
yltr = (double)((long)(yltr - 1.e0 + RND5));
}
while (yltr >= TWOMIL)
{
yltr = yltr - TWOMIL;
}
yltr = yltr - fnltr;
if (yltr < ZERO)
{
yltr = yltr + TWOMIL;
}
ltrnum[2] = (long)((yltr + RND1) / ONEHT);
if (ltrnum[2] > LETTER_H)
{
ltrnum[2] = ltrnum[2] + 1;
}
if (ltrnum[2] > LETTER_N)
{
ltrnum[2] = ltrnum[2] + 1;
}
xltr = (double)((long)(x));
if (((ltrnum[0] == LETTER_V) && (izone == 31)) &&
(((double)((long)(xltr + RND5))) == ((double)((long)(5.e5 + RND5)))))
{
xltr = (double)((long)(xltr - 1.e0 + RND5)); /* SUBTRACT 1 METER */
}
ltrnum[1] = ltrlow + ((long)((xltr + RND1) / ONEHT) - 1);
if ((ltrlow == LETTER_J) && (ltrnum[1] > LETTER_N))
{
ltrnum[1] = ltrnum[1] + 1;
}
return;
} /* END UTMMGRS */
void UPSSET(long n,
long* ltrlow,
long* ltrhi,
double *feltr,
double *fnltr,
long* ltrhy)
{ /* BEGIN UPSSET */
/*
* n : Value of 1st letter in MGRS coordinate.
* ltrlow : Low number for 2nd letter.
* ltrhi : High number for 2nd letter.
* feltr : False easting.
* fnltr : False northing.
* ltrhy : High number for 3rd letter.
*/
if (n == LETTER_Z) /* EASTERN HEMISPHERE-NORTH ZONE */
{
*ltrlow = LETTER_A;
*ltrhi = LETTER_J;
*feltr = TWOMIL;
*fnltr = ONE3HT;
*ltrhy = LETTER_P;
}
else if (n == LETTER_Y) /* WESTERN HEMISPHERE-NORTH ZONE */
{
*ltrlow = LETTER_J;
*ltrhi = LETTER_Z;
*feltr = EIGHT;
*fnltr = ONE3HT;
*ltrhy = LETTER_P;
}
else if (n == LETTER_B) /* ** EASTERN HEMISPHERE - SOUTH ZONE */
{
*ltrlow = LETTER_A;
*ltrhi = LETTER_R;
*feltr = TWOMIL;
*fnltr = EIGHT;
*ltrhy = LETTER_Z;
}
else if (n == LETTER_A) /* ** WESTERN HEMISPHERE - SOUTH ZONE */
{
*ltrlow = LETTER_J;
*ltrhi = LETTER_Z;
*feltr = EIGHT;
*fnltr = EIGHT;
*ltrhy = LETTER_Z;
}
return;
} /* END OF UPSSET */
void UPS(char* mgrs,
long* ltrnum,
double x,
double y,
long iarea)
{ /* BEGIN UPS */
/*
* mgrs : MGRS coordinate.
* ltrnum : Values of the letters in the MGRS coordinate.
* x : Easting.
* y : Northing.
* iarea : Set to UPS_NORTH or UPS_SOUTH.
*
* UPS CALLS THE FOLLOWING ROUTINES:
*
* UPSSET
*/
double feltr; /* False easting for 2nd letter */
double fnltr; /* False northing for 3rd letter */
double xltr; /* Easting used to derive 2nd letter of MGRS */
double yltr; /* Northing used to derive 3rd letter of MGRS */
long ltrlow; /* 2nd letter range - low number */
long ltrhi; /* 2nd letter range - high number */
long ltrhy; /* 3rd letter range - high number (UPS) */
if (iarea == UPS_NORTH)
{
ltrnum[0] = LETTER_Y;
if (((double)((long)(x + RND5))) >= TWOMIL)
{
ltrnum[0] = LETTER_Z;
}
}
else if (iarea == UPS_SOUTH)
{
ltrnum[0] = LETTER_A;
if (((double)((long)(x + RND5))) >= TWOMIL)
{
ltrnum[0] = LETTER_B;
}
}
UPSSET(ltrnum[0], <rlow, <rhi, &feltr, &fnltr, <rhy);
mgrs[0] = BLANK;
mgrs[1] = BLANK;
yltr = (double)((long)(y + RND5));
yltr = yltr - fnltr;
ltrnum[2] = (long)((yltr + RND1) / ONEHT);
if (ltrnum[2] > LETTER_H)
{
ltrnum[2] = ltrnum[2] + 1;
}
if (ltrnum[2] > LETTER_N)
{
ltrnum[2] = ltrnum[2] + 1;
}
xltr = (double)((long)(x + RND5));
xltr = xltr - feltr;
ltrnum[1] = ltrlow + ((long)((xltr + RND1) / ONEHT));
if (x < TWOMIL)
{
if (ltrnum[1] > LETTER_L)
{
ltrnum[1] = ltrnum[1] + 3;
}
if (ltrnum[1] > LETTER_U)
{
ltrnum[1] = ltrnum[1] + 2;
}
}
if (x >= TWOMIL)
{
if (ltrnum[1] > LETTER_C)
{
ltrnum[1] = ltrnum[1] + 2;
}
if (ltrnum[1] > LETTER_H)
{
ltrnum[1] = ltrnum[1] + 1;
}
if (ltrnum[1] > LETTER_L)
{
ltrnum[1] = ltrnum[1] + 3;
}
}
return;
} /* END OF UPS */
void LTR2UPS(long* ltrnum,
long ltrlow,
long ltrhi,
long ltrhy,
long* ierr,
double *xltr,
double *yltr,
double fnltr,
double feltr,
double *x,
double *y,
double sign)
{ /* BEGIN LTR2UPS */
/*
* ltrnum : Values of the letters in the MGRS coordinate
* ltrlow : Low number
* ltrhi : High number-2nd letter
* ltrhy : High number-3rd letter
* ierr : Error code
* xltr : Easting for 100,000 meter grid square
* yltr : Northing for 100,000 meter grid square
* fnltr : False northing for 3rd letter
* feltr : False easting for 2nd letter
* x : Easting
* y : Northing
* sign : Set to either positive or negative
*/
if (ltrnum[1] < ltrlow)
{
*ierr = TRUE;
return;
}
if (ltrnum[1] > ltrhi)
{
*ierr = TRUE;
return;
}
if (ltrnum[2] > ltrhy)
{
*ierr = TRUE;
return;
}
if ((ltrnum[1] == LETTER_D) || (ltrnum[1] == LETTER_E) ||
(ltrnum[1] == LETTER_M) || (ltrnum[1] == LETTER_N) ||
(ltrnum[1] == LETTER_V) || (ltrnum[1] == LETTER_W))
{
*ierr = TRUE;
return;
}
*yltr = (double)ltrnum[2] * ONEHT + fnltr;
if (ltrnum[2] > LETTER_I)
{
*yltr = *yltr - ONEHT;
}
if (ltrnum[2] > LETTER_O)
{
*yltr = *yltr - ONEHT;
}
*xltr = (double)((ltrnum[1]) - ltrlow) * ONEHT + feltr;
if (ltrlow != LETTER_A)
{
if (ltrnum[1] > LETTER_L)
{
*xltr = *xltr - 3.e0 * ONEHT;
}
if (ltrnum[1] > LETTER_U)
{
*xltr = *xltr - 2.e0 * ONEHT;
}
}
else if (ltrlow == LETTER_A)
{
if (ltrnum[1] > LETTER_C)
{
*xltr = *xltr - 2.e0 * ONEHT;
}
if (ltrnum[1] > LETTER_I)
{
*xltr = *xltr - ONEHT;
}
if (ltrnum[1] > LETTER_L)
{
*xltr = *xltr - 3.e0 * ONEHT;
}
}
*x = *xltr;
*y = *yltr * sign;
return;
} /* END OF LTR2UPS */
void GRID_UPS(long *Letters,
char *Hemisphere,
double *Easting,
double *Northing,
long *Error)
{ /* BEGIN GRID_UPS */
double feltr; /* False easting for 2nd letter */
double fnltr; /* False northing for 3rd letter */
//double sleast; /* Longitude east limit - UTM */
//double slwest; /* Longitude west limit -UTM */
//double spnor; /* North latitude limits based on 1st letter */
double spsou; /* South latitude limits based on 1st letter */
double x; /* easting */
double xltr; /* easting for 100,000 meter grid square */
double xnum; /* easting part of MGRS */
double y; /* northing */
double yltr; /* northing for 100,000 meter grid square */
double ynum; /* northing part of MGRS */
//long izone; /* Zone number */
long ltrhi; /* 2nd letter range - high number */
long ltrhy; /* 3rd letter range - high number (UPS) */
long ltrlow; /* 2nd letter range - low number */
long sign;
double sphi;
double slam;
if ((Letters[0] == LETTER_Y) || (Letters[0] == LETTER_Z))
{
spsou = MAX_UTM_LAT;
sign = 1;
}
else
{
spsou = MIN_UTM_LAT;
sign = -1;
}
slam = PI / 2.e0;
if ((Letters[0] == LETTER_Y) || (Letters[0] == LETTER_A))
{
slam = -slam;
}
//izone = 0;
sphi = spsou;
Set_UPS_Parameters(MGRS_a, MGRS_f);
Convert_Geodetic_To_UPS(sphi, slam, Hemisphere, &x, &y);
//spnor = sphi;
//sleast = slam;
//slwest = slam;
UPSSET(Letters[0], <rlow, <rhi, &feltr, &fnltr, <rhy);
LTR2UPS(Letters, ltrlow, ltrhi, ltrhy, Error, &xltr, &yltr, fnltr, feltr,
&x, &y, sign);
xnum = *Easting;
ynum = *Northing;
y = (yltr + ynum);
x = xltr + xnum;
*Easting = x;
*Northing = y;
return;
} /* END OF GRID_UPS */
void LTR2UTM(long* ltrnum,
long ltrlow,
long ltrhi,
long* ierr,
double *xltr,
double *yltr,
double fnltr,
double yslow,
double ylow)
{ /* BEGIN LTR2UTM */
/*
* xltr : Easting for 100,000 meter grid square.
* yltr : Northing for 100,000 meter grid square.
* ierr : Error code.
* ltrnum : Values of the letters in the MGRS coordinate.
* ltrlow : Low number.
* ltrhi : High number.
* fnltr : False northing for 3rd letter.
* yslow : Northing scaled down to less than 2 million.
* ylow : Lowest northing of area to nearest 100,000.
*/
if (ltrnum[1] < ltrlow)
{
*ierr = TRUE;
return;
}
if (ltrnum[1] > ltrhi)
{
*ierr = TRUE;
return;
}
if (ltrnum[2] > LETTER_V)
{
*ierr = TRUE;
return;
}
*yltr = (double)(ltrnum[2]) * ONEHT + fnltr;
*xltr = (double)((ltrnum[1]) - ltrlow + 1) * ONEHT;
if ((ltrlow == LETTER_J) && (ltrnum[1] > LETTER_O))
{
*xltr = *xltr - ONEHT;
}
if (ltrnum[2] > LETTER_O)
{
*yltr = *yltr - ONEHT;
}
if (ltrnum[2] > LETTER_I)
{
*yltr = *yltr - ONEHT;
}
if (((double)((long)(*yltr + RND5))) >= ((double)((long)(TWOMIL + RND5))))
{
*yltr = *yltr - TWOMIL;
}
*yltr = ((double)((long)(*yltr + RND5)));
*yltr = *yltr - yslow;
if (*yltr < ZERO)
{
*yltr = *yltr + TWOMIL;
}
*yltr = ((double)((long)(ylow + *yltr + RND5)));
return;
} /* END OF LTR2UTM */
void GRID_UTM(long *Zone,
long *Letters,
char *Hemisphere,
double *Easting,
double *Northing,
long In_Precision,
long *Error)
{ /* BEGIN GRID_UTM */
double fnltr; /* False northing for 3rd letter */
long ltrhi; /* 2nd letter range - High number */
long ltrlow; /* 2nd letter range - Low number */
long number; /* Value of ltrnum[0] + 1 */
// double slam;
double slcm; /* Central meridian */
double sleast; /* Longitude east limit - UTM */
double slwest; /* Longitude west limit -UTM */
double sphi; /* Latitude (needed by UTMLIM) */
double spnor; /* North latitude limits based on 1st letter */
double spsou; /* South latitude limits based on 1st letter */
double xltr; /* Easting for 100,000 meter grid square */
double ylow; /* Lowest northing of area to nearest 100,000 */
double yltr; /* Northing for 100,000 meter grid square */
double yslow; /* Northing scaled down to less than 2 million*/
double Latitude = 0.0;
double Longitude = 0.0;
//double divisor = 1.0;
if ((*Zone == 32) && (Letters[0] == LETTER_X))
{
*Error = TRUE;
return;
}
if ((*Zone == 34) && (Letters[0] == LETTER_X))
{
*Error = TRUE;
return;
}
if ((*Zone == 36) && (Letters[0] == LETTER_X))
{
*Error = TRUE;
return;
}
number = Letters[0] + 1;
sphi = 0.0;
UTMLIM(&number, sphi, *Zone, &spsou, &spnor, &sleast, &slwest);
Set_UTM_Parameters(MGRS_a, MGRS_f, *Zone);
slcm = (double)(*Zone * 6 - 183) * DEGRAD;
Convert_Geodetic_To_UTM(spsou, slcm, Zone, Hemisphere, &xltr, &yltr);
ylow = ((double)((long)((double)((long)(yltr / ONEHT)) * ONEHT)));
yslow = ylow;
while (yslow >= TWOMIL)
{
yslow = yslow - TWOMIL;
}
yslow = ((double)((long)(yslow)));
UTMSET(*Zone, <rlow, <rhi, &fnltr);
LTR2UTM(Letters, ltrlow, ltrhi, Error, &xltr, &yltr, fnltr, yslow, ylow);
*Easting = xltr + *Easting;
*Northing = yltr + *Northing;
/* check that point is within Zone Letter bounds */
Convert_UTM_To_Geodetic(*Zone, *Hemisphere, *Easting, *Northing, &Latitude, &Longitude);
double divisor = pow(10.0, In_Precision);
if (((spsou - DEGRAD / divisor) <= Latitude) && (Latitude <= (spnor + DEGRAD / divisor)))
return;
else
*Error = TRUE;
return;
}/* END OF GRID_UTM */
long Round_MGRS(double value)
/* Round value to nearest integer, using standard engineering rule */
{ /* Round_MGRS */
double ivalue;
long ival;
double fraction = modf(value, &ivalue);
ival = (long)(ivalue);
if ((fraction > 0.5) || ((fraction == 0.5) && (ival % 2 == 1)))
ival++;
return (ival);
} /* Round_MGRS */
long Make_MGRS_String(char* MGRS,
long Zone,
long ltrnum[MGRS_LETTERS],
double Easting,
double Northing,
long Precision)
/* Construct an MGRS string from its component parts */
{ /* Make_MGRS_String */
long i;
long j;
long error_code = MGRS_NO_ERROR;
double divisor;
long east;
long north;
i = 0;
if (Zone)
i = sprintf_s(MGRS + i, 6, "%2.2ld", Zone);
for (j = 0; j < 3; j++)
MGRS[i++] = ALBET[ltrnum[j]];
divisor = pow(10.0, (5 - Precision));
Easting = fmod(Easting, 100000.0);
if (Easting >= 99999.5)
Easting = 99999.0;
east = (long)(Easting / divisor);
i += sprintf_s(MGRS + i, 80, "%*.*ld", Precision, Precision, east);
Northing = fmod(Northing, 100000.0);
if (Northing >= 99999.5)
Northing = 99999.0;
north = (long)(Northing / divisor);
sprintf_s(MGRS + i, 80, "%*.*ld", Precision, Precision, north);
return (error_code);
} /* Make_MGRS_String */
long Break_MGRS_String(char* MGRS,
long* Zone,
long Letters[MGRS_LETTERS],
double* Easting,
double* Northing,
long* Precision)
/* Break down an MGRS string into its component parts */
{ /* Break_MGRS_String */
long error_code = MGRS_NO_ERROR;
long i = 0;
long j;
long num_digits;
long num_letters;
while (MGRS[i] == ' ')
i++; /* skip any leading blanks */
j = i;
while (isdigit(MGRS[i]))
i++;
num_digits = i - j;
if (num_digits <= 2)
if (num_digits > 0)
{
char zone_string[3];
/* get zone */
strncpy_s(zone_string, sizeof(zone_string), MGRS + j, 2);
zone_string[2] = 0;
sscanf_s(zone_string, "%ld", Zone);
if ((*Zone < 1) || (*Zone > 60))
error_code |= MGRS_STRING_ERROR;
}
else
*Zone = 0;
else
error_code |= MGRS_STRING_ERROR;
j = i;
while (isalpha(MGRS[i]))
i++;
num_letters = i - j;
if (num_letters == 3)
{
/* get letters */