You are here

function geofield_latlon_DMStoDEC in Geofield 7

Same name and namespace in other branches
  1. 7.2 geofield.module \geofield_latlon_DMStoDEC()

Decimal-Degrees-Seconds to Decimal Degrees

Converts string to decimal degrees. Has some error correction for messy strings

1 call to geofield_latlon_DMStoDEC()
geofield_element_validate in ./geofield.widgets.inc
Geofield Element Validate

File

./geofield.module, line 315

Code

function geofield_latlon_DMStoDEC($dms) {
  if (is_numeric($dms)) {

    // It's already decimal degrees, just return it
    return $dms;
  }

  // If it contains both an H and M, then it's an angular hours
  if (stripos($dms, 'H') !== FALSE && stripos($dms, 'M') !== FALSE) {
    $dms = strtr($dms, "'\"HOURSMINTECNDAhoursmintecnda", "  ");
    $dms = preg_replace('/\\s\\s+/', ' ', $dms);
    $dms = explode(" ", $dms);
    $deg = $dms[0];
    $min = $dms[1];
    $sec = $dms[2];
    $dec = floatval($deg * 15 + $min / 4 + $sec / 240);
    return $dec;
  }

  // If it contains an S or a W, then it's a negative
  if (stripos($dms, 'S') !== FALSE || stripos($dms, 'W') !== FALSE) {
    $direction = -1;
  }
  else {
    $direction = 1;
  }

  // Strip all characters and replace them with empty space
  $dms = strtr($dms, "�'\"NORTHSEAWnorthseaw'", " ");
  $dms = preg_replace('/\\s\\s+/', ' ', $dms);
  $dms = explode(" ", $dms);
  $deg = $dms[0];
  $min = $dms[1];
  $sec = $dms[2];

  // Direction should be checked only for nonnegative coordinates
  $dec = floatval($deg + ($min * 60 + $sec) / 3600);
  if ($dec > 0) {
    $dec = $direction * $dec;
  }
  return $dec;
}