Completed
Push — analysis-8LvBQn ( bd69ec )
by Doug
05:37 queued 03:55
created

OSRef::getOriginNorthing()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
/**
3
 * PHPCoord.
4
 *
5
 * @author Jonathan Stott
6
 * @author Doug Wright
7
 */
8
declare(strict_types=1);
9
10
namespace PHPCoord;
11
12
/**
13
 * Ordnance Survey grid reference
14
 * References are accurate to 1m.
15
 *
16
 * @author Jonathan Stott
17
 * @author Doug Wright
18
 */
19
class OSRef extends TransverseMercator
20
{
21
    const GRID_LETTERS = 'VWXYZQRSTULMNOPFGHJKABCDE';
22
23
    /**
24
     * @return RefEll
25
     */
26 4
    public function getReferenceEllipsoid(): RefEll
27
    {
28 4
        return RefEll::airy1830();
29
    }
30
31
    /**
32
     * @return float
33
     */
34 5
    public function getScaleFactor(): float
35
    {
36 5
        return 0.9996012717;
37
    }
38
39
    /**
40
     * @return int
41
     */
42 5
    public function getOriginNorthing(): int
43
    {
44 5
        return -100000;
45
    }
46
47
    /**
48
     * @return int
49
     */
50 5
    public function getOriginEasting(): int
51
    {
52 5
        return 400000;
53
    }
54
55
    /**
56
     * @return float
57
     */
58 5
    public function getOriginLatitude(): float
59
    {
60 5
        return 49;
61
    }
62
63
    /**
64
     * @return float
65
     */
66 5
    public function getOriginLongitude(): float
67
    {
68 5
        return -2;
69
    }
70
71
    /**
72
     * Create a new object representing a OSGB reference.
73
     *
74
     * @param int $x
75
     * @param int $y
76
     * @param int $z
77
     */
78 17
    public function __construct($x, $y, $z = 0)
79
    {
80 17
        parent::__construct($x, $y, $z, RefEll::airy1830());
81
    }
82
83
    /**
84
     * Take a string formatted as a six-figure OS grid reference (e.g.
85
     * "TG514131") and return a reference to an OSRef object that represents
86
     * that grid reference.
87
     *
88
     * @param string $ref
89
     *
90
     * @return OSRef
91
     */
92 2
    public static function fromSixFigureReference($ref): self
93
    {
94
95
        //first (major) letter is the 500km grid sq, origin at -1000000, -500000
96 2
        $majorEasting = strpos(self::GRID_LETTERS, $ref[0]) % 5 * 500000 - 1000000;
97 2
        $majorNorthing = (floor(strpos(self::GRID_LETTERS, $ref[0]) / 5)) * 500000 - 500000;
98
99
        //second (minor) letter is 100km grid sq, origin at 0,0 of this square
100 2
        $minorEasting = strpos(self::GRID_LETTERS, $ref[1]) % 5 * 100000;
101 2
        $minorNorthing = (floor(strpos(self::GRID_LETTERS, $ref[1]) / 5)) * 100000;
102
103 2
        $easting = $majorEasting + $minorEasting + (substr($ref, 2, 3) * 100);
104 2
        $northing = $majorNorthing + $minorNorthing + (substr($ref, 5, 3) * 100);
105
106 2
        return new static((int) $easting, (int) $northing);
107
    }
108
109
    /**
110
     * Convert this grid reference into a grid reference string of a
111
     * given length (2, 4, 6, 8 or 10) including the two-character
112
     * designation for the 100km square. e.g. TG514131.
113
     *
114
     * @return string
115
     */
116 7
    private function toGridReference($length): string
117
    {
118 7
        $halfLength = $length / 2;
119
120 7
        $easting = str_pad((string) $this->x, 6, '0', STR_PAD_LEFT);
121 7
        $northing = str_pad((string) $this->y, 6, '0', STR_PAD_LEFT);
122
123 7
        $adjustedX = $this->x + 1000000;
124 7
        $adjustedY = $this->y + 500000;
125 7
        $majorSquaresEast = floor($adjustedX / 500000);
126 7
        $majorSquaresNorth = floor($adjustedY / 500000);
127 7
        $majorLetterIndex = (int) (5 * $majorSquaresNorth + $majorSquaresEast);
128 7
        $majorLetter = substr(self::GRID_LETTERS, $majorLetterIndex, 1);
129
130
        //second (minor) letter is 100km grid sq, origin at 0,0 of this square
131 7
        $minorSquaresEast = $easting[0] % 5;
132 7
        $minorSquaresNorth = $northing[0] % 5;
133 7
        $minorLetterIndex = (int) (5 * $minorSquaresNorth + $minorSquaresEast);
134 7
        $minorLetter = substr(self::GRID_LETTERS, $minorLetterIndex, 1);
135
136 7
        return $majorLetter.$minorLetter.substr($easting, 1, $halfLength).substr($northing, 1, $halfLength);
137
    }
138
139
    /**
140
     * Convert this grid reference into a string using a standard two-figure
141
     * grid reference including the two-character designation for the 100km
142
     * square. e.g. TG51 (10km square).
143
     *
144
     * @return string
145
     */
146 1
    public function toTwoFigureReference(): string
147
    {
148 1
        return $this->toGridReference(2);
149
    }
150
151
    /**
152
     * Convert this grid reference into a string using a standard four-figure
153
     * grid reference including the two-character designation for the 100km
154
     * square. e.g. TG5113 (1km square).
155
     *
156
     * @return string
157
     */
158 1
    public function toFourFigureReference(): string
159
    {
160 1
        return $this->toGridReference(4);
161
    }
162
163
    /**
164
     * Convert this grid reference into a string using a standard six-figure
165
     * grid reference including the two-character designation for the 100km
166
     * square. e.g. TG514131 (100m square).
167
     *
168
     * @return string
169
     */
170 3
    public function toSixFigureReference(): string
171
    {
172 3
        return $this->toGridReference(6);
173
    }
174
175
    /**
176
     * Convert this grid reference into a string using a standard eight-figure
177
     * grid reference including the two-character designation for the 100km
178
     * square. e.g. TG51431312 (10m square).
179
     *
180
     * @return string
181
     */
182 1
    public function toEightFigureReference(): string
183
    {
184 1
        return $this->toGridReference(8);
185
    }
186
187
    /**
188
     * Convert this grid reference into a string using a standard ten-figure
189
     * grid reference including the two-character designation for the 100km
190
     * square. e.g. TG5143113121 (1m square).
191
     *
192
     * @return string
193
     */
194 1
    public function toTenFigureReference(): string
195
    {
196 1
        return $this->toGridReference(10);
197
    }
198
199
    /**
200
     * Convert this grid reference into a latitude and longitude.
201
     *
202
     * @return LatLng
203
     */
204 3 View Code Duplication
    public function toLatLng(): LatLng
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
205
    {
206 3
        $N = $this->y;
207 3
        $E = $this->x;
208 3
        $N0 = $this->getOriginNorthing();
209 3
        $E0 = $this->getOriginEasting();
210 3
        $phi0 = $this->getOriginLatitude();
211 3
        $lambda0 = $this->getOriginLongitude();
212
213 3
        return $this->convertToLatitudeLongitude($N, $E, $N0, $E0, $phi0, $lambda0);
214
    }
215
216
    /**
217
     * String version of coordinate.
218
     *
219
     * @return string
220
     */
221 5
    public function __toString(): string
222
    {
223 5
        return "({$this->x}, {$this->y})";
224
    }
225
}
226