|
9 | 9 |
|
10 | 10 | namespace PHPCoord\CoordinateReferenceSystem; |
11 | 11 |
|
12 | | -use function version_compare; |
| 12 | +use PHPCoord\CoordinateSystem\Cartesian; |
| 13 | +use PHPCoord\CoordinateSystem\CoordinateSystem; |
| 14 | +use PHPCoord\Datum\Datum; |
| 15 | +use PHPCoord\Exception\UnknownCoordinateReferenceSystemException; |
| 16 | +use PHPCoord\Geometry\BoundingArea; |
13 | 17 |
|
14 | | -use const PHP_VERSION; |
| 18 | +use function assert; |
| 19 | +use function count; |
| 20 | +use function array_map; |
15 | 21 |
|
16 | | -if (version_compare(PHP_VERSION, '8.2.0', '>=')) { |
17 | | - class Projected extends Projected82 |
| 22 | +class Projected extends CoordinateReferenceSystem |
| 23 | +{ |
| 24 | + use ProjectedConstantsChunk1; |
| 25 | + use ProjectedConstantsChunk2; |
| 26 | + use ProjectedConstantsChunk3; |
| 27 | + use ProjectedConstantsChunk4; |
| 28 | + use ProjectedSRIDData; |
| 29 | + |
| 30 | + protected Geographic2D|Geographic3D $baseCRS; |
| 31 | + |
| 32 | + protected ?string $derivingConversion; |
| 33 | + |
| 34 | + /** |
| 35 | + * @var array<string, Projected> |
| 36 | + */ |
| 37 | + private static array $cachedObjects = [ |
| 38 | + ]; |
| 39 | + |
| 40 | + public function __construct(string $srid, CoordinateSystem $coordinateSystem, Datum $datum, BoundingArea $boundingArea, string $name = '', Geographic2D|Geographic3D|null $baseCRS = null, ?string $derivingConversion = null) |
18 | 41 | { |
| 42 | + $this->srid = $srid; |
| 43 | + $this->coordinateSystem = $coordinateSystem; |
| 44 | + $this->datum = $datum; |
| 45 | + $this->boundingArea = $boundingArea; |
| 46 | + $this->name = $name; |
| 47 | + $this->baseCRS = $baseCRS ?? Geographic2D::fromSRID(Geographic2D::EPSG_WGS_84); |
| 48 | + $this->derivingConversion = $derivingConversion; |
| 49 | + assert(count($coordinateSystem->getAxes()) === 2 || count($coordinateSystem->getAxes()) === 3); |
19 | 50 | } |
20 | | -} else { |
21 | | - class Projected extends Projected81 |
| 51 | + |
| 52 | + public function getBaseCRS(): Geographic2D|Geographic3D |
| 53 | + { |
| 54 | + return $this->baseCRS; |
| 55 | + } |
| 56 | + |
| 57 | + public function getDerivingConversion(): string |
| 58 | + { |
| 59 | + return $this->derivingConversion; |
| 60 | + } |
| 61 | + |
| 62 | + public static function fromSRID(string $srid): self |
| 63 | + { |
| 64 | + if (!isset(static::$sridData[$srid])) { |
| 65 | + throw new UnknownCoordinateReferenceSystemException($srid); |
| 66 | + } |
| 67 | + if (!isset(self::$cachedObjects[$srid])) { |
| 68 | + $data = static::$sridData[$srid]; |
| 69 | + $baseCRS = Geographic::fromSRID($data['base_crs']); |
| 70 | + $extent = $data['extent'] instanceof BoundingArea ? $data['extent'] : BoundingArea::createFromExtentCodes($data['extent']); |
| 71 | + self::$cachedObjects[$srid] = new self($srid, Cartesian::fromSRID($data['coordinate_system']), $baseCRS->getDatum(), $extent, $data['name'], $baseCRS, $data['deriving_conversion']); |
| 72 | + } |
| 73 | + |
| 74 | + return self::$cachedObjects[$srid]; |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * @return array<string, string> |
| 79 | + */ |
| 80 | + public static function getSupportedSRIDs(): array |
| 81 | + { |
| 82 | + return array_map(static fn (array $data) => $data['name'], static::$sridData); |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * @return array<string, array{name: string, extent_description: string, help: string}> |
| 87 | + */ |
| 88 | + public static function getSupportedSRIDsWithHelp(): array |
| 89 | + { |
| 90 | + return array_map(static fn (array $data) => [ |
| 91 | + 'name' => $data['name'], |
| 92 | + 'extent_description' => $data['name'], |
| 93 | + 'help' => $data['help'], |
| 94 | + ], static::$sridData); |
| 95 | + } |
| 96 | + |
| 97 | + public static function registerCustomCRS(string $srid, string $name, string $baseCRSSrid, string $derivingConversionSrid, string $coordinateSystemSrid, BoundingArea $extent, string $help = ''): void |
22 | 98 | { |
| 99 | + self::$sridData[$srid] = [ |
| 100 | + 'name' => $name, |
| 101 | + 'coordinate_system' => $coordinateSystemSrid, |
| 102 | + 'base_crs' => $baseCRSSrid, |
| 103 | + 'deriving_conversion' => $derivingConversionSrid, |
| 104 | + 'extent' => $extent, |
| 105 | + 'extent_description' => '', |
| 106 | + 'help' => $help, |
| 107 | + ]; |
23 | 108 | } |
24 | 109 | } |
0 commit comments