Keys →
Functions ↓
function convertCoords ($coords) {#-k geo#- $coords can be any of:#- lat#- long - note: if no compass is indicated and only 1 coord given, it will be returned in lat#- lat long#- lat, long#- latlong in ISO 6709 formmat#- lat and long can be in the format#- type 0 -12.345678 or S12.345678#- type 1 -12:34:56.78 or 12:34:56.78S#- type 2 12° [ 34' [ 56.12" [ S ] ] ]#- returns array#- ['lat'] ['SD']#- ['DD']#- ['DMS']#- ['fancy']#- ['ISO6709']#- and similar for 'long'#- or ['err'] $type[23] = '([0-9]+)o([0-9]+)\'([0-9.]+)"([ENSW]?)'; # 23o 26' 16.45" N $type[22] = '([0-9]+)o([0-9.]+)\'([ENSW]?)'; # 23o 26.45' N $type[21] = '([0-9.]+)o([ENSW]?)'; # 23.45o N $type[13] = '([0-9]+):([0-9]+):([0-9.]+)([ENSW]+)'; # 12:34:56.78 S $type[12] = '([0-9]+):([0-9.]+)([ENSW]+)'; # 12:56.78 S $type[11] = '([0-9.]+)([ENSW]+)'; # 12.78 S $type[16] = '([-+]?)([0-9]+):([0-9]+):([0-9.]+)'; # -12:34:56.78 $type[15] = '([-+]?)([0-9]+):([0-9.]+)'; # -12:56.78 $type[ 1] = '([ENSW]+)([0-9.]+)'; # W12.34 $type[ 2] = '([-+]+)([0-9.]+)'; # -12.78 $spaces = array ( ' ', ' ', ' ' );# n12.78w12.78 $p = ''; $err = ''; $y = $w = $z = 0; $a = strtoupper(str_replace ( $spaces, '', trim($coords)) );#dumpStr ( $a ); $a = replaceSpecialChars ( $a );#dumpStr ( $a ); $ret['init'] = $coords; $ret['a'] = $a; foreach ($type as $rule => $patt) { $err = ''; $ret['rule'] = $rule; $ret['patt'] = $patt; $t = 'L,L'; if ($x = preg_match ( '/'.$patt.','.$patt.'/', $a, $matches )) break; # lat,lon $t = 'LL'; if ($x = preg_match ( '/^'.$patt.$patt.'$/', $a, $matches )) break; # latlon $t = 'L'; if ($x = preg_match ( '/^'.$patt.'/', $a, $matches )) break; # lat $err = 'Badly formatted location'; } $ret['matches'] = $matches; $ret['conc'] = $t;/* $ret now looks like this:$ret[a] => 23o26'16"n23o26'16"w [rule] => 23 [conc] => LL [patt] => ([0-9]+)o([0-9]+)'([0-9.]+)"([ENSWensw]?) [matches] => Array [0] => 23o26'16"n23o26'16"w -- type 2 format sample [1] => 23 [2] => 26 [3] => 16 [4] => nrule/$t: L LL or L,L1 { i, comp, deg } { i, comp, deg, comp, deg }11,21 { i, deg, comp } { i, deg, comp, deg, comp }12,22 { i, deg, min, comp } { i, deg, min, comp, deg, min, comp }13,23 { i, deg, min, sec, comp } { i, deg, min, sec, comp, deg, min, sec, comp }2 { i, sign, deg } { i, sign, deg, sign, deg }15 { i, sign, deg, min } { i, sign, deg, min, sign, deg, min }16 { i, sign, deg, min, sec } { i, sign, deg, min, sec, sign, deg, min, sec }*/ switch ($ret['rule']) {case 1: $ax1['c'] = $ret['matches'][1]; $ax1['d'] = $ret['matches'][2]; if (strlen($ret['conc']) > 1) { # double entry $ax2['c'] = $ret['matches'][3]; $ax2['d'] = $ret['matches'][4]; } break;case 2: $ax1['c'] = $ret['matches'][1]; $ax1['d'] = $ret['matches'][2]; if (strlen($ret['conc']) > 1) { # double entry $ax2['c'] = $ret['matches'][3]; $ax2['d'] = $ret['matches'][4]; } break;case 11:case 21: $ax1['c'] = $ret['matches'][2]; $ax1['d'] = $ret['matches'][1]; if (strlen($ret['conc']) > 1) { # double entry $ax2['c'] = $ret['matches'][4]; $ax2['d'] = $ret['matches'][3]; } break;case 12:case 22: $ax1['c'] = $ret['matches'][3]; $ax1['d'] = $ret['matches'][1] + $ret['matches'][2]/60; if (strlen($ret['conc']) > 1) { # double entry $ax2['c'] = $ret['matches'][6]; $ax2['d'] = $ret['matches'][4] + $ret['matches'][5]/60; } break;case 13:case 23: $ax1['c'] = $ret['matches'][4]; $ax1['d'] = $ret['matches'][1] + $ret['matches'][2]/60 + $ret['matches'][3]/3600; if (strlen($ret['conc']) > 1) { # double entry $ax2['c'] = $ret['matches'][8]; $ax2['d'] = $ret['matches'][5] + $ret['matches'][6]/60 + $ret['matches'][7]/3600; } break;case 15: $ax1['c'] = $ret['matches'][1]; $ax1['d'] = $ret['matches'][2] + $ret['matches'][3]/60; if (strlen($ret['conc']) > 1) { # double entry $ax2['c'] = $ret['matches'][4]; $ax2['d'] = $ret['matches'][5] + $ret['matches'][6]/60; } break;case 16: $ax1['c'] = $ret['matches'][1]; $ax1['d'] = $ret['matches'][2] + $ret['matches'][3]/60 + $ret['matches'][4]/3600; if (strlen($ret['conc']) > 1) { # double entry $ax2['c'] = $ret['matches'][5]; $ax2['d'] = $ret['matches'][6] + $ret['matches'][7]/60 + $ret['matches'][8]/3600; } break;default: } if (($ax1['c'] == 'S') or ($ax1['c'] == 'W') or ($ax1['c'] == '-')) $ax1['d'] = $ax1['d'] * -1; if (isset($ax2['d']) and (($ax2['c'] == 'S') or ($ax2['c'] == 'W') or ($ax2['c'] == '-'))) $ax2['d'] = $ax2['d'] * -1;# do we need to swap ax1 & ax2? ax1 is always NS or undetermined; ax2 is always EW or non-existant if (($ax1['c'] == 'E') or ($ax1['c'] == 'W') or (abs($ax1['d']) > 90)) { # swap. if (isset($ax2)) { swap ( $ax1,$ax2 ); } else { $ax2 = $ax1; unset($ax1); } }# can we decide on lat vs lon based on size? if (($ax2['d'] > 90) and ($ax2['c'] == '+')) ($ax2['c'] = 'E'); elseif (($ax2['d'] < -90) and ($ax2['c'] == '-')) ($ax2['c'] = 'W');# ISO 6709 coords like -6640+14001 or +5147-0000731 => -DDMM[SS]+DDDMM[SS] if (strlen($err)) { $ret['err'] = $err; } else { if (isset($ax1)) $ret['lat'] = toDms($ax1['d'],1); if (isset($ax2)) $ret['lon'] = toDms($ax2['d'],0); } return $ret; }?>