-
Notifications
You must be signed in to change notification settings - Fork 0
/
soarUMap.c
3670 lines (3233 loc) · 116 KB
/
soarUMap.c
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
#include <PalmOS.h>
#include "soarMap.h"
#include "soarUMap.h"
#include "Mathlib.h"
#include "soaring.h"
#include "soarUtil.h"
#include "soarMem.h"
#include "soarMath.h"
#include "soarForm.h"
#include "soarWay.h"
#include "soarDB.h"
#include "soarIO.h"
#include "soarWind.h"
#include "soarGPSInfo.h"
#include "soarTask.h"
/*****************************************************************************
* globals
*****************************************************************************/
TrkTrail *trail = NULL;
Int16 trailidx = -1;
Int16 idxmax = -1;
double mapscales[MAXMAPSCALEIDX+1] = { 0.2, 0.5, 1.0, 2.0, 3.0, 5.0, 8.0, 10.0, 12.0, 15.0, 20.0, 30.0, 50.0, 80.0, 100.0, 120.0, 150.0, 200.0, 250.0, 300.0};
UInt32 trkplotds[MAXMAPSCALEIDX+1] = { 1, 1, 1, 1, 2, 2, 2, 2, 2, 5, 5, 10, 10, 10, 20, 25, 50, 100, 200, 200};
// UTM Coords
static Ellipsoid ellipsoid[] =
{
{-1, "Placeholder", 0, 0},
{1, "Airy", 6377563, 0.00667054},
{2, "Australian National", 6378160, 0.006694542},
{3, "Bessel 1841", 6377397, 0.006674372},
{4, "Bessel 1841 (Nambia) ", 6377484, 0.006674372},
{5, "Clarke 1866", 6378206, 0.006768658},
{6, "Clarke 1880", 6378249, 0.006803511},
{7, "Everest", 6377276, 0.006637847},
{8, "Fischer 1960 (Mercury) ", 6378166, 0.006693422},
{9, "Fischer 1968", 6378150, 0.006693422},
{10, "GRS 1967", 6378160, 0.006694605},
{11, "GRS 1980", 6378137, 0.00669438},
{12, "Helmert 1906", 6378200, 0.006693422},
{13, "Hough", 6378270, 0.00672267},
{14, "International", 6378388, 0.00672267},
{15, "Krassovsky", 6378245, 0.006693422},
{16, "Modified Airy", 6377340, 0.00667054},
{17, "Modified Everest", 6377304, 0.006637847},
{18, "Modified Fischer 1960", 6378155, 0.006693422},
{19, "South American 1969", 6378160, 0.006694542},
{20, "WGS 60", 6378165, 0.006693422},
{21, "WGS 66", 6378145, 0.006694542},
{22, "WGS-72", 6378135, 0.006694318},
{23, "WGS-84", 6378137, 0.00669438},
{24, "FAI", 6371000, 0.000000002} // Not sure this is right!
};
extern double bigratio;
extern IndexedColorType indexGreen, indexRed, indexBlack, indexGrey, indexOrange;
extern IndexedColorType indexSink, indexWeak, indexStrong, indexWaypt;
extern double curmapscale;
extern double xdist, ydistul, ydistll, ulRng; // screen bounds in nm
extern double crashlat, crashlon;
extern Boolean offterrain;
extern UInt8 mapmode;
extern double mapmaxlat, mapminlat;
extern double mapmaxlon, mapminlon;
extern double truecse;
extern Boolean draw_task;
extern Boolean IsMap;
extern Int16 profilemaxalt;
extern Int16 profileminalt;
extern WindProfileType *windprofile;
extern double profilestep;
extern double MCCurVal;
extern double MCMult;
extern UInt32 cursecs;
extern Int16 inareasector;
/******************************************************************************
* Conversions
*****************************************************************************/
double MetersToNautical(double meters)
{
return (meters / 1852.0);
}
double NauticalToMeters(double miles)
{
return (miles * 1852.0);
}
double MetersToFeet(double meters)
{
return (meters * 3.2808333);
}
double FeetToMeters(double feet)
{
return (feet / 3.2808333);
}
double RelToTrueBrg(double relbrg, double truecse)
{
double truebrg;
truebrg = relbrg + truecse;
if (truebrg >= 360.0) {
truebrg -= 360.0;
}
return(truebrg);
}
double TrueToRelBrg(double oldbearing, double truecse)
{
double newbearing;
/* Shift Bearing of POI by True Course Amount */
newbearing = oldbearing - truecse;
if (newbearing < 0.0) {
newbearing += 360.0;
}
return(newbearing);
}
double RecipCse(double curcse)
{
double recipcse;
recipcse = curcse - 180.0;
if (recipcse < 0.0) {
recipcse += 360.0;
}
return(recipcse);
}
double modcrs(double x)
{
return(Fmod(x, 2.0*PI));
}
// Given two bearing, calculate the bisector angle
double MidCse(double leftbrg, double rightbrg)
{
double diff, angle;
Boolean opposite = false;
if (leftbrg > rightbrg) {
diff = leftbrg - rightbrg;
} else {
diff = rightbrg - leftbrg;
}
if (diff < 180.0) {
diff = 360.0 - diff;
opposite = true;
}
diff = diff/2.0;
if (leftbrg > rightbrg) {
if (opposite) {
angle = nice_brg(rightbrg - diff);
} else {
angle = nice_brg(diff + rightbrg);
}
} else {
if (opposite) {
angle = nice_brg(leftbrg - diff);
} else {
angle = nice_brg(diff + leftbrg);
}
}
return(RecipCse(angle));
}
Boolean PointOutOfBounds (double ul_lat, double ul_lon, double lr_lat,
double lr_lon, double lat, double lon)
{
// if (convP->crosses_pole != (char)0) {
// /*Pole crossing supported - do a concentric check*/
// double cos_c = Sin(convP->latOrigin)* Sin(lat)+
// Cos(convP->latOrigin)* Cos(lat) * Cos(lon - convP->lonOrigin);
// double bounds = Cos(convP->ul_lat - convP->latOrigin)*
// Cos(convP->lr_lon - convP->lonOrigin);
//
// if (cos_c < bounds)
// return true;
// else
// return false;
// }
// else if (lon > convP->lr_lon || lon < convP->ul_lon || lat < convP->lr_lat ||
// lat > convP->ul_lat)
//
if (lon > lr_lon || lon < ul_lon || lat < lr_lat || lat > ul_lat)
return(true);
else
return(false);
}
/*****************************************************************************
* LLToStringDM - for LON, print "DDD:MM.MM(MM) X" where 'X' is E or W.
* for LAT, print "DD:MM.MM(MM) X" where 'X' is N or S.
* Now supports 2-4 decimal places with 3 or 4 being the available
* options for numaddplaces and whether the colon & period are added.
*****************************************************************************/
Char LLToStringDM(double coord, Char *coordStr, Boolean lat, Boolean colon, Boolean period, Int8 numaddplaces)
{
double absCoord;
double fraction;
Int16 degrees;
Int16 minutes;
Int16 decMinutes; // decimal minutes
Char nsew;
double precmult=1.0;
if (!coordStr)
return(' ');
if (numaddplaces == 3) {
precmult = 10.0;
} else if (numaddplaces == 4) {
precmult = 100.0;
}
// break the double down to parts
absCoord = Fabs(coord);
degrees = (Int16)absCoord;
fraction = absCoord - (double)degrees;
minutes = (Int16)(fraction * 60.0);
fraction = (fraction * 60.0 - (double)minutes);
decMinutes = (Int16)(pround((fraction * (Int16)(100.0*precmult)), 0));
// catch decimal minute overflow
if (decMinutes >= (Int16)(100.0*precmult)) {
decMinutes -= (Int16)(100.0*precmult);
minutes++;
}
// catch minute overflow
if (minutes > 59) {
minutes -= 60;
degrees++;
}
// put the digits in the string, padding w/ zeros if necessary
if (lat) {
if (degrees < 10)
StrPrintF(coordStr, "0%d", degrees);
else
StrPrintF(coordStr, "%d", degrees);
if (colon) {
StrCat(coordStr, ":");
coordStr += 3;
} else {
coordStr += 2;
}
} else {
if (degrees < 10)
StrPrintF(coordStr, "00%d:", degrees);
else if (degrees < 100)
StrPrintF(coordStr, "0%d:", degrees);
else
StrPrintF(coordStr, "%d:", degrees);
if (colon) {
StrCat(coordStr, ":");
coordStr += 4;
} else {
coordStr += 3;
}
}
if (minutes < 10)
StrPrintF(coordStr, "0%d", minutes);
else
StrPrintF(coordStr, "%d", minutes);
if (period) {
StrCat(coordStr, ".");
coordStr += 3;
} else {
coordStr += 2;
}
if (lat)
nsew = (coord >= 0.0) ? 'N' : 'S';
else
nsew = (coord >= 0.0 && coord <= 180.0) ? 'E' : 'W';
if (numaddplaces == 3) {
if (decMinutes < 10) {
StrPrintF(coordStr, "00%d%c", decMinutes, nsew);
} else if (decMinutes < 100) {
StrPrintF(coordStr, "0%d%c", decMinutes, nsew);
} else {
StrPrintF(coordStr, "%d%c", decMinutes, nsew);
}
} else if (numaddplaces == 4) {
if (decMinutes < 10) {
StrPrintF(coordStr, "000%d%c", decMinutes, nsew);
} else if (decMinutes < 100) {
StrPrintF(coordStr, "00%d%c", decMinutes, nsew);
} else if (decMinutes < 1000) {
StrPrintF(coordStr, "0%d%c", decMinutes, nsew);
} else {
StrPrintF(coordStr, "%d%c", decMinutes, nsew);
}
} else {
if (decMinutes < 10) {
StrPrintF(coordStr, "0%d%c", decMinutes, nsew);
} else {
StrPrintF(coordStr, "%d%c", decMinutes, nsew);
}
}
return(nsew);
}
/*****************************************************************************
* LLToStringDMS - For LON, print "DDD:MM:SS.SS X" where 'X' is E or W.
* for LAT, print "DD:MM:SS.SS X" where 'X' is N or S.
*****************************************************************************/
Char LLToStringDMS(double coord, Char *coordStr, Boolean lat)
{
double absCoord;
double fraction;
Int16 degrees;
Int16 minutes;
Int16 seconds;
Int16 decSeconds; // decimal seconds
Char nsew;
if (!coordStr)
return(' ');
// break the double down to parts
absCoord = Fabs(coord);
degrees = (Int16)absCoord;
fraction = absCoord - (double)degrees;
minutes = (Int16)(fraction * 60.0);
fraction = (fraction * 60.0 - (double)minutes);
seconds = (Int16)(fraction * 60.0);
fraction = (fraction * 60.0 - (double)seconds);
decSeconds = (Int16)((fraction * 100.0) + .5);
// catch decimal second overflow
if (decSeconds > 99) {
decSeconds -= 100;
seconds++;
}
// catch second overflow
if (seconds > 59) {
seconds -= 60;
minutes++;
}
// catch minute overflow
if (minutes > 59) {
minutes -= 60;
degrees++;
}
// put the digits in the string, padding w/ zeros if necessary
if (lat) {
if (degrees < 10)
StrPrintF(coordStr, "0%d:", degrees);
else
StrPrintF(coordStr, "%d:", degrees);
coordStr += 3;
} else {
if (degrees < 10) {
StrPrintF(coordStr, "00%d:", degrees);
} else if (degrees < 100) {
StrPrintF(coordStr, "0%d:", degrees);
} else {
StrPrintF(coordStr, "%d:", degrees);
}
coordStr += 4;
}
if (minutes < 10) {
StrPrintF(coordStr, "0%d:", minutes);
} else {
StrPrintF(coordStr, "%d:", minutes);
}
coordStr += 3;
if (seconds < 10) {
StrPrintF(coordStr, "0%d.", seconds);
} else {
StrPrintF(coordStr, "%d.", seconds);
}
coordStr += 3;
if (lat) {
nsew = (coord >= 0.0) ? 'N' : 'S';
} else {
nsew = ((coord >= 0.0) && (coord <= 180.0)) ? 'E' : 'W';
}
if (decSeconds < 10) {
StrPrintF(coordStr, "0%d%c", decSeconds, nsew);
} else {
StrPrintF(coordStr, "%d%c", decSeconds, nsew);
}
return(nsew);
}
/*****************************************************************************
* DegMinSecToLatLon - convert decimal degrees, minutes, fractions of minutes,
* seconds and fractions of seconds to a double. 2.7778e-4 is 1/3600
*****************************************************************************/
double DegMinSecToLatLon(Int16 deg, Int16 min, double fracMin,
Int16 sec, double fracSec, Char nsew)
{
double latLon;
latLon = (double)deg + ((double)min / 60.0) + (fracMin / 60.0) + ((double)sec / 3600.0) +
(fracSec * 2.7778e-4);
if (nsew == 'w' || nsew == 'W' || nsew == 's' || nsew == 'S')
return(latLon * -1.0);
else
return(latLon);
}
/*****************************************************************************
* DegMinStringToLatLon - convert degrees, minutes, decimal mins to a double.
* String is of the form DD(D)MM.MMC where C is N, S, E or W
* If a NULL is passed in for nsew, assume the N,S,E or W are in latLonstring
*****************************************************************************/
double DegMinStringToLatLon(Char *latLonstring, Char nsew)
{
double latLondbl;
double latLon;
double deg;
double min;
Int16 i;
Boolean result = true;
if (nsew == NULL) {
// find the N, S, E, or W before we destroy it
for (i = StrLen(latLonstring)-1; i > 0 && latLonstring[i] < 'A'; i--)
; // empty for body
if (i <= 0)
result = false;
else {
nsew = latLonstring[i];
// make sure of a valid nsew character
switch (nsew) {
case 'N':
case 'n':
case 'S':
case 's':
case 'E':
case 'e':
case 'W':
case 'w':
break;
default:
result = false;
break;
}
// drop in a NULL
latLonstring[i] = '\0';
}
}
latLondbl = StrToDbl(latLonstring);
deg = trunc(latLondbl/100.0);
min = Fmod(latLondbl,100.0);
latLon = deg + (min / 60.0);
if (result == false) {
return(INVALID_LAT_LON);
}
if (nsew == 'w' || nsew == 'W' || nsew == 's' || nsew == 'S') {
return (latLon * -1.0);
} else {
return (latLon);
}
}
/*****************************************************************************
* DegMinSecStringToLatLon - convert degrees, minutes, decimal mins to a double.
* String is of the form DD(D)MM.MMC where C is N, S, E or W
*****************************************************************************/
double DegMinSecStringToLatLon(Char *latLonstring, Char nsew)
{
double latLondbl;
double latLon;
double deg;
double min;
double sec;
Char tempchar[2];
tempchar[0] = nsew;
tempchar[1] = '\0';
if (StrStr("NSEWnsew", tempchar) == NULL) return(INVALID_LAT_LON);
latLondbl = StrToDbl(latLonstring);
deg = trunc(latLondbl/10000.0);
latLondbl = latLondbl - (deg*10000.0);
min = trunc(latLondbl/100.0);
sec = Fmod(latLondbl,100.0);
latLon = deg + (min / 60.0) + (sec / 3600.0);
if (Fabs(latLon) > 180.0) return (INVALID_LAT_LON);
if (nsew == 'w' || nsew == 'W' || nsew == 's' || nsew == 'S') {
return (latLon * -1.0);
} else {
return (latLon);
}
}
/*****************************************************************************
* DegMinColonStringToLatLon - parse a lat or lon string in DD:MM(.MMM)C format
* where 'C' is N, S, E, or W. Only does three places of fractions of
* seconds.
*****************************************************************************/
double DegMinColonStringToLatLon(Char *inP)
{
Int32 degrees=0, minutes=0, seconds=0;
double fracSeconds= .0;
double fracMinutes=0.0;
char *startP, *charP=NULL, *coordP;
char nsew=NULL;
Int16 len, i;
Boolean result = true;
// create string for copying
len = StrLen(inP);
if (len < 1 || !AllocMem((void *)&coordP, len))
return INVALID_LAT_LON;
// copy the incoming string
MemMove(coordP, inP, len);
startP = coordP;
// find the N, S, E, or W before we destroy it
for (i = len - 1; i > 0 && coordP[i] < 'A'; i--)
; // empty for body
if (i <= 0)
result = false;
else {
nsew = coordP[i];
// make sure of a valid nsew character
switch (nsew) {
case 'N':
case 'n':
case 'S':
case 's':
case 'E':
case 'e':
case 'W':
case 'w':
break;
default:
result = false;
break;
}
// drop in a NULL
coordP[i] = NULL;
}
// back to the start - find degree
if (result) {
if ((charP = StrChr(startP, ':')) != NULL)
*charP = NULL;
degrees = StrAToI(startP);
}
else
result = false;
// find minutes if last search succeeded
if (result && charP) {
startP = charP + 1;
if ((startP - coordP) < len) {
// may not be any decimal seconds
if ((charP = StrChr(startP, '.')) != NULL)
*charP = NULL;
minutes = StrAToI(startP);
}
else
result = false;
}
// find decimal minutes if last search succeeded
if (result && charP)
result = RightHandToD(charP + 1, &fracMinutes);
// free local string
FreeMem((void *)&coordP);
// convert values to lat/lon
if (result) {
if (degrees > 180 || degrees < 0) {
return INVALID_LAT_LON;
}
if (minutes > 60 || minutes < 0) {
return INVALID_LAT_LON;
}
if (seconds > 60 || seconds < 0) {
return INVALID_LAT_LON;
}
if (fracMinutes >= 1.0) {
return INVALID_LAT_LON;
}
if (fracSeconds >= 1.0) {
return INVALID_LAT_LON;
}
return(DegMinSecToLatLon(degrees, minutes, fracMinutes, seconds, fracSeconds, nsew));
} else {
return(INVALID_LAT_LON);
}
}
/*****************************************************************************
* DegMinSecColonStringToLatLon - parse a lat or lon string in DD:MM:SS(.SSS)C format
* where 'C' is N, S, E, or W. Only does three places of fractions of
* seconds.
*****************************************************************************/
double DegMinSecColonStringToLatLon(Char* inP)
{
Int32 degrees=0, minutes=0, seconds=0;
double fracSeconds=0.0;
double fracMinutes=0.0;
char *startP, *charP=NULL, *coordP;
char nsew=NULL;
Int16 len, i;
Boolean result = true;
Boolean nosecs = false;
// create string for copying
len = StrLen(inP);
if (len < 1 || !AllocMem((void *)&coordP, len))
return INVALID_LAT_LON;
// copy the incoming string
MemMove(coordP, inP, len);
startP = coordP;
// find the N, S, E, or W before we destroy it
for (i = len - 1; i > 0 && coordP[i] < 'A'; i--)
; // empty for body
if (i <= 0) {
result = false;
} else {
nsew = coordP[i];
// make sure of a valid nsew character
switch (nsew) {
case 'N':
case 'n':
case 'S':
case 's':
case 'E':
case 'e':
case 'W':
case 'w':
result = true;
break;
default:
result = false;
break;
}
// drop in a NULL
coordP[i] = NULL;
}
// back to the start - find degree
if (result) {
if ((charP = StrChr(startP, ':')) != NULL)
*charP = NULL;
degrees = StrAToI(startP);
}
else
result = false;
// find minutes if last search succeeded
if (result && charP) {
startP = charP + 1;
if ((startP - coordP) < len && (charP = StrChr(startP, ':')) != NULL) {
*charP = NULL;
minutes = StrAToI(startP);
} else if ((startP - coordP) < len) {
if ((charP = StrChr(startP, ':')) != NULL)
*charP = NULL;
minutes = StrAToI(startP);
if (charP) {
result = RightHandToD(charP + 1, &fracMinutes);
}
nosecs = true;
} else {
result = false;
}
}
// find seconds if last search succeeded & there are seconds
if (!nosecs) {
if (result && charP) {
startP = charP + 1;
if ((startP - coordP) < len && (charP = StrChr(startP, '.')) != NULL)
*charP = NULL;
// may not be any decimal seconds
seconds = StrAToI(startP);
}
// find decimal seconds if last search succeeded
if (result && charP)
result = RightHandToD(charP + 1, &fracSeconds);
}
// free local string
FreeMem((void *)&coordP);
// convert values to lat/lon
if (result) {
if (degrees > 180 || degrees < 0) {
return INVALID_LAT_LON;
}
if (minutes > 60 || minutes < 0) {
return INVALID_LAT_LON;
}
if (seconds > 60 || seconds < 0) {
return INVALID_LAT_LON;
}
if (fracMinutes >= 1.0) {
return INVALID_LAT_LON;
}
if (fracSeconds >= 1.0) {
return INVALID_LAT_LON;
}
return(DegMinSecToLatLon(degrees, minutes, fracMinutes, seconds, fracSeconds, nsew));
} else {
return(INVALID_LAT_LON);
}
}
/*****************************************************************************
* PositToPixelLinearInterp - convert a Position Lat,Lon to a Pixel X,Y via
* Linear Interpolation
*****************************************************************************/
void PositToPixelLinearInterp(LinearInterp *liP, double lat, double lon, Int16 *xP, Int16 *yP)
{
double relLat, relLon, ratioX, ratioY;
if (! liP)
return;
// hope we're not stradling a hemisphere line
// offset of posit from upper left
relLat = liP->ulLat - lat;
relLon = lon - liP->ulLon;
// ratio is rel / delta
ratioX = relLon / liP->deltaLon;
*xP = (Int16)(liP->pixWidth * ratioX);
ratioY = relLat / liP->deltaLat;
*yP = (Int16)(liP->pixHeight * ratioY);
}
/*****************************************************************************
* PixelToPositLinearInterp - x and y must be relative to the raster, NOT
* the screen
*****************************************************************************/
void PixelToPositLinearInterp(LinearInterp *liP, Int32 x, Int32 y, double *latP, double *lonP)
{
double ratioX, ratioY;
// + 0.001 at end fixes rounding problems
ratioX = (double)x / (double)(liP->pixWidth) + 0.001;
ratioY = (double)y / (double)(liP->pixHeight) + 0.001;
*lonP = liP->ulLon + (ratioX * liP->deltaLon);
*latP = liP->ulLat - (ratioY * liP->deltaLat);
}
/*****************************************************************************
* CoordDeltaLinearInterp - get the delta (difference) between two coordinates
*****************************************************************************/
double CoordDeltaLinearInterp(double coord1, double coord2)
{
if (coord1 > 0.0 && coord2 < 0.0)
// coords in different hemispheres
return coord1 - coord2;
else if (coord1 < 0.0 && coord2 > 0.0)
// coords in different hemispheres
return coord2 - coord1;
else if (coord2 > coord1)
// coords in same hemispheres
return coord2 - coord1;
else
// coords in same hemispheres
return coord1 - coord2;
}
/*****************************************************************************
* CoordDeltaModLinearInterp - change coord2 so it is closer to or further
* from coord1 according to the ratio argument.
*****************************************************************************/
double CoordDeltaModLinearInterp(double coord1, double coord2, double ratio)
{
if (coord1 > 0.0 && coord2 < 0.0)
// coords in different hemispheres
return (coord1 - ((coord1 - coord2) * ratio));
else if (coord1 < 0.0 && coord2 > 0.0)
// coords in different hemispheres
return (coord1 + ((coord2 - coord1) * ratio));
else if (coord2 > coord1)
// coords in same hemispheres
return (coord1 + ((coord2 - coord1) * ratio));
else
// coords in same hemispheres
return (coord1 - ((coord1 - coord2) * ratio));
}
/*****************************************************************************
* MidCoordLinearInterp - get the coordinte between two coordinates
*****************************************************************************/
double MidCoordLinearInterp(double coord1, double coord2)
{
double divCoord;
divCoord = CoordDeltaLinearInterp(coord1, coord2) / 2.0;
return(divCoord + ((coord1 < coord2) ? coord1 : coord2));
}
/*****************************************************************************
* RangeBearingToLatLon - get the coordintes at the end of the
* range/bearing line
* THIS IS NOT WORKING CORRECTLY!!!!!!!!!!
* NOW WITH TEMP FIX
*****************************************************************************/
void RangeBearingToLatLon(double fromLat, double fromLon, double rng, double truebrg, double *toLat, double *toLon)
{
/*
double plat = 0.0, plon = 0.0;
double tlat = 0.0, tlon = 0.0;
double radtruebrg;
double radrng;
plat = DegreesToRadians(fromLat);
plon = DegreesToRadians(fromLon);
radtruebrg = DegreesToRadians(nice_brg(truebrg));
radrng = rng/NMPR;
tlat = asin((sin(plat)*cos(radrng)) + (cos(plat)*sin(radrng)*cos(radtruebrg)));
tlon = plon + atan2(sin(radtruebrg)*sin(radrng)*cos(plat), cos(radrng) - (sin(plat)*sin(tlat)));
*toLat = nice_brg(RadiansToDegrees(tlat));
*toLon = nice_brg(RadiansToDegrees(tlon));
*/
/*
// NOT WORKING!
// calculate an endpoint given a startpoint, bearing and distance
// Vincenty 'Direct' formula based on the formula as described at https://www.movable-type.co.uk/scripts/latlong-vincenty-direct.html
// original JavaScript implementation © 2002-2006 Chris Veness
double a = 6378137, b = 6356752.3142, f = 1/298.257223563; // WGS-84 ellipsiod
double lat1 = fromLat;
double lon1 = fromLon;
double s = rng;
double alpha1 = DegreesToRadians(truebrg);
double sinAlpha1 = sin(alpha1);
double cosAlpha1 = cos(alpha1);
double tanU1 = (1 - f) * tan(lat1);
double cosU1 = 1 / sqrt((1 + tanU1 * tanU1));
double sinU1 = tanU1 * cosU1;
double sigma1 = atan2(tanU1, cosAlpha1);
double sinAlpha = cosU1 * sinAlpha1;
double cosSqAlpha = 1 - sinAlpha * sinAlpha;
double uSq = cosSqAlpha * (a * a - b * b) / (b * b);
double A = 1 + uSq / 16384 * (4096 + uSq * (-768 + uSq * (320 - 175 * uSq)));
double B = uSq / 1024 * (256 + uSq * (-128 + uSq * (74 - 47 * uSq)));
double sigma = s / (b * A);
double sigmaP = 2.0 * PI;
double cos2SigmaM = 0.0;
double sinSigma = 0.0;
double cosSigma = 0.0;
double deltaSigma = 0.0;
double tmp;
double lat2;
double lambda;
double C;
double L;
double lon2;
while (abs(sigma - sigmaP) > 1e-12) {
cos2SigmaM = cos(2 * sigma1 + sigma);
sinSigma = sin(sigma);
cosSigma = cos(sigma);
deltaSigma = B * sinSigma * (cos2SigmaM + B / 4 * (cosSigma * (-1 + 2 * cos2SigmaM * cos2SigmaM) - B / 6 * cos2SigmaM * (-3 + 4 * sinSigma * sinSigma) * (-3 + 4 * cos2SigmaM * cos2SigmaM)));
sigmaP = sigma;
sigma = s / (b * A) + deltaSigma;
}
tmp = sinU1 * sinSigma - cosU1 * cosSigma * cosAlpha1;
lat2 = atan2(sinU1 * cosSigma + cosU1 * sinSigma * cosAlpha1, (1 - f) * sqrt(sinAlpha * sinAlpha + tmp * tmp));
lambda = atan2(sinSigma * sinAlpha1, cosU1 * cosSigma - sinU1 * sinSigma * cosAlpha1);
C = f / 16 * cosSqAlpha * (4 + f * (4 - 3 * cosSqAlpha));
L = lambda - (1 - C) * f * sinAlpha * (sigma + C * sinSigma * (cos2SigmaM + C * cosSigma * (-1 + 2 * cos2SigmaM * cos2SigmaM)));
lon2 = lon1 + L;
*toLat = RadiansToDegrees(lat2);
*toLon = RadiansToDegrees(lon2);
HostTraceOutputT(appErrorClass, "New =|%s|", DblToStr(*toLat, 5));
HostTraceOutputTL(appErrorClass, ": |%s|", DblToStr(*toLon, 5));
*/
// current method - bit of a fudge
double plat = 0.0, plon = 0.0;
double tlat = 0.0, tlon = 0.0;
double x_ad;
double y_ad;
double temprng, tempbrg;
if (isnan(fromLat) || isnan(fromLon))
return;
plat = DegreesToRadians(fromLat);
plon = DegreesToRadians(fromLon);
truebrg = nice_brg(truebrg);
x_ad = Fabs(sin(DegreesToRadians(truebrg)) * rng / NMPR) / cos(plat);
y_ad = Fabs(cos(DegreesToRadians(truebrg)) * rng / NMPR);
// temp fix to improve accuracy
LatLonToRangeBearingEll(fromLat, fromLon, RadiansToDegrees(plat + y_ad), RadiansToDegrees(plon + x_ad), &temprng, &tempbrg);
x_ad *= (rng/temprng)*(rng/temprng);
y_ad *= (rng/temprng)*(rng/temprng);
if (truebrg <= 90.0 || truebrg >= 270.0)
tlat = plat + y_ad;
else
tlat = plat - y_ad;
if (truebrg > 0.0 && truebrg < 180.0)
tlon = plon + x_ad;
else
tlon = plon - x_ad;
*toLat = RadiansToDegrees (tlat);
*toLon = RadiansToDegrees (_correct(tlon));
// HostTraceOutputT(appErrorClass, "Old =|%s|", DblToStr(*toLat, 5));
// HostTraceOutputTL(appErrorClass, ": |%s|", DblToStr(*toLon, 5));
// HostTraceOutputTL(appErrorClass, "--------------------");
}
/*****************************************************************************
* RangeBearingToLatLonLinearInterp - get the coordintes at the end of the
* range/bearing line
*****************************************************************************/
void RangeBearingToLatLonLinearInterp(double fromLat, double fromLon, double rng, double brg, double *toLatP, double *toLonP)
{
double plat = 0.0, plon = 0.0;
double tlat = 0.0, tlon = 0.0;
double x_ad;
double y_ad;
if (isnan(fromLat) || isnan(fromLon))
return;
plat = DegreesToRadians(fromLat);
plon = DegreesToRadians(fromLon);
brg = nice_brg(brg);
x_ad = Fabs(Sin(DegreesToRadians(brg)) * rng / NMPR) / data.input.coslat;
y_ad = Fabs(Cos(DegreesToRadians(brg)) * rng / NMPR);
if (brg <= 90.0 || brg >= 270.0)
tlat = plat + y_ad;