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