1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace DataValues\Geo\Parsers; |
4
|
|
|
|
5
|
|
|
use DataValues\Geo\Values\LatLongValue; |
6
|
|
|
use ValueParsers\ParseException; |
7
|
|
|
use ValueParsers\StringValueParser; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* ValueParser that parses the string representation of a geographical coordinate. |
11
|
|
|
* |
12
|
|
|
* The resulting objects are of type @see LatLongValue. |
13
|
|
|
* |
14
|
|
|
* Supports the following notations: |
15
|
|
|
* - Degree minute second |
16
|
|
|
* - Decimal degrees |
17
|
|
|
* - Decimal minutes |
18
|
|
|
* - Float |
19
|
|
|
* |
20
|
|
|
* And for all these notations direction can be indicated either with |
21
|
|
|
* + and - or with N/E/S/W, the later depending on the set options. |
22
|
|
|
* |
23
|
|
|
* The delimiter between latitude and longitude can be set in the options. |
24
|
|
|
* So can the symbols used for degrees, minutes and seconds. |
25
|
|
|
* |
26
|
|
|
* Some code in this class has been borrowed from the |
27
|
|
|
* MapsCoordinateParser class of the Maps extension for MediaWiki. |
28
|
|
|
* |
29
|
|
|
* @since 0.1 |
30
|
|
|
* |
31
|
|
|
* @license GPL-2.0+ |
32
|
|
|
* @author Jeroen De Dauw < [email protected] > |
33
|
|
|
*/ |
34
|
|
|
class LatLongParser extends StringValueParser { |
35
|
|
|
|
36
|
|
|
const TYPE_FLOAT = 'float'; |
37
|
|
|
const TYPE_DMS = 'dms'; |
38
|
|
|
const TYPE_DM = 'dm'; |
39
|
|
|
const TYPE_DD = 'dd'; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* The symbols representing the different directions for usage in directional notation. |
43
|
|
|
* @since 0.1 |
44
|
|
|
*/ |
45
|
|
|
const OPT_NORTH_SYMBOL = 'north'; |
46
|
|
|
const OPT_EAST_SYMBOL = 'east'; |
47
|
|
|
const OPT_SOUTH_SYMBOL = 'south'; |
48
|
|
|
const OPT_WEST_SYMBOL = 'west'; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* The symbols representing degrees, minutes and seconds. |
52
|
|
|
* @since 0.1 |
53
|
|
|
*/ |
54
|
|
|
const OPT_DEGREE_SYMBOL = 'degree'; |
55
|
|
|
const OPT_MINUTE_SYMBOL = 'minute'; |
56
|
|
|
const OPT_SECOND_SYMBOL = 'second'; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* The symbol to use as separator between latitude and longitude. |
60
|
|
|
* @since 0.1 |
61
|
|
|
*/ |
62
|
|
|
const OPT_SEPARATOR_SYMBOL = 'separator'; |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @see StringValueParser::stringParse |
66
|
|
|
* |
67
|
|
|
* @since 0.1 |
68
|
|
|
* |
69
|
|
|
* @param string $value |
70
|
|
|
* |
71
|
|
|
* @return LatLongValue |
72
|
|
|
* @throws ParseException |
73
|
|
|
*/ |
74
|
|
|
protected function stringParse( $value ) { |
75
|
|
|
foreach ( $this->getParsers() as $parser ) { |
76
|
|
|
try { |
77
|
|
|
return $parser->parse( $value ); |
78
|
|
|
} catch ( ParseException $ex ) { |
79
|
|
|
continue; |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
throw new ParseException( 'The format of the coordinate could not be determined. Parsing failed.' ); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @return StringValueParser[] |
88
|
|
|
*/ |
89
|
|
|
protected function getParsers() { |
90
|
|
|
$parsers = array(); |
91
|
|
|
|
92
|
|
|
$parsers[] = new FloatCoordinateParser( $this->options ); |
93
|
|
|
$parsers[] = new DmsCoordinateParser( $this->options ); |
94
|
|
|
$parsers[] = new DmCoordinateParser( $this->options ); |
95
|
|
|
$parsers[] = new DdCoordinateParser( $this->options ); |
96
|
|
|
|
97
|
|
|
return $parsers; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Convenience function for determining if something is a valid coordinate string. |
102
|
|
|
* Analogous to creating an instance of the parser, parsing the string and checking isValid on the result. |
103
|
|
|
* |
104
|
|
|
* @since 0.1 |
105
|
|
|
* |
106
|
|
|
* @param string $string |
107
|
|
|
* |
108
|
|
|
* @return boolean |
109
|
|
|
*/ |
110
|
|
|
public static function areCoordinates( $string ) { |
111
|
|
|
static $parser = null; |
112
|
|
|
|
113
|
|
|
if ( $parser === null ) { |
114
|
|
|
$parser = new self(); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
return $parser->parse( $string )->isValid(); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
} |
121
|
|
|
|