Skip to content

Commit

Permalink
Clean up coordinate formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
JeroenDeDauw committed Oct 1, 2018
1 parent 97ca35f commit 2063f74
Show file tree
Hide file tree
Showing 10 changed files with 71 additions and 59 deletions.
18 changes: 5 additions & 13 deletions includes/parserhooks/Maps_Coordinates.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

use DataValues\Geo\Formatters\LatLongFormatter;
use Maps\MapsFactory;

/**
* Class for the 'coordinates' parser hooks,
Expand All @@ -25,19 +25,11 @@ class MapsCoordinates extends ParserHook {
* @return string
*/
public function render( array $parameters ) {
$options = new \ValueFormatters\FormatterOptions(
[
LatLongFormatter::OPT_FORMAT => $parameters['format'],
LatLongFormatter::OPT_DIRECTIONAL => $parameters['directional'],
LatLongFormatter::OPT_PRECISION => 1 / 360000
]
return MapsFactory::globalInstance()->getCoordinateFormatter()->format(
$parameters['location'],
$parameters['format'],
$parameters['directional']
);

$coordinateFormatter = new LatLongFormatter( $options );

$output = $coordinateFormatter->format( $parameters['location'] );

return $output;
}

/**
Expand Down
16 changes: 5 additions & 11 deletions includes/parserhooks/Maps_Finddestination.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php

use DataValues\Geo\Formatters\LatLongFormatter;
use DataValues\Geo\Values\LatLongValue;
use Maps\MapsFactory;

/**
* Class for the 'finddestination' parser hooks, which can find a
Expand Down Expand Up @@ -32,17 +32,11 @@ public function render( array $parameters ) {
$parameters['distance']
);

$options = new \ValueFormatters\FormatterOptions(
[
LatLongFormatter::OPT_FORMAT => $parameters['format'],
LatLongFormatter::OPT_DIRECTIONAL => $parameters['directional'],
LatLongFormatter::OPT_PRECISION => 1 / 360000
]
return MapsFactory::globalInstance()->getCoordinateFormatter()->format(
new LatLongValue( $destination['lat'], $destination['lon'] ),
$parameters['format'],
$parameters['directional']
);

$formatter = new LatLongFormatter( $options );

return $formatter->format( new LatLongValue( $destination['lat'], $destination['lon'] ) );
}

/**
Expand Down
19 changes: 5 additions & 14 deletions includes/parserhooks/Maps_Geocode.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
<?php

use DataValues\Geo\Formatters\LatLongFormatter;
use Jeroen\SimpleGeocoder\Geocoder;
use ValueFormatters\FormatterOptions;
use Maps\MapsFactory;

/**
* Class for the 'geocode' parser hooks, which can turn
Expand Down Expand Up @@ -40,18 +39,10 @@ public function render( array $parameters ) {
return 'Geocoding failed'; // TODO: i18n
}

return $this->newCoordinateFormatter( $parameters )->format( $coordinates );
}

private function newCoordinateFormatter( array $parameters ) {
return new LatLongFormatter(
new FormatterOptions(
[
LatLongFormatter::OPT_FORMAT => $parameters['format'],
LatLongFormatter::OPT_DIRECTIONAL => $parameters['directional'],
LatLongFormatter::OPT_PRECISION => 1 / 360000
]
)
return MapsFactory::globalInstance()->getCoordinateFormatter()->format(
$coordinates,
$parameters['format'],
$parameters['directional']
);
}

Expand Down
29 changes: 29 additions & 0 deletions src/CoordinateFormatter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

declare( strict_types = 1 );

namespace Maps;

use DataValues\Geo\Formatters\LatLongFormatter;
use DataValues\Geo\Values\LatLongValue;
use ValueFormatters\FormatterOptions;

/**
* @licence GNU GPL v2+
* @author Jeroen De Dauw < [email protected] >
*/
class CoordinateFormatter {

public function format( LatLongValue $latLong, string $format, bool $directional ) {
$formatter = new LatLongFormatter( new FormatterOptions(
[
LatLongFormatter::OPT_FORMAT => $format,
LatLongFormatter::OPT_DIRECTIONAL => $directional,
LatLongFormatter::OPT_PRECISION => 1 / 360000
]
) );

return $formatter->format( $latLong );
}

}
1 change: 0 additions & 1 deletion src/GeoJson/GeoJsonContent.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace Maps\GeoJson;

use FormatJson;
use Html;
use ParserOptions;
use ParserOutput;
Expand Down
2 changes: 0 additions & 2 deletions src/GeoJson/GeoJsonContentHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

namespace Maps\GeoJson;

use EditAction;

class GeoJsonContentHandler extends \JsonContentHandler {

public function __construct( $modelId = GeoJsonContent::CONTENT_MODEL_ID ) {
Expand Down
21 changes: 20 additions & 1 deletion src/MapsFactory.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare( strict_types = 1 );

namespace Maps;

use FileFetcher\FileFetcher;
Expand All @@ -26,10 +28,23 @@ private function __construct( array $settings, MediaWikiServices $mediaWikiServi
$this->mediaWikiServices = $mediaWikiServices;
}

public static function newDefault() {
public static function newDefault(): self {
return new self( $GLOBALS, MediaWikiServices::getInstance() );
}

/**
* Only for legacy code where dependency injection is not possible
*/
public static function globalInstance(): self {
static $instance = null;

if ( $instance === null ) {
$instance = self::newDefault();
}

return $instance;
}

public function newLocationParser(): LocationParser {
return LocationParser::newInstance( $this->newGeocoder() );
}
Expand Down Expand Up @@ -97,4 +112,8 @@ public function getPageContentFetcher(): PageContentFetcher {
);
}

public function getCoordinateFormatter(): CoordinateFormatter {
return new CoordinateFormatter();
}

}
18 changes: 5 additions & 13 deletions src/Semantic/DataValues/CoordinateValue.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

namespace Maps\Semantic\DataValues;

use DataValues\Geo\Formatters\LatLongFormatter;
use DataValues\Geo\Parsers\LatLongParser;
use DataValues\Geo\Values\LatLongValue;
use InvalidArgumentException;
use Maps\MapsFactory;
use MapsDistanceParser;
use SMW\Query\Language\Description;
use SMW\Query\Language\ThingDescription;
Expand Down Expand Up @@ -159,21 +159,13 @@ public function getShortWikiText( $linked = null ) {
* @return string|null
*/
private function getFormattedCoord( SMWDIGeoCoord $dataItem, string $format = null ) {
$options = new \ValueFormatters\FormatterOptions(
[
LatLongFormatter::OPT_FORMAT => $format ?? $GLOBALS['smgQPCoodFormat'],
LatLongFormatter::OPT_DIRECTIONAL => $GLOBALS['smgQPCoodDirectional'],
LatLongFormatter::OPT_PRECISION => 1 / 360000
]
);

$coordinateFormatter = new LatLongFormatter( $options );

return $coordinateFormatter->format(
return MapsFactory::globalInstance()->getCoordinateFormatter()->format(
new LatLongValue(
$dataItem->getLatitude(),
$dataItem->getLongitude()
)
),
$format ?? $GLOBALS['smgQPCoodFormat'],
$GLOBALS['smgQPCoodDirectional']
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
use CoordinateValue;
use Maps\Semantic\ValueDescriptions\AreaDescription;
use PHPUnit\Framework\TestCase;
use SMW\DataValueFactory;
use SMWDataItem;
use SMWDIGeoCoord;

/**
Expand Down Expand Up @@ -75,7 +73,7 @@ public function testGetQueryString() {
);

$this->assertSame(
'[[1° 0\' 0", 5° 0\' 0" (10 km)]]',
'[[1° 0\' 0.00" N, 5° 0\' 0.00" E (10 km)]]',
$area->getQueryString()
);
}
Expand Down
2 changes: 1 addition & 1 deletion tests/Unit/Semantic/DataValues/CoordinateValueTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public function testConstruct() {
$this->assertInstanceOf( CoordinateValue::class, $geoValue );

$this->assertEquals( $geoDI, $geoValue->getDataItem() );
$this->assertEquals( '23° 0\' 0", 42° 0\' 0"', $geoValue->getShortWikiText() );
$this->assertSame( '23° 0\' 0.00" N, 42° 0\' 0.00" E', $geoValue->getShortWikiText() );
}

/**
Expand Down

0 comments on commit 2063f74

Please sign in to comment.