Recently I was asked by a HAM Radio friend to work on a conversion of latitude and longitude coordinates to maidenhead grid.
The code below is in “rough” PHP code on purpose to help illustrate the math behind the conversion. Feel free to clean/optimize it all I ask is that you give credit where due.
I was able to do the majority of the code thanks to Les Peter (Link Removed due to reports of bad link)
Les Peter’s Document (Pulled from Wayback Machine, now hosted here to give credit where due)
You can test the code below with debug https://www.kittell.net/tools/LatLng2Maidenhead.php?lat=-33.931833&lng=18.388544&debug=true or without debug https://www.kittell.net/tools/LatLng2Maidenhead.php?lat=-33.931833&lng=18.388544
<?php /* Author: David Kittell Twitter: @dkittell (https://twitter.com/dkittell) The code below is in "rough" PHP code on purpose to help illustrate the math behind the conversion. Feel free to clean/optimize it all I ask is that you give credit where due. I was able to do the majority of the code thanks to Les Peter's document at http://n1sv.com/PROJECTS/How%20to%20calculate%20your%208-digit%20grid%20square.pdf */ $latInput = $_GET["lat"]; $lonInput = $_GET["lng"]; $debug = $_GET["debug"]; // $latInput = 42.664048; // $lonInput = -71.661962; if ($lonInput >= 180 || $lonInput <= -180) { return "Longitude Value Incorrect"; } if ($latInput >= 90 || $latInput <= -90) { return "Latitude Value Incorrect"; } $letterA = ord('A'); $numberZero = ord('0'); #region Longitude $lon = $lonInput; // ex. -71.661962 $lon1 = $lon + 180; // ex. -71.661962 + 180 = 108.338038 $lon2 = ($lon1 / 20); // ex. 108.338038 / 20 = 5.4169019 $lon2N = intval($lon2); // ex. 5 $lon2L = chr($letterA + $lon2N); // ex. F $lon2R = ($lon2 - $lon2N) * 10; // ex. (5.4169019 - 5) * 10 = 4.169019 $lon3 = $lon2R; // ex. 4.169019 $lon3N = intval($lon3); // ex. 4 $lon3R = ($lon3 - $lon3N) * 2; // ex. (4.169019 - 4) * 2 = 0.338038 $lon4 = ($lon3R / 0.083333); // ex. 0.338038 / 0.083333 = 4.056472225888904 $lon4N = intval($lon4); // ex. 4 $lon4L = strtolower(chr($letterA + $lon4N)); // ex. e $lon4R = ($lon4 - $lon4N); // ex. 4.056472225888904 - 4 = 0.056472225888904 $lon5 = $lon4R * 10; // ex. 0.056472225888904 * 10 = 0.56472225888904 $lon5N = intval($lon5); // ex. 0 $lon5R = $lon5 - $lon5N; // ex. 0.56472225888904 - 0 = 0.56472225888904 $lon6 = ($lon5R / 5) / 0.008333; // ex. (0.56472225888904 / 5) / 0.008333 = 13.55387637 // ex. 0.112944452 / 0.008333 = 13.55387637 $lon6N = intval($lon6); // ex. 13 $lon6L = strtolower(chr($letterA + $lon6N)); // ex. e #endregion Longitude #region Latitude $lat = $latInput; // ex. 42.664048 $lat1 = $lat + 90; // ex. 42.664048 + 90 = 132.664048 $lat2 = (($lat1 / 10)); // ex. 132.664048 / 10 = 13.2664048 $lat2N = intval($lat2); // ex. 13 $lat2L = chr($letterA + $lat2N); // ex. N $lat2R = ($lat2 - $lat2N) * 10; // ex. (13.2664048 - 13) * 10 = 2.664048 $lat3 = $lat2R; // ex. 2.664048 $lat3N = intval($lat3); // ex. 2 $lat3R = ($lat3 - $lat3N); // ex. 2.664048 - 2 = 0.664048 $lat4 = ($lat3R / 0.0416665); // ex. 0.664048 / 0.0416665 = 15.937215748863 $lat4N = intval($lat4); // ex. 15 $lat4L = strtolower(chr($letterA + $lat4N)); // ex. P $lat4R = ($lat4 - $lat4N); // ex. 15.937215748863 - 15 = 0.937215748863 $lat5 = $lat4R * 10; // ex. 0.937215748863 * 10 = 9.37215748863 $lat5N = intval($lat5); // ex. 9 $lat5R = $lat5 - $lat5N; // ex. 9.37215748863 - 9 = 0.37215748863 $lat6 = ($lat5R / 5) / 0.008333; // ex. (0.56472225888904 / 5) / 0.008333 = 13.55387637 // ex. 0.112944452 / 0.008333 = 13.55387637 $lat6N = intval($lat6); // ex. 13 $lat6L = strtolower(chr($letterA + $lat6N)); // ex. e #endregion Latitude $locator = $lon2L; $locator .= $lat2L; $locator .= $lon3N; $locator .= $lat3N; $locator .= $lon4L; $locator .= $lat4L; $locator .= $lon5N; $locator .= $lat5N; $locator .= $lon6L; $locator .= $lat6L; if ($debug == "true") { echo "<strong>Longitude</strong><blockquote>"; echo "lon: $lon<br/>"; echo "lon1: $lon1<br/>"; echo "lon2: $lon2L ($lon2) ($lon2N) ($lon2R)<br/>"; echo "lon3: $lon3N ($lon3) ($lon3R)<br/>"; echo "lon4: $lon4L ($lon4N) ($lon4) ($lon4R)<br/>"; echo "lon5: $lon5N ($lon5) ($lon5R)<br/>"; echo "lon6: $lon6L ($lon6N) ($lon6)</blockquote>"; echo "<strong>Longitude</strong><blockquote>"; echo "<p>lat: $lat<br/>"; echo "lat1: $lat1<br/>"; echo "lat2: $lat2L ($lat2) ($lat2N) ($lat2R)<br/>"; echo "lat3: $lat3N ($lat3) ($lat3R)<br/>"; echo "lat4: $lat4L ($lat4N) ($lat4) ($lat4R)<br/>"; echo "lat5: $lat5N ($lat5) ($lat5R)<br/>"; echo "lat6: $lat6L ($lat6N) ($lat6)</blockquote>"; echo "<table cellpadding=4 cellspacing=0>"; echo "<tr><td><strong>Latitude:</strong></td><td>$latInput</td></tr>"; echo "<tr><td><strong>Longitude:</strong></td><td>$lonInput</td></tr>"; echo "<tr><td><strong>Locator:</strong></td><td>$locator</td></tr>"; echo "</table><p>"; } else { echo $locator; }
Last Updated on August 17, 2024