Location: PHPKode > scripts > MyOSGB PHP Script > class.myosgb.php
<?php

/*

 DETAILS
 -------
 
 PROGRAM: MyOSGB PHP Class
 VERSION: 1.0
 URL: http://www.postcodepal.com/?page=myosgb-class
 DOWNLOAD DATE: Dec 05, 2011 at 05:56 AM
 DOWNLOAD IP: 218.22.27.167


 DESCRIPTION
 -----------
 
 MyOSGB PHP Class is a simple lightweight PHP class written for PHP5.
 Its purpose is to simplify distance calculations between Eastings and
 Northings, National Grid References or a combination of all three.
 It can convert Eastings and Northings to National Grid References and
 vice-versa. All distance calculations are performed in miles, but there
 is an optional switch to change this to kilometers. It works exclusively
 on six digit (1oo metre refrencing). Less than 6 will be padded with zeros,
 in order to force backwards compatibility between the different levels of
 refrencing. Grid refrences are genearted based on these different levels,
 but you can override this, if you wish to provide longer or shorter, albeit
 less accurate points of refrence, with or without spaces. This class is
 designed to work exclusively with the Ordnance Survey's National Grid
 reference system of Great Britain ONLY! For more information regarding the
 National Grid of Great Britan, plese visit the url below.
 
 http://www.ordnancesurvey.co.uk/oswebsite/gi/nationalgrid/nghelp1.html


 NOTES
 -----
 
 We refer to six digit refrencing which is accurate to within 100 metres,
 but we must point out that in the North of Scotland you will notice that
 some grid refrences will produce six digit Eastings and SEVEN digit
 Northings. This is NOT a bug! The GB National Grid is comprised of
 7×13 100km squares. The bottom left axis in the South West corner of 0
 Eastings to the top right axis in the North East of 1200 Northings. All
 locations in squares 1000 or above will produce seven digit Northings.


 LICENSE
 -------

 This program is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program.  If not, see: http://www.gnu.org/licenses/.


 COPYRIGHT
 ---------

 This program is Copyright © 2011 Vulcan Logix. All rights reserved.

*/

class MyOSGB
{

	public $miles = true;
	public $short = true;

	public function GetGridRef($e,$n,$a=0)
	{
		$grid = $this->NationalGrid();
		
		$type = array(8 => array('sum' => 1,    'len' => 5),
			      7 => array('sum' => 1,    'len' => 5),
			      6 => array('sum' => 1,    'len' => 5),
			      5 => array('sum' => 10,   'len' => 4),
			      4 => array('sum' => 100,  'len' => 3),
			      3 => array('sum' => 1000, 'len' => 2),
			      2 => array('sum' => 10000,'len' => 1));

		$unit = $a <=6 && $a >=2 ? $type[$a] : $type[strlen($e)];

		foreach (func_get_args() as $int)
		{
			if (strtoupper($int) == 'T')
				$trim = 1;
			else
			{
				if ($int == $n && strlen($n) == 7)
					$int = $this->padval($int,7);
				else
					$int = $this->padval($int);

				$ref[] = floor($int/100000);
				$num[] = round(fmod($int,100000)/$unit['sum']);
			}
		}

		$sp = isset($trim) ? '' : ' ';

		if (isset($grid[$ref[1]][$ref[0]]))
			return (sprintf('%s'.$sp.'%0'.$unit['len'].'d'.$sp.'%0'.$unit['len'].'d',
					$grid[$ref[1]][$ref[0]],$num[0],$num[1]));
		else
			return false;
	}

	public function GetGridRefCoords($gref)
	{

		$gref = str_replace(' ','',$gref);
		$char = substr(strtoupper($gref),0,2);
		$int1 = intval(substr($gref,2,(strlen($gref)-2)/2));
		$int2 = intval(substr($gref,-(strlen($gref)-2)/2));

		$i=-1;

		foreach ($this->NationalGrid() as $squares)
		{
			$i++;
			if (in_array($char,$squares))
				$square = array_search($char,array_values($squares)).','.$i;
		}

		if (isset($square))
		{
			$ref = explode(',',$square);
			
			if ($ref[1] >= 10)
				$n = 7;
			else
				$n = 6;
			
			return (array('eastings'=>$this->padval($ref[0].$int1),
				      'northings'=>$this->padval($ref[1].$int2,$n)));
		}

		else
			return false;
	}

	public function GetDistanceBetweenCoords()
	{
		foreach (func_get_args() as $int)
		{

			if (strlen($int) == 7)
				$n = 7;
			else
				$n = 6;

			$arg[] = $this->padval($int,$n);
		}

		$sum = $this->miles == true ? 0.000621371 : 0.001;
		$res = sqrt(pow(abs($arg[0]-$arg[2]),2)+pow(abs($arg[1]-$arg[3]),2))*$sum;

		if ($this->short == true)
			return (number_format($res,2,'.',''));
		else
			return ($res);
	}

	public function GetDistanceBetweenGridRefs($gref1,$gref2)
	{
		$xy1 = $this->GetGridRefCoords($gref1);
		$xy2 = $this->GetGridRefCoords($gref2);
		return ($this->GetDistanceBetweenCoords($xy1['eastings'],$xy1['northings'],
							$xy2['eastings'],$xy2['northings']));
	}

	public function GetDistanceBetweenCoordsAndGridRef()
	{
		foreach (func_get_args() as $arg)
			if (is_numeric($arg))
				$esnt[] = $arg;
			else
				$gref = $arg;

		if (!is_array($esnt) || count($esnt) != 2 || !isset($gref))
			return false;
		else
		{
			$xy = $this->GetGridRefCoords($gref);
			return ($this->GetDistanceBetweenCoords($xy['eastings'],$xy['northings'],
								$esnt[0],$esnt[1]));
		}
	}

	private function NationalGrid()
	{
		return (array_reverse(array( // GB National Grid
			     // 0  0100 0200 0300 0400 0500 0600
	     /* 1200 */ array('HL','HM','HN','HO','HP','JL','JM'), // 1200
	     /* 1100 */ array('HQ','HR','HS','HT','HU','JQ','JR'), // 1100
	     /* 1000 */ array('HV','HW','HX','HY','HZ','JV','JW'), // 1000
	     /* 0900 */ array('NA','NB','NC','ND','NE','OA','OB'), // 0900
	     /* 0800 */ array('NF','NG','NH','NJ','NK','OF','OG'), // 0800
	     /* 0700 */ array('NL','NM','NN','NO','NP','OL','OM'), // 0700
	     /* 0600 */ array('NQ','NR','NS','NT','NU','OQ','OR'), // 0600
	     /* 0500 */ array('NV','NW','NX','NY','NZ','OV','OW'), // 0500
	     /* 0400 */ array('SA','SB','SC','SD','SE','TA','TB'), // 0400
	     /* 0300 */ array('SF','SG','SH','SJ','SK','TF','TG'), // 0300
	     /* 0200 */ array('SL','SM','SN','SO','SP','TL','TM'), // 0200
	     /* 0100 */ array('SQ','SR','SS','ST','SU','TQ','TR'), // 0100
	     /* -- 0 */ array('SV','SW','SX','SY','SZ','TV','TW')))); // 0
			     // 0  0100 0200 0300 0400 0500 0600
	}

	private function padval($n,$l=6)
	{
		return (intval(substr($n.'0000',0,$l)));
	}

}

/*******************************************************
 * Copyright © 2011 Vulcan Logix. All rights reserved. *
 *******************************************************/

?>
Return current item: MyOSGB PHP Script