Skip to content

Commit

Permalink
Add toEightFigureString and toTenFigureString
Browse files Browse the repository at this point in the history
Update to OSRef.php to add toEightFigureString and toTenFigureString. Allows for 10m and 1m accuracy grid references to be created from easting northing.
  • Loading branch information
stevegoddard authored and dvdoug committed Jan 19, 2016
1 parent 55b57ec commit 0a4fd0d
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 5 deletions.
67 changes: 62 additions & 5 deletions OSRef.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,14 +87,16 @@ public static function fromSixFigureReference($ref)
}

/**
* Convert this grid reference into a string using a standard six-figure
* grid reference including the two-character designation for the 100km
* square. e.g. TG514131.
* Convert this grid reference into a grid reference string of a
* given length (2, 4, 6, 8 or 10) including the two-character
* designation for the 100km square. e.g. TG514131.
* @return string
*/
public function toSixFigureReference()
private function toGridReference($length)
{

$halfLength = $length / 2;

$easting = str_pad($this->x, 6, 0, STR_PAD_LEFT);
$northing = str_pad($this->y, 6, 0, STR_PAD_LEFT);

Expand All @@ -112,7 +114,62 @@ public function toSixFigureReference()
$minorLetterIndex = (int)(5 * $minorSquaresNorth + $minorSquaresEast);
$minorLetter = substr(self::GRID_LETTERS, $minorLetterIndex, 1);

return $majorLetter . $minorLetter . substr($easting, 1, 3) . substr($northing, 1, 3);
return $majorLetter . $minorLetter . substr($easting, 1, $halfLength) . substr($northing, 1, $halfLength);
}

/**
* Convert this grid reference into a string using a standard two-figure
* grid reference including the two-character designation for the 100km
* square. e.g. TG51 (10km square).
* @return string
*/
public function toTwoFigureReference()
{
return $this->toGridReference(2);
}

/**
* Convert this grid reference into a string using a standard four-figure
* grid reference including the two-character designation for the 100km
* square. e.g. TG5113 (1km square).
* @return string
*/
public function toFourFigureReference()
{
return $this->toGridReference(4);
}

/**
* Convert this grid reference into a string using a standard six-figure
* grid reference including the two-character designation for the 100km
* square. e.g. TG514131 (100m square).
* @return string
*/
public function toSixFigureReference()
{
return $this->toGridReference(6);
}

/**
* Convert this grid reference into a string using a standard eight-figure
* grid reference including the two-character designation for the 100km
* square. e.g. TG51431312 (10m square).
* @return string
*/
public function toEightFigureReference()
{
return $this->toGridReference(8);
}

/**
* Convert this grid reference into a string using a standard ten-figure
* grid reference including the two-character designation for the 100km
* square. e.g. TG5143113121 (1m square).
* @return string
*/
public function toTenFigureReference()
{
return $this->toGridReference(10);
}

/**
Expand Down
40 changes: 40 additions & 0 deletions tests/OSRefTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,26 @@ public function testLatLngOSWorkedExample()
self::assertEquals($expected, $LatLng->__toString());
}

public function testToTwoFigureString()
{

$OSRef = new OSRef(530140, 184184);

$expected = 'TQ38';

self::assertEquals($expected, $OSRef->toTwoFigureReference());
}

public function testToFourFigureString()
{

$OSRef = new OSRef(530140, 184184);

$expected = 'TQ3084';

self::assertEquals($expected, $OSRef->toFourFigureReference());
}

public function testToSixFigureString()
{

Expand Down Expand Up @@ -66,6 +86,26 @@ public function testToSixFigureString3()
self::assertEquals($expected, $OSRef->toSixFigureReference());
}

public function testToEightFigureString()
{

$OSRef = new OSRef(216600, 771200);

$expected = 'NN16607120';

self::assertEquals($expected, $OSRef->toEightFigureReference());
}

public function testToTenFigureString()
{

$OSRef = new OSRef(216600, 771200);

$expected = 'NN1660071200';

self::assertEquals($expected, $OSRef->toTenFigureReference());
}

public function testFromSixFigureString()
{

Expand Down

0 comments on commit 0a4fd0d

Please sign in to comment.