Location: PHPKode > scripts > phpFlickrSynch > phpflickrsynch/_publicApi/class_Location.php
<?php
	/**
	  * Class to access a specific location in the photo collection
	  * Relies on the Yahoo! GeoPlanet API - http://developer.yahoo.com/geo/geoplanet/
	  */
	class Location {
	
		private $woeid = NULL;
		private $southWestLatitude = NULL;
		private $southWestLongitude = NULL;
		private $northEastLatitude = NULL;
		private $northEastLongitude = NULL;
		private $centroidLatitude = NULL;
		private $centroidLongitude = NULL;
		private $name = NULL;
	
		/**
		  * Constructor
		  * @param $woeid WhereOnEarthID
		  */
		public function __construct($woeid) {
			$this->woeid = $woeid;

			if (!$this->exists()) {
				$this->register();
			}
		}		
		
		/**
		  * Returns the bounding box for the WhereOnEarthID
		  */
		public function getSouthWestLatitude() { return $this->southWestLatitude; }
		public function getSouthWestLongitude() { return $this->southWestLongitude; }
		public function getNorthEastLatitude() { return $this->northEastLatitude; }
		public function getNorthEastLongitude() { return $this->northEastLongitude; }
		public function getCentroidLatitude() { return $this->centroidLatitude; }
		public function getCentroidLongitude() { return $this->centroidLongitude; }
		
		/**
		  * Returns the location name
		  */
		public function getName() { return $this->name; }

		/**
		  * Checks if the WhereOnEarthID exists in the local DB
		  */
		private function exists() {
			global $FLDB;
			$st = $FLDB->prepare('SELECT woeid_name, woeid_sw_lon, woeid_sw_lat, woeid_ne_lon, woeid_ne_lat, woeid_cent_lon, woeid_cent_lat FROM flickr_woeid WHERE woeid = ?');
			$st->bindParam(1, $this->woeid, PDO::PARAM_INT);
			$tab = Helpers::fetchOneRow($st);		
		
			if ($tab) {
				$this->name = $tab['woeid_name'];
				$this->southWestLatitude = $tab['woeid_sw_lat'];
				$this->southWestLongitude = $tab['woeid_sw_lon'];
				$this->northEastLatitude = $tab['woeid_ne_lat']; 
				$this->northEastLongitude = $tab['woeid_ne_lon']; 
				$this->centroidLatitude = $tab['woeid_cent_lat']; 
				$this->centroidLongitude = $tab['woeid_cent_lon'];
				return true;
			} else {
				return false;
			}
		}

		/**
		  * Register the WhereOnEarthID in the local DB
		  */
		private function register() {
			global $FLDB;
			$res = $this->resolveWOEID();
								
			$this->southWestLatitude = $res->boundingBox->southWest->latitude;
			$this->southWestLongitude = $res->boundingBox->southWest->longitude;
			$this->northEastLatitude = $res->boundingBox->northEast->latitude;
			$this->northEastLongitude = $res->boundingBox->northEast->longitude;
			$this->centroidLatitude = $res->centroid->latitude;
			$this->centroidLongitude = $res->centroid->longitude;	
			$this->name = $this->buildLocationName($res);
			
			$st = $FLDB->prepare('INSERT INTO flickr_woeid (woeid, woeid_sw_lon, woeid_sw_lat, woeid_ne_lon, woeid_ne_lat, woeid_cent_lon, woeid_cent_lat, woeid_name) VALUES (?,?,?,?,?,?,?,?)');
			
			$st->bindParam(1, $this->woeid, PDO::PARAM_INT);
			$st->bindParam(2, $this->southWestLongitude, PDO::PARAM_STR);
			$st->bindParam(3, $this->southWestLatitude, PDO::PARAM_STR);
			$st->bindParam(4, $this->northEastLongitude, PDO::PARAM_STR);
			$st->bindParam(5, $this->northEastLatitude, PDO::PARAM_STR);
			$st->bindParam(6, $this->centroidLongitude, PDO::PARAM_STR);
			$st->bindParam(7, $this->centroidLatitude, PDO::PARAM_STR);
			$st->bindParam(8, $this->name, PDO::PARAM_STR);
			Helpers::executeStatement($st);
		}
		
		/**
		  * Builds the location name
		  */
		private function buildLocationName($res) {
			$str = '';			
			if (isset($res->name) && strlen($res->name)>0) $str = ', ' . $res->name;
			if (isset($res->admin3) && strlen($res->admin3)>0) $str .= ', ' . $res->admin3;
			if (isset($res->admin2) && strlen($res->admin2)>0) $str .= ', ' . $res->admin2;
			if (isset($res->admin1) && strlen($res->admin1)>0) $str .= ', ' . $res->admin1;	
			if (isset($res->country) && strlen($res->country)>0) $str .= ', ' . $res->country;						
			if (strlen($str) > 0) $str = substr($str, 2);		
			return $str;
		}
		
		/**
		  * Calls the Yahoo! GeoPlanet API to retrieve the WOEID
		  */
		private function resolveWOEID() {
			$url = 'http://where.yahooapis.com/v1/place/' . $this->woeid . '?appid=' . PHPFLICKRSYNCH_YAHOO_APPID . '&format=json';
			
			$response = file_get_contents($url);			
			if ($response === false) {
				die('Request failed');
			}		
			
			$json = new Services_JSON();
			$res = $json->decode($response);
			
			return $res->place;
		}	
	}
?>
Return current item: phpFlickrSynch