latlon.inc 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <?php
  2. /**
  3. * @file
  4. * Plugin to provide a google geocoder.
  5. */
  6. /**
  7. * Plugins are described by creating a $plugin array which will be used
  8. * by the system that includes this file.
  9. */
  10. $plugin = array(
  11. 'title' => t("Latitude / Longitude"),
  12. 'description' => t('Parse location from freeform latitude and longitude string'),
  13. 'callback' => 'geocoder_latlon',
  14. 'field_types' => array('text', 'text_long', 'text_with_summary', 'computed'),
  15. 'field_callback' => 'geocoder_latlon_field',
  16. );
  17. /**
  18. * Process Markup
  19. */
  20. function geocoder_latlon($latlon_string, $options = array()) {
  21. geophp_load();
  22. $delims = array("," , "/" , " " , "\t");
  23. foreach ($delims as $delim) {
  24. $parts = explode($delim, $latlon_string);
  25. if (count($parts) == 2) {
  26. $lat = geocoder_latlon_DMStoDEC(trim($parts[0]));
  27. $lon = geocoder_latlon_DMStoDEC(trim($parts[1]));
  28. return new Point($lon, $lat);
  29. }
  30. }
  31. // Failed to parse
  32. return FALSE;
  33. }
  34. function geocoder_latlon_DMStoDEC($dms) {
  35. if (is_numeric($dms)) {
  36. // It's already decimal degrees, just return it
  37. return $dms;
  38. }
  39. // If it contains both an H and M, then it's an angular hours
  40. if (stripos($dms, 'H') !== FALSE && stripos($dms, 'M') !== FALSE) {
  41. $dms = strtr($dms, "'\"HOURSMINTECNDAhoursmintecnda", " ");
  42. $dms = preg_replace('/\s\s+/', ' ', $dms);
  43. $dms = explode(" ", $dms);
  44. $deg = $dms[0];
  45. $min = $dms[1];
  46. $sec = $dms[2];
  47. $dec = floatval(($deg*15) + ($min/4) + ($sec/240));
  48. return $dec;
  49. }
  50. // If it contains an S or a W, then it's a negative
  51. if (stripos($dms, 'S') !== FALSE || stripos($dms, 'W') !== FALSE) {
  52. $direction = -1;
  53. }
  54. else {
  55. $direction = 1;
  56. }
  57. // Strip all characters and replace them with empty space
  58. $dms = strtr($dms, "�'\"NORTHSEAWnorthseaw'", " ");
  59. $dms = preg_replace('/\s\s+/', ' ', $dms);
  60. $dms = explode(" ", $dms);
  61. $deg = $dms[0];
  62. $min = $dms[1];
  63. $sec = $dms[2];
  64. // Direction should be checked only for nonnegative coordinates
  65. $dec = floatval($deg+((($min*60)+($sec))/3600));
  66. if ($dec > 0) {
  67. $dec = $direction * $dec;
  68. }
  69. return $dec;
  70. }
  71. function geocoder_latlon_field($field, $field_item) {
  72. return geocoder_latlon($field_item['value']);
  73. }