-
-
Notifications
You must be signed in to change notification settings - Fork 18
/
Point.php
326 lines (282 loc) · 13.8 KB
/
Point.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
<?php
/**
* PHPCoord.
*
* @author Doug Wright
*/
declare(strict_types=1);
namespace PHPCoord;
use function abs;
use function acos;
use function asin;
use function atan;
use function atan2;
use function cos;
use DateTimeImmutable;
use const M_PI;
use PHPCoord\CoordinateOperation\CoordinateOperationMethods;
use PHPCoord\CoordinateOperation\CoordinateOperations;
use PHPCoord\CoordinateOperation\GeographicValue;
use PHPCoord\CoordinateReferenceSystem\Compound;
use PHPCoord\CoordinateReferenceSystem\CoordinateReferenceSystem;
use PHPCoord\CoordinateReferenceSystem\Geocentric;
use PHPCoord\CoordinateReferenceSystem\Geographic2D;
use PHPCoord\CoordinateReferenceSystem\Geographic3D;
use PHPCoord\CoordinateReferenceSystem\Projected;
use PHPCoord\CoordinateReferenceSystem\Vertical;
use PHPCoord\Datum\Ellipsoid;
use PHPCoord\UnitOfMeasure\Angle\Angle;
use PHPCoord\UnitOfMeasure\Length\Length;
use PHPCoord\UnitOfMeasure\Length\Metre;
use PHPCoord\UnitOfMeasure\Scale\Coefficient;
use PHPCoord\UnitOfMeasure\Scale\Scale;
use PHPCoord\UnitOfMeasure\UnitOfMeasure;
use PHPCoord\UnitOfMeasure\UnitOfMeasureFactory;
use function sin;
use function sqrt;
use function sscanf;
use function str_starts_with;
use Stringable;
use function tan;
abstract class Point implements Stringable
{
protected const ITERATION_CONVERGENCE_FORMULA = 1e-10;
protected const ITERATION_CONVERGENCE_GRID = 0.0001;
protected const METHODS_REQUIRING_HORIZONTAL_POINT = [
CoordinateOperationMethods::EPSG_VERTICAL_OFFSET_AND_SLOPE => CoordinateOperationMethods::EPSG_VERTICAL_OFFSET_AND_SLOPE,
CoordinateOperationMethods::EPSG_ZERO_TIDE_HEIGHT_TO_MEAN_TIDE_HEIGHT_EVRF2019 => CoordinateOperationMethods::EPSG_ZERO_TIDE_HEIGHT_TO_MEAN_TIDE_HEIGHT_EVRF2019,
CoordinateOperationMethods::EPSG_VERTICAL_OFFSET_BY_GRID_INTERPOLATION_GTX => CoordinateOperationMethods::EPSG_VERTICAL_OFFSET_BY_GRID_INTERPOLATION_GTX,
CoordinateOperationMethods::EPSG_VERTICAL_OFFSET_BY_GRID_INTERPOLATION_PL_TXT => CoordinateOperationMethods::EPSG_VERTICAL_OFFSET_BY_GRID_INTERPOLATION_PL_TXT,
CoordinateOperationMethods::EPSG_VERTICAL_OFFSET_BY_GRID_INTERPOLATION_BEV_AT => CoordinateOperationMethods::EPSG_VERTICAL_OFFSET_BY_GRID_INTERPOLATION_BEV_AT,
];
protected const METHODS_THAT_REQUIRE_DIRECTION = [
CoordinateOperationMethods::EPSG_SIMILARITY_TRANSFORMATION => CoordinateOperationMethods::EPSG_SIMILARITY_TRANSFORMATION,
CoordinateOperationMethods::EPSG_AFFINE_PARAMETRIC_TRANSFORMATION => CoordinateOperationMethods::EPSG_AFFINE_PARAMETRIC_TRANSFORMATION,
CoordinateOperationMethods::EPSG_NADCON5_2D => CoordinateOperationMethods::EPSG_NADCON5_2D,
CoordinateOperationMethods::EPSG_NADCON5_3D => CoordinateOperationMethods::EPSG_NADCON5_3D,
CoordinateOperationMethods::EPSG_NTV2 => CoordinateOperationMethods::EPSG_NTV2,
CoordinateOperationMethods::EPSG_ZERO_TIDE_HEIGHT_TO_MEAN_TIDE_HEIGHT_EVRF2019 => CoordinateOperationMethods::EPSG_ZERO_TIDE_HEIGHT_TO_MEAN_TIDE_HEIGHT_EVRF2019,
CoordinateOperationMethods::EPSG_GEOCENTRIC_TRANSLATION_BY_GRID_INTERPOLATION_IGN => CoordinateOperationMethods::EPSG_GEOCENTRIC_TRANSLATION_BY_GRID_INTERPOLATION_IGN,
CoordinateOperationMethods::EPSG_VERTICAL_OFFSET_BY_GRID_INTERPOLATION_GTX => CoordinateOperationMethods::EPSG_VERTICAL_OFFSET_BY_GRID_INTERPOLATION_GTX,
CoordinateOperationMethods::EPSG_VERTICAL_OFFSET_BY_GRID_INTERPOLATION_PL_TXT => CoordinateOperationMethods::EPSG_VERTICAL_OFFSET_BY_GRID_INTERPOLATION_PL_TXT,
CoordinateOperationMethods::EPSG_VERTICAL_OFFSET_BY_GRID_INTERPOLATION_BEV_AT => CoordinateOperationMethods::EPSG_VERTICAL_OFFSET_BY_GRID_INTERPOLATION_BEV_AT,
];
private static array $gridCache = [];
/**
* @internal
*/
public function performOperation(string $srid, Compound|Geocentric|Geographic2D|Geographic3D|Projected|Vertical $to, bool $inReverse, array $additionalParams = []): self
{
$operation = CoordinateOperations::getOperationData($srid);
if ($operation['method'] === CoordinateOperationMethods::EPSG_ALIAS) {
$point = clone $this;
$point->crs = $to;
return $point;
} else {
$method = CoordinateOperationMethods::getFunctionName($operation['method']);
$params = self::resolveParamsByOperation($srid, $operation['method'], $inReverse);
if (isset(self::METHODS_REQUIRING_HORIZONTAL_POINT[$operation['method']])) {
$params['horizontalPoint'] = $additionalParams['horizontalPoint'];
}
return $this->$method($to, ...$params);
}
}
protected static function resolveParamsByOperation(string $operationSrid, string $methodSrid, bool $inReverse): array
{
$params = [];
$powerCoefficients = [];
foreach (CoordinateOperations::getParamData($operationSrid) as $paramName => $paramData) {
if (isset($paramData['fileProvider'])) {
$params[$paramName] = static::$gridCache[$paramData['fileProvider']] ??= (new $paramData['fileProvider']())->provideGrid();
} else {
if ($inReverse && $paramData['reverses']) {
$paramData['value'] *= -1;
}
if ($paramData['uom']) {
$param = UnitOfMeasureFactory::makeUnit($paramData['value'], $paramData['uom']);
} else {
$param = $paramData['value'];
}
if (str_starts_with($paramName, 'Au') || str_starts_with($paramName, 'Bu')) {
$powerCoefficients[$paramName] = $param;
} else {
$params[$paramName] = $param;
}
}
}
if ($powerCoefficients) {
$params['powerCoefficients'] = $powerCoefficients;
}
if (isset(self::METHODS_THAT_REQUIRE_DIRECTION[$methodSrid])) {
$params['inReverse'] = $inReverse;
}
return $params;
}
protected static function sign(float $number): int
{
if ($number < 0) {
return -1;
}
return 1;
}
/**
* Calculate surface distance between two points.
*/
protected static function vincenty(GeographicValue $from, GeographicValue $to, Ellipsoid $ellipsoid): Length
{
$a = $ellipsoid->getSemiMajorAxis()->asMetres()->getValue();
$b = $ellipsoid->getSemiMinorAxis()->asMetres()->getValue();
$f = $ellipsoid->getInverseFlattening();
$U1 = atan((1 - $f) * tan($from->getLatitude()->asRadians()->getValue()));
$U2 = atan((1 - $f) * tan($to->getLatitude()->asRadians()->getValue()));
$L = $to->getLongitude()->subtract($from->getLongitude())->asRadians()->getValue();
$lambda = $L;
do {
$lambdaN = $lambda;
$sinSigma = sqrt((cos($U2) * sin($lambda)) ** 2 + (cos($U1) * sin($U2) - sin($U1) * cos($U2) * cos($lambda)) ** 2);
$cosSigma = sin($U1) * sin($U2) + cos($U1) * cos($U2) * cos($lambda);
$sigma = atan2($sinSigma, $cosSigma);
$sinAlpha = cos($U1) * cos($U2) * sin($lambda) / $sinSigma;
$cosSqAlpha = (1 - $sinAlpha ** 2);
$cos2SigmaM = $cosSqAlpha ? $cosSigma - (2 * sin($U1) * sin($U2) / $cosSqAlpha) : 0;
$C = $f / 16 * $cosSqAlpha * (4 + $f * (4 - 3 * $cosSqAlpha));
$lambda = $L + (1 - $C) * $f * $sinAlpha * ($sigma + $C * $sinSigma * ($cos2SigmaM + $C * $cosSigma * (-1 + 2 * $cos2SigmaM ** 2)));
} while (abs($lambda - $lambdaN) >= static::ITERATION_CONVERGENCE_FORMULA && abs($lambda) < M_PI);
// Antipodal case
if (abs($lambda) >= M_PI) {
if ($L >= 0) {
$LPrime = M_PI - $L;
} else {
$LPrime = -M_PI - $L;
}
$lambdaPrime = 0;
$sigma = M_PI - abs($U1 + $U2);
$sinSigma = sin($sigma);
$cosSqAlpha = 0.5;
$sinAlpha = 0;
do {
$sinAlphaN = $sinAlpha;
$C = $f / 16 * $cosSqAlpha * (4 + $f * (4 - 3 * $cosSqAlpha));
$cos2SigmaM = cos($sigma) - 2 * sin($U1) * sin($U2) / $cosSqAlpha;
$D = (1 - $C) * $f * ($sigma + $C * $sinSigma * ($cos2SigmaM + $C * cos($sigma) * (-1 + 2 * $cos2SigmaM ** 2)));
$sinAlpha = ($LPrime - $lambdaPrime) / $D;
$cosSqAlpha = (1 - $sinAlpha ** 2);
$sinLambdaPrime = ($sinAlpha * $sinSigma) / (cos($U1) * cos($U2));
$lambdaPrime = self::asin($sinLambdaPrime);
$sinSqSigma = (cos($U2) * $sinLambdaPrime) ** 2 + (cos($U1) * sin($U2) + sin($U1) * cos($U2) * cos($lambdaPrime)) ** 2;
$sinSigma = sqrt($sinSqSigma);
} while (abs($sinAlpha - $sinAlphaN) >= static::ITERATION_CONVERGENCE_FORMULA);
}
$E = sqrt(1 + (($a ** 2 - $b ** 2) / $b ** 2) * $cosSqAlpha);
$F = ($E - 1) / ($E + 1);
$A = (1 + $F ** 2 / 4) / (1 - $F);
$B = $F * (1 - 3 / 8 * $F ** 2);
$deltaSigma = $B * $sinSigma * ($cos2SigmaM + $B / 4 * ($cosSigma * (-1 + 2 * $cos2SigmaM ** 2) - $B / 6 * $cos2SigmaM * (-3 + 4 * $sinSigma ** 2) * (-3 + 4 * $cos2SigmaM ** 2)));
return new Metre($b * $A * ($sigma - $deltaSigma));
}
/**
* General polynomial.
* @param Coefficient[] $powerCoefficients
*/
protected function generalPolynomialUnitless(
float $xs,
float $ys,
UnitOfMeasure $ordinate1OfEvaluationPointInSourceCRS,
UnitOfMeasure $ordinate2OfEvaluationPointInSourceCRS,
UnitOfMeasure $ordinate1OfEvaluationPointInTargetCRS,
UnitOfMeasure $ordinate2OfEvaluationPointInTargetCRS,
Scale $scalingFactorForSourceCRSCoordDifferences,
Scale $scalingFactorForTargetCRSCoordDifferences,
Scale $A0,
Scale $B0,
array $powerCoefficients
): array {
$xso = $ordinate1OfEvaluationPointInSourceCRS->getValue();
$yso = $ordinate2OfEvaluationPointInSourceCRS->getValue();
$xto = $ordinate1OfEvaluationPointInTargetCRS->getValue();
$yto = $ordinate2OfEvaluationPointInTargetCRS->getValue();
$U = $scalingFactorForSourceCRSCoordDifferences->asUnity()->getValue() * ($xs - $xso);
$V = $scalingFactorForSourceCRSCoordDifferences->asUnity()->getValue() * ($ys - $yso);
$mTdX = $A0->getValue();
foreach ($powerCoefficients as $coefficientName => $coefficientValue) {
if ($coefficientName[0] === 'A') {
sscanf($coefficientName, 'Au%dv%d', $uPower, $vPower);
$mTdX += $coefficientValue->getValue() * $U ** $uPower * $V ** $vPower;
}
}
$mTdY = $B0->getValue();
foreach ($powerCoefficients as $coefficientName => $coefficientValue) {
if ($coefficientName[0] === 'B') {
sscanf($coefficientName, 'Bu%dv%d', $uPower, $vPower);
$mTdY += $coefficientValue->getValue() * $U ** $uPower * $V ** $vPower;
}
}
$xt = $xs - $xso + $xto + $mTdX / $scalingFactorForTargetCRSCoordDifferences->asUnity()->getValue();
$yt = $ys - $yso + $yto + $mTdY / $scalingFactorForTargetCRSCoordDifferences->asUnity()->getValue();
return ['xt' => $xt, 'yt' => $yt];
}
/**
* Reversible polynomial.
*/
protected function reversiblePolynomialUnitless(
float $xs,
float $ys,
Angle $ordinate1OfEvaluationPoint,
Angle $ordinate2OfEvaluationPoint,
Scale $scalingFactorForCoordDifferences,
Scale $A0,
Scale $B0,
array $powerCoefficients
): array {
$xo = $ordinate1OfEvaluationPoint->getValue();
$yo = $ordinate2OfEvaluationPoint->getValue();
$U = $scalingFactorForCoordDifferences->asUnity()->getValue() * ($xs - $xo);
$V = $scalingFactorForCoordDifferences->asUnity()->getValue() * ($ys - $yo);
$mTdX = $A0->getValue();
foreach ($powerCoefficients as $coefficientName => $coefficientValue) {
if ($coefficientName[0] === 'A') {
sscanf($coefficientName, 'Au%dv%d', $uPower, $vPower);
$mTdX += $coefficientValue->getValue() * $U ** $uPower * $V ** $vPower;
}
}
$mTdY = $B0->getValue();
foreach ($powerCoefficients as $coefficientName => $coefficientValue) {
if ($coefficientName[0] === 'B') {
sscanf($coefficientName, 'Bu%dv%d', $uPower, $vPower);
$mTdY += $coefficientValue->getValue() * $U ** $uPower * $V ** $vPower;
}
}
$xt = $xs + $mTdX * $scalingFactorForCoordDifferences->asUnity()->getValue();
$yt = $ys + $mTdY * $scalingFactorForCoordDifferences->asUnity()->getValue();
return ['xt' => $xt, 'yt' => $yt];
}
/**
* Floating point vagaries mean that it's possible for inputs to be e.g. 1.00000000000001 which makes PHP give a
* silent NaN as output so inputs need to be capped. atan/atan2 are not affected, they seem to cap internally.
*/
protected static function acos(float $num): float
{
if ($num > 1.0) {
$num = 1.0;
} elseif ($num < -1) {
$num = -1.0;
}
return acos($num);
}
/**
* Floating point vagaries mean that it's possible for inputs to be e.g. 1.00000000000001 which makes PHP give a
* silent NaN as output so inputs need to be capped. atan/atan2 are not affected, they seem to cap internally.
*/
protected static function asin(float $num): float
{
if ($num > 1.0) {
$num = 1.0;
} elseif ($num < -1.0) {
$num = -1.0;
}
return asin($num);
}
abstract public function getCRS(): CoordinateReferenceSystem;
abstract public function getCoordinateEpoch(): ?DateTimeImmutable;
abstract public function calculateDistance(self $to): Length;
}