Index: trunk/extensions/Maps/test/MapsCoordinateParserTest.php |
— | — | @@ -38,13 +38,13 @@ |
39 | 39 | "55° 45.34716' N, 37° 37.05798' W", |
40 | 40 | "55° 45.34716', -37° 37.05798'", |
41 | 41 | "55° S, 37° 37.05798'W", |
42 | | - "-55°, 37° -37.05798'", |
| 42 | + "-55°, -37° 37.05798'", |
43 | 43 | "55°S, 37°37.05798'W ", |
44 | | - "-55°, 37°-37.05798' " |
| 44 | + "-55°, 37°37.05798' " |
45 | 45 | ), |
46 | 46 | 'dms' => array( |
47 | 47 | "55° 45' 21\" N, 37° 37' 3\" W", |
48 | | - "55° 45' 21\" N, -37° 37' 3\"", |
| 48 | + "55° 45' 21\", -37° 37' 3\"", |
49 | 49 | "55° 45' S, 37° 37' 3\"W", |
50 | 50 | "-55°, -37° 37' 3\"", |
51 | 51 | "55°45'S,37°37'3\"W ", |
— | — | @@ -125,10 +125,12 @@ |
126 | 126 | |
127 | 127 | foreach( self::$coordinates['dms'] as $coord ) { |
128 | 128 | $this->assertEquals( Maps_COORDS_DMS, MapsCoordinateParser::getCoordinatesType( $coord ), "$coord not recognized as dms." ); |
129 | | - } |
| 129 | + } |
| 130 | + |
130 | 131 | } |
131 | 132 | |
132 | | - public function provider() { |
| 133 | + /* |
| 134 | + public function coordinatesProvider() { |
133 | 135 | die(__METHOD__); |
134 | 136 | $coords = array(); |
135 | 137 | |
— | — | @@ -138,23 +140,22 @@ |
139 | 141 | } |
140 | 142 | } |
141 | 143 | return $coords; |
142 | | - } |
| 144 | + } |
| 145 | + */ |
143 | 146 | |
144 | 147 | /** |
145 | | - * @dataProvider provider |
| 148 | + * @dataProvider coordinatesProvider |
146 | 149 | */ |
147 | 150 | public function testAreCoordinates( $coord ) { |
148 | | - $this->assertTrue( MapsCoordinateParser::areCoordinates( $coord ), "$coord not recognized as coordinate." ); |
149 | | - |
| 151 | + foreach( self::$coordinates as $coordsOfType ) { |
| 152 | + foreach( $coordsOfType as $coord ) { |
| 153 | + $this->assertTrue( MapsCoordinateParser::areCoordinates( $coord ), "$coord not recognized as coordinate." ); |
| 154 | + } |
| 155 | + } |
150 | 156 | |
151 | | - /* |
152 | | - * Tests MapsCoordinateParser::areCoordinates() |
153 | | - * |
154 | 157 | foreach ( self::$fakeCoordinates as $coord ) { |
155 | 158 | $this->assertFalse( MapsCoordinateParser::areCoordinates( $coord ), "$coord was recognized as coordinate." ); |
156 | 159 | } |
157 | | - */ |
158 | | - |
159 | 160 | } |
160 | 161 | |
161 | 162 | /** |
Index: trunk/extensions/Maps/Includes/Maps_CoordinateParser.php |
— | — | @@ -18,7 +18,13 @@ |
19 | 19 | * Supports floats, DMS, decimal degrees, and decimal minutes notations, both directional and non-directional. |
20 | 20 | * Internal representatations are arrays with lat and lon key with float values. |
21 | 21 | * |
22 | | - * TODO: it migt be a lot nicer to return the releveant segments from the regexes instead of manually parsing them out. |
| 22 | + * TODO: |
| 23 | + * Clean up the coordinate recognition and parsing. |
| 24 | + * It's probably a better approach to use the regexes to do both. |
| 25 | + * Also, the regexes can be improved, so coordinate sets composes of lat and lon |
| 26 | + * in different notations can be recognized, and there is no need for the dms |
| 27 | + * regex to also accept dm and dd, which can give unexpected results in certain |
| 28 | + * usecases. The different seperator support could also be made nice. |
23 | 29 | * |
24 | 30 | * @ingroup Maps |
25 | 31 | * @since 0.6 |
— | — | @@ -283,12 +289,12 @@ |
284 | 290 | switch ( $coordType ) { |
285 | 291 | case Maps_COORDS_FLOAT: |
286 | 292 | return $coordinate; |
| 293 | + case Maps_COORDS_DD: |
| 294 | + return self::parseDDCoordinate( $coordinate ); |
| 295 | + case Maps_COORDS_DM: |
| 296 | + return self::parseDMCoordinate( $coordinate ); |
287 | 297 | case Maps_COORDS_DMS: |
288 | 298 | return self::parseDMSCoordinate( $coordinate ); |
289 | | - case Maps_COORDS_DD: |
290 | | - return self::parseDDCoordinate( $coordinate ); |
291 | | - case Maps_COORDS_DM: |
292 | | - return self::parseDMCoordinate( $coordinate ); |
293 | 299 | default: |
294 | 300 | throw new Exception( __METHOD__ . " does not support parsing of the $coordType coordinate type." ); |
295 | 301 | } |
— | — | @@ -305,8 +311,8 @@ |
306 | 312 | */ |
307 | 313 | public static function areFloatCoordinates( $coordinates ) { |
308 | 314 | $sep = self::getSeparatorsRegex(); |
309 | | - return preg_match( '/^(-)?\d{1,3}(\.\d{1,20})?' . $sep . '(\s)?(-)?\d{1,3}(\.\d{1,20})?$/i', $coordinates ) // Non-directional |
310 | | - || preg_match( '/^\d{1,3}(\.\d{1,20})?(\s)?(N|S)' . $sep . '(\s)?\d{1,3}(\.\d{1,20})?(\s)?(E|W)$/i', $coordinates ); // Directional |
| 315 | + return preg_match( '/^(-)?\d{1,3}(\.\d{1,20})?' . $sep . '(-)?\d{1,3}(\.\d{1,20})?$/i', $coordinates ) // Non-directional |
| 316 | + || preg_match( '/^\d{1,3}(\.\d{1,20})?(N|S)' . $sep . '\d{1,3}(\.\d{1,20})?(E|W)$/i', $coordinates ); // Directional |
311 | 317 | } |
312 | 318 | |
313 | 319 | /** |
— | — | @@ -320,10 +326,10 @@ |
321 | 327 | */ |
322 | 328 | public static function areDMSCoordinates( $coordinates ) { |
323 | 329 | $sep = self::getSeparatorsRegex(); |
324 | | - return preg_match( '/^(-)?(\d{1,3}°)((\s)?\d{1,2}(\′|\'))?(((\s)?\d{1,2}(″|"))?|((\s)?\d{1,2}\.\d{1,2}(″|"))?)' |
325 | | - . $sep . '(\s)?(-)?(\d{1,3}°)((\s)?\d{1,2}(\′|\'))?(((\s)?\d{1,2}(″|"))?|((\s)?\d{1,2}\.\d{1,2}(″|"))?)$/i', $coordinates ) // Non-directional |
326 | | - || preg_match( '/^(\d{1,3}°)((\s)?\d{1,2}(\′|\'))?(((\s)?\d{1,2}(″|"))?|((\s)?\d{1,2}\.\d{1,2}(″|"))?)(\s)?(N|S)' |
327 | | - . $sep . '(\s)?(\d{1,3}°)((\s)?\d{1,2}(\′|\'))?(((\s)?\d{1,2}(″|"))?|((\s)?\d{1,2}\.\d{1,2}(″|"))?)(\s)?(E|W)$/i', $coordinates ); // Directional |
| 330 | + return preg_match( '/^(-)?(\d{1,3}°)(\d{1,2}(\′|\'))?((\d{1,2}(″|"))?|(\d{1,2}\.\d{1,2}(″|"))?)' |
| 331 | + . $sep . '(-)?(\d{1,3}°)(\d{1,2}(\′|\'))?((\d{1,2}(″|"))?|(\d{1,2}\.\d{1,2}(″|"))?)$/i', $coordinates ) // Non-directional |
| 332 | + || preg_match( '/^(\d{1,3}°)(\d{1,2}(\′|\'))?((\d{1,2}(″|"))?|(\d{1,2}\.\d{1,2}(″|"))?)(N|S)' |
| 333 | + . $sep . '(\d{1,3}°)(\d{1,2}(\′|\'))?((\d{1,2}(″|"))?|(\d{1,2}\.\d{1,2}(″|"))?)(E|W)$/i', $coordinates ); // Directional |
328 | 334 | } |
329 | 335 | |
330 | 336 | /** |
— | — | @@ -337,8 +343,8 @@ |
338 | 344 | */ |
339 | 345 | public static function areDDCoordinates( $coordinates ) { |
340 | 346 | $sep = self::getSeparatorsRegex(); |
341 | | - return preg_match( '/^(-)?\d{1,3}(|\.\d{1,20})°' . $sep . '(\s)?(-)?(\s)?\d{1,3}(|\.\d{1,20})°$/i', $coordinates ) // Non-directional |
342 | | - || preg_match( '/^\d{1,3}(|\.\d{1,20})°(\s)?(N|S)' . $sep . '(\s)?(\s)?\d{1,3}(|\.\d{1,20})°(\s)?(E|W)?$/i', $coordinates ); // Directional |
| 347 | + return preg_match( '/^(-)?\d{1,3}(|\.\d{1,20})°' . $sep . '(-)?\d{1,3}(|\.\d{1,20})°$/i', $coordinates ) // Non-directional |
| 348 | + || preg_match( '/^\d{1,3}(|\.\d{1,20})°(N|S)' . $sep . '\d{1,3}(|\.\d{1,20})°(E|W)?$/i', $coordinates ); // Directional |
343 | 349 | } |
344 | 350 | |
345 | 351 | /** |
— | — | @@ -352,8 +358,8 @@ |
353 | 359 | */ |
354 | 360 | public static function areDMCoordinates( $coordinates ) { |
355 | 361 | $sep = self::getSeparatorsRegex(); |
356 | | - return preg_match( '/(-)?\d{1,3}°(\s)?\d{1,2}(\.\d{1,20}\')?' . $sep . '(\s)?(-)?\d{1,3}°(\s)?\d{1,2}(\.\d{1,20}\')?$/i', $coordinates ) // Non-directional |
357 | | - || preg_match( '/\d{1,3}°(\s)?\d{1,2}(\.\d{1,20}\')?(\s)?(N|S)' . $sep . '(\s)?\d{1,3}°(\s)?\d{1,2}(\.\d{1,20}\')?(\s)?(E|W)?$/i', $coordinates ); // Directional |
| 362 | + return preg_match( '/(-)?\d{1,3}°(\d{1,2}(\.\d{1,20}\')?)?' . $sep . '(-)?\d{1,3}°(\d{1,2}(\.\d{1,20}\')?)?$/i', $coordinates ) // Non-directional |
| 363 | + || preg_match( '/\d{1,3}°(\d{1,2}(\.\d{1,20}\')?)?(N|S)' . $sep . '\d{1,3}°(\d{1,2}(\.\d{1,20}\')?)?(E|W)?$/i', $coordinates ); // Directional |
358 | 364 | } |
359 | 365 | |
360 | 366 | /** |