\n";
$lres=OSlet(substr($mats[1],0,1));
$mats[4]=500000*($lres[0]-2); $mats[5]=500000*($lres[1]-1);
# echo "lres = {$lres[0]},{$lres[1]} e_n = {$mats[4]}_{$mats[5]}
\n";
$lres=OSlet(substr($mats[1],1,1));
$mats[4] += 100000*$lres[0]; $mats[5] += 100000*$lres[1];
# echo "lres = {$lres[0]},{$lres[1]} e_n = {$mats[4]}_{$mats[5]}
\n";
$j=strlen($mats[2]);
if (($j%2)!=0 || $j>10) {
$mats[7]='If alpha used, there must be an even number of digits';
break;
}
$j /= 2;
$mats[4] += rnum(substr($mats[2],0,$j)); $mats[5] += rnum(substr($mats[2],$j,$j));
} else { // see if it is all-numeric format
if ( ($j=strpos($gr,','))>0 ) {
$mats[4]=substr($gr,0,$j); $mats[5]=substr($gr,$j+1);
} else {
$mats[4]=$gr; $_g['pms']=substr($_g['pms'],1); /* remove the first '_' */
$mats[5]=get_parm_term();
}
if (preg_match('/^[0-9]*$/',$mats[4])==0 || preg_match('/^[0-9]*$/',$mats[5])==0) {
$mats[7]='Invalid grid reference format';
break;
}
}
if (strlen($mats[1])!=1) {
if ( $mats[4]<0 || $mats[4]>=700000 || $mats[5]<0 || $mats[5]>=1300000 ) {
$mats[7]= "easting={$mats[4]} and/or northing={$mats[5]} is outside the ".
"valid range for Great Britain (0,0 to 700000,1300000)";
break;
}
}
} while (false);
return $mats;
}
function OSlet ( $ch )
{
$j = strpos('abcdefghjklmnopqrstuvwxyzABCDEFGHJKLMNOPQRSTUVWXYZ',$ch);
if ($j!==false) { $j %= 25; $rs[1]= 4-(int)($j/5); $rs[0]=$j%5; }
return $rs;
}
function rnum ( $ns ) // round up easting or northing to 5 digits
{
return substr($ns.'50000',0,5);
}
function get_parm_term () // extract first term from $_g['pms']
{
global $_g;
$_g['pms']=str_replace(array(' ',' ',' ',' '),'',$_g['pms']);
$j=strpos($_g['pms'],'_');
if ($j!==false) {
$gr=substr($_g['pms'],0,$j); $_g['pms']=substr($_g['pms'],$j);
} else {
$gr=$_g['pms']; $_g['pms']='';
}
return $gr;
}
function gr_diff($gr1,$gr2)
{
global $_g;
$_g['pms']=$gr1; $mats1=gr2en(); $_g['pms']=$gr2; $mats2=gr2en();
$dife=$mats1[4]-$mats2[4]; $difn=$mats1[5]-$mats2[5];
return round(sqrt($dife*$dife + $difn*$difn),0);
}
?>