Passed
Push — master ( fefee4...ec3bb4 )
by Doug
39:04
created

GeographicGrid   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 17
c 1
b 0
f 0
dl 0
loc 31
ccs 17
cts 17
cp 1
rs 10
wmc 8

2 Methods

Rating   Name   Duplication   Size   Complexity  
A applyForwardAdjustment() 0 9 3
A applyReverseAdjustment() 0 16 5
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