Advertisement
Guest User

Untitled

a guest
Mar 22nd, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.33 KB | None | 0 0
  1. function DMStoDD($latlng) {
  2. $valid = false;
  3. $decimal_degrees = 0;
  4. $degrees = 0; $minutes = 0; $seconds = 0; $direction = 1;
  5. // Determine if there are extra periods in the input string
  6. $num_periods = substr_count($latlng, '.');
  7.  
  8. if ($num_periods > 1) {
  9. $temp = preg_replace('/\./', ' ', $latlng, $num_periods - 1); // replace all but last period with delimiter
  10. $temp = trim(preg_replace('/[a-zA-Z]/','',$temp)); // when counting chunks we only want numbers
  11. $chunk_count = count(explode(' ',$temp));
  12.  
  13. if ($chunk_count > 2) {
  14. $latlng = preg_replace('/\./', ' ', $latlng, $num_periods - 1); // remove last period
  15. } else {
  16. $latlng = str_replace('.', ' ',$latlng); // remove all periods, not enough chunks left by keeping last one
  17. }
  18. }
  19.  
  20. // Remove unneeded characters
  21. $latlng = trim($latlng);
  22. $latlng = str_replace('ΒΊ', ' ',$latlng);
  23. $latlng = str_replace('Β°', ' ',$latlng);
  24. $latlng = str_replace('\'', ' ',$latlng);
  25. $latlng = str_replace('"', ' ',$latlng);
  26. $latlng = str_replace(' ', ' ',$latlng);
  27. $latlng = substr($latlng, 0, 1) . str_replace('-', ' ', substr($latlng, 1)); // remove all but first dash
  28.  
  29. if ($latlng != '') {
  30. // DMS with the direction at the start of the string
  31. if (preg_match("/^([nsewNSEW]?)\s*(\d{1,3})\s+(\d{1,3})\s+(\d+\.?\d*)$/",$latlng,$matches)) {
  32. $valid = true;
  33. $degrees = intval($matches[2]);
  34. $minutes = intval($matches[3]);
  35. $seconds = floatval($matches[4]);
  36. if (strtoupper($matches[1]) == 'S' || strtoupper($matches[1]) == 'W')
  37. $direction = -1;
  38. }
  39. // DMS with the direction at the end of the string
  40. else if (preg_match("/^(-?\d{1,3})\s+(\d{1,3})\s+(\d+(?:\.\d+)?)\s*([nsewNSEW]?)$/",$latlng,$matches)) {
  41. $valid = true;
  42. $degrees = intval($matches[1]);
  43. $minutes = intval($matches[2]);
  44. $seconds = floatval($matches[3]);
  45. if (strtoupper($matches[4]) == 'S' || strtoupper($matches[4]) == 'W' || $degrees < 0) {
  46. $direction = -1;
  47. $degrees = abs($degrees);
  48. }
  49. }
  50. if ($valid) {
  51. // A match was found, do the calculation
  52. $decimal_degrees = ($degrees + ($minutes / 60) + ($seconds / 3600)) * $direction;
  53. } else {
  54. // Decimal degrees with a direction at the start of the string
  55. if (preg_match("/^([nsewNSEW]?)\s*(\d+(?:\.\d+)?)$/",$latlng,$matches)) {
  56. $valid = true;
  57. if (strtoupper($matches[1]) == 'S' || strtoupper($matches[1]) == 'W')
  58. $direction = -1;
  59. $decimal_degrees = $matches[2] * $direction;
  60. }
  61. // Decimal degrees with a direction at the end of the string
  62. elseif (preg_match("/^(-?\d+(?:\.\d+)?)\s*([nsewNSEW]?)$/",$latlng,$matches)) {
  63. $valid = true;
  64. if (strtoupper($matches[2]) == 'S' || strtoupper($matches[2]) == 'W' || $degrees < 0) {
  65. $direction = -1;
  66. $degrees = abs($degrees);
  67. }
  68. $decimal_degrees = $matches[1] * $direction;
  69. }
  70. }
  71. }
  72.  
  73. return $valid ? $decimal_degrees : false;
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement