You are here

function getlocations_dms_to_dd in Get Locations 7

Same name and namespace in other branches
  1. 7.2 getlocations.module \getlocations_dms_to_dd()

Convert dms string to decimal degrees. Should be reasonably tolerant of sloppy input

Parameters

string:

Return value

string

File

./getlocations.module, line 5941
getlocations.module @author Bob Hutchinson http://drupal.org/user/52366 @copyright GNU GPL

Code

function getlocations_dms_to_dd($dms) {

  // If it ends with a word starting with S or W, then it's a negative
  // case insensitive
  $direction = 1;
  preg_match("/\\s(\\w+)\\b\$/", $dms, $m);
  if (preg_match("/^s/i", $m[1]) || preg_match("/^w/i", $m[1])) {
    $direction = -1;
  }
  $dmsarr = explode(' ', $dms);
  $dmsarr2 = array();
  foreach ($dmsarr as $v) {
    if ($v) {

      // strip out non-numbers found at the end of the string so we keep '.'
      $tmp = preg_replace("/\\D+\$/", '', $v);
      $tmp = trim($tmp);
      if ($tmp) {
        $dmsarr2[] = $tmp;
      }
    }
  }
  $dd = FALSE;
  if (count($dmsarr2) == 3) {
    list($degrees, $minutes, $seconds) = $dmsarr2;
    $dd = floatval($degrees + ($minutes * 60 + $seconds) / 3600);
    if ($dd > 0) {
      $dd = $direction * $dd;
    }
  }
  return $dd;
}