|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace DataValues\Geo\Parsers; |
|
4
|
|
|
|
|
5
|
|
|
use DataValues\Geo\Values\GlobeCoordinateValue; |
|
6
|
|
|
use DataValues\Geo\Values\LatLongValue; |
|
7
|
|
|
use ValueParsers\ParseException; |
|
8
|
|
|
use ValueParsers\ParserOptions; |
|
9
|
|
|
use ValueParsers\StringValueParser; |
|
10
|
|
|
use ValueParsers\ValueParser; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Extends the LatLongParser by adding precision detection support. |
|
14
|
|
|
* |
|
15
|
|
|
* The object that gets constructed is a GlobeCoordinateValue rather then a LatLongValue. |
|
16
|
|
|
* |
|
17
|
|
|
* @since 0.1 |
|
18
|
|
|
* |
|
19
|
|
|
* @license GPL-2.0+ |
|
20
|
|
|
* @author Jeroen De Dauw < [email protected] > |
|
21
|
|
|
* @author H. Snater < [email protected] > |
|
22
|
|
|
* @author Thiemo Mättig |
|
23
|
|
|
*/ |
|
24
|
|
|
class GlobeCoordinateParser implements ValueParser { |
|
25
|
|
|
|
|
26
|
|
|
const FORMAT_NAME = 'globe-coordinate'; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Option specifying the globe. Should be a string containing a Wikidata concept URI. Defaults |
|
30
|
|
|
* to Earth. |
|
31
|
|
|
*/ |
|
32
|
|
|
const OPT_GLOBE = 'globe'; |
|
33
|
|
|
|
|
34
|
|
|
private $options; |
|
35
|
|
|
|
|
36
|
119 |
|
public function __construct( ParserOptions $options = null ) { |
|
37
|
119 |
|
$this->options = $options ?: new ParserOptions(); |
|
38
|
|
|
|
|
39
|
119 |
|
$this->options->defaultOption( ValueParser::OPT_LANG, 'en' ); |
|
40
|
119 |
|
$this->options->defaultOption( self::OPT_GLOBE, 'http://www.wikidata.org/entity/Q2' ); |
|
41
|
119 |
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @see StringValueParser::stringParse |
|
45
|
|
|
* |
|
46
|
|
|
* @param string $value |
|
47
|
|
|
* |
|
48
|
|
|
* @throws ParseException |
|
49
|
|
|
* @return GlobeCoordinateValue |
|
50
|
|
|
*/ |
|
51
|
119 |
|
public function parse( $value ) { |
|
52
|
119 |
|
foreach ( $this->getParsers() as $precisionDetector => $parser ) { |
|
53
|
|
|
try { |
|
54
|
119 |
|
$latLong = $parser->parse( $value ); |
|
55
|
|
|
|
|
56
|
117 |
|
return new GlobeCoordinateValue( |
|
57
|
117 |
|
new LatLongValue( |
|
58
|
117 |
|
$latLong->getLatitude(), |
|
59
|
117 |
|
$latLong->getLongitude() |
|
60
|
|
|
), |
|
61
|
117 |
|
$this->detectPrecision( $latLong, $precisionDetector ), |
|
62
|
117 |
|
$this->options->getOption( self::OPT_GLOBE ) |
|
63
|
|
|
); |
|
64
|
86 |
|
} catch ( ParseException $parseException ) { |
|
65
|
86 |
|
continue; |
|
66
|
|
|
} |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
2 |
|
throw new ParseException( |
|
70
|
2 |
|
'The format of the coordinate could not be determined.', |
|
71
|
2 |
|
$value, |
|
72
|
2 |
|
self::FORMAT_NAME |
|
73
|
|
|
); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* @param LatLongValue $latLong |
|
78
|
|
|
* @param string $precisionDetector |
|
79
|
|
|
* |
|
80
|
|
|
* @return float|int |
|
81
|
|
|
*/ |
|
82
|
117 |
|
private function detectPrecision( LatLongValue $latLong, $precisionDetector ) { |
|
83
|
117 |
|
if ( $this->options->hasOption( 'precision' ) ) { |
|
84
|
|
|
return $this->options->getOption( 'precision' ); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
117 |
|
return min( |
|
88
|
117 |
|
call_user_func( [ $this, $precisionDetector ], $latLong->getLatitude() ), |
|
89
|
117 |
|
call_user_func( [ $this, $precisionDetector ], $latLong->getLongitude() ) |
|
90
|
|
|
); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* @return ValueParser[] |
|
95
|
|
|
*/ |
|
96
|
119 |
|
private function getParsers() { |
|
97
|
119 |
|
$parsers = []; |
|
98
|
|
|
|
|
99
|
119 |
|
$parsers['detectFloatPrecision'] = new FloatCoordinateParser( $this->options ); |
|
100
|
119 |
|
$parsers['detectDmsPrecision'] = new DmsCoordinateParser( $this->options ); |
|
101
|
119 |
|
$parsers['detectDmPrecision'] = new DmCoordinateParser( $this->options ); |
|
102
|
119 |
|
$parsers['detectDdPrecision'] = new DdCoordinateParser( $this->options ); |
|
103
|
|
|
|
|
104
|
119 |
|
return $parsers; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* @param float $degree |
|
109
|
|
|
* |
|
110
|
|
|
* @return float|int |
|
111
|
|
|
*/ |
|
112
|
29 |
|
protected function detectDdPrecision( $degree ) { |
|
113
|
29 |
|
return $this->detectFloatPrecision( $degree ); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* @param float $degree |
|
118
|
|
|
* |
|
119
|
|
|
* @return float|int |
|
120
|
|
|
*/ |
|
121
|
25 |
|
protected function detectDmPrecision( $degree ) { |
|
122
|
25 |
|
$minutes = $degree * 60; |
|
123
|
25 |
|
$split = explode( '.', round( $minutes, 6 ) ); |
|
124
|
|
|
|
|
125
|
25 |
|
if ( isset( $split[1] ) ) { |
|
126
|
12 |
|
return $this->detectDmsPrecision( $degree ); |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
13 |
|
return 1 / 60; |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* @param float $degree |
|
134
|
|
|
* |
|
135
|
|
|
* @return float|int |
|
136
|
|
|
*/ |
|
137
|
42 |
|
protected function detectDmsPrecision( $degree ) { |
|
138
|
42 |
|
$seconds = $degree * 3600; |
|
139
|
42 |
|
$split = explode( '.', round( $seconds, 4 ) ); |
|
140
|
|
|
|
|
141
|
42 |
|
if ( isset( $split[1] ) ) { |
|
142
|
16 |
|
return pow( 10, -strlen( $split[1] ) ) / 3600; |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
26 |
|
return 1 / 3600; |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
/** |
|
149
|
|
|
* @param float $degree |
|
150
|
|
|
* |
|
151
|
|
|
* @return float|int |
|
152
|
|
|
*/ |
|
153
|
62 |
|
protected function detectFloatPrecision( $degree ) { |
|
154
|
62 |
|
$split = explode( '.', round( $degree, 8 ) ); |
|
155
|
|
|
|
|
156
|
62 |
|
if ( isset( $split[1] ) ) { |
|
157
|
52 |
|
return pow( 10, -strlen( $split[1] ) ); |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
34 |
|
return 1; |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
} |
|
164
|
|
|
|