latlon.inc 2.2 KB

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