-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclsGridRef.vb
1638 lines (1265 loc) · 59.9 KB
/
clsGridRef.vb
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
Imports System.Math
Public Class clsGridRef
'This code was produced by Richard Burkmar November 2005.
'Code taken from elsewhere is accredited in further comments.
'Please feel free to copy and use this code.
'No liability is accepted for screw-ups!
Public sGridRef As String
Public sErrorMessage As String
Public gsPrefix(50) As String
Public giEastingPrefix(50) As Integer
Public giNorthingPrefix(50) As Integer
Public miElement As Integer
Public i As Integer
Public sRefType As String
Public bSuffixFound As Boolean
Public fWidth As Double
Public msPolyline As String
Public msPoint As String
Public zoomLevel As Double
Public s100 As String = ""
Public sHectad As String = ""
Public sQuadrantSuffixes As String = ""
Public sQuadrant() As String = {"", "", "", "", ""}
Public sTetrad As String = ""
Public sMonad As String = ""
Public s6Fig As String = ""
Public s8Fig As String = ""
Public s10Fig As String = ""
Public sEastingLL As String
Public sNorthingLL As String
Public sEastingC As String
Public sNorthingC As String
Public East As Double
Public North As Double
'Lat/Long conversion constants - these are drawn taken from the first
'page of the spreadsheet produced by the OS to demonstrate coordinate
'conversions. This could be found here:
'http://www.gps.gov.uk/additionalInfo/gpsSpreadsheet.asp
'in November 2005.
Dim a As Double
Dim b As Double
Dim e0 As Double
Dim n0 As Double
Dim f0 As Double
Dim PHI0 As Double
Dim LAM0 As Double
Property GridRef() As String
Get
Return sGridRef
End Get
Set(ByVal Value As String)
sGridRef = Value.Replace(" ", "")
End Set
End Property
Sub ErrorMessage(ByVal sError As String)
If sErrorMessage <> "" Then
sErrorMessage = sErrorMessage & " "
End If
sErrorMessage = sErrorMessage & sError
End Sub
Sub MakePrefixArrays()
a = 6377563.396
b = 6356256.91
e0 = 400000
n0 = -100000
f0 = 0.9996012717
PHI0 = 49
LAM0 = -2
Call ArrayElements("SV", 0, 0)
Call ArrayElements("SW", 1, 0)
Call ArrayElements("SX", 2, 0)
Call ArrayElements("SY", 3, 0)
Call ArrayElements("SZ", 4, 0)
Call ArrayElements("TV", 5, 0)
Call ArrayElements("SR", 1, 1)
Call ArrayElements("SS", 2, 1)
Call ArrayElements("ST", 3, 1)
Call ArrayElements("SU", 4, 1)
Call ArrayElements("TQ", 5, 1)
Call ArrayElements("TR", 6, 1)
Call ArrayElements("SM", 1, 2)
Call ArrayElements("SN", 2, 2)
Call ArrayElements("SO", 3, 2)
Call ArrayElements("SP", 4, 2)
Call ArrayElements("TL", 5, 2)
Call ArrayElements("TM", 6, 2)
Call ArrayElements("SH", 2, 3)
Call ArrayElements("SJ", 3, 3)
Call ArrayElements("SK", 4, 3)
Call ArrayElements("TF", 5, 3)
Call ArrayElements("TG", 6, 3)
Call ArrayElements("SC", 2, 4)
Call ArrayElements("SD", 3, 4)
Call ArrayElements("SE", 4, 4)
Call ArrayElements("TA", 5, 4)
Call ArrayElements("NW", 1, 5)
Call ArrayElements("NX", 2, 5)
Call ArrayElements("NY", 3, 5)
Call ArrayElements("NZ", 4, 5)
Call ArrayElements("NR", 1, 6)
Call ArrayElements("NS", 2, 6)
Call ArrayElements("NT", 3, 6)
Call ArrayElements("NU", 4, 6)
Call ArrayElements("NL", 0, 7)
Call ArrayElements("NM", 1, 7)
Call ArrayElements("NN", 2, 7)
Call ArrayElements("NO", 3, 7)
'Call ArrayElements("HY", 5, 7)
'Call ArrayElements("HZ", 6, 7)
Call ArrayElements("NF", 0, 8)
Call ArrayElements("NG", 1, 8)
Call ArrayElements("NH", 2, 8)
Call ArrayElements("NJ", 3, 8)
Call ArrayElements("NK", 4, 8)
'Call ArrayElements("HT", 5, 8)
'Call ArrayElements("HU", 6, 8)
Call ArrayElements("NA", 0, 9)
Call ArrayElements("NB", 1, 9)
Call ArrayElements("NC", 2, 9)
Call ArrayElements("ND", 3, 9)
'Call ArrayElements("HT", 3, 10)
'Call ArrayElements("HU", 4, 10)
'Call ArrayElements("HP", 4, 11)
End Sub
Sub ArrayElements(ByVal sTilePrefix As String, ByVal iEastingPrefix As Integer, ByVal iNorthingPrefix As Integer)
miElement = miElement + 1
gsPrefix(miElement) = sTilePrefix
giEastingPrefix(miElement) = iEastingPrefix
giNorthingPrefix(miElement) = iNorthingPrefix
End Sub
Function EN26fig(ByVal dblEasting As Double, ByVal dblNorthing As Double) As String
Dim i As Integer
Dim strEasting As String = CStr(dblEasting \ 1)
Dim strNorthing As String = CStr(dblNorthing \ 1)
If strEasting.Length = 5 Then
strEasting = "0" & strEasting
End If
If strNorthing.Length = 5 Then
strNorthing = "0" & strNorthing
End If
Dim strPrefix As String = ""
For i = 1 To miElement
If giEastingPrefix(i) = dblEasting \ 100000 And _
giNorthingPrefix(i) = dblNorthing \ 100000 Then
strPrefix = gsPrefix(i)
Exit For
End If
Next
EN26fig = strPrefix & strEasting.Substring(1, 3) & strNorthing.Substring(1, 3)
End Function
Function EN28fig(ByVal dblEasting As Double, ByVal dblNorthing As Double) As String
Dim i As Integer
Dim strEasting As String = CStr(dblEasting \ 1)
Dim strNorthing As String = CStr(dblNorthing \ 1)
Dim strPrefix As String = ""
If strEasting.Length = 5 Then
strEasting = "0" & strEasting
End If
If strNorthing.Length = 5 Then
strNorthing = "0" & strNorthing
End If
For i = 1 To miElement
If giEastingPrefix(i) = dblEasting \ 100000 And _
giNorthingPrefix(i) = dblNorthing \ 100000 Then
strPrefix = gsPrefix(i)
Exit For
End If
Next
EN28fig = strPrefix & strEasting.Substring(1, 4) & strNorthing.Substring(1, 4)
End Function
Function EN210fig(ByVal dblEasting As Double, ByVal dblNorthing As Double) As String
Dim i As Integer
Dim strEasting As String = CStr(dblEasting \ 1)
Dim strNorthing As String = CStr(dblNorthing \ 1)
Dim strPrefix As String = ""
If strEasting.Length = 5 Then
strEasting = "0" & strEasting
End If
If strNorthing.Length = 5 Then
strNorthing = "0" & strNorthing
End If
For i = 1 To miElement
If giEastingPrefix(i) = dblEasting \ 100000 And _
giNorthingPrefix(i) = dblNorthing \ 100000 Then
strPrefix = gsPrefix(i)
Exit For
End If
Next
EN210fig = strPrefix & strEasting.Substring(1, 5) & strNorthing.Substring(1, 5)
End Function
Function EN2Monad(ByVal dblEasting As Double, ByVal dblNorthing As Double) As String
Dim i As Integer
Dim strEasting As String = CStr(dblEasting \ 1)
Dim strNorthing As String = CStr(dblNorthing \ 1)
Dim strPrefix As String = ""
If strEasting.Length = 5 Then
strEasting = "0" & strEasting
End If
If strNorthing.Length = 5 Then
strNorthing = "0" & strNorthing
End If
For i = 1 To miElement
If giEastingPrefix(i) = dblEasting \ 100000 And _
giNorthingPrefix(i) = dblNorthing \ 100000 Then
strPrefix = gsPrefix(i)
Exit For
End If
Next
EN2Monad = strPrefix & strEasting.Substring(1, 2) & strNorthing.Substring(1, 2)
End Function
Function EN2Tetrad(ByVal dblEasting As Double, ByVal dblNorthing As Double) As String
Dim i As Integer
Dim strEasting As String = CStr(dblEasting \ 1)
Dim strNorthing As String = CStr(dblNorthing \ 1)
Dim strPrefix As String = ""
If strEasting.Length = 5 Then
strEasting = "0" & strEasting
End If
If strNorthing.Length = 5 Then
strNorthing = "0" & strNorthing
End If
For i = 1 To miElement
If giEastingPrefix(i) = dblEasting \ 100000 And _
giNorthingPrefix(i) = dblNorthing \ 100000 Then
strPrefix = gsPrefix(i)
Exit For
End If
Next
EN2Tetrad = strPrefix & strEasting.Substring(1, 1) & strNorthing.Substring(1, 1) & Mon2TetSuf(strEasting.Substring(2, 1), strNorthing.Substring(2, 1))
End Function
Function EN2Hectad(ByVal dblEasting As Double, ByVal dblNorthing As Double) As String
Dim i As Integer
Dim strEasting As String = CStr(dblEasting \ 1)
Dim strNorthing As String = CStr(dblNorthing \ 1)
Dim strPrefix As String = ""
If strEasting.Length = 5 Then
strEasting = "0" & strEasting
End If
If strNorthing.Length = 5 Then
strNorthing = "0" & strNorthing
End If
For i = 1 To miElement
If giEastingPrefix(i) = dblEasting \ 100000 And _
giNorthingPrefix(i) = dblNorthing \ 100000 Then
strPrefix = gsPrefix(i)
Exit For
End If
Next
EN2Hectad = strPrefix & strEasting.Substring(1, 1) & strNorthing.Substring(1, 1)
End Function
Function Prefix2Easting(ByVal sPrefix As String) As String
Dim i As Integer
Dim sEasting As String = ""
For i = 1 To miElement
If gsPrefix(i) = UCase(sPrefix) Then
sEasting = CStr(giEastingPrefix(i))
Exit For
End If
Next
Prefix2Easting = sEasting
End Function
Function Prefix2Northing(ByVal sPrefix As String) As String
Dim i As Integer
Dim sNorthing As String = ""
For i = 1 To miElement
If gsPrefix(i) = UCase(sPrefix) Then
sNorthing = CStr(giNorthingPrefix(i))
Exit For
End If
Next
Prefix2Northing = sNorthing
End Function
Function TetSuf2QuadSuf(ByVal sTetSuf As String) As String
TetSuf2QuadSuf = ""
If InStr(1, "ABFG", UCase(sTetSuf)) Then
TetSuf2QuadSuf = "SW"
ElseIf InStr(1, "DEIJ", UCase(sTetSuf)) Then
TetSuf2QuadSuf = "NW"
ElseIf InStr(1, "QRVW", UCase(sTetSuf)) Then
TetSuf2QuadSuf = "SE"
ElseIf InStr(1, "TUYZ", UCase(sTetSuf)) Then
TetSuf2QuadSuf = "NE"
ElseIf InStr(1, "LK", UCase(sTetSuf)) Then
TetSuf2QuadSuf = "SW,SE"
ElseIf InStr(1, "PN", UCase(sTetSuf)) Then
TetSuf2QuadSuf = "NW/NE"
ElseIf InStr(1, "CH", UCase(sTetSuf)) Then
TetSuf2QuadSuf = "NW/SW"
ElseIf InStr(1, "SX", UCase(sTetSuf)) Then
TetSuf2QuadSuf = "NE/SE"
ElseIf UCase(sTetSuf) = "M" Then
TetSuf2QuadSuf = "NW/NE/SW/SE"
End If
End Function
Function Mon2QuadSuf(ByVal sE As String, ByVal sN As String) As String
Dim sNS As String
Dim sEW As String
If CInt(sE) < 5 Then
sEW = "W"
Else
sEW = "E"
End If
If CInt(sN) < 5 Then
sNS = "S"
Else
sNS = "N"
End If
Mon2QuadSuf = sNS & sEW
End Function
Function Mon2TetSuf(ByVal sE As String, ByVal sN As String) As String
Dim intE As Integer
Dim intN As Integer
intE = CInt(sE)
intN = CInt(sN)
Mon2TetSuf = ""
If intE < 2 And intN < 2 Then Mon2TetSuf = "A"
If intE < 2 And intN >= 2 And Int(sN) < 4 Then Mon2TetSuf = "B"
If intE < 2 And intN >= 4 And Int(sN) < 6 Then Mon2TetSuf = "C"
If intE < 2 And intN >= 6 And Int(sN) < 8 Then Mon2TetSuf = "D"
If intE < 2 And intN >= 8 Then Mon2TetSuf = "E"
If intE < 4 And intE >= 2 And intN < 2 Then Mon2TetSuf = "F"
If intE < 4 And intE >= 2 And intN >= 2 And intN < 4 Then Mon2TetSuf = "G"
If intE < 4 And intE >= 2 And intN >= 4 And intN < 6 Then Mon2TetSuf = "H"
If intE < 4 And intE >= 2 And intN >= 6 And intN < 8 Then Mon2TetSuf = "I"
If intE < 4 And intE >= 2 And intN >= 8 Then Mon2TetSuf = "J"
If intE < 6 And intE >= 4 And intN < 2 Then Mon2TetSuf = "K"
If intE < 6 And intE >= 4 And intN >= 2 And intN < 4 Then Mon2TetSuf = "L"
If intE < 6 And intE >= 4 And intN >= 4 And intN < 6 Then Mon2TetSuf = "M"
If intE < 6 And intE >= 4 And intN >= 6 And intN < 8 Then Mon2TetSuf = "N"
If intE < 6 And intE >= 4 And intN >= 8 Then Mon2TetSuf = "P"
If intE < 8 And intE >= 6 And intN < 2 Then Mon2TetSuf = "Q"
If intE < 8 And intE >= 6 And intN >= 2 And intN < 4 Then Mon2TetSuf = "R"
If intE < 8 And intE >= 6 And intN >= 4 And intN < 6 Then Mon2TetSuf = "S"
If intE < 8 And intE >= 6 And intN >= 6 And intN < 8 Then Mon2TetSuf = "T"
If intE < 8 And intE >= 6 And intN >= 8 Then Mon2TetSuf = "U"
If intE >= 8 And intN < 2 Then Mon2TetSuf = "V"
If intE >= 8 And intN >= 2 And intN < 4 Then Mon2TetSuf = "W"
If intE >= 8 And intN >= 4 And intN < 6 Then Mon2TetSuf = "X"
If intE >= 8 And intN >= 6 And intN < 8 Then Mon2TetSuf = "Y"
If intE >= 8 And intN >= 8 Then Mon2TetSuf = "Z"
End Function
Function QuadSuf2DX(ByVal sSuffix As String, ByVal bCentre As String) As String
If UCase(sSuffix) = "NW" Or UCase(sSuffix) = "SW" Then
If bCentre Then
QuadSuf2DX = "2500"
Else
QuadSuf2DX = "0000"
End If
Else
If bCentre Then
QuadSuf2DX = "7500"
Else
QuadSuf2DX = "5000"
End If
End If
End Function
Function QuadSuf2DY(ByVal sSuffix As String, ByVal bCentre As String) As String
If UCase(sSuffix) = "SW" Or UCase(sSuffix) = "SE" Then
If bCentre Then
QuadSuf2DY = "2500"
Else
QuadSuf2DY = "0000"
End If
Else
If bCentre Then
QuadSuf2DY = "7500"
Else
QuadSuf2DY = "5000"
End If
End If
End Function
Function TetSuf2DX(ByVal sSuffix As String, ByVal bCentre As String) As String
If InStr(1, "ABCDE", UCase(sSuffix)) Then
If bCentre Then
TetSuf2DX = "1000"
Else
TetSuf2DX = "0000"
End If
ElseIf InStr(1, "FGHIJ", UCase(sSuffix)) Then
If bCentre Then
TetSuf2DX = "3000"
Else
TetSuf2DX = "2000"
End If
ElseIf InStr(1, "KLMNP", UCase(sSuffix)) Then
If bCentre Then
TetSuf2DX = "5000"
Else
TetSuf2DX = "4000"
End If
ElseIf InStr(1, "QRSTU", UCase(sSuffix)) Then
If bCentre Then
TetSuf2DX = "7000"
Else
TetSuf2DX = "6000"
End If
Else
If bCentre Then
TetSuf2DX = "9000"
Else
TetSuf2DX = "8000"
End If
End If
End Function
Function TetSuf2DY(ByVal sSuffix As String, ByVal bCentre As String) As String
If InStr(1, "AFKQV", UCase(sSuffix)) Then
If bCentre Then
TetSuf2DY = "1000"
Else
TetSuf2DY = "0000"
End If
ElseIf InStr(1, "BGLRW", UCase(sSuffix)) Then
If bCentre Then
TetSuf2DY = "3000"
Else
TetSuf2DY = "2000"
End If
ElseIf InStr(1, "CHMSX", UCase(sSuffix)) Then
If bCentre Then
TetSuf2DY = "5000"
Else
TetSuf2DY = "4000"
End If
ElseIf InStr(1, "DINTY", UCase(sSuffix)) Then
If bCentre Then
TetSuf2DY = "7000"
Else
TetSuf2DY = "6000"
End If
Else
If bCentre Then
TetSuf2DY = "9000"
Else
TetSuf2DY = "8000"
End If
End If
End Function
Function TestNumeric(ByVal sString As String) As Boolean
Dim i As Integer
For i = 1 To Len(sString)
If Not InStr(1, "0123456789", Mid(sString, i, 1)) > 0 Then
TestNumeric = False
Exit Function
End If
Next
TestNumeric = True
End Function
Function TestQuadrantSuffix(ByVal sString As String) As Boolean
If Not UCase(sString) = "SW" And Not UCase(sString) = "SE" And Not UCase(sString) = "NW" And Not UCase(sString) = "NE" Then
TestQuadrantSuffix = False
Exit Function
End If
TestQuadrantSuffix = True
End Function
Function TestTetradSuffix(ByVal sString As String) As Boolean
If Not InStr(1, "ABCDEFGHIJKLMNPQRSTUVWXYZ", UCase(sString)) > 0 Then
TestTetradSuffix = False
Exit Function
End If
TestTetradSuffix = True
End Function
Sub ParseGridRef(ByVal bMin As Boolean)
Dim i As Integer
If Len(sGridRef) < 2 Then
ErrorMessage("The grid reference must be at least two characters long.")
ElseIf Len(sGridRef) = 2 Then
sRefType = "100km"
End If
bSuffixFound = False
For i = 1 To miElement
If gsPrefix(i) = UCase(Left(sGridRef, 2)) Then
bSuffixFound = True
Exit For
End If
Next
If Not bSuffixFound Then
ErrorMessage(UCase(Left(sGridRef, 2)) & " is not a valid suffix for a grid reference.")
End If
If Len(sGridRef) = 3 Then
ErrorMessage("A grid reference cannot be 3 characters long.")
End If
If Len(sGridRef) = 7 Then
ErrorMessage("A grid reference cannot be 7 characters long.")
End If
If Len(sGridRef) = 9 Then
ErrorMessage("A grid reference cannot be 9 characters long.")
End If
If Len(sGridRef) = 11 Then
ErrorMessage("A grid reference cannot be 11 characters long.")
End If
If Len(sGridRef) > 12 Then
ErrorMessage("The grid reference must not be more than 12 characters long.")
End If
If Len(sGridRef) = 4 Then
sRefType = "hectad"
If Not TestNumeric(Mid(sGridRef, 3, 2)) Then
ErrorMessage("Invalid hectad reference: characters 3 and 4 must be numeric.")
End If
End If
If Len(sGridRef) = 5 Then
sRefType = "tetrad"
If Not TestNumeric(Mid(sGridRef, 3, 2)) Then
ErrorMessage("Invalid tetrad reference: characters 3 and 4 must be numeric.")
End If
If Not TestTetradSuffix(Right(sGridRef, 1)) Then
ErrorMessage("Invalid tetrad reference: last character must be a letter (but not o).")
End If
End If
If Len(sGridRef) = 6 Then
If TestNumeric(Mid(sGridRef, 3, 4)) Then
sRefType = "monad"
Else
sRefType = "quadrant"
If Not TestNumeric(Mid(sGridRef, 3, 2)) Then
ErrorMessage("Invalid monad or quadrant reference: characters 3 and 4 must be numeric.")
ElseIf Not TestQuadrantSuffix(Right(sGridRef, 2)) Then
ErrorMessage("Invalid quadrant reference: last two characters must be SW, SE, NW or NE.")
Else
If bMin Then
'ErrorMessage("You've specified a quadrant reference, but for the atlas the grid reference must resolve at least to tetrad level.")
End If
End If
End If
End If
If Len(sGridRef) = 8 Then
sRefType = "6fig"
If Not TestNumeric(Mid(sGridRef, 3, 6)) Then
ErrorMessage("Invalid 6 figure grid reference: characters 3-8 must be numeric.")
End If
End If
If Len(sGridRef) = 10 Then
sRefType = "8fig"
If Not TestNumeric(Mid(sGridRef, 3, 8)) Then
ErrorMessage("Invalid 8 figure grid reference: characters 3-10 must be numeric.")
End If
End If
If Len(sGridRef) = 12 Then
sRefType = "10fig"
If Not TestNumeric(Mid(sGridRef, 3, 10)) Then
ErrorMessage("Invalid 10 figure grid reference: characters 3-12 must be numeric.")
End If
End If
If bMin And Len(sGridRef) < 5 Then
'ErrorMessage("For the atlas, the grid reference must resolve at least to tetrad level (i.e. must be five characters or more).")
End If
End Sub
Function E_N_to_Lat(ByVal East As Double, ByVal North As Double, ByVal a As Double, ByVal b As Double, ByVal e0 As Double, ByVal n0 As Double, ByVal f0 As Double, ByVal PHI0 As Double, ByVal LAM0 As Double) As Double
'This function taken from the first
'the spreadsheet produced by the OS to demonstrate coordinate
'conversions. This could be found here:
'http://www.gps.gov.uk/additionalInfo/gpsSpreadsheet.asp
'in November 2005.
'Un-project Transverse Mercator eastings and northings back to latitude.
'Input: - _
'eastings (East) and northings (North) in meters; _
' ellipsoid axis dimensions (a & b) in meters; _
' eastings (e0) and northings (n0) of false origin in meters; _
' central meridian scale factor (f0) and _
' latitude (PHI0) and longitude (LAM0) of false origin in decimal degrees.
'REQUIRES THE "Marc" AND "InitialLat" FUNCTIONS
Dim Pi As Double
Dim RadPHI0 As Double
Dim RadLAM0 As Double
Dim af0 As Double
Dim bf0 As Double
Dim e2 As Double
Dim n As Double
Dim Et As Double
Dim PHId As Double
Dim nu As Double
Dim rho As Double
Dim eta2 As Double
Dim VII As Double
Dim VIII As Double
Dim IX As Double
'Convert angle measures to radians
Pi = 3.14159265358979
RadPHI0 = PHI0 * (Pi / 180)
RadLAM0 = LAM0 * (Pi / 180)
'Compute af0, bf0, e squared (e2), n and Et
af0 = a * f0
bf0 = b * f0
e2 = ((af0 ^ 2) - (bf0 ^ 2)) / (af0 ^ 2)
n = (af0 - bf0) / (af0 + bf0)
Et = East - e0
'Compute initial value for latitude (PHI) in radians
PHId = InitialLat(North, n0, af0, RadPHI0, n, bf0)
'Compute nu, rho and eta2 using value for PHId
nu = af0 / (Sqrt(1 - (e2 * ((Sin(PHId)) ^ 2))))
rho = (nu * (1 - e2)) / (1 - (e2 * (Sin(PHId)) ^ 2))
eta2 = (nu / rho) - 1
'Compute Latitude
VII = (Tan(PHId)) / (2 * rho * nu)
VIII = ((Tan(PHId)) / (24 * rho * (nu ^ 3))) * (5 + (3 * ((Tan(PHId)) ^ 2)) + eta2 - (9 * eta2 * ((Tan(PHId)) ^ 2)))
IX = ((Tan(PHId)) / (720 * rho * (nu ^ 5))) * (61 + (90 * ((Tan(PHId)) ^ 2)) + (45 * ((Tan(PHId)) ^ 4)))
E_N_to_Lat = (180 / Pi) * (PHId - ((Et ^ 2) * VII) + ((Et ^ 4) * VIII) - ((Et ^ 6) * IX))
End Function
Function Marc(ByVal bf0 As Double, ByVal n As Double, ByVal PHI0 As Double, ByVal PHI As Double) As Double
'This function taken from the first
'the spreadsheet produced by the OS to demonstrate coordinate
'conversions. This could be found here:
'http://www.gps.gov.uk/additionalInfo/gpsSpreadsheet.asp
'in November 2005.
'Compute meridional arc.
'Input: - _
'ellipsoid semi major axis multiplied by central meridian scale factor (bf0) in meters; _
'n (computed from a, b and f0); _
'lat of false origin (PHI0) and initial or final latitude of point (PHI) IN RADIANS.
'THIS FUNCTION IS CALLED BY THE - "Lat_Long_to_North" and "InitialLat" FUNCTIONS
'THIS FUNCTION IS ALSO USED ON IT'S OWN IN THE "Projection and Transformation Calculations.xls" SPREADSHEET
Marc = bf0 * (((1 + n + ((5 / 4) * (n ^ 2)) + ((5 / 4) * (n ^ 3))) * (PHI - PHI0)) _
- (((3 * n) + (3 * (n ^ 2)) + ((21 / 8) * (n ^ 3))) * (Sin(PHI - PHI0)) * (Cos(PHI + PHI0))) _
+ ((((15 / 8) * (n ^ 2)) + ((15 / 8) * (n ^ 3))) * (Sin(2 * (PHI - PHI0))) * (Cos(2 * (PHI + PHI0)))) _
- (((35 / 24) * (n ^ 3)) * (Sin(3 * (PHI - PHI0))) * (Cos(3 * (PHI + PHI0)))))
End Function
Function InitialLat(ByVal North As Double, ByVal n0 As Double, ByVal afo As Double, ByVal PHI0 As Double, ByVal n As Double, ByVal bfo As Double) As Double
'This function taken from the first
'the spreadsheet produced by the OS to demonstrate coordinate
'conversions. This could be found here:
'http://www.gps.gov.uk/additionalInfo/gpsSpreadsheet.asp
'in November 2005.
'Compute initial value for Latitude (PHI) IN RADIANS.
'Input: - _
' northing of point (North) and northing of false origin (n0) in meters; _
' semi major axis multiplied by central meridian scale factor (af0) in meters; _
' latitude of false origin (PHI0) IN RADIANS; _
' n (computed from a, b and f0) and _
' ellipsoid semi major axis multiplied by central meridian scale factor (bf0) in meters.
'REQUIRES THE "Marc" FUNCTION
'THIS FUNCTION IS CALLED BY THE "E_N_to_Lat", "E_N_to_Long" and "E_N_to_C" FUNCTIONS
'THIS FUNCTION IS ALSO USED ON IT'S OWN IN THE "Projection and Transformation Calculations.xls" SPREADSHEET
Dim PHI1 As Double
Dim M As Double
Dim PHI2 As Double
'First PHI value (PHI1)
PHI1 = ((North - n0) / afo) + PHI0
'Calculate M
M = Marc(bfo, n, PHI0, PHI1)
'Calculate new PHI value (PHI2)
PHI2 = ((North - n0 - M) / afo) + PHI1
'Iterate to get final value for InitialLat
Do While Abs(North - n0 - M) > 0.00001
PHI2 = ((North - n0 - M) / afo) + PHI1
M = Marc(bfo, n, PHI0, PHI2)
PHI1 = PHI2
Loop
InitialLat = PHI2
End Function
Function E_N_to_Long(ByVal East As Double, ByVal North As Double, ByVal a As Double, ByVal b As Double, ByVal e0 As Double, ByVal n0 As Double, ByVal f0 As Double, ByVal PHI0 As Double, ByVal LAM0 As Double) As Double
'This function taken from the first
'the spreadsheet produced by the OS to demonstrate coordinate
'conversions. This could be found here:
'http://www.gps.gov.uk/additionalInfo/gpsSpreadsheet.asp
'in November 2005.
'Un-project Transverse Mercator eastings and northings back to longitude.
'Input: - _
' eastings (East) and northings (North) in meters; _
' ellipsoid axis dimensions (a & b) in meters; _
' eastings (e0) and northings (n0) of false origin in meters; _
' central meridian scale factor (f0) and _
' latitude (PHI0) and longitude (LAM0) of false origin in decimal degrees.
'REQUIRES THE "Marc" AND "InitialLat" FUNCTIONS
Dim Pi As Double
Dim RadPHI0 As Double
Dim RadLAM0 As Double
Dim af0 As Double
Dim bf0 As Double
Dim e2 As Double
Dim n As Double
Dim Et As Double
Dim PHId As Double
Dim nu As Double
Dim rho As Double
Dim eta2 As Double
Dim X As Double
Dim XI As Double
Dim XII As Double
Dim XIIA As Double
'Convert angle measures to radians
Pi = 3.14159265358979
RadPHI0 = PHI0 * (Pi / 180)
RadLAM0 = LAM0 * (Pi / 180)
'Compute af0, bf0, e squared (e2), n and Et
af0 = a * f0
bf0 = b * f0
e2 = ((af0 ^ 2) - (bf0 ^ 2)) / (af0 ^ 2)
n = (af0 - bf0) / (af0 + bf0)
Et = East - e0
'Compute initial value for latitude (PHI) in radians
PHId = InitialLat(North, n0, af0, RadPHI0, n, bf0)
'Compute nu, rho and eta2 using value for PHId
nu = af0 / (Sqrt(1 - (e2 * ((Sin(PHId)) ^ 2))))
rho = (nu * (1 - e2)) / (1 - (e2 * (Sin(PHId)) ^ 2))
eta2 = (nu / rho) - 1
'Compute Longitude
X = ((Cos(PHId)) ^ -1) / nu
XI = (((Cos(PHId)) ^ -1) / (6 * (nu ^ 3))) * ((nu / rho) + (2 * ((Tan(PHId)) ^ 2)))
XII = (((Cos(PHId)) ^ -1) / (120 * (nu ^ 5))) * (5 + (28 * ((Tan(PHId)) ^ 2)) + (24 * ((Tan(PHId)) ^ 4)))
XIIA = (((Cos(PHId)) ^ -1) / (5040 * (nu ^ 7))) * (61 + (662 * ((Tan(PHId)) ^ 2)) + (1320 * ((Tan(PHId)) ^ 4)) + (720 * ((Tan(PHId)) ^ 6)))
E_N_to_Long = (180 / Pi) * (RadLAM0 + (Et * X) - ((Et ^ 3) * XI) + ((Et ^ 5) * XII) - ((Et ^ 7) * XIIA))
End Function
Function Lat_Long_to_East(ByVal PHI As Double, ByVal LAM As Double, ByVal a As Double, ByVal b As Double, ByVal e0 As Double, ByVal f0 As Double, ByVal PHI0 As Double, ByVal LAM0 As Double) As Double
'Project Latitude and longitude to Transverse Mercator eastings.
'Input: -
'Latitude (PHI) and Longitude (LAM) in decimal degrees;
'ellipsoid axis dimensions (a & b) in meters;
'eastings of false origin (e0) in meters;
'central meridian scale factor (f0);
'latitude (PHI0) and longitude (LAM0) of false origin in decimal degrees.
Dim Pi As Double
Dim RadPHI0 As Double
Dim RadLAM0 As Double
Dim RadPHI As Double
Dim RadLAM As Double
Dim af0 As Double
Dim bf0 As Double
Dim e2 As Double
Dim n As Double
Dim nu As Double
Dim rho As Double
Dim eta2 As Double
Dim p As Double
Dim IV As Double
Dim V As Double
Dim VI As Double
'Convert angle measures to radians
Pi = 3.14159265358979
RadPHI = PHI * (Pi / 180)
RadLAM = LAM * (Pi / 180)
RadPHI0 = PHI0 * (Pi / 180)
RadLAM0 = LAM0 * (Pi / 180)
af0 = a * f0
bf0 = b * f0
e2 = ((af0 ^ 2) - (bf0 ^ 2)) / (af0 ^ 2)
n = (af0 - bf0) / (af0 + bf0)
nu = af0 / (Sqrt(1 - (e2 * ((Sin(RadPHI)) ^ 2))))
rho = (nu * (1 - e2)) / (1 - (e2 * (Sin(RadPHI)) ^ 2))
eta2 = (nu / rho) - 1
p = RadLAM - RadLAM0
IV = nu * (Cos(RadPHI))
V = (nu / 6) * ((Cos(RadPHI)) ^ 3) * ((nu / rho) - ((Tan(RadPHI) ^ 2)))
VI = (nu / 120) * ((Cos(RadPHI)) ^ 5) * (5 - (18 * ((Tan(RadPHI)) ^ 2)) + ((Tan(RadPHI)) ^ 4) + (14 * eta2) - (58 * ((Tan(RadPHI)) ^ 2) * eta2))
Lat_Long_to_East = e0 + (p * IV) + ((p ^ 3) * V) + ((p ^ 5) * VI)
End Function
Function Lat_Long_to_North(ByVal PHI As Double, ByVal LAM As Double, ByVal a As Double, ByVal b As Double, ByVal e0 As Double, ByVal n0 As Double, ByVal f0 As Double, ByVal PHI0 As Double, ByVal LAM0 As Double) As Double
'Project Latitude and longitude to Transverse Mercator northings
'Input: - Latitude (PHI) and Longitude (LAM) in decimal degrees;
'ellipsoid axis dimensions (a & b) in meters;
'eastings (e0) and northings (n0) of false origin in meters;
'central meridian scale factor (f0);
'latitude (PHI0) and longitude (LAM0) of false origin in decimal degrees.
'REQUIRES THE "Marc" FUNCTION
Dim Pi As Double
Dim RadPHI0 As Double
Dim RadLAM0 As Double
Dim RadPHI As Double
Dim RadLAM As Double
Dim af0 As Double
Dim bf0 As Double
Dim e2 As Double
Dim n As Double
Dim nu As Double
Dim rho As Double
Dim eta2 As Double
Dim p As Double
Dim M As Double
Dim I As Double
Dim II As Double
Dim III As Double
Dim IIIa As Double
'Convert angle measures to radians
Pi = 3.14159265358979