Completed
Push — master ( b0c99e...a2c34e )
by Doug
02:39
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

Importance

Changes 5
Bugs 0 Features 1
Metric Value
c 5
b 0
f 1
dl 0
loc 16
ccs 8
cts 8
cp 1
rs 9.4286
cc 1
eloc 8
nc 1
nop 1
crap 1
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
    const GRID_LETTERS = "VWXYZQRSTULMNOPFGHJKABCDE";
21
22 2
    public function getReferenceEllipsoid()
23
    {
24 2
        return RefEll::airy1830();
25
    }
26
27 4
    public function getScaleFactor()
28
    {
29 4
        return 0.9996012717;
30
    }
31
32 4
    public function getOriginNorthing()
33
    {
34 4
        return -100000;
35
    }
36
37 4
    public function getOriginEasting()
38
    {
39 4
        return 400000;
40
    }
41
42 4
    public function getOriginLatitude()
43
    {
44 4
        return 49;
45
    }
46
47 4
    public function getOriginLongitude()
48
    {
49 4
        return -2;
50
    }
51
52
    /**
53
     * Create a new object representing a UTM reference.
54
     *
55
     * @param int $x
56
     * @param int $y
57
     */
58 10
    public function __construct($x, $y)
59
    {
60 10
        parent::__construct($x, $y, 0, RefEll::airy1830());
61 10
    }
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 2
    public static function fromSixFigureReference($ref)
72
    {
73
74
        //first (major) letter is the 500km grid sq, origin at -1000000, -500000
75 2
        $majorEasting = strpos(self::GRID_LETTERS, $ref[0]) % 5  * 500000 - 1000000;
76 2
        $majorNorthing = (floor(strpos(self::GRID_LETTERS, $ref[0]) / 5)) * 500000 - 500000;
77
78
        //second (minor) letter is 100km grid sq, origin at 0,0 of this square
79 2
        $minorEasting = strpos(self::GRID_LETTERS, $ref[1]) % 5  * 100000;
80 2
        $minorNorthing = (floor(strpos(self::GRID_LETTERS, $ref[1]) / 5)) * 100000;
81
82 2
        $easting = $majorEasting + $minorEasting + (substr($ref, 2, 3) * 100);
83 2
        $northing = $majorNorthing + $minorNorthing + (substr($ref, 5, 3) * 100);
84
85 2
        return new OSRef($easting, $northing);
86
    }
87
88
    /**
89
     * 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
     */
94 3
    public function toSixFigureReference()
95
    {
96
97 3
        $easting = str_pad($this->x, 6, 0, STR_PAD_LEFT);
98 3
        $northing = str_pad($this->y, 6, 0, STR_PAD_LEFT);
99
100
101 3
        $adjustedX = $this->x + 1000000;
102 3
        $adjustedY = $this->y + 500000;
103 3
        $majorSquaresEast = floor($adjustedX / 500000);
104 3
        $majorSquaresNorth = floor($adjustedY / 500000);
105 3
        $majorLetterIndex = (int)(5 * $majorSquaresNorth + $majorSquaresEast);
106 3
        $majorLetter = substr(self::GRID_LETTERS, $majorLetterIndex, 1);
107
108
        //second (minor) letter is 100km grid sq, origin at 0,0 of this square
109 3
        $minorSquaresEast = $easting[0] % 5;
110 3
        $minorSquaresNorth = $northing[0] % 5;
111 3
        $minorLetterIndex = (int)(5 * $minorSquaresNorth + $minorSquaresEast);
112 3
        $minorLetter = substr(self::GRID_LETTERS, $minorLetterIndex, 1);
113
114 3
        return $majorLetter . $minorLetter . substr($easting, 1, 3) . substr($northing, 1, 3);
115
    }
116
117
    /**
118
     * Convert this grid reference into a latitude and longitude
119
     * @return LatLng
120
     */
121 2 View Code Duplication
    public function toLatLng()
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...
122
    {
123 2
        $N = $this->y;
124 2
        $E = $this->x;
125 2
        $N0 = $this->getOriginNorthing();
126 2
        $E0 = $this->getOriginEasting();
127 2
        $phi0 = $this->getOriginLatitude();
128 2
        $lambda0 = $this->getOriginLongitude();
129
130 2
        return $this->convertToLatitudeLongitude($N, $E, $N0, $E0, $phi0, $lambda0);
131
    }
132
133
    /**
134
     * String version of coordinate.
135
     * @return string
136
     */
137 5
    public function __toString()
138
    {
139 5
        return "({$this->x}, {$this->y})";
140
    }
141
}
142