Passed
Push — master ( fefee4...ec3bb4 )
by Doug
39:04
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 PHPCoord\CoordinateReferenceSystem\Geographic2D;
12
use PHPCoord\CoordinateReferenceSystem\Geographic3D;
13
use PHPCoord\GeographicPoint;
14
use PHPCoord\UnitOfMeasure\Angle\ArcSecond;
15
16
use function abs;
17
18
abstract class GeographicGrid extends Grid
19
{
20
    protected const ITERATION_CONVERGENCE = 0.0001;
21
22 9
    public function applyForwardAdjustment(GeographicPoint $point, Geographic2D|Geographic3D $to): GeographicPoint
23
    {
24 9
        $shifts = $this->getValues($point->getLongitude()->getValue(), $point->getLatitude()->getValue());
25
26 9
        $latitude = $point->getLatitude()->add($shifts[0]);
27 9
        $longitude = $point->getLongitude()->add($shifts[1]);
28 9
        $height = $point->getHeight() && $shifts[2] ? $point->getHeight()->add($shifts[2]) : null;
29
30 9
        return GeographicPoint::create($to, $latitude, $longitude, $height, $point->getCoordinateEpoch());
31
    }
32
33 6
    public function applyReverseAdjustment(GeographicPoint $point, Geographic2D|Geographic3D $to): GeographicPoint
34
    {
35 6
        $shifts = [new ArcSecond(0), new ArcSecond(0)];
36 6
        $latitude = $point->getLatitude();
37 6
        $longitude = $point->getLongitude();
38
39
        do {
40 6
            $prevShifts = $shifts;
41 6
            $shifts = $this->getValues($longitude->getValue(), $latitude->getValue());
42 6
            $latitude = $point->getLatitude()->subtract($shifts[0]);
43 6
            $longitude = $point->getLongitude()->subtract($shifts[1]);
44 6
        } while (abs($shifts[0]->subtract($prevShifts[0])->getValue()) > self::ITERATION_CONVERGENCE && abs($shifts[1]->subtract($prevShifts[1])->getValue()) > self::ITERATION_CONVERGENCE);
45
46 6
        $height = $point->getHeight() && $shifts[2] ? $point->getHeight()->subtract($shifts[2]) : null;
47
48 6
        return GeographicPoint::create($to, $latitude, $longitude, $height, $point->getCoordinateEpoch());
49
    }
50
}
51