summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/Maps/src/SemanticMW/ValueDescriptions/AreaDescription.php
blob: ac8850954acf852b9c755d9497c854f5fac82517 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
<?php

namespace Maps\SemanticMW\ValueDescriptions;

use DataValues\Geo\Values\LatLongValue;
use InvalidArgumentException;
use Maps\GeoFunctions;
use Maps\Presentation\MapsDistanceParser;
use SMW\DataValueFactory;
use SMW\DIProperty;
use SMW\Query\Language\ValueDescription;
use SMWDataItem;
use SMWDIGeoCoord;
use SMWThingDescription;
use Wikimedia\Rdbms\IDatabase;

/**
 * Description of a geographical area defined by a coordinates set and a distance to the bounds.
 * The bounds are a 'rectangle' (but bend due to the earths curvature), as the resulting query
 * would otherwise be to resource intensive.
 *
 * @licence GNU GPL v2+
 * @author Jeroen De Dauw < jeroendedauw@gmail.com >
 */
class AreaDescription extends ValueDescription {

	/**
	 * @var SMWDIGeoCoord
	 */
	private $center;

	private $radius;

	public function __construct( SMWDataItem $areaCenter, int $comparator, string $radius, DIProperty $property = null ) {
		if ( !( $areaCenter instanceof SMWDIGeoCoord ) ) {
			throw new InvalidArgumentException( '$areaCenter needs to be a SMWDIGeoCoord' );
		}

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

		$this->center = $areaCenter;
		$this->radius = $radius;
	}

	/**
	 * @see Description::prune
	 */
	public function prune( &$maxsize, &$maxdepth, &$log ) {
		if ( ( $maxsize < $this->getSize() ) || ( $maxdepth < $this->getDepth() ) ) {
			$log[] = $this->getQueryString();

			$result = new SMWThingDescription();
			$result->setPrintRequests( $this->getPrintRequests() );

			return $result;
		}

		$maxsize = $maxsize - $this->getSize();
		$maxdepth = $maxdepth - $this->getDepth();

		return $this;
	}

	public function getQueryString( $asValue = false ) {
		$centerString = DataValueFactory::getInstance()->newDataValueByItem(
			$this->center,
			$this->getProperty()
		)->getWikiValue();

		$queryString = "$centerString ({$this->radius})";

		return $asValue ? $queryString : "[[$queryString]]";
	}

	/**
	 * @see SomePropertyInterpreter::mapValueDescription
	 *
	 * FIXME: store specific code should be in the store component
	 *
	 * @param string $tableName
	 * @param string[] $fieldNames
	 * @param IDatabase $db
	 *
	 * @return string|false
	 */
	public function getSQLCondition( $tableName, array $fieldNames, IDatabase $db ) {
		if ( $this->center->getDIType() != SMWDataItem::TYPE_GEO ) {
			throw new \LogicException( 'Constructor should have prevented this' );
		}

		if ( !$this->comparatorIsSupported() ) {
			return false;
		}

		$bounds = $this->getBoundingBox();

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

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

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

		$conditions = [];

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

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

	private function comparatorIsSupported(): bool {
		return $this->getComparator() === SMW_CMP_EQ || $this->getComparator() === SMW_CMP_NEQ;
	}

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

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

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

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

}