Skip to content

Commit

Permalink
Implemented directional notation support in GeoCoordinateFormatter
Browse files Browse the repository at this point in the history
  • Loading branch information
JeroenDeDauw committed Nov 30, 2013
1 parent ebf9356 commit 969053b
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 18 deletions.
2 changes: 1 addition & 1 deletion Geo.php
Expand Up @@ -15,7 +15,7 @@
return 1;
}

define( 'DATAVALUES_GEO_VERSION', '0.1' );
define( 'DATAVALUES_GEO_VERSION', '0.1.1' );

if ( defined( 'MEDIAWIKI' ) ) {
$GLOBALS['wgExtensionCredits']['datavalues'][] = array(
Expand Down
3 changes: 2 additions & 1 deletion README.md
Expand Up @@ -53,8 +53,9 @@ It is based upon and contains a lot of code written by [Jeroen De Dauw]

## Release notes

### 0.1.1 (dev)
### 0.1.1 (2013-11-30)

* Added support for direction notation to GeoCoordinateFormatter
* Decreased complexity of GeoCoordinateFormatter
* Decreased complexity and coupling of GeoCoordinateFormatterTest

Expand Down
59 changes: 43 additions & 16 deletions src/ValueFormatters/GeoCoordinateFormatter.php
Expand Up @@ -15,8 +15,6 @@
* - Decimal minutes
* - Float
*
* TODO: support directional notation
*
* Some code in this class has been borrowed from the
* MapsCoordinateParser class of the Maps extension for MediaWiki.
*
Expand Down Expand Up @@ -94,25 +92,54 @@ public function format( $value ) {
throw new InvalidArgumentException( 'The ValueFormatters\GeoCoordinateFormatter can only format instances of DataValues\LatLongValue' );
}

$latitude = $this->formatCoordinate( $value->getLatitude() );
$longitude = $this->formatCoordinate( $value->getLongitude() );

$formatted = implode( $this->getOption( self::OPT_SEPARATOR_SYMBOL ) . ' ', array( $latitude, $longitude ) );
$formatted = implode(
$this->getOption( self::OPT_SEPARATOR_SYMBOL ) . ' ',
array(
$this->formatLatitude( $value->getLatitude() ),
$this->formatLongitude( $value->getLongitude() )
)
);

return $formatted;
}

/**
* Formats a single coordinate
*
* @param string $coordinate
*
* @return string
* @throws InvalidArgumentException
*/
protected function formatCoordinate( $coordinate ) {
$options = $this->options;
private function formatLatitude( $latitude ) {
return $this->makeDirectionalIfNeeded(
$this->formatCoordinate( $latitude ),
$this->options->getOption( self::OPT_NORTH_SYMBOL ),
$this->options->getOption( self::OPT_SOUTH_SYMBOL )
);
}

private function formatLongitude( $longitude ) {
return $this->makeDirectionalIfNeeded(
$this->formatCoordinate( $longitude ),
$this->options->getOption( self::OPT_EAST_SYMBOL ),
$this->options->getOption( self::OPT_WEST_SYMBOL )
);
}

private function makeDirectionalIfNeeded( $coordinate, $positiveSymbol, $negativeSymbol ) {
if ( $this->options->getOption( self::OPT_DIRECTIONAL ) ) {
return $this->makeDirectional( $coordinate , $positiveSymbol, $negativeSymbol);
}

return $coordinate;
}

private function makeDirectional( $coordinate, $positiveSymbol, $negativeSymbol ) {
$isNegative = $coordinate{0} == '-';

if ( $isNegative ) {
$coordinate = substr( $coordinate, 1 );
}

$symbol = $isNegative ? $negativeSymbol : $positiveSymbol;

return $coordinate . ' ' . $symbol;
}

private function formatCoordinate( $coordinate ) {
switch ( $this->getOption( self::OPT_FORMAT ) ) {
case self::TYPE_FLOAT:
return $this->getInFloatFormat( $coordinate );
Expand Down
38 changes: 38 additions & 0 deletions tests/ValueFormatters/GeoCoordinateFormatterTest.php
Expand Up @@ -90,4 +90,42 @@ private function assertFormatsCorrectly( LatLongValue $latLong, $options, $expec
);
}

public function testDirectionalOptionGetsAppliedForDecimalMinutes() {
$coordinates = array(
'55° 0\' N, 37° 0\' E' => array( 55, 37 ),
'55° 30\' N, 37° 30\' W' => array( 55.5, -37.5 ),
'55° 30\' S, 37° 30\' E' => array( -55.5, 37.5 ),
'55° 30\' S, 37° 30\' W' => array( -55.5, -37.5 ),
'0° 0\' N, 0° 0\' E' => array( 0, 0 ),
);

$this->assertIsDirectionalFormatMap( $coordinates, GeoCoordinateFormatter::TYPE_DM );
}

private function assertIsDirectionalFormatMap( array $coordinates, $format ) {
foreach ( $coordinates as $expected => $arguments ) {
$options = new FormatterOptions();
$options->setOption( GeoCoordinateFormatter::OPT_FORMAT, $format );
$options->setOption( GeoCoordinateFormatter::OPT_DIRECTIONAL, true );

$this->assertFormatsCorrectly(
new LatLongValue( $arguments[0], $arguments[1] ),
$options,
$expected
);
}
}

public function testDirectionalOptionGetsAppliedForFloats() {
$coordinates = array(
'55.755786 N, 37.617633 W' => array( 55.755786, -37.617633 ),
'55.755786 S, 37.617633 E' => array( -55.755786, 37.617633 ),
'55 S, 37.617633 W' => array( -55, -37.617633 ),
'5.5 N, 37 E' => array( 5.5, 37 ),
'0 N, 0 E' => array( 0, 0 ),
);

$this->assertIsDirectionalFormatMap( $coordinates, GeoCoordinateFormatter::TYPE_FLOAT );
}

}

0 comments on commit 969053b

Please sign in to comment.