forked from DataValues/Geo
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLatLongFormatter.php
More file actions
373 lines (304 loc) · 11.1 KB
/
LatLongFormatter.php
File metadata and controls
373 lines (304 loc) · 11.1 KB
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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
<?php
declare( strict_types = 1 );
namespace DataValues\Geo\Formatters;
use DataValues\Geo\Values\LatLongValue;
use InvalidArgumentException;
use ValueFormatters\FormatterOptions;
use ValueFormatters\ValueFormatter;
/**
* Geographical coordinates formatter.
* Formats LatLongValue objects.
*
* Supports the following notations:
* - Degree minute second
* - Decimal degrees
* - Decimal minutes
* - Float
*
* Some code in this class has been borrowed from the
* MapsCoordinateParser class of the Maps extension for MediaWiki.
*
* @since 0.1, renamed in 2.0
* @api
*
* @license GPL-2.0-or-later
* @author Jeroen De Dauw < jeroendedauw@gmail.com >
* @author Addshore
* @author Thiemo Kreuz
*/
class LatLongFormatter implements ValueFormatter {
/**
* Output formats for use with the self::OPT_FORMAT option.
*/
public const TYPE_FLOAT = 'float';
public const TYPE_DMS = 'dms';
public const TYPE_DM = 'dm';
public const TYPE_DD = 'dd';
/**
* The symbols representing the different directions for usage in directional notation.
* @since 0.1
*/
public const OPT_NORTH_SYMBOL = 'north';
public const OPT_EAST_SYMBOL = 'east';
public const OPT_SOUTH_SYMBOL = 'south';
public const OPT_WEST_SYMBOL = 'west';
/**
* The symbols representing degrees, minutes and seconds.
* @since 0.1
*/
public const OPT_DEGREE_SYMBOL = 'degree';
public const OPT_MINUTE_SYMBOL = 'minute';
public const OPT_SECOND_SYMBOL = 'second';
/**
* Flags for use with the self::OPT_SPACING_LEVEL option.
*/
public const OPT_SPACE_LATLONG = 'latlong';
public const OPT_SPACE_DIRECTION = 'direction';
public const OPT_SPACE_COORDPARTS = 'coordparts';
/**
* Option specifying the output format (also referred to as output type). Must be one of the
* self::TYPE_… constants.
*/
public const OPT_FORMAT = 'geoformat';
/**
* Boolean option specifying if negative coordinates should have minus signs, e.g. "-1°, -2°"
* (false) or cardinal directions, e.g. "1° S, 2° W" (true). Default is false.
*/
public const OPT_DIRECTIONAL = 'directional';
/**
* Option for the separator character between latitude and longitude. Defaults to a comma.
*/
public const OPT_SEPARATOR_SYMBOL = 'separator';
/**
* Option specifying the amount and position of space characters in the output. Must be an array
* containing zero or more of the self::OPT_SPACE_… flags.
*/
public const OPT_SPACING_LEVEL = 'spacing';
/**
* Option specifying the precision in fractional degrees. Must be a number or numeric string.
*/
public const OPT_PRECISION = 'precision';
private const DEFAULT_PRECISION = 1 / 3600;
private FormatterOptions $options;
public function __construct( ?FormatterOptions $options = null ) {
$this->options = $options ?? new FormatterOptions();
$this->defaultOption( self::OPT_NORTH_SYMBOL, 'N' );
$this->defaultOption( self::OPT_EAST_SYMBOL, 'E' );
$this->defaultOption( self::OPT_SOUTH_SYMBOL, 'S' );
$this->defaultOption( self::OPT_WEST_SYMBOL, 'W' );
$this->defaultOption( self::OPT_DEGREE_SYMBOL, '°' );
$this->defaultOption( self::OPT_MINUTE_SYMBOL, "'" );
$this->defaultOption( self::OPT_SECOND_SYMBOL, '"' );
$this->defaultOption( self::OPT_FORMAT, self::TYPE_FLOAT );
$this->defaultOption( self::OPT_DIRECTIONAL, false );
$this->defaultOption( self::OPT_SEPARATOR_SYMBOL, ',' );
$this->defaultOption( self::OPT_SPACING_LEVEL, [
self::OPT_SPACE_LATLONG,
self::OPT_SPACE_DIRECTION,
self::OPT_SPACE_COORDPARTS,
] );
$this->defaultOption( self::OPT_PRECISION, 0 );
// Not used in this component, only here for downstream compatibility reasons
$this->defaultOption( ValueFormatter::OPT_LANG, 'en' );
}
/**
* @see ValueFormatter::format
*
* Calls formatLatLongValue() using OPT_PRECISION for the $precision parameter.
*
* @param LatLongValue $value
*
* @return string Plain text
* @throws InvalidArgumentException
*/
public function format( $value ): string {
if ( !( $value instanceof LatLongValue ) ) {
throw new InvalidArgumentException( 'Data value type mismatch. Expected a LatLongValue.' );
}
return $this->formatLatLongValue( $value, $this->getPrecisionFromOptions() );
}
private function getPrecisionFromOptions(): float {
$precision = $this->options->getOption( self::OPT_PRECISION );
if ( is_string( $precision ) ) {
return (float)$precision;
}
if ( is_float( $precision ) || is_int( $precision ) ) {
return $precision;
}
return self::DEFAULT_PRECISION;
}
/**
* Formats a LatLongValue with the desired precision.
*
* @since 0.5
*
* @param LatLongValue $value
* @param ?float $precision The desired precision, given as fractional degrees.
*
* @return string Plain text
* @throws InvalidArgumentException
*/
public function formatLatLongValue( LatLongValue $value, ?float $precision ): string {
if ( $precision === null || $precision <= 0 || !is_finite( $precision ) ) {
$precision = self::DEFAULT_PRECISION;
}
return implode(
$this->options->getOption( self::OPT_SEPARATOR_SYMBOL ) . $this->getSpacing( self::OPT_SPACE_LATLONG ),
[
$this->formatLatitude( $value->getLatitude(), $precision ),
$this->formatLongitude( $value->getLongitude(), $precision )
]
);
}
/**
* @param string $spacingLevel One of the self::OPT_SPACE_… constants
*
* @return string
*/
private function getSpacing( string $spacingLevel ): string {
return in_array( $spacingLevel, $this->options->getOption( self::OPT_SPACING_LEVEL ) ) ? ' ' : '';
}
private function formatLatitude( float $latitude, float $precision ): string {
return $this->makeDirectionalIfNeeded(
$this->formatCoordinate( $latitude, $precision ),
$this->options->getOption( self::OPT_NORTH_SYMBOL ),
$this->options->getOption( self::OPT_SOUTH_SYMBOL )
);
}
private function formatLongitude( float $longitude, float $precision ): string {
return $this->makeDirectionalIfNeeded(
$this->formatCoordinate( $longitude, $precision ),
$this->options->getOption( self::OPT_EAST_SYMBOL ),
$this->options->getOption( self::OPT_WEST_SYMBOL )
);
}
private function makeDirectionalIfNeeded( string $coordinate, string $positiveSymbol,
string $negativeSymbol ): string {
if ( $this->options->getOption( self::OPT_DIRECTIONAL ) ) {
return $this->makeDirectional( $coordinate, $positiveSymbol, $negativeSymbol );
}
return $coordinate;
}
private function makeDirectional( string $coordinate, string $positiveSymbol,
string $negativeSymbol ): string {
$isNegative = substr( $coordinate, 0, 1 ) === '-';
if ( $isNegative ) {
$coordinate = substr( $coordinate, 1 );
}
$symbol = $isNegative ? $negativeSymbol : $positiveSymbol;
return $coordinate . $this->getSpacing( self::OPT_SPACE_DIRECTION ) . $symbol;
}
private function formatCoordinate( float $degrees, float $precision ): string {
// Remove insignificant detail
$degrees = $this->roundDegrees( $degrees, $precision );
$format = $this->options->getOption( self::OPT_FORMAT );
if ( $format === self::TYPE_FLOAT ) {
return $this->getInFloatFormat( $degrees );
}
if ( $format !== self::TYPE_DD ) {
$precision = $this->getUpdatedPrecision( $precision );
}
if ( $format === self::TYPE_DD || $precision >= 1 ) {
return $this->getInDecimalDegreeFormat( $degrees, $precision );
}
if ( $format === self::TYPE_DM || $precision >= 1 / 60 ) {
return $this->getInDecimalMinuteFormat( $degrees, $precision );
}
if ( $format === self::TYPE_DMS ) {
return $this->getInDegreeMinuteSecondFormat( $degrees, $precision );
}
throw new InvalidArgumentException( 'Invalid coordinate format specified in the options' );
}
private function getUpdatedPrecision( float $precision ): float {
if ( $precision >= 1 - 1 / 60 && $precision < 1 ) {
return 1;
}
if ( $precision >= 1 / 60 - 1 / 3600 && $precision < 1 / 60 ) {
return 1 / 60;
}
return $precision;
}
private function roundDegrees( float $degrees, float $precision ): float {
$sign = $degrees > 0 ? 1 : -1;
$reduced = round( abs( $degrees ) / $precision );
$expanded = $reduced * $precision;
return $sign * $expanded;
}
private function getInFloatFormat( float $floatDegrees ): string {
$stringDegrees = (string)$floatDegrees;
if ( $stringDegrees === '-0' ) {
return '0';
}
return $stringDegrees;
}
private function getInDecimalDegreeFormat( float $floatDegrees, float $precision ): string {
$degreeDigits = $this->getSignificantDigits( 1, $precision );
$stringDegrees = $this->formatNumber( $floatDegrees, $degreeDigits );
return $stringDegrees . $this->options->getOption( self::OPT_DEGREE_SYMBOL );
}
private function getInDegreeMinuteSecondFormat( float $floatDegrees, float $precision ): string {
$isNegative = $floatDegrees < 0;
$secondDigits = $this->getSignificantDigits( 3600, $precision );
$seconds = round( abs( $floatDegrees ) * 3600, max( 0, $secondDigits ) );
$minutes = (int)( $seconds / 60 );
$degrees = (int)( $minutes / 60 );
$seconds -= $minutes * 60;
$minutes -= $degrees * 60;
$space = $this->getSpacing( self::OPT_SPACE_COORDPARTS );
$result = $this->formatNumber( $degrees )
. $this->options->getOption( self::OPT_DEGREE_SYMBOL )
. $space
. $this->formatNumber( $minutes )
. $this->options->getOption( self::OPT_MINUTE_SYMBOL )
. $space
. $this->formatNumber( $seconds, $secondDigits )
. $this->options->getOption( self::OPT_SECOND_SYMBOL );
if ( $isNegative && ( $degrees + $minutes + $seconds ) > 0 ) {
$result = '-' . $result;
}
return $result;
}
private function getInDecimalMinuteFormat( float $floatDegrees, float $precision ): string {
$isNegative = $floatDegrees < 0;
$minuteDigits = $this->getSignificantDigits( 60, $precision );
$minutes = round( abs( $floatDegrees ) * 60, max( 0, $minuteDigits ) );
$degrees = (int)( $minutes / 60 );
$minutes -= $degrees * 60;
$space = $this->getSpacing( self::OPT_SPACE_COORDPARTS );
$result = $this->formatNumber( $degrees )
. $this->options->getOption( self::OPT_DEGREE_SYMBOL )
. $space
. $this->formatNumber( $minutes, $minuteDigits )
. $this->options->getOption( self::OPT_MINUTE_SYMBOL );
if ( $isNegative && ( $degrees + $minutes ) > 0 ) {
$result = '-' . $result;
}
return $result;
}
/**
* @param float $unitsPerDegree The number of target units per degree
* (60 for minutes, 3600 for seconds)
* @param float $degreePrecision
*
* @return int The number of digits to show after the decimal point
* (resp. before, if the result is negative).
*/
private function getSignificantDigits( float $unitsPerDegree, float $degreePrecision ): int {
return (int)ceil( -log10( $unitsPerDegree * $degreePrecision ) );
}
/**
* @param float $number
* @param int $digits The number of digits after the decimal point.
*
* @return string
*/
private function formatNumber( float $number, int $digits = 0 ): string {
// TODO: use NumberLocalizer
return sprintf( '%.' . ( max( $digits, 0 ) ) . 'F', $number );
}
private function defaultOption( string $option, mixed $default ): void {
if ( !$this->options->hasOption( $option ) ) {
$this->options->setOption( $option, $default );
}
}
}