Skip to content

Commit

Permalink
Added AreaDescriptionTest
Browse files Browse the repository at this point in the history
  • Loading branch information
JeroenDeDauw committed Apr 13, 2017
1 parent 484b1aa commit 998a690
Show file tree
Hide file tree
Showing 8 changed files with 138 additions and 86 deletions.
10 changes: 6 additions & 4 deletions SemanticMaps/SemanticMaps.hooks.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

use Maps\Semantic\DataValues\CoordinateValue;
use Maps\Semantic\DataValues\GeoPolygonValue;
use SMW\DataTypeRegistry;

/**
Expand Down Expand Up @@ -43,13 +45,13 @@ public static function addToAdminLinks( ALTree &$admin_links_tree ) {
public static function initGeoDataTypes() {
DataTypeRegistry::getInstance()->registerDatatype(
'_geo',
SMGeoCoordsValue::class,
CoordinateValue::class,
SMWDataItem::TYPE_GEO
);

DataTypeRegistry::getInstance()->registerDatatype(
'_gpo',
SMGeoPolygonsValue::class,
GeoPolygonValue::class,
SMWDataItem::TYPE_BLOB
);

Expand All @@ -65,8 +67,8 @@ public static function initGeoDataTypes() {
* @since 1.0
*
* @param $format Mixed: The format (string), or false when not set yet
* @param $printRequests Array: The print requests made
* @param $params Array: The parameters for the query printer
* @param array $printRequests The print requests made
* @param array $params The parameters for the query printer
*
* @return boolean
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,26 @@
<?php

namespace Maps\Semantic\DataValues;

use DataValues\Geo\Formatters\GeoCoordinateFormatter;
use DataValues\Geo\Parsers\GeoCoordinateParser;
use DataValues\Geo\Values\LatLongValue;
use InvalidArgumentException;
use MapsDistanceParser;
use SMW\Query\Language\Description;
use SMW\Query\Language\ThingDescription;
use SMWDataItem;
use SMWDataValue;
use SMWDIGeoCoord;
use SMWOutputs;
use ValueParsers\ParseException;

/**
* Implementation of datavalues that are geographic coordinates.
*
* @licence GNU GPL v2+
* @author Jeroen De Dauw < jeroendedauw@gmail.com >
* @author Markus Krötzsch
*/
class SMGeoCoordsValue extends SMWDataValue {
class CoordinateValue extends SMWDataValue {

private $wikiValue;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
<?php

namespace Maps\Semantic\DataValues;

use PolygonHandler;
use SMWDataItem;
use SMWDataValue;
use SMWDIBlob;

/**
* Implementation of datavalues that are geographic shapes.
*
* @file SM_GeoPolgonValue.php
* @ingroup SemanticMaps
* @ingroup SMWDataValues
*
* @author Nischay Nahata
*/
class SMGeoPolygonsValue extends SMWDataValue {
class GeoPolygonValue extends SMWDataValue {

/**
* @see SMWDataValue::setDataItem()
Expand Down Expand Up @@ -38,10 +41,11 @@ protected function parseUserValue( $value ) {
if ( $value === '' ) {
$this->addError( wfMessage( 'smw_emptystring' )->inContentLanguage()->text() );
}
$polyHandler = new PolygonHandler ( $value );
foreach( $polyHandler->getValidationErrors() as $errMsg ) {

foreach( ( new PolygonHandler( $value ) )->getValidationErrors() as $errMsg ) {
$this->addError( $errMsg );
}

$this->m_dataitem = new SMWDIBlob( $value, $this->m_typeid );
}

Expand All @@ -53,9 +57,9 @@ protected function parseUserValue( $value ) {
public function getShortWikiText( $linked = null ) {
if ( $this->isValid() ) {
return $this->m_dataitem->getString();
} else {
return $this->getErrorText();
}

return $this->getErrorText();
}

/**
Expand Down
92 changes: 37 additions & 55 deletions src/Semantic/ValueDescriptions/AreaDescription.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,6 @@
*/
class AreaDescription extends ValueDescription {

/**
* Associative array containing the bounds of the area, or false when not set.
*
* @var float[]|false
*/
private $bounds = false;

/**
* @var SMWDIGeoCoord
*/
Expand All @@ -56,32 +49,8 @@ public function __construct( SMWDataItem $areaCenter, $comparator, $radius, DIPr

parent::__construct( $areaCenter, $property, $comparator );

$this->radius = MapsDistanceParser::parseDistance( $radius );
$this->center = $areaCenter;

$this->bounds = $this->createBoundingBox();
}

/**
* @return float[] An associative array containing the limits with keys north, east, south and west.
*/
private function createBoundingBox() {
$center = new LatLongValue(
$this->center->getLatitude(),
$this->center->getLongitude()
);

$north = MapsGeoFunctions::findDestination( $center, 0, $this->radius );
$east = MapsGeoFunctions::findDestination( $center, 90, $this->radius );
$south = MapsGeoFunctions::findDestination( $center, 180, $this->radius );
$west = MapsGeoFunctions::findDestination( $center, 270, $this->radius );

return [
'north' => $north['lat'],
'east' => $east['lon'],
'south' => $south['lat'],
'west' => $west['lon'],
];
$this->radius = $radius;
}

private function getPropertyCompat() {
Expand All @@ -95,7 +64,7 @@ private function getPropertyCompat() {
* @return string
*/
public function getQueryString( $asValue = false ) {
if ( $this->getDataItem() === null ) {
if ( $this->getDataItem() === null ) { // TODO: dead code? should never be null
return $asValue ? '+' : '';
}

Expand Down Expand Up @@ -123,17 +92,6 @@ public function prune( &$maxsize, &$maxdepth, &$log ) {
}

/**
* Returns the bounds of the area.
*
* @since 0.6
*
* @return array
*/
public function getBounds() {
return $this->bounds;
}

/**
* @see \SMW\Query\Language\Description::getSQLCondition
*
* FIXME: store specific code should be in the store component
Expand All @@ -153,27 +111,51 @@ public function getSQLCondition( $tableName, array $fieldNames, DatabaseBase $db
return false;
}

$north = $dbs->addQuotes( $this->bounds['north'] );
$east = $dbs->addQuotes( $this->bounds['east'] );
$south = $dbs->addQuotes( $this->bounds['south'] );
$west = $dbs->addQuotes( $this->bounds['west'] );
$bounds = $this->getBoundingBox();

$north = $dbs->addQuotes( $bounds['north'] );
$east = $dbs->addQuotes( $bounds['east'] );
$south = $dbs->addQuotes( $bounds['south'] );
$west = $dbs->addQuotes( $bounds['west'] );

$isEq = $this->getComparator() == SMW_CMP_EQ;

$conditions = [];
$smallerThen = $isEq ? '<' : '>=';
$biggerThen = $isEq ? '>' : '<=';
$joinCond = $isEq ? 'AND' : 'OR';

$smallerThen = $isEq ? '<' : '>=';
$biggerThen = $isEq ? '>' : '<=';
$joinCond = $isEq ? 'AND' : 'OR';
$conditions = [];

$conditions[] = "{$tableName}.$fieldNames[1] $smallerThen $north";
$conditions[] = "{$tableName}.$fieldNames[1] $smallerThen $north";
$conditions[] = "{$tableName}.$fieldNames[1] $biggerThen $south";
$conditions[] = "{$tableName}.$fieldNames[2] $smallerThen $east";
$conditions[] = "{$tableName}.$fieldNames[2] $biggerThen $west";

$sql = implode( " $joinCond ", $conditions );
return implode( " $joinCond ", $conditions );
}

return $sql;
/**
* @return float[] An associative array containing the limits with keys north, east, south and west.
*/
public function getBoundingBox() {
$center = new LatLongValue(
$this->center->getLatitude(),
$this->center->getLongitude()
);

$radiusInMeters = MapsDistanceParser::parseDistance( $this->radius ); // TODO: this can return false

$north = MapsGeoFunctions::findDestination( $center, 0, $radiusInMeters );
$east = MapsGeoFunctions::findDestination( $center, 90, $radiusInMeters );
$south = MapsGeoFunctions::findDestination( $center, 180, $radiusInMeters );
$west = MapsGeoFunctions::findDestination( $center, 270, $radiusInMeters );

return [
'north' => $north['lat'],
'east' => $east['lon'],
'south' => $south['lat'],
'west' => $west['lon'],
];
}

}
12 changes: 6 additions & 6 deletions src/Semantic/ValueDescriptions/CoordinateDescription.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

namespace Maps\Semantic\ValueDescriptions;

use Maps\Semantic\DatabaseBase;
use Maps\Semantic\SMWDIGeoCoord;
use DatabaseBase;
use SMW\DataValueFactory;
use SMW\Query\Language\ValueDescription;
use SMWDIGeoCoord;

/**
* Description of one data value of type Geographical Coordinates.
Expand All @@ -23,12 +23,12 @@ class CoordinateDescription extends ValueDescription {
* @return string
*/
public function getQueryString( $asValue = false ) {
if ( $this->getDataItem() !== null ) {
$queryString = DataValueFactory::newDataItemValue( $this->getDataItem(), $this->getPropertyCompat() )->getWikiValue();
return $asValue ? $queryString : "[[$queryString]]";
} else {
if ( $this->getDataItem() === null ) {
return $asValue ? '+' : '';
}

$queryString = DataValueFactory::newDataItemValue( $this->getDataItem(), $this->getPropertyCompat() )->getWikiValue();
return $asValue ? $queryString : "[[$queryString]]";
}

private function getPropertyCompat() {
Expand Down
2 changes: 1 addition & 1 deletion tests/Integration/MapsDistanceParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use MapsDistanceParser;

/**
* @covers MapsCoordinates
* @covers MapsDistanceParser
*
* @licence GNU GPL v2+
* @author Jeroen De Dauw < jeroendedauw@gmail.com >
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

namespace Maps\Tests\Semantic\ValueDescriptions;

use Maps\Semantic\ValueDescriptions\AreaDescription;
use Maps\Semantic\ValueDescriptions\CoordinateDescription;
use CoordinateValue;
use SMW\DataValueFactory;
use SMWDataItem;
use SMWDIGeoCoord;

/**
* @covers \Maps\Semantic\ValueDescriptions\AreaDescription
*
* @licence GNU GPL v2+
* @author Jeroen De Dauw < jeroendedauw@gmail.com >
*/
class AreaDescriptionTest extends \PHPUnit_Framework_TestCase {

public function setUp() {
if ( !defined( 'SMW_VERSION' ) ) {
$this->markTestSkipped( 'SMW is not available' );
}
}

public function testGetBoundingBox() {
$area = new AreaDescription(
new SMWDIGeoCoord( 0, 0 ),
SMW_CMP_EQ,
'10 km'
);

$this->assertEquals(
[
'north' => 0.089932160591873,
'east' => 0.089932160591873,
'south' => -0.089932160591873,
'west' => -0.089932160591873
],
$area->getBoundingBox()
);
}

public function testGetSQLCondition() {
$area = new AreaDescription(
new SMWDIGeoCoord( 0, 0 ),
SMW_CMP_EQ,
'10 km'
);

$this->assertSame(
'geo_table.lat_field < \'0.089932160591873\' AND geo_table.lat_field > \'-0.089932160591873\' '
. 'AND geo_table.long_field < \'0.089932160591873\' AND geo_table.long_field > \'-0.089932160591873\'',
$area->getSQLCondition( 'geo_table', ['id_field', 'lat_field', 'long_field'], wfGetDB( DB_MASTER ) )
);
}

}

0 comments on commit 998a690

Please sign in to comment.