|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* PHPCoord. |
|
4
|
|
|
* |
|
5
|
|
|
* @author Doug Wright |
|
6
|
|
|
*/ |
|
7
|
|
|
declare(strict_types=1); |
|
8
|
|
|
|
|
9
|
|
|
namespace PHPCoord\CoordinateOperation; |
|
10
|
|
|
|
|
11
|
|
|
use DateTimeImmutable; |
|
12
|
|
|
use PHPCoord\CompoundPoint; |
|
13
|
|
|
use PHPCoord\CoordinateReferenceSystem\Compound; |
|
14
|
|
|
use PHPCoord\CoordinateReferenceSystem\CoordinateReferenceSystem; |
|
15
|
|
|
use PHPCoord\CoordinateReferenceSystem\Geocentric; |
|
16
|
|
|
use PHPCoord\CoordinateReferenceSystem\Geographic2D; |
|
17
|
|
|
use PHPCoord\CoordinateReferenceSystem\Geographic3D; |
|
18
|
|
|
use PHPCoord\CoordinateReferenceSystem\Projected; |
|
|
|
|
|
|
19
|
|
|
use PHPCoord\CoordinateReferenceSystem\Vertical; |
|
20
|
|
|
use PHPCoord\Exception\UnknownConversionException; |
|
21
|
|
|
use PHPCoord\GeocentricPoint; |
|
22
|
|
|
use PHPCoord\GeographicPoint; |
|
23
|
|
|
use PHPCoord\Geometry\BoundingArea; |
|
24
|
|
|
use PHPCoord\Geometry\RegionMap; |
|
25
|
|
|
use PHPCoord\Point; |
|
26
|
|
|
use PHPCoord\ProjectedPoint; |
|
27
|
|
|
use PHPCoord\UnitOfMeasure\Time\Year; |
|
28
|
|
|
|
|
29
|
|
|
use function abs; |
|
30
|
|
|
use function array_column; |
|
31
|
|
|
use function array_shift; |
|
32
|
|
|
use function array_sum; |
|
33
|
|
|
use function array_unique; |
|
34
|
|
|
use function assert; |
|
35
|
|
|
use function class_exists; |
|
36
|
|
|
use function count; |
|
37
|
|
|
use function in_array; |
|
38
|
|
|
use function usort; |
|
39
|
|
|
use function str_ends_with; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @internal |
|
43
|
|
|
*/ |
|
44
|
|
|
trait AutoConversion |
|
45
|
|
|
{ |
|
46
|
|
|
private int $maxChainDepth = 6; // if traits could have constants... |
|
47
|
|
|
|
|
48
|
|
|
private static array $completePathCache = []; |
|
49
|
|
|
|
|
50
|
2389 |
|
public function convert(Compound|Geocentric|Geographic2D|Geographic3D|Projected|Vertical $to, bool $ignoreBoundaryRestrictions = false): Point |
|
51
|
|
|
{ |
|
52
|
2389 |
|
if ($this->getCRS() == $to) { |
|
53
|
225 |
|
return $this; |
|
|
|
|
|
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
2209 |
|
$point = $this; |
|
57
|
2209 |
|
$path = $this->findOperationPath($this->getCRS(), $to, $ignoreBoundaryRestrictions); |
|
58
|
|
|
|
|
59
|
2173 |
|
foreach ($path as $step) { |
|
60
|
2173 |
|
$target = CoordinateReferenceSystem::fromSRID($step['in_reverse'] ? $step['source_crs'] : $step['target_crs']); |
|
61
|
2173 |
|
$point = $point->performOperation($step['operation'], $target, $step['in_reverse']); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
2173 |
|
return $point; |
|
|
|
|
|
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
2209 |
|
protected function findOperationPath(Compound|Geocentric|Geographic2D|Geographic3D|Projected|Vertical $source, Compound|Geocentric|Geographic2D|Geographic3D|Projected|Vertical $target, bool $ignoreBoundaryRestrictions): array |
|
68
|
|
|
{ |
|
69
|
2209 |
|
$boundaryCheckPoint = $ignoreBoundaryRestrictions ? null : $this->getPointForBoundaryCheck(); |
|
70
|
|
|
|
|
71
|
|
|
// Iteratively calculate permutations of intermediate CRSs |
|
72
|
2209 |
|
$candidatePaths = $this->buildTransformationPathsToCRS($source, $target); |
|
73
|
2209 |
|
usort($candidatePaths, static fn (array $a, array $b) => $a['accuracy'] <=> $b['accuracy'] ?: count($a['path']) <=> count($b['path'])); |
|
74
|
|
|
|
|
75
|
2209 |
|
foreach ($candidatePaths as $candidatePath) { |
|
76
|
2191 |
|
if ($this->validatePath($candidatePath['path'], $boundaryCheckPoint)) { |
|
77
|
2173 |
|
return $candidatePath['path']; |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
1998 |
|
throw new UnknownConversionException('Unable to perform conversion, please file a bug if you think this is incorrect'); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
2191 |
|
protected function validatePath(array $candidatePath, ?GeographicValue $boundaryCheckPoint): bool |
|
85
|
|
|
{ |
|
86
|
2191 |
|
foreach ($candidatePath as $pathStep) { |
|
87
|
2191 |
|
$operation = CoordinateOperations::getOperationData($pathStep['operation']); |
|
88
|
2191 |
|
if ($boundaryCheckPoint) { |
|
89
|
|
|
// filter out operations that only operate outside this point |
|
90
|
2155 |
|
$polygon = $operation['extent'] = $operation['extent'] instanceof BoundingArea ? $operation['extent'] : BoundingArea::createFromExtentCodes($operation['extent']); |
|
91
|
2155 |
|
if (!$polygon->containsPoint($boundaryCheckPoint)) { |
|
92
|
108 |
|
return false; |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
2182 |
|
$operation = CoordinateOperations::getOperationData($pathStep['operation']); |
|
97
|
2182 |
|
$methodParams = CoordinateOperationMethods::getMethodData($operation['method'])['paramData']; |
|
98
|
|
|
|
|
99
|
|
|
// filter out operations that require an epoch if we don't have one |
|
100
|
2182 |
|
if ((isset($methodParams['transformationReferenceEpoch']) || isset($methodParams['parameterReferenceEpoch'])) && !$this->getCoordinateEpoch()) { |
|
101
|
94 |
|
return false; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
2173 |
|
$operationParams = CoordinateOperations::getParamData($pathStep['operation']); |
|
105
|
|
|
|
|
106
|
|
|
// filter out operations that require a specific epoch |
|
107
|
2173 |
|
if (isset($methodParams['transformationReferenceEpoch'])) { |
|
108
|
9 |
|
$pointEpoch = Year::fromDateTime($this->getCoordinateEpoch()); |
|
|
|
|
|
|
109
|
9 |
|
if (!(abs($pointEpoch->subtract($operationParams['transformationReferenceEpoch'])->getValue()) <= 0.001)) { |
|
110
|
|
|
return false; |
|
111
|
|
|
} |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
// filter out operations that require a grid file that we don't have, or where boundaries are not being |
|
115
|
|
|
// checked (a formula-based conversion will always return *a* result, outside a grid boundary does not...) |
|
116
|
2173 |
|
foreach ($operationParams as $paramName => $paramValue) { |
|
117
|
2173 |
|
if (str_ends_with($paramName, 'File') && $paramValue !== null && (!$boundaryCheckPoint || !class_exists($paramValue))) { |
|
118
|
162 |
|
return false; |
|
119
|
|
|
} |
|
120
|
|
|
} |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
2173 |
|
return true; |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* Build the set of possible paths that lead from the current CRS to the target CRS. |
|
128
|
|
|
*/ |
|
129
|
2209 |
|
protected function buildTransformationPathsToCRS(Compound|Geocentric|Geographic2D|Geographic3D|Projected|Vertical $source, Compound|Geocentric|Geographic2D|Geographic3D|Projected|Vertical $target): array |
|
130
|
|
|
{ |
|
131
|
2209 |
|
$iterations = 0; |
|
132
|
2209 |
|
$sourceSRID = $source->getSRID(); |
|
133
|
2209 |
|
$targetSRID = $target->getSRID(); |
|
134
|
2209 |
|
$previousSimplePaths = [[$sourceSRID]]; |
|
135
|
2209 |
|
$cacheKey = $sourceSRID . '|' . $targetSRID; |
|
136
|
|
|
|
|
137
|
2209 |
|
if (!isset(self::$completePathCache[$cacheKey])) { |
|
138
|
409 |
|
$transformationsByCRS = self::buildSupportedTransformationsByCRS($source, $target); |
|
139
|
409 |
|
$transformationsByCRSPair = self::buildSupportedTransformationsByCRSPair($source, $target); |
|
140
|
409 |
|
self::$completePathCache[$cacheKey] = []; |
|
141
|
|
|
|
|
142
|
409 |
|
while ($iterations <= $this->maxChainDepth) { |
|
143
|
409 |
|
$completePaths = []; |
|
144
|
409 |
|
$simplePaths = []; |
|
145
|
|
|
|
|
146
|
409 |
|
foreach ($previousSimplePaths as $simplePath) { |
|
147
|
409 |
|
$current = $simplePath[$iterations]; |
|
148
|
409 |
|
if ($current === $targetSRID) { |
|
149
|
382 |
|
$completePaths[] = $simplePath; |
|
150
|
409 |
|
} elseif (isset($transformationsByCRS[$current])) { |
|
151
|
400 |
|
foreach ($transformationsByCRS[$current] as $next) { |
|
152
|
400 |
|
if (!in_array($next, $simplePath, true)) { |
|
153
|
400 |
|
$simplePaths[] = [...$simplePath, $next]; |
|
154
|
|
|
} |
|
155
|
|
|
} |
|
156
|
|
|
} |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
// Then expand each CRS->CRS permutation with the various ways of achieving that (can be lots :/) |
|
160
|
409 |
|
$fullPaths = $this->expandSimplePaths($transformationsByCRSPair, $completePaths, $sourceSRID, $targetSRID); |
|
161
|
|
|
|
|
162
|
409 |
|
$paths = []; |
|
163
|
409 |
|
foreach ($fullPaths as $fullPath) { |
|
164
|
382 |
|
$paths[] = ['path' => $fullPath, 'accuracy' => array_sum(array_column($fullPath, 'accuracy'))]; |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
409 |
|
$previousSimplePaths = $simplePaths; |
|
168
|
409 |
|
self::$completePathCache[$cacheKey] = [...self::$completePathCache[$cacheKey], ...$paths]; |
|
169
|
409 |
|
++$iterations; |
|
170
|
|
|
} |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
2209 |
|
return self::$completePathCache[$cacheKey]; |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
409 |
|
protected function expandSimplePaths(array $transformationsByCRSPair, array $simplePaths, string $fromSRID, string $toSRID): array |
|
177
|
|
|
{ |
|
178
|
409 |
|
$fullPaths = []; |
|
179
|
409 |
|
foreach ($simplePaths as $simplePath) { |
|
180
|
382 |
|
$transformationsToMakePath = [[]]; |
|
181
|
382 |
|
$from = array_shift($simplePath); |
|
182
|
382 |
|
assert($from === $fromSRID); |
|
183
|
|
|
do { |
|
184
|
382 |
|
$to = array_shift($simplePath); |
|
185
|
382 |
|
$wipTransformationsInPath = []; |
|
186
|
382 |
|
foreach ($transformationsByCRSPair[$from . '|' . $to] ?? [] as $transformation) { |
|
187
|
382 |
|
foreach ($transformationsToMakePath as $transformationToMakePath) { |
|
188
|
382 |
|
$wipTransformationsInPath[] = [...$transformationToMakePath, $transformation]; |
|
189
|
|
|
} |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
382 |
|
$transformationsToMakePath = $wipTransformationsInPath; |
|
193
|
382 |
|
$from = $to; |
|
194
|
382 |
|
} while (count($simplePath) > 0); |
|
195
|
382 |
|
assert($to === $toSRID); |
|
196
|
|
|
|
|
197
|
382 |
|
foreach ($transformationsToMakePath as $transformationToMakePath) { |
|
198
|
382 |
|
$fullPaths[] = $transformationToMakePath; |
|
199
|
|
|
} |
|
200
|
|
|
} |
|
201
|
|
|
|
|
202
|
409 |
|
return $fullPaths; |
|
203
|
|
|
} |
|
204
|
|
|
|
|
205
|
|
|
/** |
|
206
|
|
|
* Boundary polygons are defined as WGS84, so theoretically all that needs to happen is |
|
207
|
|
|
* to conversion to WGS84 by calling ->convert(). However, that leads quickly to either circularity |
|
208
|
|
|
* when a conversion is possible, or an exception because not every CRS has a WGS84 transformation |
|
209
|
|
|
* available to it even when chaining. |
|
210
|
|
|
*/ |
|
211
|
2191 |
|
protected function getPointForBoundaryCheck(): ?GeographicValue |
|
212
|
|
|
{ |
|
213
|
2191 |
|
if ($this instanceof CompoundPoint) { |
|
214
|
46 |
|
$point = $this->getHorizontalPoint(); |
|
215
|
|
|
} else { |
|
216
|
2154 |
|
$point = $this; |
|
217
|
|
|
} |
|
218
|
|
|
|
|
219
|
|
|
try { |
|
220
|
|
|
// try converting to WGS84 if possible... |
|
221
|
2191 |
|
return $point->convert(Geographic2D::fromSRID(Geographic2D::EPSG_WGS_84), true)->asGeographicValue(); |
|
|
|
|
|
|
222
|
1935 |
|
} catch (UnknownConversionException) { |
|
223
|
|
|
/* |
|
224
|
|
|
* If Projected then either the point is inside the boundary by definition |
|
225
|
|
|
* or the user is deliberately exceeding the safe zone so safe to make a no-op either way. |
|
226
|
|
|
*/ |
|
227
|
1935 |
|
if ($point instanceof ProjectedPoint) { |
|
228
|
|
|
return null; |
|
229
|
|
|
} |
|
230
|
|
|
|
|
231
|
|
|
/* |
|
232
|
|
|
* Otherwise, compensate for non-Greenwich Prime Meridian, but otherwise assume that coordinates |
|
233
|
|
|
* are interchangeable between the actual CRS and WGS84. Boundaries are only defined to the nearest |
|
234
|
|
|
* ≈1km so the error bound should be acceptable within the area of interest |
|
235
|
|
|
*/ |
|
236
|
1935 |
|
if ($point instanceof GeographicPoint) { |
|
237
|
1926 |
|
return new GeographicValue($point->getLatitude(), $point->getLongitude()->subtract($point->getCRS()->getDatum()->getPrimeMeridian()->getGreenwichLongitude()), null, $point->getCRS()->getDatum()); |
|
238
|
|
|
} |
|
239
|
|
|
|
|
240
|
9 |
|
if ($point instanceof GeocentricPoint) { |
|
241
|
9 |
|
$asGeographic = $point->asGeographicValue(); |
|
242
|
|
|
|
|
243
|
9 |
|
return new GeographicValue($asGeographic->getLatitude(), $asGeographic->getLongitude()->subtract($asGeographic->getDatum()->getPrimeMeridian()->getGreenwichLongitude()), null, $asGeographic->getDatum()); |
|
244
|
|
|
} |
|
245
|
|
|
} |
|
246
|
|
|
} |
|
247
|
|
|
|
|
248
|
409 |
|
protected static function buildSupportedTransformationsByCRS(Compound|Geocentric|Geographic2D|Geographic3D|Projected|Vertical $source, Compound|Geocentric|Geographic2D|Geographic3D|Projected|Vertical $target): array |
|
249
|
|
|
{ |
|
250
|
409 |
|
$regions = array_unique([$source->getBoundingArea()->getRegion(), $target->getBoundingArea()->getRegion(), RegionMap::REGION_GLOBAL]); |
|
251
|
409 |
|
$relevantRegionData = CoordinateOperations::getCustomTransformations(); |
|
252
|
409 |
|
foreach ($regions as $region) { |
|
253
|
409 |
|
$regionData = match ($region) { |
|
254
|
409 |
|
RegionMap::REGION_GLOBAL => CRSTransformationsGlobal::getSupportedTransformations(), |
|
255
|
409 |
|
RegionMap::REGION_AFRICA => CRSTransformationsAfrica::getSupportedTransformations(), |
|
256
|
409 |
|
RegionMap::REGION_ANTARCTIC => CRSTransformationsAntarctic::getSupportedTransformations(), |
|
257
|
409 |
|
RegionMap::REGION_ARCTIC => CRSTransformationsArctic::getSupportedTransformations(), |
|
258
|
409 |
|
RegionMap::REGION_ASIA => CRSTransformationsAsia::getSupportedTransformations(), |
|
259
|
409 |
|
RegionMap::REGION_EUROPE => CRSTransformationsEurope::getSupportedTransformations(), |
|
260
|
409 |
|
RegionMap::REGION_NORTHAMERICA => CRSTransformationsNorthAmerica::getSupportedTransformations(), |
|
261
|
409 |
|
RegionMap::REGION_OCEANIA => CRSTransformationsOceania::getSupportedTransformations(), |
|
262
|
409 |
|
RegionMap::REGION_SOUTHAMERICA => CRSTransformationsSouthAmerica::getSupportedTransformations(), |
|
263
|
409 |
|
}; |
|
264
|
409 |
|
$relevantRegionData = [...$relevantRegionData, ...$regionData]; |
|
265
|
|
|
} |
|
266
|
|
|
|
|
267
|
409 |
|
$transformationsByCRS = []; |
|
268
|
409 |
|
foreach ($relevantRegionData as $transformation) { |
|
269
|
409 |
|
$operation = CoordinateOperations::getOperationData($transformation['operation']); |
|
270
|
409 |
|
$method = CoordinateOperationMethods::getMethodData($operation['method']); |
|
271
|
|
|
|
|
272
|
409 |
|
if (!isset($transformationsByCRS[$transformation['source_crs']][$transformation['target_crs']])) { |
|
273
|
409 |
|
$transformationsByCRS[$transformation['source_crs']][$transformation['target_crs']] = $transformation['target_crs']; |
|
274
|
|
|
} |
|
275
|
409 |
|
if ($method['reversible'] && !isset($transformationsByCRS[$transformation['target_crs']][$transformation['source_crs']])) { |
|
276
|
409 |
|
$transformationsByCRS[$transformation['target_crs']][$transformation['source_crs']] = $transformation['source_crs']; |
|
277
|
|
|
} |
|
278
|
|
|
} |
|
279
|
|
|
|
|
280
|
409 |
|
return $transformationsByCRS; |
|
281
|
|
|
} |
|
282
|
|
|
|
|
283
|
409 |
|
protected static function buildSupportedTransformationsByCRSPair(Compound|Geocentric|Geographic2D|Geographic3D|Projected|Vertical $source, Compound|Geocentric|Geographic2D|Geographic3D|Projected|Vertical $target): array |
|
284
|
|
|
{ |
|
285
|
409 |
|
$regions = array_unique([$source->getBoundingArea()->getRegion(), $target->getBoundingArea()->getRegion(), RegionMap::REGION_GLOBAL]); |
|
286
|
409 |
|
$relevantRegionData = []; |
|
287
|
409 |
|
foreach ($regions as $region) { |
|
288
|
409 |
|
$regionData = match ($region) { |
|
289
|
409 |
|
RegionMap::REGION_GLOBAL => [...CRSTransformationsGlobal::getSupportedTransformations(), ...CoordinateOperations::getCustomTransformations()], |
|
290
|
409 |
|
RegionMap::REGION_AFRICA => CRSTransformationsAfrica::getSupportedTransformations(), |
|
291
|
409 |
|
RegionMap::REGION_ANTARCTIC => CRSTransformationsAntarctic::getSupportedTransformations(), |
|
292
|
409 |
|
RegionMap::REGION_ARCTIC => CRSTransformationsArctic::getSupportedTransformations(), |
|
293
|
409 |
|
RegionMap::REGION_ASIA => CRSTransformationsAsia::getSupportedTransformations(), |
|
294
|
409 |
|
RegionMap::REGION_EUROPE => CRSTransformationsEurope::getSupportedTransformations(), |
|
295
|
409 |
|
RegionMap::REGION_NORTHAMERICA => CRSTransformationsNorthAmerica::getSupportedTransformations(), |
|
296
|
409 |
|
RegionMap::REGION_OCEANIA => CRSTransformationsOceania::getSupportedTransformations(), |
|
297
|
409 |
|
RegionMap::REGION_SOUTHAMERICA => CRSTransformationsSouthAmerica::getSupportedTransformations(), |
|
298
|
409 |
|
}; |
|
299
|
409 |
|
$relevantRegionData = [...$relevantRegionData, ...$regionData]; |
|
300
|
|
|
} |
|
301
|
|
|
|
|
302
|
409 |
|
$transformationsByCRSPair = []; |
|
303
|
409 |
|
foreach ($relevantRegionData as $key => $transformation) { |
|
304
|
409 |
|
$operation = CoordinateOperations::getOperationData($transformation['operation']); |
|
305
|
409 |
|
$method = CoordinateOperationMethods::getMethodData($operation['method']); |
|
306
|
|
|
|
|
307
|
409 |
|
$transformationsByCRSPair[$transformation['source_crs'] . '|' . $transformation['target_crs']][$key] = $transformation; |
|
308
|
409 |
|
$transformationsByCRSPair[$transformation['source_crs'] . '|' . $transformation['target_crs']][$key]['in_reverse'] = false; |
|
309
|
409 |
|
if ($method['reversible']) { |
|
310
|
409 |
|
$transformationsByCRSPair[$transformation['target_crs'] . '|' . $transformation['source_crs']][$key] = $transformation; |
|
311
|
409 |
|
$transformationsByCRSPair[$transformation['target_crs'] . '|' . $transformation['source_crs']][$key]['in_reverse'] = true; |
|
312
|
|
|
} |
|
313
|
|
|
} |
|
314
|
|
|
|
|
315
|
409 |
|
return $transformationsByCRSPair; |
|
316
|
|
|
} |
|
317
|
|
|
|
|
318
|
|
|
abstract public function getCRS(): CoordinateReferenceSystem; |
|
319
|
|
|
|
|
320
|
|
|
abstract public function getCoordinateEpoch(): ?DateTimeImmutable; |
|
321
|
|
|
|
|
322
|
|
|
abstract protected function performOperation(string $srid, Compound|Geocentric|Geographic2D|Geographic3D|Projected|Vertical $to, bool $inReverse): Point; |
|
323
|
|
|
} |
|
324
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths