Passed
Push — master ( fd93b5...48e916 )
by Doug
40:26 queued 29:39
created

GeographicGrid::applyForwardAdjustment()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 5
nc 4
nop 2
dl 0
loc 9
ccs 6
cts 6
cp 1
crap 3
rs 10
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 7
    public function applyForwardAdjustment(GeographicPoint $point, Geographic $to): GeographicPoint
21
    {
22 7
        $shifts = $this->getValues($point->getLongitude()->getValue(), $point->getLatitude()->getValue());
23
24 7
        $latitude = $point->getLatitude()->add($shifts[0]);
25 7
        $longitude = $point->getLongitude()->add($shifts[1]);
26 7
        $height = $point->getHeight() && $shifts[2] ? $point->getHeight()->add($shifts[2]) : null;
27
28 7
        return GeographicPoint::create($latitude, $longitude, $height, $to, $point->getCoordinateEpoch());
29
    }
30
31 5
    public function applyReverseAdjustment(GeographicPoint $point, Geographic $to): GeographicPoint
32
    {
33 5
        $shifts = [new ArcSecond(0), new ArcSecond(0)];
34 5
        $latitude = $point->getLatitude();
35 5
        $longitude = $point->getLongitude();
36
37
        do {
38 5
            $prevShifts = $shifts;
39 5
            $shifts = $this->getValues($longitude->getValue(), $latitude->getValue());
40 5
            $latitude = $point->getLatitude()->subtract($shifts[0]);
41 5
            $longitude = $point->getLongitude()->subtract($shifts[1]);
42 5
        } while (abs($shifts[0]->subtract($prevShifts[0])->getValue()) > self::ITERATION_CONVERGENCE && abs($shifts[1]->subtract($prevShifts[1])->getValue()) > self::ITERATION_CONVERGENCE);
43
44 5
        $height = $point->getHeight() && $shifts[2] ? $point->getHeight()->subtract($shifts[2]) : null;
45
46 5
        return GeographicPoint::create($latitude, $longitude, $height, $to, $point->getCoordinateEpoch());
47
    }
48
}
49