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

applyReverseAdjustment()   A

Complexity

Conditions 4
Paths 1

Size

Total Lines 21
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

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