Completed
Push — master ( 33bb7e...99e19c )
by Doug
02:14
created

OSRef::getOSRefFromSixFigureReference()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 1.1017

Importance

Changes 5
Bugs 0 Features 1
Metric Value
c 5
b 0
f 1
dl 0
loc 16
ccs 8
cts 15
cp 0.5333
rs 9.4286
cc 1
eloc 8
nc 1
nop 1
crap 1.1017
1
<?php
2
/**
3
 * PHPCoord
4
 * @package PHPCoord
5
 * @author Jonathan Stott
6
 * @author Doug Wright
7
 */
8
namespace PHPCoord;
9
10
/**
11
 * Ordnance Survey grid reference
12
 * References are accurate to 1m
13
 * @author Jonathan Stott
14
 * @author Doug Wright
15
 * @package PHPCoord
16
 */
17
class OSRef extends TransverseMercator
18
{
19
20 2
    const GRID_LETTERS = "VWXYZQRSTULMNOPFGHJKABCDE";
21
22 2
    public function getReferenceEllipsoid()
23
    {
24
        return RefEll::airy1830();
25 4
    }
26
27 4
    public function getScaleFactor()
28
    {
29
        return 0.9996012717;
30 4
    }
31
32 4
    public function getOriginNorthing()
33
    {
34
        return -100000;
35 4
    }
36
37 4
    public function getOriginEasting()
38
    {
39
        return 400000;
40 4
    }
41
42 4
    public function getOriginLatitude()
43
    {
44
        return 49;
45 4
    }
46
47 4
    public function getOriginLongitude()
48
    {
49
        return -2;
50
    }
51
52
    /**
53
     * Create a new object representing a UTM reference.
54
     *
55
     * @param int $x
56 8
     * @param int $y
57
     */
58 8
    public function __construct($x, $y)
59 8
    {
60
        parent::__construct($x, $y, 0, RefEll::airy1830());
61
    }
62
63
    /**
64
     * Take a string formatted as a six-figure OS grid reference (e.g.
65
     * "TG514131") and return a reference to an OSRef object that represents
66
     * that grid reference.
67
     *
68
     * @param string $ref
69
     * @return OSRef
70
     */
71 1
    public static function getOSRefFromSixFigureReference($ref)
72
    {
73 1
74 1
        //first (major) letter is the 500km grid sq, origin at -1000000, -500000
75 1
        $majorEasting = strpos(self::GRID_LETTERS, $ref[0]) % 5  * 500000 - 1000000;
76 1
        $majorNorthing = (floor(strpos(self::GRID_LETTERS, $ref[0]) / 5)) * 500000 - 500000;
77 1
78
        //second (minor) letter is 100km grid sq, origin at 0,0 of this square
79
        $minorEasting = strpos(self::GRID_LETTERS, $ref[1]) % 5  * 100000;
80 1
        $minorNorthing = (floor(strpos(self::GRID_LETTERS, $ref[1]) / 5)) * 100000;
81
82
        $easting = $majorEasting + $minorEasting + (substr($ref, 2, 3) * 100);
83 1
        $northing = $majorNorthing + $minorNorthing + (substr($ref, 5, 3) * 100);
84
85
        return new OSRef($easting, $northing);
86
    }
87 1
88 1
    /**
89 1
     * Convert this grid reference into a string using a standard six-figure
90
     * grid reference including the two-character designation for the 100km
91
     * square. e.g. TG514131.
92
     * @return string
93 1
     */
94 1
    public function toSixFigureString()
95 1
    {
96 1
97 1
        $easting = str_pad($this->x, 6, 0, STR_PAD_LEFT);
98 1
        $northing = str_pad($this->y, 6, 0, STR_PAD_LEFT);
99 1
100
101
        $adjustedX = $this->x + 1000000;
102
        $adjustedY = $this->y + 500000;
103
        $majorSquaresEast = floor($adjustedX / 500000);
104
        $majorSquaresNorth = floor($adjustedY / 500000);
105
        $majorLetterIndex = (int)(5 * $majorSquaresNorth + $majorSquaresEast);
106
        $majorLetter = self::GRID_LETTERS[$majorLetterIndex];
107
108 2
        //second (minor) letter is 100km grid sq, origin at 0,0 of this square
109
        $minorSquaresEast = $easting[0] % 5;
110
        $minorSquaresNorth = $northing[0] % 5;
111 2
        $minorLetterIndex = (int)(5 * $minorSquaresNorth + $minorSquaresEast);
112 2
        $minorLetter = self::GRID_LETTERS[$minorLetterIndex];
113
114 2
        return $majorLetter . $minorLetter . substr($easting, 1, 3) . substr($northing, 1, 3);
115 2
    }
116
117 2
    /**
118 1
     * Convert this grid reference into a latitude and longitude
119 1
     * @return LatLng
120 1
     */
121 1
    public function toLatLng()
122 1
    {
123
        $N = $this->y;
124
        $E = $this->x;
125
        $N0 = $this->getOriginNorthing();
126
        $E0 = $this->getOriginEasting();
127
        $phi0 = $this->getOriginLatitude();
128
        $lambda0 = $this->getOriginLongitude();
129
130
        return $this->convertToLatitudeLongitude($N, $E, $N0, $E0, $phi0, $lambda0);
131
    }
132
133
    /**
134
     * String version of coordinate.
135 2
     * @return string
136 2
     */
137 2
    public function __toString()
138 2
    {
139 2
        return "({$this->x}, {$this->y})";
140
    }
141
}
142