Mercator::loadCoordinate()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 6
rs 9.4285
1
<?php
2
namespace Nubs\Coordinator\CoordinateSystem;
3
4
use Nubs\Coordinator\CoordinateFactory;
5
use Nubs\Coordinator\Spheroid;
6
7
class Mercator
8
{
9
    /**
10
     * The spheroid being referenced.
11
     *
12
     * @var \Nubs\Coordinator\Spheroid
13
     */
14
    private $_spheroid;
15
16
    /**
17
     * The coordinate factory.
18
     *
19
     * @var \Nubs\Coordinator\CoordinateFactory
20
     */
21
    private $_coordinateFactory;
22
23
    /**
24
     * Initialize a Mercator Coordinate System for the given spheroid.
25
     *
26
     * @param \Nubs\Coordinator\Spheroid $spheroid The spheroid being referenced.
27
     * @param \Nubs\Coordinator\CoordinateFactory $coordinateFactory The coordinate factory
28
     */
29
    public function __construct(Spheroid $spheroid, CoordinateFactory $coordinateFactory)
30
    {
31
        $this->_spheroid = $spheroid;
32
        $this->_coordinateFactory = $coordinateFactory;
33
    }
34
35
    /**
36
     * Load a coordinate from the mercator coordinates.
37
     *
38
     * This uses a spherical model of the spheroid.
39
     *
40
     * @param float $x The x coordinate.
41
     * @param float $y The y coordinate.
42
     * @return \Nubs\Coordinator\Coordinate The coordinate after conversion.
43
     */
44
    public function loadCoordinate($x, $y)
45
    {
46
        $radius = $this->_spheroid->equatorialRadius();
47
48
        return $this->_coordinateFactory->createFromRadians($this->_gudermannian($y / $radius), $x / $radius);
49
    }
50
51
    /**
52
     * Compute the Gudermannian of the given number.
53
     *
54
     * @see http://en.wikipedia.org/wiki/Gudermannian_function
55
     *
56
     * Useful for computing the latitude in radians when given the latitudinal distance from the equator.
57
     *
58
     * @param float $x
59
     * @return float
60
     */
61
    private function _gudermannian($x)
62
    {
63
        return atan(sinh($x));
64
    }
65
}
66