-
Notifications
You must be signed in to change notification settings - Fork 1
/
geographics.inc.php
91 lines (85 loc) · 3.19 KB
/
geographics.inc.php
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
<?php
// public static void LatLongFromMaidenhead(string maidenhead, out double latitude, out double longitude)
// {
// if (maidenhead == null || maidenhead.Length != 6)
// throw new ArgumentException("Locator must be 6 characters long", "maidenhead");
//
// // Get everything into nice workable ints :-)
// maidenhead = maidenhead.Trim().ToUpperInvariant();
// int fieldLong = maidenhead[0] - 'A';
// int fieldLat = maidenhead[1] - 'A';
// if (fieldLong > 18 || fieldLong < 0 || fieldLat > 18 || fieldLat < 0)
// throw new ArgumentException("Field is outside range A-R", "maidenhead");
// int squareLong = maidenhead[2] - '0';
// int squareLat = maidenhead[3] - '0';
// if (squareLong > 9 || squareLong < 0 || squareLat > 9 || squareLat < 0)
// throw new ArgumentException("Square must be 0-9", "maidenhead");
// int subLong = maidenhead[4] - 'A';
// int subLat = maidenhead[5] - 'A';
// if (subLong > 24 || subLong < 0 || subLat > 24 || subLat < 0)
// throw new ArgumentException("Subsquare must be a-x", "maidenhead");
//
// latitude = ((double)fieldLat * 10) + (double)squareLat + ((double)subLat / 24) - 90;
// longitude = ((double)fieldLong * 20) + (double)squareLong * 2 + ((double)subLong / 12) - 180;
// }
function letterToInt($str, $index) {
$alphabet = "abcdefghijklmnopqrstuvwxyz";
$justOurLetter = substr($str, $index, 1);
return strpos($alphabet, $justOurLetter, 0);
}
function numberToInt($str, $index) {
$alphabet = "0123456789";
$justOurLetter = substr($str, $index, 1);
return strpos($alphabet, $justOurLetter, 0);
}
function minLongFromMaidenhead ($maidenhead) {
$fieldLong = letterToInt($maidenhead, 0);
$longitude = $fieldLong * 20;
if (strlen($maidenhead) > 2) {
$squareLong = numberToInt($maidenhead, 2);
$longitude += $squareLong * 2;
}
if (strlen($maidenhead) > 4) {
$subLong = letterToInt($maidenhead, 4);
$longitude += $subLong / 12;
}
return $longitude - 180;
}
function minLatFromMaidenhead ($maidenhead) {
$fieldLong = letterToInt($maidenhead, 1);
$longitude = $fieldLong * 10;
if (strlen($maidenhead) > 2) {
$squareLong = numberToInt($maidenhead, 3);
$longitude += $squareLong;
}
if (strlen($maidenhead) > 4) {
$subLong = letterToInt($maidenhead, 5);
$longitude += $subLong / 24;
}
return $longitude - 90;
}
function maxLongFromMaidenhead($maidenhead) {
$figures = strlen($maidenhead);
if ($figures == 2) {
return minLongFromMaidenhead($maidenhead) + 20;
} elseif ($figures == 4) {
return minLongFromMaidenhead($maidenhead) + 2;
} elseif ($figures == 6) {
return minLongFromMaidenhead($maidenhead) + (1/12);
} else {
die("Invalid number of figures: " . $figures);
}
}
function maxLatFromMaidenhead($maidenhead) {
$figures = strlen($maidenhead);
if ($figures == 2) {
return minLatFromMaidenhead($maidenhead) + 10;
} elseif ($figures == 4) {
return minLatFromMaidenhead($maidenhead) + 1;
} elseif ($figures == 6) {
return minLatFromMaidenhead($maidenhead) + (1/24);
} else {
die("Invalid number of figures: " . $figures);
}
}
?>