Guest

Untitled

By: a guest on Jul 9th, 2010  |  syntax: PHP  |  size: 3.89 KB  |  hits: 481  |  expires: Never
download  |  raw  |  embed  |  report abuse
This paste has a previous version, view the difference. Copied
  1. <?php
  2.  
  3. function geoCodeiPhone($location) {
  4.         // see if there are coordinates in the location, ie. "iPhone: 52.043900,5.555832". if so, use those coordinates as lat & lng
  5.  
  6.         // translate coordinates to english
  7.         $translate = array(
  8.                 'NB' => 'N',
  9.                 'OL' => 'E',
  10.                 'O' => 'E',
  11.         );
  12.         $location = str_replace(array_keys($translate), array_values($translate), $location);
  13.  
  14.         // grab the coordinates out from the location
  15.         preg_match_all(
  16.                 '/\s*
  17.                 (?<!\d)
  18.                 (\d{1,3}(?:[,.]\d+)?)
  19.                 [^\d\snwes]*
  20.                 (?:
  21.                         \s*
  22.                         (\d{1,2}(?:[,.]\d+)?)
  23.                         [^\d\snwes]*
  24.                         (?:
  25.                                 \s*
  26.                                 (\d{1,2}(?:[,.]\d+)?)
  27.                                 [^\d\snwe]*
  28.                         )?
  29.                 )?
  30.                 [\s-]*([nwes])
  31.  
  32.                 |
  33.  
  34.                 (?<!\d)
  35.                 (?:([nwes-]?)\s*)
  36.                 (\d{1,3}(?:[,.]\d+)?)
  37.                 (?:
  38.                         [^\d,]+\s*
  39.                         (\d{1,2}(?:[,.]\d+)?)
  40.                         [^\d\s]*
  41.                         (?:
  42.                                 \s*
  43.                                 (\d{1,2}(?:[,.]\d+)?)
  44.                                 [^\d\s]*
  45.                         )?
  46.                 )?
  47.                 (?!\d)/imx'
  48.                 ,$location, $coords, PREG_SET_ORDER);
  49.  
  50.         // look for the various bits in the matches and combine them as needed
  51.         foreach ($coords as $i => $coord) {
  52.                 $val = 999; // set to some number that will throw a false below
  53.  
  54.                 // pad the array so we don't throw any notices
  55.                 $coord = array_pad($coord, 10, '');
  56.  
  57.                 // check for DMS value with trailing cardinal point
  58.                 if ('' !== (string) $coord[1]) {
  59.                         $val = DMS2dec($coord[1], $coord[2], $coord[3], $coord[4]);
  60.                 }
  61.  
  62.                 // check for DMS value with leading cardinal point (or signed decimal)
  63.                 if ('' !== (string) $coord[6]) {
  64.                         $val = DMS2dec($coord[6], $coord[7], $coord[8], $coord[5]);
  65.                 }
  66.  
  67.                 // store the value in the proper coordinate
  68.                 if (0 == $i) {
  69.                         $lat = $val;
  70.                 }
  71.                 else {
  72.                         $lng = $val;
  73.                 }
  74.         }
  75.  
  76.         $invalid_coords = isset($coords[2]); // more than two full matches, something is wrong
  77.         $invalid_lat = ( ! isset($lat) || ( -90 > $lat) || ( 90 < $lat));
  78.         $invalid_lng = ( ! isset($lng) || (-180 > $lng) || (180 < $lng));
  79.         if ($invalid_coords || $invalid_lat || $invalid_lng) {
  80.                 return false;
  81.         }
  82.  
  83.         return array($lng, $lat);
  84. }
  85.  
  86. function DMS2dec($deg, $min = 0, $sec = 0, $card = '') {
  87.         // convert all inputs to floats
  88.         $deg = (float) str_replace(',', '.', trim($deg));
  89.         $min = (float) str_replace(',', '.', trim($min));
  90.         $sec = (float) str_replace(',', '.', trim($sec));
  91.  
  92.         // make value positive if we need to
  93.         if (0 > $deg) {
  94.                 $deg = -$deg;
  95.                 $card = '-';
  96.         }
  97.  
  98.         // convert to decimal value
  99.         $dec  = $deg;
  100.         $dec += $min / 60;
  101.         $dec += $sec / (60 * 60);
  102.  
  103.         // adjust value according to cardinal point
  104.         if (in_array(strtoupper(trim($card)), array('S', 'W', '-'))) {
  105.                 $dec = -$dec;
  106.         }
  107.  
  108.         return $dec;
  109. }
  110.  
  111.  
  112. var_dump(geoCodeiPhone('iPhone: -52.043900,-5.555832'));
  113. var_dump(geoCodeiPhone('34.3330°S, 150.9170°E'));
  114. var_dump(geoCodeiPhone('-34.3330°, 150.9170°'));
  115. var_dump(geoCodeiPhone('N 52°35\' 0" / W 2°1\' 0"'));
  116. var_dump(geoCodeiPhone('N 35 32 4.678 W 124 23 18.234'));
  117. var_dump(geoCodeiPhone('N 35 32 4,678 W 124 23 18,234'));
  118. var_dump(geoCodeiPhone('- 35 32.678 - 124 23.234'));
  119.  
  120. var_dump(geoCodeiPhone('45 Reseda, Ca. 01335'));
  121. var_dump(geoCodeiPhone('Reseda, Ca. 91335'));
  122. var_dump(geoCodeiPhone('38d 33m 06.32sN 121d 29m 04.75'));
  123. var_dump(geoCodeiPhone('Pre: 39.8714500,-105.01943'));
  124. var_dump(geoCodeiPhone('51° 50\' North 006°50\' East'));
  125. var_dump(geoCodeiPhone('35°29\'18.15?N 76°37\'06.18W'));
  126. var_dump(geoCodeiPhone('52°15\' NB 6°9\' OL'));
  127.  
  128. var_dump(geoCodeiPhone('53°15\'10.69"N 5°15\'6.87"O'));
  129. var_dump(geoCodeiPhone('+29° 35\' 3.6, -95° 13\' 45.1'));
  130. var_dump(geoCodeiPhone('40° 0\' N 105° 16\' W'));
  131. var_dump(geoCodeiPhone('Hengelo, 52.2670°N, 6.8000°E'));
  132. var_dump(geoCodeiPhone('35°57\'17.10 N, 84°06\'03.79 W'));
  133. var_dump(geoCodeiPhone('30°19?10?N 81°39?36?'));
  134.  
  135. var_dump(geoCodeiPhone('1.3819056°N 103.8448167°E'));
  136. var_dump(geoCodeiPhone('1°16\'S & 36°48\'E'));
  137. var_dump(geoCodeiPhone('ÜT: 52.07764,4.35581'));
  138. var_dump(geoCodeiPhone('ÜT: 52,07764,4,35581'));
  139. var_dump(geoCodeiPhone('51°14\'0.93N, 0°10\'2.60W'));