Passed
Push — master ( f00c65...31a7ea )
by Doug
61:30
created

GeographicGrid::applyReverseAdjustment()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 16
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 11
nc 4
nop 2
dl 0
loc 16
rs 9.6111
c 1
b 0
f 0
1
<?php
2
/**
3
 * PHPCoord.
4
 *
5
 * @author Doug Wright
6
 */
7
declare(strict_types=1);
8
9
namespace PHPCoord\CoordinateOperation;
10
11
use function abs;
12
use PHPCoord\CoordinateReferenceSystem\Geographic;
13
use PHPCoord\GeographicPoint;
14
use PHPCoord\UnitOfMeasure\Angle\ArcSecond;
15
16
abstract class GeographicGrid extends Grid
17
{
18
    protected const ITERATION_CONVERGENCE = 0.0001;
19
20
    public function applyForwardAdjustment(GeographicPoint $point, Geographic $to): GeographicPoint
21
    {
22
        $shifts = $this->getValues($point->getLongitude()->getValue(), $point->getLatitude()->getValue());
23
24
        $latitude = $point->getLatitude()->add($shifts[0]);
25
        $longitude = $point->getLongitude()->add($shifts[1]);
26
        $height = $point->getHeight() && $shifts[2] ? $point->getHeight()->add($shifts[2]) : null;
27
28
        return GeographicPoint::create($latitude, $longitude, $height, $to, $point->getCoordinateEpoch());
29
    }
30
31
    public function applyReverseAdjustment(GeographicPoint $point, Geographic $to): GeographicPoint
32
    {
33
        $shifts = [new ArcSecond(0), new ArcSecond(0)];
34
        $latitude = $point->getLatitude();
35
        $longitude = $point->getLongitude();
36
37
        do {
38
            $prevShifts = $shifts;
39
            $shifts = $this->getValues($longitude->getValue(), $latitude->getValue());
40
            $latitude = $point->getLatitude()->subtract($shifts[0]);
41
            $longitude = $point->getLongitude()->subtract($shifts[1]);
42
        } while (abs($shifts[0]->subtract($prevShifts[0])->getValue()) > self::ITERATION_CONVERGENCE && abs($shifts[1]->subtract($prevShifts[1])->getValue()) > self::ITERATION_CONVERGENCE);
43
44
        $height = $point->getHeight() && $shifts[2] ? $point->getHeight()->subtract($shifts[2]) : null;
45
46
        return GeographicPoint::create($latitude, $longitude, $height, $to, $point->getCoordinateEpoch());
47
    }
48
}
49