Skip to content

Instantly share code, notes, and snippets.

@mrmascott10
Last active September 22, 2022 16:39
Show Gist options
  • Save mrmascott10/46b618ccb2ca07050394d78b0ea2afc3 to your computer and use it in GitHub Desktop.
Save mrmascott10/46b618ccb2ca07050394d78b0ea2afc3 to your computer and use it in GitHub Desktop.
Convert a string of degrees decimal minutes to a usable decimal degrees latitude, longitude
<?php
// Accepts: 12 34.567N, 1 2.345W
// Returns: [12.345, 12.345]
function ddmToDDConvert($original) {
$coords = [];
$original = explode(',', $original);
$dirDict = ['S' => -1,'N' => 1,'W' => -1,'E' => 1];
if (count($original) == 2) {
$ddmLat = explode(' ', trim(ltrim($original[0])));
$ddmLng = explode(' ', trim(ltrim($original[1])));
$ddmLat[] = $dirDict[substr($ddmLat[1], -1)];
$ddmLng[] = $dirDict[substr($ddmLng[1], -1)];
$ddmLat[1] = substr($ddmLat[1], 0, -1);
$ddmLng[1] = substr($ddmLng[1], 0, -1);
$arr = [[$ddmLng[0], $ddmLng[1], $ddmLng[2]], [$ddmLat[0], $ddmLat[1], $ddmLat[2]]];
print_r($arr);
foreach ($arr as $el) {
$deg = (float)$el[0];
$min = (float)$el[1];
$dir = (float)$el[2];
$coords[] = $dir * ($deg + ($min / 60.0));
}
return $coords;
} else return false;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment