1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Maps\Semantic\ValueDescriptions; |
4
|
|
|
|
5
|
|
|
use DatabaseBase; |
6
|
|
|
use DataValues\Geo\Values\LatLongValue; |
7
|
|
|
use InvalidArgumentException; |
8
|
|
|
use MapsDistanceParser; |
9
|
|
|
use MapsGeoFunctions; |
10
|
|
|
use SMW\DataValueFactory; |
11
|
|
|
use SMW\DIProperty; |
12
|
|
|
use SMW\Query\Language\ValueDescription; |
13
|
|
|
use SMWDataItem; |
14
|
|
|
use SMWDIGeoCoord; |
15
|
|
|
use SMWThingDescription; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Description of a geographical area defined by a coordinates set and a distance to the bounds. |
19
|
|
|
* The bounds are a 'rectangle' (but bend due to the earths curvature), as the resulting query |
20
|
|
|
* would otherwise be to resource intensive. |
21
|
|
|
* |
22
|
|
|
* @licence GNU GPL v2+ |
23
|
|
|
* @author Jeroen De Dauw < [email protected] > |
24
|
|
|
*/ |
25
|
|
|
class AreaDescription extends ValueDescription { |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var SMWDIGeoCoord |
29
|
|
|
*/ |
30
|
|
|
private $center; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var string |
34
|
|
|
*/ |
35
|
|
|
private $radius; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @param SMWDataItem $areaCenter |
39
|
|
|
* @param string $comparator |
40
|
|
|
* @param string $radius |
41
|
|
|
* @param DIProperty|null $property |
42
|
|
|
* |
43
|
|
|
* @throws InvalidArgumentException |
44
|
|
|
*/ |
45
|
|
|
public function __construct( SMWDataItem $areaCenter, $comparator, $radius, DIProperty $property = null ) { |
46
|
|
|
if ( !( $areaCenter instanceof SMWDIGeoCoord ) ) { |
|
|
|
|
47
|
|
|
throw new InvalidArgumentException( '$areaCenter needs to be a SMWDIGeoCoord' ); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
parent::__construct( $areaCenter, $property, $comparator ); |
51
|
|
|
|
52
|
|
|
$this->center = $areaCenter; |
53
|
|
|
$this->radius = $radius; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @see \SMW\Query\Language\Description::getQueryString |
58
|
|
|
* |
59
|
|
|
* @param boolean $asValue |
60
|
|
|
* @return string |
61
|
|
|
*/ |
62
|
|
|
public function getQueryString( $asValue = false ) { |
63
|
|
|
$centerString = DataValueFactory::newDataItemValue( $this->center, $this->getPropertyCompat() )->getWikiValue(); |
64
|
|
|
|
65
|
|
|
$queryString = "$centerString ({$this->radius})"; |
66
|
|
|
|
67
|
|
|
return $asValue ? $queryString : "[[$queryString]]"; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
private function getPropertyCompat() { |
71
|
|
|
return method_exists( $this, 'getProperty' ) ? $this->getProperty() : $this->m_property; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @see \SMW\Query\Language\Description::prune |
76
|
|
|
*/ |
77
|
|
|
public function prune( &$maxsize, &$maxdepth, &$log ) { |
78
|
|
|
if ( ( $maxsize < $this->getSize() ) || ( $maxdepth < $this->getDepth() ) ) { |
79
|
|
|
$log[] = $this->getQueryString(); |
80
|
|
|
|
81
|
|
|
$result = new SMWThingDescription(); |
82
|
|
|
$result->setPrintRequests( $this->getPrintRequests() ); |
83
|
|
|
|
84
|
|
|
return $result; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
$maxsize = $maxsize - $this->getSize(); |
88
|
|
|
$maxdepth = $maxdepth - $this->getDepth(); |
89
|
|
|
|
90
|
|
|
return $this; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @see \SMW\Query\Language\Description::getSQLCondition |
95
|
|
|
* |
96
|
|
|
* FIXME: store specific code should be in the store component |
97
|
|
|
* |
98
|
|
|
* @param string $tableName |
99
|
|
|
* @param array $fieldNames |
100
|
|
|
* @param DatabaseBase $dbs |
101
|
|
|
* |
102
|
|
|
* @return string|false |
103
|
|
|
*/ |
104
|
|
|
public function getSQLCondition( $tableName, array $fieldNames, DatabaseBase $dbs ) { |
105
|
|
|
if ( $this->center->getDIType() != SMWDataItem::TYPE_GEO ) { |
106
|
|
|
throw new \LogicException( 'Constructor should have prevented this' ); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
if ( !$this->comparatorIsSupported() ) { |
110
|
|
|
return false; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
$bounds = $this->getBoundingBox(); |
114
|
|
|
|
115
|
|
|
$north = $dbs->addQuotes( $bounds['north'] ); |
116
|
|
|
$east = $dbs->addQuotes( $bounds['east'] ); |
117
|
|
|
$south = $dbs->addQuotes( $bounds['south'] ); |
118
|
|
|
$west = $dbs->addQuotes( $bounds['west'] ); |
119
|
|
|
|
120
|
|
|
$isEq = $this->getComparator() == SMW_CMP_EQ; |
121
|
|
|
|
122
|
|
|
$smallerThen = $isEq ? '<' : '>='; |
123
|
|
|
$biggerThen = $isEq ? '>' : '<='; |
124
|
|
|
$joinCond = $isEq ? 'AND' : 'OR'; |
125
|
|
|
|
126
|
|
|
$conditions = []; |
127
|
|
|
|
128
|
|
|
$conditions[] = "{$tableName}.$fieldNames[1] $smallerThen $north"; |
129
|
|
|
$conditions[] = "{$tableName}.$fieldNames[1] $biggerThen $south"; |
130
|
|
|
$conditions[] = "{$tableName}.$fieldNames[2] $smallerThen $east"; |
131
|
|
|
$conditions[] = "{$tableName}.$fieldNames[2] $biggerThen $west"; |
132
|
|
|
|
133
|
|
|
return implode( " $joinCond ", $conditions ); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
private function comparatorIsSupported() { |
137
|
|
|
return $this->getComparator() === SMW_CMP_EQ || $this->getComparator() === SMW_CMP_NEQ; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* @return float[] An associative array containing the limits with keys north, east, south and west. |
142
|
|
|
*/ |
143
|
|
|
public function getBoundingBox() { |
144
|
|
|
$center = new LatLongValue( |
145
|
|
|
$this->center->getLatitude(), |
146
|
|
|
$this->center->getLongitude() |
147
|
|
|
); |
148
|
|
|
|
149
|
|
|
$radiusInMeters = MapsDistanceParser::parseDistance( $this->radius ); // TODO: this can return false |
150
|
|
|
|
151
|
|
|
$north = MapsGeoFunctions::findDestination( $center, 0, $radiusInMeters ); |
|
|
|
|
152
|
|
|
$east = MapsGeoFunctions::findDestination( $center, 90, $radiusInMeters ); |
|
|
|
|
153
|
|
|
$south = MapsGeoFunctions::findDestination( $center, 180, $radiusInMeters ); |
|
|
|
|
154
|
|
|
$west = MapsGeoFunctions::findDestination( $center, 270, $radiusInMeters ); |
|
|
|
|
155
|
|
|
|
156
|
|
|
return [ |
157
|
|
|
'north' => $north['lat'], |
158
|
|
|
'east' => $east['lon'], |
159
|
|
|
'south' => $south['lat'], |
160
|
|
|
'west' => $west['lon'], |
161
|
|
|
]; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
} |