1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
/** |
5
|
|
|
* Coordinate Factory |
6
|
|
|
* |
7
|
|
|
* @author Marcus Jaschen <[email protected]> |
8
|
|
|
* @license https://opensource.org/licenses/MIT |
9
|
|
|
* @link https://github.com/mjaschen/phpgeo |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Location\Factory; |
13
|
|
|
|
14
|
|
|
use Location\Coordinate; |
15
|
|
|
use Location\Ellipsoid; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Coordinate Factory |
19
|
|
|
* |
20
|
|
|
* @author Marcus Jaschen <[email protected]> |
21
|
|
|
* @license https://opensource.org/licenses/MIT |
22
|
|
|
* @link https://github.com/mjaschen/phpgeo |
23
|
|
|
*/ |
24
|
|
|
class CoordinateFactory implements GeometryFactoryInterface |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* Creates a Coordinate instance from the given string. |
28
|
|
|
* |
29
|
|
|
* The string is parsed by a regular expression for a known |
30
|
|
|
* format of geographical coordinates. |
31
|
|
|
* |
32
|
|
|
* @param string $string formatted geographical coordinate |
33
|
|
|
* @param \Location\Ellipsoid $ellipsoid |
34
|
|
|
* |
35
|
|
|
* @return Coordinate |
36
|
|
|
* @throws \InvalidArgumentException |
37
|
|
|
*/ |
38
|
|
|
public static function fromString(string $string, Ellipsoid $ellipsoid = null): Coordinate |
39
|
|
|
{ |
40
|
|
|
$string = self::mergeSecondsToMinutes($string); |
41
|
|
|
|
42
|
|
|
$result = self::parseDecimalMinutesWithoutCardinalLetters($string, $ellipsoid); |
|
|
|
|
43
|
|
|
|
44
|
|
|
if ($result instanceof Coordinate) { |
45
|
|
|
return $result; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
$result = self::parseDecimalMinutesWithCardinalLetters($string, $ellipsoid); |
|
|
|
|
49
|
|
|
|
50
|
|
|
if ($result instanceof Coordinate) { |
51
|
|
|
return $result; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
$result = self::parseDecimalDegreesWithoutCardinalLetters($string, $ellipsoid); |
|
|
|
|
55
|
|
|
|
56
|
|
|
if ($result instanceof Coordinate) { |
57
|
|
|
return $result; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
$result = self::parseDecimalDegreesWithCardinalLetters($string, $ellipsoid); |
|
|
|
|
61
|
|
|
|
62
|
|
|
if ($result instanceof Coordinate) { |
63
|
|
|
return $result; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
throw new \InvalidArgumentException('Format of coordinates was not recognized'); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @param $string |
71
|
|
|
* @param $ellipsoid |
72
|
|
|
* |
73
|
|
|
* @return Coordinate|null |
74
|
|
|
* @throws \InvalidArgumentException |
75
|
|
|
*/ |
76
|
|
|
private static function parseDecimalMinutesWithoutCardinalLetters(string $string, Ellipsoid $ellipsoid = null) |
77
|
|
|
{ |
78
|
|
|
// Decimal minutes without cardinal letters, e. g. "52 12.345, 13 23.456", |
79
|
|
|
// "52° 12.345, 13° 23.456", "52° 12.345′, 13° 23.456′", "52 12.345 N, 13 23.456 E", |
80
|
|
|
// "N52° 12.345′ E13° 23.456′" |
81
|
|
|
if (preg_match('/(-?\d{1,2})°?\s+(\d{1,2}\.?\d*)[\'′]?[, ]\s*(-?\d{1,3})°?\s+(\d{1,2}\.?\d*)[\'′]?/ui', $string, $match)) { |
82
|
|
|
$latitude = $match[1] >= 0 ? $match[1] + $match[2] / 60 : $match[1] - $match[2] / 60; |
83
|
|
|
$longitude = $match[3] >= 0 ? $match[3] + $match[4] / 60 : $match[3] - $match[4] / 60; |
84
|
|
|
|
85
|
|
|
return new Coordinate((float)$latitude, (float)$longitude, $ellipsoid); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
return null; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @param $string |
93
|
|
|
* @param $ellipsoid |
94
|
|
|
* |
95
|
|
|
* @return Coordinate|null |
96
|
|
|
* @throws \InvalidArgumentException |
97
|
|
|
*/ |
98
|
|
|
private static function parseDecimalMinutesWithCardinalLetters(string $string, Ellipsoid $ellipsoid = null) |
99
|
|
|
{ |
100
|
|
|
// Decimal minutes with cardinal letters, e. g. "52 12.345, 13 23.456", |
101
|
|
|
// "52° 12.345, 13° 23.456", "52° 12.345′, 13° 23.456′", "52 12.345 N, 13 23.456 E", |
102
|
|
|
// "N52° 12.345′ E13° 23.456′" |
103
|
|
|
if (preg_match('/([NS]?\s*)(\d{1,2})°?\s+(\d{1,2}\.?\d*)[\'′]?(\s*[NS]?)[, ]\s*([EW]?\s*)(\d{1,3})°?\s+(\d{1,2}\.?\d*)[\'′]?(\s*[EW]?)/ui', $string, $match)) { |
104
|
|
|
$latitude = $match[2] + $match[3] / 60; |
105
|
|
View Code Duplication |
if (strtoupper(trim($match[1])) === 'S' || strtoupper(trim($match[4])) === 'S') { |
|
|
|
|
106
|
|
|
$latitude = - $latitude; |
107
|
|
|
} |
108
|
|
|
$longitude = $match[6] + $match[7] / 60; |
109
|
|
View Code Duplication |
if (strtoupper(trim($match[5])) === 'W' || strtoupper(trim($match[8])) === 'W') { |
|
|
|
|
110
|
|
|
$longitude = - $longitude; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
return new Coordinate((float)$latitude, (float)$longitude, $ellipsoid); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
return null; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @param $string |
121
|
|
|
* @param $ellipsoid |
122
|
|
|
* |
123
|
|
|
* @return Coordinate|null |
124
|
|
|
* @throws \InvalidArgumentException |
125
|
|
|
*/ |
126
|
|
|
private static function parseDecimalDegreesWithoutCardinalLetters(string $string, Ellipsoid $ellipsoid = null) |
127
|
|
|
{ |
128
|
|
|
// The most simple format: decimal degrees without cardinal letters, |
129
|
|
|
// e. g. "52.5, 13.5" or "53.25732 14.24984" |
130
|
|
|
if (preg_match('/(-?\d{1,2}\.?\d*)°?[, ]\s*(-?\d{1,3}\.?\d*)°?/u', $string, $match)) { |
131
|
|
|
return new Coordinate((float)$match[1], (float)$match[2], $ellipsoid); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
return null; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* @param $string |
139
|
|
|
* @param $ellipsoid |
140
|
|
|
* |
141
|
|
|
* @return Coordinate|null |
142
|
|
|
* @throws \InvalidArgumentException |
143
|
|
|
*/ |
144
|
|
|
private static function parseDecimalDegreesWithCardinalLetters(string $string, Ellipsoid $ellipsoid = null) |
145
|
|
|
{ |
146
|
|
|
// Decimal degrees with cardinal letters, e. g. "N52.5, E13.5", |
147
|
|
|
// "40.2S, 135.3485W", or "56.234°N, 157.245°W" |
148
|
|
|
if (preg_match('/([NS]?\s*)(\d{1,2}\.?\d*)°?(\s*[NS]?)[, ]\s*([EW]?\s*)(\d{1,3}\.?\d*)°?(\s*[EW]?)/ui', $string, $match)) { |
149
|
|
|
$latitude = $match[2]; |
150
|
|
View Code Duplication |
if (strtoupper(trim($match[1])) === 'S' || strtoupper(trim($match[3])) === 'S') { |
|
|
|
|
151
|
|
|
$latitude = - $latitude; |
152
|
|
|
} |
153
|
|
|
$longitude = $match[5]; |
154
|
|
View Code Duplication |
if (strtoupper(trim($match[4])) === 'W' || strtoupper(trim($match[6])) === 'W') { |
|
|
|
|
155
|
|
|
$longitude = - $longitude; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
return new Coordinate((float)$latitude, (float)$longitude, $ellipsoid); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
return null; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* @param string $string |
166
|
|
|
* @return null|string|string[] |
167
|
|
|
*/ |
168
|
|
|
private static function mergeSecondsToMinutes(string $string) |
169
|
|
|
{ |
170
|
|
|
return preg_replace_callback('/(\d+)(°|\s)\s*(\d+)(\'|′|\s)(\s*([0-9\.]*))("|\'\'|′′)?/', function (array $matches) { |
171
|
|
|
return sprintf('%d %d.%s', $matches[1], $matches[3], substr(sprintf('%f', floatval($matches[6]) / 60), 2)); |
172
|
|
|
}, $string); |
173
|
|
|
} |
174
|
|
|
} |
175
|
|
|
|
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.