670 lines
33 KiB
PHP
670 lines
33 KiB
PHP
<?php
|
||
require_once 'framework/common/curl.php';
|
||
require_once __DIR__ . '/MMapFunctions.php';
|
||
|
||
class Metier_Partenaires_MMap
|
||
{
|
||
protected $iDb;
|
||
private $accesDist = true;
|
||
private $referer ='';
|
||
private $body = '';
|
||
private $header = '';
|
||
private $cookie = '';
|
||
private $codeRetour = 0;
|
||
public $codeRetourG = 0; // Code retour de l'api Google
|
||
public $codeRetourY = 0; // Code retour de l'api Yahoo
|
||
public $codeRetourM = 0; // Code retour de l'api Mappy
|
||
public $codeRetourC = 0; // Code retour de l'api Cloudmade
|
||
|
||
public $geocodeur='';
|
||
|
||
public $latitudeDec= 0; // Latitude en Décimal
|
||
public $latitudeDeg= 0; // Latitude en Dégrés
|
||
public $longitudeDec= 0; // Longitude en Décimal
|
||
public $longitudeDeg= 0; // Longitude en Dégrés
|
||
public $altitude = NULL; // Altitude en mètres
|
||
public $l93x = NULL; // Lambert 93, coordonnée X
|
||
public $l93y = NULL; // Lambert 93, coordonnée Y
|
||
|
||
public $precision = 0;
|
||
public $adresseValidee='';
|
||
public $enCache=false;
|
||
|
||
// private $apiKeyG='ABQIAAAAuKBtUyFonYJBl1fqfc78tRQvADPcxwXf3Q2QIE-M32vuSkrxiBRLUHDB_YSLeTscTDeWRKM_wILaaw';
|
||
private $apiKeyG='AIzaSyBfyIMaVwm1nn8AupBf-asC816R71LZl8g';
|
||
// private $apiKey='ABQIAAAAuKBtUyFonYJBl1fqfc78tRT4Qe_2x2fax4Crd4sBzQI-tDkl3BRm2l2feTV5Lnx0Ah-aTIlwHtglpA';
|
||
private $apiKeyY='klII0gzV34Ho2TCsN3XiXeh4No033AifxMeDXfFXj8TDCXO3WAtOLj9c74VsV45HcII-';
|
||
private $apiKeyC='ae5216123e414fcd84b8ca9a0f43a68c';
|
||
|
||
public function __construct($accesDist=true, $db = null)
|
||
{
|
||
if ( $db === null ) {
|
||
$this->iDb = new Metier_Util_Db();
|
||
} else {
|
||
$this->iDb = $db;
|
||
}
|
||
$this->accesDist=$accesDist;
|
||
}
|
||
|
||
/*
|
||
http://platform.beta.mapquest.com/geocoding/v1/address?key=YOUR_KEY_HERE&location=Lancaster,PA&callback=renderGeocode
|
||
http://platform.beta.mapquest.com/geocoding/v1/address?key=YOUR_KEY_HERE&callback=renderOptions&inFormat=kvp&outFormat=json&location=77 rue lost al lann, 29000 quimper,france
|
||
|
||
Client ID: 83955
|
||
Email Address: buzuk77@aim.com
|
||
Registry Password: tU2cA4qV
|
||
Forums User Name:
|
||
JS/AS3 Key: Fmjtd%7Clu61nuuzlu%2Cr0%3Do5-5a1sd
|
||
*/
|
||
|
||
/*
|
||
GeoCodingHandler.ashx?addr=
|
||
<wsdl:import namespace="http://dev.virtualearth.net/webservices/v1/imagery/contracts" location="http://dev.virtualearth.net/webservices/v1/metadata/imageryservice/imageryservice1.wsdl" />
|
||
API keybing = 56D6CBA671C986D3EA11B1B48F97507BE9B46999
|
||
*/
|
||
public function geoCodeAdrCpVilleM($adresse,$cp,$ville,$pays='')
|
||
{
|
||
if ($pays=='') $pays='France';
|
||
if (preg_match('/^([0-9]{1,4} )(.*)$/', $adresse, $matches)) {
|
||
$adresseVoie=trim($matches[2]);
|
||
$adresseNum=trim($matches[1]);
|
||
} else {
|
||
$adresseVoie=trim($adresse);
|
||
$adresseNum='';
|
||
}
|
||
$url='http://axe.mappy.com/services/loc/get.aspx?countryName='.urlencode($pays).'&townName='.urlencode($ville).'&wayName='.urlencode($adresseVoie).'&number='.urlencode($adresseNum).'&namedPlaceSearch=0&interactive=1&auth=maiaTest&format=json';
|
||
|
||
$page=getUrl($url, '', '', '', false, '', '', 3);
|
||
|
||
$res=json_decode($page['body']);
|
||
$result=$res->geocode->answer;
|
||
//print_r($page);
|
||
//print_r($result);die($url);
|
||
//$this->codeRetourM=
|
||
|
||
switch ($result->geocode_level->code) {
|
||
case 0: $this->precision=0; break; // No geocoding
|
||
case 1: $this->precision=1; break; // Country level geocoding
|
||
case 2: $this->precision=4; break; // Town level geocoding
|
||
case 3: $this->precision=6; break; // Way level geocoding
|
||
case 4: $this->precision=8; break; // Road element level geocoding
|
||
default: /*die('Precisions Inconnue : "'.$result->geocode_level->code.'"='.$result->geocode_level->label.PHP_EOL.$page['body']);*/ break;
|
||
}
|
||
|
||
$this->adresseValidee=@preg_replace('/ +/', ' ', strtoupper(str_replace(''', "'", trim(htmlspecialchars_decode($result->road_element->number.' '.$result->way->name,ENT_QUOTES)))));
|
||
|
||
$this->codeRetourM=@$result->global_score;
|
||
/*
|
||
$result->postal_code->postal_code
|
||
$result->subcountry->iso_code // 11
|
||
$result->subcountry->language
|
||
$result->subcountry->Ile-de-France
|
||
|
||
$result->town->language] => FRE
|
||
$result->town->name] => Nanterre
|
||
$result->town->official_code] => 92050
|
||
$result->town->postal_codes->postal_code] => 92000
|
||
*/
|
||
|
||
|
||
/*
|
||
0 Unknown location. (Since 2.59)
|
||
1 Country level accuracy. (Since 2.59)
|
||
2 Region (state, province, prefecture, etc.) level accuracy. (Since 2.59)
|
||
3 Sub-region (county, municipality, etc.) level accuracy. (Since 2.59)
|
||
4 Town (city, village) level accuracy. (Since 2.59)
|
||
5 Post code (zip code) level accuracy. (Since 2.59)
|
||
6 Street level accuracy. (Since 2.59)
|
||
7 Intersection level accuracy. (Since 2.59)
|
||
8 Address level accuracy. (Since 2.59)
|
||
*/
|
||
$this->latitudeDec=@$result->location->coordinates->y;
|
||
$this->longitudeDec=@$result->location->coordinates->x;
|
||
$this->latitudeDeg=dec2dms($this->latitudeDec);
|
||
$this->longitudeDeg=dec2dms($this->longitudeDec);
|
||
|
||
$this->geocodeur='Mappy';
|
||
|
||
//if ($this->codeRetourM>0)
|
||
return true;
|
||
|
||
return false;
|
||
}
|
||
|
||
public function geoCodeAdrCpVilleC($adresse,$cp,$ville,$pays='')
|
||
{
|
||
$this->geocodeur='Cloudmade';
|
||
$this->precision=0;
|
||
|
||
$fileName=LOG_PATH.'/geoCodeur'.$this->geocodeur.'.log';
|
||
$tmp=@explode('|',file_get_contents($fileName));
|
||
$nbQuery=@$tmp[1]*1;
|
||
|
||
if ($pays=='') $pays='France';
|
||
/*if (preg_match('/^([0-9]{1,4} )(.*)$/', $adresse, $matches)) {
|
||
$adresseVoie=trim($matches[2]);
|
||
$adresseNum=trim($matches[1]);
|
||
} else {
|
||
$adresseVoie=trim($adresse);
|
||
$adresseNum='';
|
||
}*/
|
||
$url='http://beta.geocoding.cloudmade.com/v3/'.$this->apiKeyC.'/api/geo.location.search.2?format=json&source=OSM&enc=UTF-8&limit=10&locale=fr&q='.urlencode("$adresse $cp $ville $pays");
|
||
$page=getUrl($url, '', '', '', false, '', '', 15);
|
||
$this->body=$page['body'];
|
||
$this->codeRetour=$page['code'];
|
||
$this->header=$page['header'];
|
||
|
||
$nbQuery++;
|
||
file_put_contents($fileName, $this->geocodeur.'|'.$nbQuery.'|'.$this->codeRetour."|$url|".date('Y-m-d H:i:s').'|'.$this->body.PHP_EOL);
|
||
|
||
$res=@json_decode(utf8_decode($page['body']));
|
||
if ($this->codeRetour==200 && $res->status->success==1) {
|
||
$geoCodage=$res->places[0];
|
||
$this->codeRetourC=1;
|
||
switch($geoCodage->addressType) {
|
||
case 'housenumber': $this->precision=8; break;
|
||
case 'street': $this->precision=6; break;
|
||
case 'zip': $this->precision=5; break;
|
||
case 'city': $this->precision=4; break;
|
||
case 'district': $this->precision=3; break;
|
||
case 'sight': $this->precision=0; break;
|
||
default:
|
||
//print_r($res);
|
||
//die('addressType inconnue : '.$geoCodage->addressType.PHP_EOL);
|
||
break;
|
||
}
|
||
|
||
$this->adresseValidee=@trim(strtoupper(preg_replace('/\s+/',' ',$geoCodage->houseNumber.' '.$geoCodage->street.', '.
|
||
$geoCodage->zip.' '.$geoCodage->city.', '.str_replace(',',' ',$geoCodage->country))));
|
||
|
||
$this->latitudeDec=$geoCodage->position->lat;
|
||
$this->longitudeDec=$geoCodage->position->lon;
|
||
$this->latitudeDeg=dec2dms($this->latitudeDec);
|
||
$this->longitudeDeg=dec2dms($this->longitudeDec);
|
||
|
||
/*print_r($res);
|
||
echo $this->adresseValidee.PHP_EOL;
|
||
echo 'lat='.$this->latitudeDec.PHP_EOL;
|
||
echo 'lon='.$this->longitudeDec.PHP_EOL;
|
||
echo 'precis='.$this->precision.PHP_EOL;
|
||
die($url);*/
|
||
if ($this->precision>0)
|
||
return true;
|
||
}
|
||
//print_r($page);
|
||
//print_r($res);
|
||
//die($url);
|
||
return false;
|
||
}
|
||
|
||
|
||
private function geoCodeAdrCpVilleY($address)
|
||
{
|
||
$this->geocodeur='Yahoo';
|
||
$this->precision=0;
|
||
|
||
$fileName=LOG_PATH.'/geoCodeur'.$this->geocodeur.'.log';
|
||
$tmp=@explode('|',file_get_contents($fileName));
|
||
$nbQuery=@$tmp[1]*1;
|
||
|
||
$url='http://query.yahooapis.com/v1/public/yql?q=select+*+from+geo.placefinder+where+text%3D%22'.rawurlencode($address).'%22&format=xml';
|
||
$_result = false;
|
||
$page=getUrl($url, '', '', '', false, '', '', 3);
|
||
$this->body=$page['body'];
|
||
$this->codeRetour=$page['code'];
|
||
$this->header=$page['header'];
|
||
|
||
$nbQuery++;
|
||
file_put_contents($fileName, $this->geocodeur.'|'.$nbQuery.'|'.$this->codeRetour."|$url|".date('Y-m-d H:i:s').'|'.$this->body.PHP_EOL);
|
||
|
||
if ($this->codeRetour==200) {
|
||
|
||
/*Réponse Yahoo = <?xml version="1.0" encoding="UTF-8"?>
|
||
<query xmlns:yahoo="http://www.yahooapis.com/v1/base.rng" yahoo:count="1" yahoo:created="2013-09-25T13:52:43Z" yahoo:lang="en-US"><results><Result><quality>71</quality>
|
||
<latitude>45.782864</latitude><longitude>4.828002</longitude><offsetlat>45.782864</offsetlat><offsetlon>4.828002</offsetlon><radius>400</radius>
|
||
<name/><line1>Rue Jacques Louis-Henon</line1><line2>69004 Lyon</line2><line3/><line4>France</line4><house/><street>Rue Jacques Louis-Henon</street><xstreet/>
|
||
<unittype/><unit/><postal>69004</postal><neighborhood>4E Arrondissement</neighborhood><city>Lyon</city><county>Rhône</county><state>Rhone-Alpes</state>
|
||
<country>France</country><countrycode>FR</countrycode><statecode/><countycode/><uzip>69004</uzip><hash/><woeid>20068131</woeid><woetype>11</woetype></Result></results>
|
||
</query><!-- total: 90 -->
|
||
<!-- engine8.yql.bf1.yahoo.com -->
|
||
|
||
<query xmlns:yahoo="http://www.yahooapis.com/v1/base.rng" yahoo:count="1" yahoo:created="2014-05-20T18:13:30Z" yahoo:lang="en-US"><results><Result><quality>87</quality>
|
||
<addressMatchType>POINT_ADDRESS</addressMatchType>
|
||
<latitude>48.281769</latitude><longitude>4.09041</longitude><offsetlat>48.281731</offsetlat><offsetlon>4.09047</offsetlon><radius>400</radius>
|
||
<name/><line1>38B, Rue Maurice Romagon</line1><line2>10800 Saint-Julien-les-Villas</line2><line3/><line4>France</line4><house>38B</house><street>Rue Maurice Romagon</street><xstreet/>
|
||
<unittype/><unit/><postal>10800</postal><neighborhood/><city>Saint-Julien-les-Villas</city><county>Aube</county><state>Champagne-Ardenne</state><country>France</country><countrycode>FR</countrycode><statecode/><countycode>10</countycode><uzip>10800</uzip><hash>D49A3CF6AD6DEB1C</hash><woeid>12722975</woeid><woetype>11</woetype></Result></results></query><!-- total: 49 -->
|
||
<!-- engine4.yql.bf1.yahoo.com -->
|
||
|
||
*/
|
||
|
||
//echo "Yahoo :'$address'".PHP_EOL.$this->body.PHP_EOL;
|
||
if (preg_match('! yahoo:count="0" !U', $this->body, $_match)) {
|
||
$this->codeRetourY='ZERO_RESULTS';
|
||
//echo $this->codeRetourY.PHP_EOL;
|
||
return false;
|
||
} elseif (preg_match('!<quality>(.*)</quality>(?:.*)<latitude>(.*)</latitude><longitude>(.*)</longitude>!U', $this->body, $_match)) {
|
||
if (preg_match('!<quality>(.*)</quality><addressMatchType>(.*)</addressMatchType><latitude>(.*)</latitude><longitude>(.*)</longitude>!U', $this->body, $_match2)) {
|
||
$quality=$_match2[1];
|
||
$qualityTxt=$_match2[2];
|
||
$this->codeRetourY='OK';
|
||
if ($quality>87 || $qualityTxt=='POINT_ADDRESS') $this->precision=9; // POI or Coordinate
|
||
elseif ($quality>82 || $qualityTxt=='INTERPOLATED') $this->precision=8; // Address (N° compris dans une Tranche d'adresse)
|
||
elseif ($quality>75) $this->precision=-7; // Intersection
|
||
elseif ($quality>70) $this->precision=-6; // Street match
|
||
elseif ($quality>40) $this->precision=-5; // Zip Code
|
||
elseif ($quality>30) $this->precision=-4; // Town
|
||
elseif ($quality>20) $this->precision=-3; // Sous/Région
|
||
elseif ($quality>10) $this->precision=-2; // Region/state
|
||
elseif ($quality> 8) $this->precision=-1; // Country
|
||
else { $this->precision=0; // Not an address
|
||
$this->codeRetourY='ZERO_RESULTS';
|
||
}
|
||
} else {
|
||
$quality=$_match[1];
|
||
$this->codeRetourY='OK';
|
||
if ($quality>87) $this->precision=9; // POI or Coordinate
|
||
elseif ($quality>82) $this->precision=8; // Address
|
||
elseif ($quality>75) $this->precision=7; // Intersection
|
||
elseif ($quality>70) $this->precision=6; // Street match
|
||
elseif ($quality>40) $this->precision=5; // Zip Code
|
||
elseif ($quality>30) $this->precision=4; // Town
|
||
elseif ($quality>20) $this->precision=3; // Sous/Région
|
||
elseif ($quality>10) $this->precision=2; // Region/state
|
||
elseif ($quality> 8) $this->precision=1; // Country
|
||
else { $this->precision=0; // Not an address
|
||
$this->codeRetourY='ZERO_RESULTS';
|
||
}
|
||
}
|
||
$this->latitudeDec=$_match[2];
|
||
$this->longitudeDec=$_match[3];
|
||
$this->latitudeDeg=dec2dms($this->latitudeDec);
|
||
$this->longitudeDeg=dec2dms($this->longitudeDec);
|
||
if (preg_match('!<line1>(.*)</line1>(?:|.*)<line2>(.*)</line2>(?:|.*)<line4>(.*)</line4>!Ui', $this->body, $_match)) {
|
||
$adrValide=utf8_decode($_match[1].', '.$_match[2].', '.$_match[3]);
|
||
$this->adresseValidee=strtoupper(str_replace(''', "'", htmlspecialchars_decode($adrValide,ENT_QUOTES)));
|
||
$this->codeRetourY='OK';
|
||
} elseif (preg_match('!<line2>(.*)</line2>(?:|.*)<line4>(.*)</line4>!Ui', $this->body, $_match)) {
|
||
$adrValide=utf8_decode($_match[2].', '.$_match[3]);
|
||
$this->adresseValidee=strtoupper(str_replace(''', "'", htmlspecialchars_decode($adrValide,ENT_QUOTES)));
|
||
$this->codeRetourY='OK';
|
||
} else
|
||
$this->codeRetourY='INVALID_REQUEST';
|
||
} else
|
||
//die('Code Retour Yahoo 200 mais :'.$this->body.PHP_EOL);
|
||
|
||
if ($this->latitudeDec==0 && ($this->longitudeDec==0 || $this->precision==0)) {
|
||
//die('Réponse Yahoo = '.$this->body.PHP_EOL);
|
||
return false;
|
||
}
|
||
return true;//array("lat"=>$lat,"lng"=>$lng,"address"=>$address);
|
||
} else
|
||
$this->codeRetourY='INVALID_REQUEST';
|
||
|
||
return false;
|
||
}
|
||
|
||
private function geoCodeAdrCpVilleG($adresse,$cp,$ville,$pays, $proxy='')
|
||
{
|
||
$this->geocodeur='Google';
|
||
$this->precision=0;
|
||
|
||
$fileName=LOG_PATH.'/geoCodeur'.$this->geocodeur.'.log';
|
||
$tmp=@explode('|',file_get_contents($fileName));
|
||
$nbQuery=@$tmp[1]*1;
|
||
|
||
//$url='http://maps.googleapis.com/maps/api/geocode/xml?address='.urlencode($adresse.', '.$cp.' '. $ville.', '.$pays).'&sensor=false';
|
||
$url='https://maps.googleapis.com/maps/api/geocode/xml?address='.urlencode($adresse.', '.$cp.' '. $ville.', '.$pays).'&sensor=false&key='.$this->apiKeyG;
|
||
|
||
$page=getUrl($url, '', '', $this->referer, false, 'maps.googleapis.com', $proxy,3);
|
||
//print_r($page);
|
||
//die($url);
|
||
//getUrl($url, $strCookies='', $postData='', $referer='', $debug=false, $host='', $proxy='', $timeout=0) {
|
||
$this->body=$page['body'];
|
||
$this->codeRetour=$page['code'];
|
||
$this->header=$page['header'];
|
||
|
||
$nbQuery++;
|
||
file_put_contents($fileName, $this->geocodeur.'|'.$nbQuery.'|'.$this->codeRetour."|$url|".date('Y-m-d H:i:s').'|'.$this->body.PHP_EOL);
|
||
|
||
if ($this->codeRetour==200) {
|
||
$this->codeRetourG=@getTextInHtml($this->body, '<GeocodeResponse>', '<status>', '</status>');
|
||
$location_type=@getTextInHtml($this->body, '<geometry>', '<location_type>', '</location_type>');
|
||
switch($location_type) {
|
||
case 'ROOFTOP': // Precise geocode for which we have location information accurate down to street address precision.
|
||
$this->precision=8;
|
||
break;
|
||
case 'RANGE_INTERPOLATED': // Approximation (usually on a road) interpolated between two precise points (such as intersections). Interpolated results are generally returned when rooftop geocodes are unavailable for a street address.
|
||
$this->precision=7;
|
||
break;
|
||
case 'GEOMETRIC_CENTER': // Geometric center of a result such as a polyline (for example, a street) or polygon (region).
|
||
$this->precision=6;
|
||
break;
|
||
case 'APPROXIMATE': // indicates that the returned result is approximate.
|
||
$this->precision=4;
|
||
break;
|
||
default:
|
||
if ($this->codeRetourG=='ZERO_RESULTS') // Indicates that the geocode was successful but returned no results.
|
||
$this->precision=0; // This may occur if the geocode was passed a non-existent address or a latlng in a remote location.
|
||
break;
|
||
}
|
||
|
||
$this->adresseValidee=strtoupper(str_replace(''', "'", htmlspecialchars_decode(utf8_decode(@getTextInHtml($this->body, '<result', '<formatted_address>', '</formatted_address>')),ENT_QUOTES)));
|
||
if (preg_match('!<geometry>(?:.*)<location>(?:.*)<lat>(.*)</lat>(?:.*)<lng>(.*)</lng>(?:.*)</location>!Uis', $this->body, $matches2)) {
|
||
$this->latitudeDec=@$matches2[1];
|
||
$this->longitudeDec=@$matches2[2];
|
||
$this->latitudeDeg=dec2dms($this->latitudeDec);
|
||
$this->longitudeDeg=dec2dms($this->longitudeDec);
|
||
}
|
||
/*
|
||
200 G_GEO_SUCCESS No errors occurred; the address was successfully parsed and its geocode has been returneds->geocodeur='Google';
|
||
|
||
400 G_GEO_BAD_REQUEST A directions request could not be successfully parsed.
|
||
500 G_GEO_SERVER_ERROR A geocoding or directions request could not be successfully processed, yet the exact reason for the failure is not known.
|
||
601 G_GEO_MISSING_QUERY The HTTP q parameter was either missing or had no value. For geocoding requests, this means that an empty address was
|
||
specified as input. For directions requests, this means that no query was specified in the input.
|
||
602 G_GEO_UNKNOWN_ADDRESS No corresponding geographic location could be found for the specified address. This may be due to the fact that the address
|
||
is relatively new, or it may be incorrect.
|
||
603 G_GEO_UNAVAILABLE_ADDRESS The geocode for the given address or the route for the given directions query cannot be returned due to legal or contractual
|
||
reasons.
|
||
604 G_GEO_UNKNOWN_DIRECTIONS The GDirections object could not compute directions between the points mentioned in the query. This is usually because
|
||
there is no route available between the two points, or because we do not have data for routing in that region.
|
||
610 G_GEO_BAD_KEY The given key is either invalid or does not match the domain for which it was given.
|
||
620 G_GEO_TOO_MANY_QUERIES The given key has gone over the requests limit in the 24 hour period.
|
||
|
||
<GeocodeResponse><status>OK</status><result><type>street_address</type><formatted_address>2 Route du Cure, 92410 Ville-d'Avray, France</formatted_address><address_component><long_name>2</long_name><short_name>2</short_name><type>street_number</type></address_component><address_component><long_name>Route du Cure</long_name><short_name>Route du Cure</short_name><type>route</type></address_component><address_component><long_name>Ville-d'Avray</long_name><short_name>Ville-d'Avray</short_name><type>locality</type><type>political</type></address_component><address_component><long_name>Hauts-de-Seine</long_name><short_name>92</short_name><type>administrative_area_level_2</type><type>political</type></address_component><address_component><long_name>Île-de-France</long_name><short_name>IDF</short_name><type>administrative_area_level_1</type><type>political</type></address_component><address_component><long_name>France</long_name><short_name>FR</short_name><type>country</type><type>political</type></address_component><address_component><long_name>92410</long_name><short_name>92410</short_name><type>postal_code</type></address_component><geometry><location><lat>48.8252802</lat><lng>2.1821693</lng></location>
|
||
|
||
/*
|
||
"OK" indicates that no errors occurred; the address was successfully parsed and at least one geocode was returned.
|
||
|
||
"OVER_QUERY_LIMIT" indicates that you are over your quota.
|
||
"REQUEST_DENIED" indicates that your request was denied, generally because of lack of a sensor parameter.
|
||
"" generally indicates that the query (address or latlng) is missing.
|
||
UNKNOWN_ERROR indicates that the request could not be processed due to a server error. The request may succeed if you try again.
|
||
*/
|
||
if ($this->codeRetourG=='OK') {
|
||
return true;
|
||
}
|
||
elseif ($this->codeRetourG=='ZERO_RESULTS' || // http://maps.googleapis.com/maps/api/geocode/xml?address=0044+RUE+JACQUES+-+LOUIS+HENON%2C+69004+LYON+4EME%2C+France&sensor=false
|
||
$this->codeRetourG=='INVALID_REQUEST' || // http://maps.googleapis.com/maps/api/geocode/xml?address=0028+B+RUE+MGR+DE+BEAUMONT%2C+97400+SAINT+DENIS%2C+La+R%E9union&sensor=false
|
||
$this->codeRetourG=='OVER_QUERY_LIMIT') {
|
||
return false;
|
||
} else {
|
||
return false;
|
||
}
|
||
}
|
||
return false;
|
||
}
|
||
|
||
/** Géocodage d'une adresse
|
||
0 Unknown location. (Since 2.59)
|
||
1 Country level accuracy. (Since 2.59)
|
||
2 Region (state, province, prefecture, etc.) level accuracy. (Since 2.59)
|
||
3 Sub-region (county, municipality, etc.) level accuracy. (Since 2.59)
|
||
4 Town (city, village) level accuracy. (Since 2.59)
|
||
5 Post code (zip code) level accuracy. (Since 2.59)
|
||
6 Street level accuracy. (Since 2.59)
|
||
7 Intersection level accuracy. (Since 2.59)
|
||
8 Address level accuracy. (Since 2.59)
|
||
9 Address level accuracy +++
|
||
**/
|
||
public function geoCodeAdresse($adrNum, $adrIndRep, $adrTypeVoieCourt, $adrTypeVoieLong, $adrLibVoie, $cp, $ville='', $pays='France', $codeRivoli='')
|
||
{
|
||
$tabRep = array();
|
||
$adrNum = str_pad($adrNum, 4, 0, STR_PAD_LEFT);
|
||
$adresse = addslashes(trim(preg_replace('/ +/',' ', "$adrNum $adrIndRep $adrTypeVoieLong $adrLibVoie")));
|
||
$adresseCourt = addslashes(trim(preg_replace('/ +/',' ', "$adrNum $adrIndRep $adrTypeVoieCourt $adrLibVoie")));
|
||
$ville = addslashes($ville);
|
||
$ret = $this->iDb->select('jo.zonageXY', 'lat, lon, l93_x, l93_y, alt, precis, adresseValidee, IF(dateUpdate>dateInsert, dateUpdate, dateInsert) AS dateInsert ',
|
||
"(address='$adresse' OR address='$adresseCourt') AND adr_cp='$cp' AND adr_ville='$ville' ORDER BY dateInsert DESC LIMIT 1",
|
||
false, MYSQL_ASSOC);
|
||
|
||
//Existe dans la base
|
||
if (count($ret)>0) {
|
||
$zonage=$ret[0];
|
||
$this->precision=$zonage['precis']*1;
|
||
$this->adresseValidee=strtoupper($zonage['adresseValidee']);
|
||
$this->latitudeDec=$zonage['lat']*1;
|
||
$this->longitudeDec=$zonage['lon']*1;
|
||
$this->l93x=$zonage['l93_x'];
|
||
$this->l93y=$zonage['l93_y'];
|
||
$this->altitude=$zonage['alt'];
|
||
if ($this->altitude==NULL && $this->precision>5) {
|
||
if ($this->accesDist) {
|
||
$alt=$this->getAltitude($this->latitudeDec,$this->longitudeDec);
|
||
$this->altitude=$alt['alt'];
|
||
}
|
||
}
|
||
if ($this->l93x==NULL && $this->precision>5) {
|
||
$tmp=geos2lambert93($this->latitudeDec,$this->longitudeDec);
|
||
$this->l93x=$tmp['x_93'];
|
||
$this->l93y=$tmp['y_93'];
|
||
}
|
||
if ($this->altitude<>NULL || $this->l93x<>NULL) {
|
||
$tabUpdate=array(
|
||
'alt'=>$this->altitude,
|
||
'l93_x'=>$this->l93x,
|
||
'l93_y'=>$this->l93y
|
||
);
|
||
$this->iDb->update('jo.zonageXY', $tabUpdate, "address='$adresse' AND adr_cp='$cp' AND adr_ville='$ville'");
|
||
}
|
||
|
||
$this->latitudeDeg = dec2dms($this->latitudeDec);
|
||
$this->longitudeDeg = dec2dms($this->longitudeDec);
|
||
$this->enCache=true;
|
||
$ligne='En base : '.print_r($zonage, true);
|
||
}
|
||
|
||
//Acces Distant
|
||
elseif ($this->accesDist)
|
||
{
|
||
$this->enCache=false;
|
||
$retG=$retY=$retC=$retM=false;
|
||
if (substr($codeRivoli,0,3)*1==971) $pays='Guadeloupe';
|
||
elseif (substr($codeRivoli,0,3)*1==972) $pays='Martinique';
|
||
elseif (substr($codeRivoli,0,3)*1==973) $pays='Guyane';
|
||
elseif (substr($codeRivoli,0,3)*1==974) $pays='La Réunion';
|
||
elseif (substr($codeRivoli,0,3)*1==975) $pays='Saint-Pierre-et-Miquelon';
|
||
elseif (substr($codeRivoli,0,3)*1==976) $pays='Mayotte';
|
||
elseif (substr($codeRivoli,0,3)*1==977) $pays='Saint-Martin';
|
||
elseif (substr($codeRivoli,0,3)*1==978) $pays='Saint-Barthélémy';
|
||
elseif (substr($codeRivoli,0,3)*1==985) $pays='Saint-Pierre-et-Miquelon';//98501 à 98502 St Pierre et Miquelon (anciens codes non compatibles 97501 à 97502)
|
||
elseif (substr($codeRivoli,0,3)*1==986) $pays='Wallis-et-Futuna'; // 98601 à 98617 Mayotte (anciens codes non compatibles : 98501 à 98517)
|
||
elseif (substr($codeRivoli,0,3)*1==987) $pays='Polynésie française'; // 98711 à 98758,98799 Polynésie française (sans changement)
|
||
elseif (substr($codeRivoli,0,3)*1==988) $pays='Nouvelle Calédonie'; // 98801 à 98832 Nouvelle Calédonie (sans changement)
|
||
elseif (substr($codeRivoli,0,3)*1==989) $pays='Wallis-et-Futuna'; // 98911 à 98913 Wallis et Futuna (anciens codes non compatibles : 98611 à 98613)
|
||
/*elseif (substr($codeRivoli,0,2)*1>96 && substr($codeRivoli,0,2)*1<99)
|
||
die("Code Rivoli non géré : '$codeRivoli'".PHP_EOL);*/
|
||
|
||
$retG=$this->geoCodeAdrCpVilleG($adresse, $cp, $ville, $pays);
|
||
if ($this->codeRetourG=='OVER_QUERY_LIMIT' || $this->precision==0) {
|
||
$retY=$this->geoCodeAdrCpVilleY("$adresse, $cp $ville, $pays");
|
||
if ($this->codeRetourY=='OVER_QUERY_LIMIT' || $this->codeRetourY<>'OK' || $this->precision==0)
|
||
$retC=$this->geoCodeAdrCpVilleC($adresse, $cp, $ville, $pays);
|
||
// $retM=$this->geoCodeAdrCpVilleM($adresse, $cp, $ville, $pays);
|
||
}
|
||
|
||
// On récupère l'altitude
|
||
//echo ('Précision : '. $this->precision.PHP_EOL);
|
||
if ($this->precision>5 && $this->latitudeDec<>0 && $this->longitudeDec<>0) {
|
||
$alt=$this->getAltitude($this->latitudeDec,$this->longitudeDec);
|
||
$this->altitude=$alt['alt'];
|
||
}
|
||
$tmp=geos2lambert93($this->latitudeDec,$this->longitudeDec);
|
||
$this->l93x=$tmp['x_93'];
|
||
$this->l93y=$tmp['y_93'];
|
||
|
||
//echo ('$retG='.$retG.PHP_EOL.'$retY='.$retY.PHP_EOL.'$retC='.$retC.PHP_EOL.'$retM='.$retM.PHP_EOL);
|
||
|
||
if ($retG || $retY || $retM || $retC) {
|
||
$adresse=stripslashes(strtoupper(trim(preg_replace('/ +/',' ', "$adrNum $adrIndRep $adrTypeVoieLong $adrLibVoie"))));
|
||
$ville=stripslashes($ville);
|
||
$tabInsert=array( 'address'=>$adresse,
|
||
'adr_cp'=>$cp,
|
||
'adr_ville'=>$ville,
|
||
'adrNum'=>$adrNum,
|
||
'adrIndRep'=>$adrIndRep,
|
||
'adrTypeVoie'=>$adrTypeVoieCourt,
|
||
'adrLibVoie'=>$adrLibVoie,
|
||
'rivoli'=>$codeRivoli,
|
||
'adresseValidee'=>$this->adresseValidee,
|
||
'lat'=>$this->latitudeDec,
|
||
'lon'=>$this->longitudeDec,
|
||
'l93_x'=>$this->l93x,
|
||
'l93_y'=>$this->l93y,
|
||
'alt'=>$this->altitude,
|
||
'precis'=>$this->precision,
|
||
'source'=>$this->geocodeur,
|
||
'dateInsert'=>DATETIME,
|
||
);
|
||
/** Insertion de l'adresse que si elle est possible
|
||
**/
|
||
//echo ('Avant Insert '.$this->latitudeDec.PHP_EOL.$this->longitudeDec.PHP_EOL);
|
||
if ($this->latitudeDec<>0 && $this->longitudeDec<>0 && $this->precision>0) {
|
||
// print_r($tabInsert);
|
||
//echo ($this->latitudeDec.PHP_EOL.$this->longitudeDec.PHP_EOL);
|
||
$this->iDb->insert('jo.zonageXY', $tabInsert);
|
||
/*if (mysql_errno()>0) echo mysql_error().die(PHP_EOL);*/
|
||
} else {
|
||
$this->precision=$this->latitudeDec=$this->longitudeDec=0;
|
||
$this->adresseValidee='';
|
||
$this->latitudeDeg=$this->longitudeDeg='';
|
||
$ligne="Erreur Geocodage : $adrNum, $adrIndRep, $adrTypeVoieCourt, $adrTypeVoieLong, $adrLibVoie, $cp, $ville, $pays, $codeRivoli, ".$this->body;
|
||
//die('Latitude et longitude à zero :'.$this->latitudeDec.PHP_EOL.$this->longitudeDec.PHP_EOL);
|
||
}
|
||
//echo ('Après Insert : '.$this->latitudeDec.PHP_EOL.$this->longitudeDec.PHP_EOL);
|
||
$ligne='Accès Google : '.print_r($tabInsert, true);
|
||
}
|
||
else {
|
||
$this->precision=$this->latitudeDec=$this->longitudeDec=0;
|
||
$this->adresseValidee='';
|
||
$this->latitudeDeg=$this->longitudeDeg='';
|
||
$ligne="Erreur : $adrNum, $adrIndRep, $adrTypeVoieCourt, $adrTypeVoieLong, $adrLibVoie, $cp, $ville, $pays, $codeRivoli, ".$this->body;
|
||
}
|
||
}
|
||
}
|
||
|
||
/** Retourne la distance en kilomètres entre 2 coordonnées GPS à la surface de la terre
|
||
**
|
||
** @param double $latA Latitude du point A en décimal
|
||
** @param double $lonA Longitude du point A en décimal
|
||
** @param double $latB Latitude du point B en décimal
|
||
** @param double $lonB Longitude du point B en décimal
|
||
** @param string $sphere Type de sphère
|
||
** @return unknown
|
||
**/
|
||
function distance($latA=0, $lonA=0, $latB=0, $lonB=0, $sphere='GRS80')
|
||
{
|
||
switch ($sphere) {
|
||
case 'HAYFORD': $R=6378.388; // Demi grand axe ou Rayon de la sphère International Hayford 1909 en Kms
|
||
$f=1/297; // Aplatissement
|
||
break;
|
||
case 'PICARD': $R=6371.598; // Demi grand axe ou Rayon de la sphère Picard en Kms
|
||
break;
|
||
case 'GRS80':
|
||
default: $R=6378.137; // Demi grand axe ou Rayon de la sphère GRS80 en Kms
|
||
$f=1/298.257222101;
|
||
break;
|
||
}
|
||
$a=pi()/180;
|
||
$e=$latA*$a;
|
||
$f=$lonA*$a;
|
||
$g=$latB*$a;
|
||
$h=$lonB*$a;
|
||
|
||
$j=acos(cos($e)*cos($g)*cos($f)*cos($h) + cos($e)*sin($f)*cos($g)*sin($h) + sin($e)*sin($g));
|
||
|
||
return round($R*$j,3); // div par 1.852 ==> résultat en miles nautiques
|
||
}
|
||
|
||
/** Retourne la distance en mètres entre 2 points sur une surface plane
|
||
** @param double $x1 Coordonnée X du point 1 en décimal
|
||
** @param double $y1 Coordonnée Y du point 1 en décimal
|
||
** @param double $x2 Coordonnée X du point 2 en décimal
|
||
** @param double $y2 Coordonnée Y du point 2 en décimal
|
||
** @return double
|
||
**/
|
||
function distanceLambert($x1,$y1,$x2,$y2)
|
||
{
|
||
return sqrt ( pow($x1-$x2,2) + (pow($y1-$y2,2) ) );
|
||
}
|
||
|
||
/** Retourne l'altitude en mètres d'un point GPS. Shuttle Radar Topography Mission (SRTM) elevation data (resolution 90mx90m)
|
||
** @url http://ws.geonames.org/export/web-services.html
|
||
** @param double $tabLatLon Latitude du point ou Tableau de Points avec latitudes longitudes array('lat'=>x,'lon'=>y)
|
||
** @param double $lon Longitude ou false si tableau
|
||
** @return double
|
||
**/
|
||
function getAltitude($tabLatLon,$lon=false)
|
||
{
|
||
if ($lon) {
|
||
/*
|
||
Elevation - SRTM3
|
||
Shuttle Radar Topography Mission (SRTM) elevation data. SRTM consisted of a specially modified radar system that flew onboard the Space Shuttle Endeavour during an 11-day mission in February of 2000. The dataset covers land areas between 60 degrees north and 56 degrees south.
|
||
This web service is using SRTM3 data with data points located every 3-arc-second (approximately 90 meters) on a latitude/longitude grid. Documentation : Nasa
|
||
|
||
Webservice Type : REST
|
||
Url : api.geonames.org/srtm3?
|
||
Parameters : lat,lng;
|
||
sample area: ca 90m x 90m Result : a single number giving the elevation in meters according to srtm3, ocean areas have been masked as "no data" and have been assigned a value of -32768
|
||
Example http://api.geonames.org/srtm3?lat=50.01&lng=10.2&username=demo
|
||
|
||
This service is also available in XML and JSON format :api.geonames.org/srtm3XML?lat=50.01&lng=10.2&username=demo api.geonames.org/srtm3JSON?lat=50.01&lng=10.2&username=demo
|
||
|
||
The text version of the service also accepts a list of lat/lng for the parameters 'lats' and 'lngs'. On the free server the number of points per call is limited to 20, for the premium service the limit is 2000:
|
||
|
||
[0] => the daily limit of 30000 credits 78.31.45.206 has been exceeded. Please throttle your requests or use the commercial service
|
||
[1] => daily
|
||
[2] => 30000
|
||
[3] => 78.31.45.206
|
||
|
||
*/
|
||
$url="http://ws.geonames.org/srtm3?lat=$tabLatLon&lng=$lon&username=scores_et_decisions&password=bj10sx";
|
||
$page=getUrl($url, '', '', '', false, '', '', 3);
|
||
$alt=trim($page['body']);
|
||
if (preg_match('/the (.*) limit of (.*) credits(.*) has been exceeded\. Please throttle your requests or use the commercial service/Uis', $alt, $matches)) {
|
||
print_r($matches);
|
||
$alt=NULL;
|
||
} elseif ($page['code']<>200 || $alt==-32768) {
|
||
if ($page['code']<>200) echo '==========================>'. $page['code']. '<=================================='.PHP_EOL;
|
||
$alt=NULL;
|
||
}
|
||
return array('lat'=>$tabLatLon,'lon'=>$lon,'alt'=>$alt);
|
||
} else {
|
||
$LatLon='';
|
||
$Lats='';
|
||
$Lons='';
|
||
foreach ($tabLatLon as $i=>$latlon) {
|
||
$lat=$latlon['lat'];
|
||
$lon2=$latlon['lon'];
|
||
if ($i>0) {
|
||
$LatLon.="%0D%0A";
|
||
$Lats.=',';
|
||
$Lons.=',';
|
||
}
|
||
$LatLon.="$lat%2C$lon2";
|
||
$Lats.=$lat;
|
||
$Lons.=$lon;
|
||
}
|
||
}
|
||
|
||
$url='http://www.fredorando.fr/Pages/altitude.php';
|
||
$post=array('LatLon'=>$LatLon,'qui'=>'');
|
||
$page=getUrl($url, '', $post, '', false, '', '', 3);
|
||
$body=$page['body'];
|
||
$tabRet=false;
|
||
if (preg_match('/<BODY>\s+IP\:(?:.*)<br\/>\s+url\:(?:.*)<br\/>(?:.*)<br\/>(?:.*)<br\/>(.*)<br\/>(?:.*)<a href="http/Uis', $body, $matches)) {
|
||
$tabRet=array();
|
||
$tabTmp=explode('<br />', $matches[1]);
|
||
foreach($tabTmp as $i=>$ret) {
|
||
$ret2=explode(',',trim($ret));
|
||
$alt=$ret2[2];
|
||
if ($alt==-32768) $alt=NULL;
|
||
$tabRet[$i]=@array('lat'=>$ret2[0],'lon'=>$ret2[1],'alt'=>$alt);
|
||
}
|
||
unset($tabRet[$i]);
|
||
}
|
||
if ($lon) return $tabRet[0];
|
||
return $tabRet;
|
||
}
|
||
}
|