Passed
Push — master ( 705a37...2b8bad )
by Doug
17:25 queued 45s
created

applyForwardAdjustment()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 6
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 9
ccs 3
cts 3
cp 1
crap 1
rs 10
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\Geographic2D;
13
use PHPCoord\CoordinateReferenceSystem\Geographic3D;
14
use PHPCoord\GeographicPoint;
15
use PHPCoord\UnitOfMeasure\Length\Metre;
16
17
class IGNFGeocentricTranslationGrid extends GeographicGrid
18
{
19
    use IGNFGrid;
20
21 4
    public function applyForwardAdjustment(GeographicPoint $point, Geographic2D|Geographic3D $to): GeographicPoint
22
    {
23 4
        [$tx, $ty, $tz] = $this->getValues($point->getLongitude()->asDegrees()->getValue(), $point->getLatitude()->asDegrees()->getValue());
24
25 4
        return $point->geocentricTranslation(
26
            $to,
27
            $tx,
28
            $ty,
29
            $tz,
30
        );
31
    }
32
33 2
    public function applyReverseAdjustment(GeographicPoint $point, Geographic2D|Geographic3D $to): GeographicPoint
34
    {
35 2
        $adjustment = [new Metre(0), new Metre(0), new Metre(0)];
36 2
        $latitude = $point->getLatitude();
37 2
        $longitude = $point->getLongitude();
38
39
        do {
40 2
            $prevAdjustment = $adjustment;
41 2
            $adjustment = $this->getValues($longitude->asDegrees()->getValue(), $latitude->asDegrees()->getValue());
42 2
            $newPoint = $point->geocentricTranslation(
43
                $to,
44 2
                $adjustment[0]->multiply(-1),
45 2
                $adjustment[1]->multiply(-1),
46 2
                $adjustment[2]->multiply(-1),
47
            );
48
49 2
            $latitude = $newPoint->getLatitude();
50 2
            $longitude = $newPoint->getLongitude();
51 2
        } while (abs($adjustment[0]->subtract($prevAdjustment[0])->getValue()) > self::ITERATION_CONVERGENCE && abs($adjustment[1]->subtract($prevAdjustment[1])->getValue()) > self::ITERATION_CONVERGENCE && abs($adjustment[2]->subtract($prevAdjustment[2])->getValue()) > self::ITERATION_CONVERGENCE);
52
53 2
        return $newPoint;
54
    }
55
56
    /**
57
     * @return Metre[]
58
     */
59 4
    public function getValues(float $x, float $y): array
60
    {
61 4
        $offsets = $this->interpolate($x, $y);
62
63 4
        return [new Metre($offsets[0]), new Metre($offsets[1]), new Metre($offsets[2])];
64
    }
65
}
66