extranet/library/Scores/Google/Streetview.php
2016-02-12 14:25:27 +00:00

238 lines
4.1 KiB
PHP

<?php
/**
* https://developers.google.com/maps/documentation/streetview/
* Image de 640x640 max
* Heading 0 à 360
* 0, 45, 90, 135, 180, 225, 270, 315
* Récup de chaque image enregistré SIRET-DEG
*/
class Scores_Google_Streetview
{
/**
* Service URL
* @var string
*/
protected $url = 'http://maps.googleapis.com/maps/api/streetview?';
/**
* Max size 640x640
* @var string
*/
protected $size = '640x640';
/**
* GPS ou Adresse
* @var string
*/
protected $location = null;
/**
* Defaut 90, Max 120, represents zoom, with smaller numbers indicating a higher level of zoom
* @var unknown
*/
protected $fov = 90;
/**
* (default is 0) Specifies the up or down angle of the camera relative to the Street View vehicle
* @var unknown
*/
protected $pitch = 0;
/**
* (optional) identifies your application for quota purposes
* @var string
*/
protected $key = null;
/**
* always false
* @var string
*/
protected $sensor = 'false';
/**
* Indicates the compass heading of the camera (0 to 360)
*/
protected $heading = null;
/**
* Number of image by the circle (360°)
* @var int
*/
protected $nbImage = 8;
/**
* File extension
* @var string
*/
protected $extension = 'jpg';
/**
* Path where files are stored
*/
protected $path = '';
/**
*
* @var string
*/
protected $siret;
/**
* Maximum request per day
* @var int
*/
protected $maxRequestPerDay = 25000;
/**
*
* @var unknown
*/
protected $mode;
/**
* Mode GPS
* @var unknown
*/
const MODE_GPS = 1;
/**
* Mode Adresse
* @var unknown
*/
const MODE_ADDRESS = 2;
/**
* Google Streetview
* Get image by GPS coord or by adresse
* Save image reference in a database
* GpsLat, GpsLong, Adresse, Image, Date
* Save request that give no imagery
* Siren, Nic, Date
* Save in database, with a counter the number of request by day
*/
public function __construct($siret)
{
$this->size = '320x320';
$this->siret = $siret;
$c = Zend_Registry::get('config');
$this->path = $c->profil->path->shared.'/persist/streetview';
}
/**
*
* @param unknown $lattitude
* @param unknown $longitude
*/
public function setLocationGeo($lattitude, $longitude)
{
$this->mode = self::MODE_GPS;
$this->location = $lattitude.','.$longitude;
}
/**
*
* @param string $adresse
*/
public function setLocationTxt($adresse)
{
$this->mode = self::MODE_ADDRESS;
$this->location = $adresse;
}
/**
*
* @param int $num
*/
public function setHeading($num)
{
$this->heading = $num;
}
/**
*
* @return multitype:number
*/
public function getNumDeg()
{
$mark = array();
$deg = 360/$this->nbImage;
$i=$calc=0;
while ($calc<360) {
$mark[$i] = $calc;
$calc+=$deg;
$i++;
}
return $mark;
}
/**
* Construct the image URL
* @return string
*/
public function urlImg()
{
$url = '';
$params = array( 'size', 'location', 'fov', 'pitch', 'sensor', 'heading' );
foreach ($params as $param) {
if ( $this->{$param} !== null ) {
$url.= '&'.$param.'='. urlencode($this->{$param});
}
}
return $this->url . substr($url,1);
}
/**
*
*/
public function fileImg()
{
if ($this->mode == self::MODE_GPS) {
$filename = $this->siret.'-'.$this->heading . '.' . $this->extension;
} else if ($this->mode == self::MODE_ADDRESS) {
$filename = $this->siret.'-ADDRESS' . $this->extension;
}
return $filename;
}
/**
*
*/
public function getImg()
{
try {
$client = new Zend_Http_Client($this->url);
$client->setStream();
$response = $client->request('GET');
if ( $response->isSuccessful() ) {
if (!copy($response->getStreamName(), $this->pathImg())) {
Zend_Registry::get('firebug')->info('Erreur copie image !');
return false;
}
}
} catch (Zend_Http_Client_Exception $e) {
Zend_Registry::get('firebug')->info('HTTP Exception : '.$e->getMessage());
return false;
}
}
/**
*
* @return string
*/
public function pathImg()
{
$filename = $this->path . '/' . $this->fileImg();
if (file_exists($filename)) {
return $this->path . '/' . $filename;
}
return false;
}
}