|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* PHPCoord. |
|
4
|
|
|
* |
|
5
|
|
|
* @author Doug Wright |
|
6
|
|
|
*/ |
|
7
|
|
|
declare(strict_types=1); |
|
8
|
|
|
|
|
9
|
|
|
namespace PHPCoord\UnitOfMeasure; |
|
10
|
|
|
|
|
11
|
|
|
use PHPCoord\EPSG\Repository; |
|
12
|
|
|
use PHPCoord\Exception\UnknownUnitOfMeasureException; |
|
13
|
|
|
use PHPCoord\UnitOfMeasure\Angle\Angle; |
|
14
|
|
|
use PHPCoord\UnitOfMeasure\Angle\ArcSecond; |
|
15
|
|
|
use PHPCoord\UnitOfMeasure\Angle\Degree; |
|
16
|
|
|
use PHPCoord\UnitOfMeasure\Angle\ExoticAngle; |
|
17
|
|
|
use PHPCoord\UnitOfMeasure\Angle\Radian; |
|
18
|
|
|
use PHPCoord\UnitOfMeasure\Length\ExoticLength; |
|
19
|
|
|
use PHPCoord\UnitOfMeasure\Length\Length; |
|
20
|
|
|
use PHPCoord\UnitOfMeasure\Length\Metre; |
|
21
|
|
|
use PHPCoord\UnitOfMeasure\Scale\ExoticScale; |
|
22
|
|
|
use PHPCoord\UnitOfMeasure\Scale\Unity; |
|
23
|
|
|
use PHPCoord\UnitOfMeasure\Time\Second; |
|
24
|
|
|
use PHPCoord\UnitOfMeasure\Time\Year; |
|
25
|
|
|
|
|
26
|
|
|
class UnitOfMeasureFactory implements UnitOfMeasureIds |
|
27
|
|
|
{ |
|
28
|
|
|
/** @var Repository */ |
|
29
|
|
|
private static $repository; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @param float|string $measurement |
|
33
|
|
|
*/ |
|
34
|
7005 |
|
public static function makeUnit($measurement, int $epsgUnitCode): UnitOfMeasure |
|
35
|
|
|
{ |
|
36
|
7005 |
|
$repository = static::$repository ?? new Repository(); |
|
|
|
|
|
|
37
|
7005 |
|
$allData = $repository->getUnitsOfMeasure(true); |
|
|
|
|
|
|
38
|
|
|
|
|
39
|
7005 |
|
if (!isset($allData[$epsgUnitCode])) { |
|
40
|
1 |
|
throw new UnknownUnitOfMeasureException($epsgUnitCode); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
7004 |
|
$unitData = $allData[$epsgUnitCode]; |
|
44
|
|
|
|
|
45
|
|
|
/* |
|
46
|
|
|
* Common units (and those that require special handling) having discrete implementations, |
|
47
|
|
|
* try those first. |
|
48
|
|
|
*/ |
|
49
|
|
|
switch ($epsgUnitCode) { |
|
50
|
7004 |
|
case self::EPSG_ANGLE_RADIAN_PER_SECOND: |
|
51
|
1 |
|
return new Rate(new Radian($measurement), new Second(1)); |
|
|
|
|
|
|
52
|
7003 |
|
case self::EPSG_ANGLE_ARC_SECONDS_PER_YEAR: |
|
53
|
1 |
|
return new Rate(new ArcSecond($measurement), new Year(1)); |
|
|
|
|
|
|
54
|
7002 |
|
case self::EPSG_ANGLE_MILLIARC_SECONDS_PER_YEAR: |
|
55
|
1 |
|
return new Rate(self::makeUnit($measurement, self::EPSG_ANGLE_MILLIARC_SECOND), new Year(1)); |
|
56
|
7002 |
|
case self::EPSG_LENGTH_METRE_PER_SECOND: |
|
57
|
1 |
|
return new Rate(new Metre($measurement), new Second(1)); |
|
|
|
|
|
|
58
|
7001 |
|
case self::EPSG_LENGTH_METRES_PER_YEAR: |
|
59
|
1 |
|
return new Rate(new Metre($measurement), new Year(1)); |
|
60
|
7000 |
|
case self::EPSG_LENGTH_MILLIMETRES_PER_YEAR: |
|
61
|
1 |
|
return new Rate(self::makeUnit($measurement, self::EPSG_LENGTH_MILLIMETRE), new Year(1)); |
|
62
|
7000 |
|
case self::EPSG_LENGTH_CENTIMETRES_PER_YEAR: |
|
63
|
1 |
|
return new Rate(self::makeUnit($measurement, self::EPSG_LENGTH_CENTIMETRE), new Year(1)); |
|
64
|
7000 |
|
case self::EPSG_SCALE_UNITY_PER_SECOND: |
|
65
|
1 |
|
return new Rate(new Unity($measurement), new Second(1)); |
|
|
|
|
|
|
66
|
6999 |
|
case self::EPSG_SCALE_PARTS_PER_BILLION_PER_YEAR: |
|
67
|
1 |
|
return new Rate(self::makeUnit($measurement, self::EPSG_SCALE_PARTS_PER_BILLION), new Year(1)); |
|
68
|
6999 |
|
case self::EPSG_SCALE_PARTS_PER_MILLION_PER_YEAR: |
|
69
|
1 |
|
return new Rate(self::makeUnit($measurement, self::EPSG_SCALE_PARTS_PER_MILLION), new Year(1)); |
|
70
|
6999 |
|
case self::EPSG_ANGLE_RADIAN: |
|
71
|
1 |
|
return new Radian($measurement); |
|
72
|
6998 |
|
case self::EPSG_ANGLE_DEGREE: |
|
73
|
6692 |
|
return new Degree($measurement); |
|
|
|
|
|
|
74
|
6995 |
|
case self::EPSG_ANGLE_ARC_SECOND: |
|
75
|
1 |
|
return new ArcSecond($measurement); |
|
76
|
6994 |
|
case self::EPSG_ANGLE_DEGREE_MINUTE_SECOND: |
|
77
|
1 |
|
return Degree::fromDegreeMinuteSecond((string) $measurement); |
|
78
|
6993 |
|
case self::EPSG_ANGLE_DEGREE_MINUTE_SECOND_HEMISPHERE: |
|
79
|
1 |
|
return Degree::fromDegreeMinuteSecondHemisphere((string) $measurement); |
|
80
|
6992 |
|
case self::EPSG_ANGLE_HEMISPHERE_DEGREE_MINUTE_SECOND: |
|
81
|
1 |
|
return Degree::fromHemisphereDegreeMinuteSecond((string) $measurement); |
|
82
|
6991 |
|
case self::EPSG_ANGLE_DEGREE_MINUTE: |
|
83
|
1 |
|
return Degree::fromDegreeMinute((string) $measurement); |
|
84
|
6990 |
|
case self::EPSG_ANGLE_DEGREE_MINUTE_HEMISPHERE: |
|
85
|
1 |
|
return Degree::fromDegreeMinuteHemisphere((string) $measurement); |
|
86
|
6989 |
|
case self::EPSG_ANGLE_HEMISPHERE_DEGREE_MINUTE: |
|
87
|
1 |
|
return Degree::fromHemisphereDegreeMinute((string) $measurement); |
|
88
|
6988 |
|
case self::EPSG_ANGLE_DEGREE_HEMISPHERE: |
|
89
|
1 |
|
return Degree::fromDegreeHemisphere((string) $measurement); |
|
90
|
6987 |
|
case self::EPSG_ANGLE_HEMISPHERE_DEGREE: |
|
91
|
1 |
|
return Degree::fromHemisphereDegree((string) $measurement); |
|
92
|
6986 |
|
case self::EPSG_ANGLE_SEXAGESIMAL_DMS_S: |
|
93
|
1 |
|
return Degree::fromSexagesimalDMSS((string) $measurement); |
|
94
|
6985 |
|
case self::EPSG_ANGLE_SEXAGESIMAL_DMS: |
|
95
|
105 |
|
return Degree::fromSexagesimalDMS((string) $measurement); |
|
96
|
6972 |
|
case self::EPSG_ANGLE_SEXAGESIMAL_DM: |
|
97
|
1 |
|
return Degree::fromSexagesimalDM((string) $measurement); |
|
98
|
6971 |
|
case self::EPSG_LENGTH_METRE: |
|
99
|
6812 |
|
return new Metre($measurement); |
|
100
|
213 |
|
case self::EPSG_SCALE_UNITY: |
|
101
|
1 |
|
return new Unity($measurement); |
|
102
|
212 |
|
case self::EPSG_TIME_SECOND: |
|
103
|
1 |
|
return new Second($measurement); |
|
|
|
|
|
|
104
|
211 |
|
case self::EPSG_TIME_YEAR: |
|
105
|
1 |
|
return new Year($measurement); |
|
|
|
|
|
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/* |
|
109
|
|
|
* Check that the unit can be defined by reference to SI, if it can't it needs special handling and needs to be |
|
110
|
|
|
* in the list above |
|
111
|
|
|
*/ |
|
112
|
|
|
// @codeCoverageIgnoreStart |
|
113
|
|
|
if ($unitData['factor_b'] === null || |
|
114
|
|
|
!in_array( |
|
115
|
|
|
$unitData['target_uom_code'], |
|
116
|
|
|
[ |
|
117
|
|
|
self::EPSG_ANGLE_RADIAN, |
|
118
|
|
|
self::EPSG_LENGTH_METRE, |
|
119
|
|
|
self::EPSG_SCALE_UNITY, |
|
120
|
|
|
//self::EPSG_TIME_SECOND, all time units in the DB are currently handled above |
|
121
|
|
|
], |
|
122
|
|
|
true |
|
123
|
|
|
) |
|
124
|
|
|
) { |
|
125
|
|
|
throw new UnknownUnitOfMeasureException($epsgUnitCode); |
|
126
|
|
|
} |
|
127
|
|
|
// @codeCoverageIgnoreEnd |
|
128
|
|
|
|
|
129
|
210 |
|
switch ($unitData['unit_of_meas_type']) { |
|
130
|
210 |
|
case 'angle': |
|
131
|
64 |
|
return new ExoticAngle($measurement, $unitData['unit_of_meas_name'], $unitData['factor_b'], $unitData['factor_c']); |
|
|
|
|
|
|
132
|
147 |
|
case 'length': |
|
133
|
137 |
|
return new ExoticLength($measurement, $unitData['unit_of_meas_name'], $unitData['factor_b'], $unitData['factor_c']); |
|
|
|
|
|
|
134
|
10 |
|
case 'scale': |
|
135
|
10 |
|
return new ExoticScale($measurement, $unitData['unit_of_meas_name'], $unitData['factor_b'], $unitData['factor_c']); |
|
|
|
|
|
|
136
|
|
|
} |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
5 |
|
public static function convertAngle(Angle $angle, int $targetEpsgUnitCode): Angle |
|
140
|
|
|
{ |
|
141
|
5 |
|
$repository = static::$repository ?? new Repository(); |
|
|
|
|
|
|
142
|
5 |
|
$allData = $repository->getUnitsOfMeasure(true); |
|
|
|
|
|
|
143
|
|
|
|
|
144
|
5 |
|
if (!isset($allData[$targetEpsgUnitCode])) { |
|
145
|
|
|
throw new UnknownUnitOfMeasureException($targetEpsgUnitCode); |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
5 |
|
$targetUnitData = $allData[$targetEpsgUnitCode]; |
|
149
|
|
|
|
|
150
|
5 |
|
return self::makeUnit($angle->asRadians()->getValue() * $targetUnitData['factor_c'] / $targetUnitData['factor_b'], $targetEpsgUnitCode); |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
16 |
|
public static function convertLength(Length $length, int $targetEpsgUnitCode): Length |
|
154
|
|
|
{ |
|
155
|
16 |
|
$repository = static::$repository ?? new Repository(); |
|
|
|
|
|
|
156
|
16 |
|
$allData = $repository->getUnitsOfMeasure(true); |
|
|
|
|
|
|
157
|
|
|
|
|
158
|
16 |
|
if (!isset($allData[$targetEpsgUnitCode])) { |
|
159
|
|
|
throw new UnknownUnitOfMeasureException($targetEpsgUnitCode); |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
16 |
|
$targetUnitData = $allData[$targetEpsgUnitCode]; |
|
163
|
|
|
|
|
164
|
16 |
|
return self::makeUnit($length->asMetres()->getValue() * $targetUnitData['factor_c'] / $targetUnitData['factor_b'], $targetEpsgUnitCode); |
|
165
|
|
|
} |
|
166
|
|
|
} |
|
167
|
|
|
|