You are here

public static function GeolocationItem::sexagesimalToDecimal in Geolocation Field 8.3

Same name and namespace in other branches
  1. 8.2 src/Plugin/Field/FieldType/GeolocationItem.php \Drupal\geolocation\Plugin\Field\FieldType\GeolocationItem::sexagesimalToDecimal()

Transform sexagesimal notation to float.

Sexagesimal means a string like - X° Y' Z"

Parameters

string $sexagesimal: String in DMS notation.

Return value

float|false The regular float notation or FALSE if not sexagesimal.

1 call to GeolocationItem::sexagesimalToDecimal()
GeolocationLatlngWidget::massageFormValues in src/Plugin/Field/FieldWidget/GeolocationLatlngWidget.php
Massages the form values into the format expected for field values.

File

src/Plugin/Field/FieldType/GeolocationItem.php, line 210

Class

GeolocationItem
Plugin implementation of the 'geolocation' field type.

Namespace

Drupal\geolocation\Plugin\Field\FieldType

Code

public static function sexagesimalToDecimal($sexagesimal = '') {
  $pattern = "/(?<degree>-?\\d{1,3})°[ ]?((?<minutes>\\d{1,2})')?[ ]?((?<seconds>(\\d{1,2}|\\d{1,2}\\.\\d+))\")?/";
  preg_match($pattern, $sexagesimal, $gps_matches);
  if (!empty($gps_matches)) {
    $value = $gps_matches['degree'];
    if (!empty($gps_matches['minutes'])) {
      $value += $gps_matches['minutes'] / 60;
    }
    if (!empty($gps_matches['seconds'])) {
      $value += $gps_matches['seconds'] / 3600;
    }
  }
  else {
    return FALSE;
  }
  return $value;
}