00001 <?php
00002
00018 class SMAreaValueDescription extends SMWValueDescription {
00019
00027 protected $bounds = false;
00028
00038 public function __construct( SMWDataItem $dataItem, $comparator, $radius ) {
00039 parent::__construct( $dataItem, $comparator );
00040
00041
00042 if ( self::geoFunctionsAreAvailable() ) {
00043 $this->calculateBounds( $dataItem, $radius );
00044 }
00045 }
00046
00055 protected function calculateBounds( SMWDIGeoCoord $dataItem, $radius ) {
00056 $this->bounds = self::getBoundingBox(
00057 array( 'lat' => $dataItem->getLatitude(), 'lon' => $dataItem->getLongitude() ),
00058 MapsDistanceParser::parseDistance( $radius )
00059 );
00060 }
00061
00069 public function getQueryString( $asValue = false ) {
00070 if ( $this->getDataItem() !== null ) {
00071 $queryString = SMWDataValueFactory::newDataItemValue( $this->getDataItem(), $this->m_property )->getWikiValue();
00072 return $asValue ? $queryString : "[[$queryString]]";
00073 } else {
00074 return $asValue ? '+' : '';
00075 }
00076 }
00077
00083 public function prune( &$maxsize, &$maxdepth, &$log ) {
00084 if ( ( $maxsize < $this->getSize() ) || ( $maxdepth < $this->getDepth() ) ) {
00085 $log[] = $this->getQueryString();
00086
00087 $result = new SMWThingDescription();
00088 $result->setPrintRequests( $this->getPrintRequests() );
00089
00090 return $result;
00091 } else {
00092 $maxsize = $maxsize - $this->getSize();
00093 $maxdepth = $maxdepth - $this->getDepth();
00094 return $this;
00095 }
00096 }
00097
00105 public function getBounds() {
00106 return $this->bounds;
00107 }
00108
00120 public function getSQLCondition( $tableName, array $fieldNames, $dbs ) {
00121
00122
00123 if ( $this->getDataItem()->getDIType() != SMWDataItem::TYPE_GEO
00124 || ( $this->getComparator() != SMW_CMP_EQ && $this->getComparator() != SMW_CMP_NEQ )
00125 ) {
00126 return false;
00127 }
00128
00129 $north = $dbs->addQuotes( $this->bounds['north'] );
00130 $east = $dbs->addQuotes( $this->bounds['east'] );
00131 $south = $dbs->addQuotes( $this->bounds['south'] );
00132 $west = $dbs->addQuotes( $this->bounds['west'] );
00133
00134 $isEq = $this->getComparator() == SMW_CMP_EQ;
00135
00136 $conditions = array();
00137
00138 $smallerThen = $isEq ? '<' : '>=';
00139 $biggerThen = $isEq ? '>' : '<=';
00140 $joinCond = $isEq ? 'AND' : 'OR';
00141
00142 $conditions[] = "{$tableName}.$fieldNames[0] $smallerThen $north";
00143 $conditions[] = "{$tableName}.$fieldNames[0] $biggerThen $south";
00144 $conditions[] = "{$tableName}.$fieldNames[1] $smallerThen $east";
00145 $conditions[] = "{$tableName}.$fieldNames[1] $biggerThen $west";
00146
00147 $sql = implode( " $joinCond ", $conditions );
00148
00149 return $sql;
00150 }
00151
00162 protected static function getBoundingBox( array $centerCoordinates, $circleRadius ) {
00163 $north = MapsGeoFunctions::findDestination( $centerCoordinates, 0, $circleRadius );
00164 $east = MapsGeoFunctions::findDestination( $centerCoordinates, 90, $circleRadius );
00165 $south = MapsGeoFunctions::findDestination( $centerCoordinates, 180, $circleRadius );
00166 $west = MapsGeoFunctions::findDestination( $centerCoordinates, 270, $circleRadius );
00167
00168 return array(
00169 'north' => $north['lat'],
00170 'east' => $east['lon'],
00171 'south' => $south['lat'],
00172 'west' => $west['lon'],
00173 );
00174 }
00175
00183 protected static function geoFunctionsAreAvailable() {
00184 return class_exists( 'MapsGeoFunctions' );
00185 }
00186
00187 }