797 lines
31 KiB
PHP
Raw Normal View History

2010-11-04 11:05:01 +00:00
<?
if ( !function_exists('htmlspecialchars_decode') )
{
function htmlspecialchars_decode($text)
{
return strtr($text, array_flip(get_html_translation_table(HTML_SPECIALCHARS)));
}
}
include_once(FWK_PATH.'common/curl.php');
function supprDecimales($dec) {
if ($dec>0 )
return floor($dec);
else
return ceil($dec);
}
function dec2dms($dec) {
$d = supprDecimales($dec);
$m = supprDecimales(($dec - $d) * 60);
$s = abs(round(((($dec - $d) * 60) - $m) * 60));
$m = abs($m);
return $d.'<27>'.$m."'".$s.'"';
2010-11-04 11:05:01 +00:00
}
function ALG0001($phi,$e) {
$temp = ( 1 - ( $e * sin( $phi ) ) ) / ( 1 + ( $e * sin( $phi ) ) );
$L = log ( tan ( (pi()/4) + ($phi/2) ) * pow ($temp, ($e/2) ));
return $L;
}
function ALG0002($L,$e,$epsilon) {
$phi[0] = 2 * atan(exp($L)) - (pi()/2);
$i=0;
do
{
$i++;
$temp = ( 1 + ( $e * sin( $phi[$i-1] ) ) ) / ( 1 - ( $e * sin( $phi[$i-1] ) ) );
$phi[$i] = 2 * atan ( pow ($temp, ($e/2)) * exp ($L) ) - pi()/2;
}
while (abs($phi[$i] - $phi[$i - 1]) >= $epsilon);
return $phi[$i];
}
function ALG0004($X,$Y,$n,$c,$Xs,$Ys,$lambdac,$e,$epsilon) {
$R = sqrt( pow(($X - $Xs),2) + pow(($Y - $Ys),2) );
$gamma = atan(($X - $Xs)/($Ys - $Y));
$lambda = $lambdac + ($gamma / $n);
$L = (-1 / $n) * log(abs($R/$c));
$phi = ALG0002($L,$e,$epsilon);
$coords['lambda']=$lambda;
$coords['phi']=$phi;
return $coords;
}
function ALG0009($lambda,$phi,$he,$a,$e) {
$N = ALG0021($phi,$a,$e);
$X = ($N + $he) * cos($phi) * cos($lambda);
$Y = ($N + $he) * cos($phi) * sin($lambda);
$Z = ($N * (1 - $e*$e) + $he) * sin ($phi);
$coords['X']=$X;
$coords['Y']=$Y;
$coords['Z']=$Z;
return $coords;
}
function ALG0012($X,$Y,$Z,$a,$e,$epsilon) {
$lambda = atan ($Y/$X);
$P = sqrt($X*$X + $Y*$Y);
$phi[0] = atan ($Z/ ($P * (1 - ( ($a*$e*$e)/sqrt($X*$X + $Y*$Y + $Z*$Z) ) ) ) );
$i = 0;
do
{
$i++;
$temp = pow((1 - ( $a * $e*$e * cos($phi[$i - 1] )/( $P * sqrt(1 - $e*$e * sin($phi[$i - 1])*sin($phi[$i - 1]))))),-1);
$phi[$i] = atan( $temp * $Z / $P );
}
while (abs($phi[$i] - $phi[$i - 1]) >= $epsilon);
$phix = $phi[$i];
$he = ($P/cos($phix)) - ($a/sqrt(1 - $e*$e * sin($phix)*sin($phix)));
$coords['lambda']=$lambda;
$coords['phi']=$phix;
$coords['he']=$he;
return $coords;
}
function ALG0013($Tx,$Ty,$Tz,$D,$Rx,$Ry,$Rz,$U) {
$V['X'] = $Tx + $U['X'] * (1 + $D) + $U['Z'] * $Ry - $U['Y'] * $Rz;
$V['Y'] = $Ty + $U['Y'] * (1 + $D) + $U['X'] * $Rz - $U['Z'] * $Rx;
$V['Z'] = $Tz + $U['Z'] * (1 + $D) + $U['Y'] * $Rx - $U['X'] * $Ry;
return $V;
}
function ALG0019($lambda0,$phi0,$k0,$X0,$Y0,$a,$e) {
$lambdac = $lambda0;
$n = sin($phi0);
$C = $k0 * ALG0021($phi0,$a,$e) * tan (pi()/2 - $phi0) * exp ( $n * ALG0001($phi0,$e) );
$Xs = $X0;
$Ys = $Y0 + $k0 * ALG0021($phi0,$a,$e) * tan (pi()/2 - $phi0) ;
$tab ['e'] = $e;
$tab ['n'] = $n;
$tab ['C'] = $C;
$tab ['lambdac'] = $lambdac;
$tab ['Xs'] = $Xs;
$tab ['Ys'] = $Ys;
return $tab;
}
function ALG0021($phi,$a,$e) {
$N = $a/sqrt( 1 - $e * $e * sin($phi) * sin($phi) );
return $N;
}
function ALG0054($lambda0,$phi0,$X0,$Y0,$phi1,$phi2,$a,$e) {
$lambdac = $lambda0;
$n = ( (log( (ALG0021($phi2,$a,$e)*cos($phi2))/(ALG0021($phi1,$a,$e)*cos($phi1)) ))/(ALG0001($phi1,$e) - ALG0001($phi2,$e) ));
$C = ((ALG0021($phi1,$a,$e)* cos($phi1))/$n) * exp($n * ALG0001($phi1,$e));
if ($phi0 == (pi()/2))
{
$Xs = $X0;
$Ys = $Y0;
}
else
{
echo ('coucou');
$Xs = $X0;
$Ys = $Y0 + $C * exp(-1 * $n * ALG0001($phi0,$e));
}
$tab ['e'] = $e;
$tab ['n'] = $n;
$tab ['C'] = $C;
$tab ['lambdac'] = $lambdac;
$tab ['Xs'] = $Xs;
$tab ['Ys'] = $Ys;
return $tab;
}
function Lambert2WGS84($X,$Y,$orig='LIIe') {
$epsilon = 0.00000000001;
switch ($orig)
{
case 'LII' :
$n = 0.7289686274;
$c = 11745793.39;
$Xs = 600000;
$Ys = 6199695.768;
$lambdac = 0.04079234433; // pour greenwich
$e = 0.08248325676; //(premi<6D>re excentricit<69> de l ellipso<73>de Clarke 1880 fran<61>ais)
2010-11-04 11:05:01 +00:00
$he = 100;
$a = 6378249.2;
$Tx = -168;
$Ty = -60;
$Tz = +320;
$D = 0;
$Rx = $Ry = $Rz = 0;
break;
case 'L93' :
$n = 0.7256077650;
$c = 11745255.426;
$Xs = 700000;
$Ys = 12655612.050;
$lambdac = 0.04079234433; // pour greenwich
$e = 0.08248325676; //(premi<6D>re excentricit<69> de l ellipso<73>de Clarke 1880 fran<61>ais)
2010-11-04 11:05:01 +00:00
$he = 100;
$a = 6378249.2;
$Tx = -168;
$Ty = -60;
$Tz = +320;
$D = 0;
$Rx = $Ry = $Rz = 0;
break;
case 'LIIe' :
default:
$n = 0.7289686274;
$c = 11745793.39;
$Xs = 600000;
$Ys = 8199695.768;
$lambdac = 0.04079234433; // pour greenwich
$e = 0.08248325676; //(premi<6D>re excentricit<69> de l ellipso<73>de Clarke 1880 fran<61>ais)
2010-11-04 11:05:01 +00:00
$he = 100;
$a = 6378249.2;
$Tx = -168;
$Ty = -60;
$Tz = +320;
$D = 0;
$Rx = $Ry = $Rz = 0;
break;
}
$coords = ALG0004($X,$Y,$n,$c,$Xs,$Ys,$lambdac,$e,$epsilon);
$coords = ALG0009($coords['lambda'],$coords['phi'],$he,$a,$e);
$coords = ALG0013($Tx,$Ty,$Tz,$D,$Rx,$Ry,$Rz,$coords);
$a = 6378137.0; // ellipso<73>des WGS84
2010-11-04 11:05:01 +00:00
$f = 1/298.257223563;
$b = $a*(1-$f);
$e = sqrt(($a*$a - $b*$b)/($a*$a));
$X = $coords['X'];
$Y = $coords['Y'];
$Z = $coords['Z'];
$coords = ALG0012($X,$Y,$Z,$a,$e,$epsilon);
$xy['long'] = rad2deg($coords['lambda']);
$xy['lat'] = rad2deg($coords['phi']);
return $xy;
}
/**
* Function to convert geographic coordinates to "lambert 2 etendue" coordinates
* from: http://www.forumsig.org/showthread.php?p=64050#post64050
**/
function geos2lambert($latitude,$longitude) {
//0)degres-minutes-secondes + orientation (d,m,s,o) en radian
$lambda_w = $longitude * pi()/180 ;
$phi_w = $latitude * pi()/180 ;
//1) coordonn<6E>es g<>ographiques WGS84 (phi_w,lambda_w) en coordonn<6E>es cart<72>siennes WGS84 (X_w,Y_w,Z_w)
2010-11-04 11:05:01 +00:00
$a_w = 6378137.0;
$b_w = 6356752.314;
$e2_w = ($a_w*$a_w-$b_w*$b_w)/($a_w*$a_w);
//et on a la grande normale de l'ellipsoide WGS84
$N = $a_w/sqrt(1-$e2_w*pow(sin($phi_w),2));
//ainsi on obtient
$X_w = $N * cos($phi_w) * cos($lambda_w);
$Y_w = $N * cos($phi_w) * sin($lambda_w);
$Z_w = $N * (1-$e2_w) * sin($phi_w);
//Ref.: http://www.ign.fr/telechargement/MPr...RCE/NTG_80.pdf and http://de.wikipedia.org/wiki/WGS84
//2) coordonn<6E>es cart<72>siennes WGS84 (X_w,Y_w,Z_w) en coordonn<6E>es cart<72>siennes NTF (X_n,Y_n,Z_n)
2010-11-04 11:05:01 +00:00
$dX = 168.0;
$dY = 60.0;
$dZ = -320.0;
$X_n = $X_w + $dX;
$Y_n = $Y_w + $dY;
$Z_n = $Z_w + $dZ;
//ref.: http://support.esrifrance.fr/Documents/Generalites/Projections/Generalites/Generalites.htm#2
//3) coordonn<6E>es cart<72>siennes NTF (X_n,Y_n,Z_n) en coordonn<6E>es g<>ographiques NTF (phi_n,lambda_n)
2010-11-04 11:05:01 +00:00
$a_n = 6378249.2;
$b_n = 6356515.0;
$e2_n = ($a_n*$a_n-$b_n*$b_n)/($a_n*$a_n);
//on d<>finit une tol<6F>rance de convergence
2010-11-04 11:05:01 +00:00
$epsilon = pow(10,-10);
//puis on amorce une boucle de calcul
$p0=atan($Z_n/sqrt($X_n*$X_n+$Y_n*$Y_n)*(1-($a_n*$e2_n)/(sqrt($X_n*$X_n+$Y_n*$Y_n+$Z_n*$Z_n))));
$p1=atan(($Z_n/sqrt($X_n*$X_n+$Y_n*$Y_n))/(1-($a_n*$e2_n*cos($p0))/(sqrt(($X_n*$X_n+$Y_n*$Y_n)*(1-$e2_n*pow(sin($p0),2))))));
while(!(abs($p1-$p0)<$epsilon)){
$p0 = $p1;
$p1 = atan(($Z_n/sqrt($X_n*$X_n+$Y_n*$Y_n))/(1-($a_n*$e2_n*cos($p0))/(sqrt(($X_n*$X_n+$Y_n*$Y_n)*(1-$e2_n*pow(sin($p0),2))))));
}
$phi_n = $p1;
$lambda_n = atan($Y_n/$X_n);
//4) coordonn<6E>es g<>ographiques NTF (phi_n,lambda_n) en coordonn<6E>es projet<65>es en Lambert II <20>tendu (X_l2e, Y_l2e)
2010-11-04 11:05:01 +00:00
$n = 0.7289686274;
$c = 11745793.39;
$Xs = 600000.0;
$Ys = 8199695.768;
$e_n = sqrt($e2_n);
$lambda0 = 0.04079234433198; //correspond <20> la longitude en radian de Paris (2<>20'14.025" E) par rapport <20> Greenwich
//puis on calcule la latitude isom<6F>trique
2010-11-04 11:05:01 +00:00
$L = log(tan(pi()/4 + $phi_n/2) * pow(((1-$e_n*sin($phi_n))/(1+$e_n*sin($phi_n))),($e_n/2)));
//enfin on projette
$X_l2e = $Xs + $c*exp((-$n*$L))*sin($n*($lambda_n-$lambda0));
$Y_l2e = $Ys - $c*exp((-$n*$L))*cos($n*($lambda_n-$lambda0));
return array("x_l2e"=>$X_l2e,"y_l2e"=>$Y_l2e);
}
/** Conversion WGS84 en Lambert93
** NTG_71.pdf
** http://www.ign.fr/affiche_rubrique.asp?rbr_id=1700&lng_id=FR
**/
function geos2lambert93($latitude,$longitude) {
// Syst<73>me WGS84
2010-11-04 11:05:01 +00:00
$a=6378137; // demi grand axe de l'ellipsoide (m)
$e=0.08181919106; // premi<6D>re excentricit<69> de l'ellipsoide
2010-11-04 11:05:01 +00:00
// Param<61>tres de projections
$l0=$lc=deg2rad(3); // longitude de r<>f<EFBFBD>rence
2010-11-04 11:05:01 +00:00
$phi0=deg2rad(46.5); // latitude d'origine en radian
$phi1=deg2rad(44); // 1er parallele automécoïque
$phi2=deg2rad(49); // 2eme parallele automécoïque
2010-11-04 11:05:01 +00:00
$x0=700000; //coordonn<6E>es <20> l'origine
$y0=6600000; //coordonn<6E>es <20> l'origine
2010-11-04 11:05:01 +00:00
// coordonn<6E>es du point <20> traduire
2010-11-04 11:05:01 +00:00
$phi=deg2rad($latitude);
$l=deg2rad($longitude);
// calcul des grandes normales
$gN1=$a/sqrt(1-$e*$e*sin($phi1)*sin($phi1));
$gN2=$a/sqrt(1-$e*$e*sin($phi2)*sin($phi2));
//calculs de slatitudes isom<6F>triques
2010-11-04 11:05:01 +00:00
$gl1=log(tan(pi()/4+$phi1/2)*pow((1-$e*sin($phi1))/(1+$e*sin($phi1)),$e/2));
$gl2=log(tan(pi()/4+$phi2/2)*pow((1-$e*sin($phi2))/(1+$e*sin($phi2)),$e/2));
$gl0=log(tan(pi()/4+$phi0/2)*pow((1-$e*sin($phi0))/(1+$e*sin($phi0)),$e/2));
$gl=log(tan(pi()/4+$phi/2)*pow((1-$e*sin($phi))/(1+$e*sin($phi)),$e/2));
//calcul de l'exposant de la projection
$n=(log(($gN2*cos($phi2))/($gN1*cos($phi1))))/($gl1-$gl2);//ok
//calcul de la constante de projection
$c=(($gN1*cos($phi1))/$n)*exp($n*$gl1);//ok
//calcul des coordonn<6E>es
2010-11-04 11:05:01 +00:00
$ys=$y0+$c*exp(-1*$n*$gl0);
//calcul des coordonn<6E>es lambert
2010-11-04 11:05:01 +00:00
$x93=$x0+$c*exp(-1*$n*$gl)*sin($n*($l-$lc));
$y93=$ys-$c*exp(-1*$n*$gl)*cos($n*($l-$lc));
return array("x_93"=>$x93,"y_93"=>$y93);
}
//http://www.ign.fr/telechargement/MPro/geodesie/CIRCE/transfo.pdf
//http://www.ign.fr/telechargement/MPro/geodesie/CIRCE/NTG_80.pdf
//http://www.ign.fr/telechargement/MPro/geodesie/CIRCE/NTG_71.pdf
/**
print_r(Lambert2WGS84(591647.56,2426659.65));
print_r(geos2lambert(48.838245134184 ,2.2227849815878));
**/
class MMap {
private $accesDist = true;
private $referer ='';
private $body = '';
private $header = '';
private $cookie = '';
private $codeRetour = 0;
private $codeRetourG = 0; // Code retour de l'api Google
private $codeRetourY = 0; // Code retour de l'api Yahoo
private $codeRetourM = 0; // Code retour de l'api Mappy
public $geocodeur='';
public $latitudeDec= 0; // Latitude en D<>cimal
public $latitudeDeg= 0; // Latitude en D<>gr<67>s
public $longitudeDec= 0; // Longitude en D<>cimal
public $longitudeDeg= 0; // Longitude en D<>gr<67>s
2010-11-04 11:05:01 +00:00
public $precision = 0;
public $adresseValidee='';
public $enCache=false;
private $apiKeyG='ABQIAAAAuKBtUyFonYJBl1fqfc78tRQvADPcxwXf3Q2QIE-M32vuSkrxiBRLUHDB_YSLeTscTDeWRKM_wILaaw';
// private $apiKey='ABQIAAAAuKBtUyFonYJBl1fqfc78tRT4Qe_2x2fax4Crd4sBzQI-tDkl3BRm2l2feTV5Lnx0Ah-aTIlwHtglpA';
private $apiKeyY='klII0gzV34Ho2TCsN3XiXeh4No033AifxMeDXfFXj8TDCXO3WAtOLj9c74VsV45HcII-';
function __construct($accesDist=true) {// $adresse, $cp, $ville, $pays='France') {
$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($result);die();
//$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.EOL.$page['body']); break;
}
$this->adresseValidee=@preg_replace('/ +/', ' ', strtoupper(str_replace('&apos;', "'", 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;
}
private function geoCodeAdrCpVilleY($address) {
$_url = 'http://api.local.yahoo.com/MapsService/V1/geocode';
$_url .= sprintf('?appid=%s&location=%s',$this->apiKeyY,rawurlencode($address));
$_result = false;
$page=getUrl($_url, '', '', '', false, '', '', 3);
$_result=$page['body'];
// die("URL='$_url'".EOL.print_r($page));
$try=1;
while ($page['code']!=200 && $page['code']!=403) {
$page=getUrl($_url, '', '', '', false, '', '', 3);
$_result=$page['body'];
echo 'Yahoo '.$page['code']." : $address... Attente...".EOL;
$try++;
if ($try>=4) break;
sleep(1);
}
if ($page['code']==400 && preg_match('/<Message>unable to parse location<\/Message>/', $_result)) {
$this->latitudeDec=$this->longitudeDec=0;
$this->latitudeDeg=$this->longitudeDeg=0;
$this->geocodeur='Yahoo';
$this->precision=0;
return false;
} elseif ($page['code']==403 && preg_match('/<Message>limit exceeded<\/Message>/', $_result)) {
$this->latitudeDec=$this->longitudeDec=0;
$this->latitudeDeg=$this->longitudeDeg=0;
$this->geocodeur='Yahoo';
$this->precision=0;
$this->codeRetourY=620; // On adopte les codes Google
} elseif ($page['code']==408) { // Connexion impossible au site du partenaire
$this->latitudeDec=$this->longitudeDec=0;
$this->latitudeDeg=$this->longitudeDeg=0;
$this->geocodeur='Yahoo';
$this->precision=0;
$this->codeRetourY=500; // On adopte les codes Google
} elseif ($page['code']==200) {
preg_match('!<Latitude>(.*)</Latitude><Longitude>(.*)</Longitude>!U', $_result, $_match);
$lng = $_match[2];
$lat = $_match[1];
preg_match('/warning="(.*)"/Ui', $_result, $_match);
/*
1 Country level accuracy. (Since 2.59)
2 Region (state, province, prefecture, etc.) level accuracy. (Since 2.59)
7 Intersection level accuracy. (Since 2.59)
*/
$this->precision=0; // Unknown location
switch(@$_match[1]) {
case "The exact location could not be found. Here is a nearby county.": $this->precision=3; break; // Sub-region
case "The city could not be found. Here is the state/province.": $this->precision=3; break; // Sub-region
case "The exact location could not be found. Here is a nearby neighorhood.": $this->precision=4; break; // Town level
case "The exact location could not be found. Here is the center of the ZIP code.":
case "The street could not be found. Here is the center of the city.": $this->precision=5; break; // Post code (zip code)
case "The street number could not be found. Here is a nearby location.":
case "The Street name might have been changed": $this->precision=6; break; // Street level accuracy
case '': $this->precision=8; break; // Address level
default:
if (preg_match('/The exact location could not be found, here is the closest match/', @$_match[1]))
$this->precision=6;
else
die('Precisions Inconnue : "'.$_match[1].'"'.EOL.$_result);
break;
}
preg_match('!<Address>(.*)</Address>(?:|.*)<City>(.*)</City>(?:|.*)<State>(.*)</State>!Ui', $_result, $_match);
$adrValide=utf8_decode($_match[1].', '.$_match[2].', '.$_match[3]);
$this->adresseValidee=strtoupper(str_replace('&apos;', "'", htmlspecialchars_decode($adrValide,ENT_QUOTES)));
$this->latitudeDec=$lat;
$this->longitudeDec=$lng;
$this->latitudeDeg=dec2dms($this->latitudeDec);
$this->longitudeDeg=dec2dms($this->longitudeDec);
$this->geocodeur='Yahoo';
if ($lat==0 && $lng==0 || $this->precision==0) die('R<>ponse Yahoo = '.$_result.EOL);
2010-11-04 11:05:01 +00:00
return true;//array("lat"=>$lat,"lng"=>$lng,"address"=>$address);
}
else {die("URL='$_url'".EOL.print_r($page));
return false;
}
}
private function geoCodeAdrCpVilleG($adresse,$cp,$ville,$pays, $proxy='') {
$url='http://maps.google.com/maps/geo?q='.urlencode($adresse.', '.$cp.' '. $ville.', '.$pays).'&output=xml&key='.$this->apiKeyG;
$page=getUrl($url, '', '', $this->referer, false, 'maps.google.com', $proxy,3);
//getUrl($url, $strCookies='', $postData='', $referer='', $debug=false, $host='', $proxy='', $timeout=0) {
$this->body=$page['body'];
$this->codeRetour=$page['code'];
$this->header=$page['header'];
// REVERSE GEOCODING
// http://maps.google.com/maps/geo?oe=utf-8&ll=48.808955,2.34227&output=xml&key=ABQIAAAAuKBtUyFonYJBl1fqfc78tRQvADPcxwXf3Q2QIE-M32vuSkrxiBRLUHDB_YSLeTscTDeWRKM_wILaaw
//die($this->body=$page['body']);
//{"name":"3 rue vi<76>te, 75017 paris, france","Status":{"code":200,"request":"geocode"},"Placemark":[{"id":"p1","address":"3, Rue Vi<56>te, 75017 17<31>me Arrondissement, Paris, France","AddressDetails":{"Country":{"CountryNameCode":"FR","AdministrativeArea":{"AdministrativeAreaName":"Ile-de-France","SubAdministrativeArea":{"SubAdministrativeAreaName":"Paris","Locality":{"LocalityName":"Paris","DependentLocality":{"DependentLocalityName":"17<31>me Arrondissement","Thoroughfare":{"ThoroughfareName":"3, Rue Vi<56>te"},"PostalCode":{"PostalCodeNumber":"75017"}}}}}},"Accuracy": 8},"Point":{"coordinates":[2.306174,48.883705,0]}}]}
/*<?xml version="1.0" encoding="UTF-8"?><kml xmlns="http://earth.google.com/kml/2.0"><Response><name>3 rue vi<76>te, 75017 paris, france</name><Status><code>200</code><request>geocode</request></Status><Placemark id="p1"><address>3, Rue Vi<56>te, 75017 17<31>me Arrondissement, Paris, France</address><AddressDetails Accuracy="8" xmlns="urn:oasis:names:tc:ciq:xsdschema:xAL:2.0"><Country><CountryNameCode>FR</CountryNameCode><AdministrativeArea><AdministrativeAreaName>Ile-de-France</AdministrativeAreaName><SubAdministrativeArea><SubAdministrativeAreaName>Paris</SubAdministrativeAreaName><Locality><LocalityName>Paris</LocalityName><DependentLocality><DependentLocalityName>17<31>me Arrondissement</DependentLocalityName><Thoroughfare><ThoroughfareName>3, Rue Vi<56>te</ThoroughfareName></Thoroughfare><PostalCode><PostalCodeNumber>75017</PostalCodeNumber></PostalCode></DependentLocality></Locality></SubAdministrativeArea></AdministrativeArea></Country></AddressDetails><Point><coordinates>2.306174,48.883705,0</coordinates></Point></Placemark></Response></kml>*/
2010-11-04 11:05:01 +00:00
/*
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->codeRetourG=@getTextInHtml($this->body, '<Status>', '<code>', '/code>');
$this->precision=@getTextInHtml($this->body, '<AddressDetails Accuracy', '="', '" ');
$this->adresseValidee=strtoupper(str_replace('&apos;', "'", htmlspecialchars_decode(@getTextInHtml($this->body, '<Placemark', '<address>', '</address>'),ENT_QUOTES)));
$strTmp=@getTextInHtml($this->body, '<Point><coordinates>', '<coordinates>', '</coordinates>');
$tabTmp=explode(',', $strTmp);
$this->latitudeDec=@$tabTmp[1];
$this->longitudeDec=$tabTmp[0];
$this->latitudeDeg=dec2dms($this->latitudeDec);
$this->longitudeDeg=dec2dms($this->longitudeDec);
$this->geocodeur='Google';
/*
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.
*/
if ($this->codeRetourG==200)
return true;
return false;
}
function geoCodeAdresse($adrNum, $adrIndRep, $adrTypeVoieCourt, $adrTypeVoieLong, $adrLibVoie, $cp, $ville='', $pays='France', $codeRivoli='') {
$iDb=new WDB();
$tabRep=array();
$adresse=addslashes(trim(preg_replace('/ +/',' ', "$adrNum $adrIndRep $adrTypeVoieLong $adrLibVoie")));
$ville=addslashes($ville);
$ret=$iDb->select( 'zonageXY', 'lat, lon, precis, adresseValidee, dateInsert', "address='$adresse' AND adr_cp='$cp' AND adr_ville='$ville'",false, MYSQL_ASSOC);
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->latitudeDeg=dec2dms($this->latitudeDec);
$this->longitudeDeg=dec2dms($this->longitudeDec);
$this->enCache=true;
$ligne='En base : '.print_r($zonage, true);
} else {
$this->enCache=false;
$retM=$retY=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';
2010-11-04 11:05:01 +00:00
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<74>l<EFBFBD>my';
2010-11-04 11:05:01 +00:00
elseif (substr($codeRivoli,0,3)*1==986) $pays='Wallis-et-Futuna';
elseif (substr($codeRivoli,0,3)*1==987) $pays='Polyn<79>sie fran<61>aise';
elseif (substr($codeRivoli,0,3)*1==988) $pays='Nouvelle Cal<61>donie';
2010-11-04 11:05:01 +00:00
elseif (substr($codeRivoli,0,2)*1>96 && substr($codeRivoli,0,2)*1<99)
die("Code Rivoli non g<>r<EFBFBD> : '$codeRivoli'".EOL);
2010-11-04 11:05:01 +00:00
$retG=$this->geoCodeAdrCpVilleG($adresse, $cp, $ville, $pays);
if ($this->codeRetourG==620 || $this->precision==0)
if ($this->codeRetourG==620)
$retY=$this->geoCodeAdrCpVilleY("$adresse, $cp $ville");
if ($this->codeRetourY==620 || $this->precision==0)
$retM=$this->geoCodeAdrCpVilleM($adresse, $cp, $ville, $pays);
if ($retG || $retY || $retM) {
$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,
'precis'=>$this->precision,
'source'=>$this->geocodeur,
'dateInsert'=>DATETIME,
);
/** Insertion de l'adresse que si elle est possible
**/
if ($this->latitudeDec<>0 && $this->longitudeDec<>0)
$iDb->insert('zonageXY', $tabInsert);
$ligne='Acc<63>s Google : '.print_r($tabInsert, true);
2010-11-04 11:05:01 +00:00
}
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;
}
}
/*$fp=fopen('/var/www/log/accesGMap.log', 'a');
fwrite($fp,$ligne.EOL);
fclose($fp);*/
}
/* if (latnorth == 'S') latdir='-'
if (lonwest == 'W') longdir='-'
latdec=Math.round(lat1)+lat2/60
londec=Math.round(long1)+long2/60
gmdatalat=latdir+latdec.toFixed(6)
gmdatalon=longdir+londec.toFixed(6)
//GM_log( 'Latdec:'+gmdatalat+' LongDec:'+gmdatalon)
gmx=gmdatalat*Math.pow(10,6)
gmy=gmdatalon*Math.pow(10,6)
if (gmx < 0) gmx=gmx+Math.pow(2,32)*/
/** Retourne la distance en kilom<EFBFBD>tres entre 2 points <EFBFBD> la surface de la terre
**<EFBFBD>Calcul effectu<EFBFBD> avec la sph<EFBFBD>re <EFBFBD><EFBFBD>GRS80<EFBFBD><EFBFBD> de rayon R = 6378,187 km
** Autre sph<EFBFBD>re possible : <EFBFBD><EFBFBD>Picard<EFBFBD><EFBFBD> de rayon R = 6371,598 km
2010-11-04 11:05:01 +00:00
**
** @param double $latA Latitude du point A en d<EFBFBD>cimal
** @param double $lonA Longitude du point A en d<EFBFBD>cimal
** @param double $latB Latitude du point B en d<EFBFBD>cimal
** @param double $lonB Longitude du point B en d<EFBFBD>cimal
2010-11-04 11:05:01 +00:00
** @return unknown
**/
function distance($latA=0, $lonA=0, $latB=0, $lonB=0) {
//s(AB) = arc cos (sinjA sinjB + cos jA cosjB cosdl)
// avec dl = lB - lA
$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(6378.187*$j,3); // div par 1.852 ==> r<>sultat en miles nautiques
2010-11-04 11:05:01 +00:00
}
/*function distance(lat_a, lon_a, lat_b, lon_b)
{
a = Math.PI / 180;
lat1 = lat_a * a;
lat2 = lat_b * a;
lon1 = lon_a * a;
lon2 = lon_b * a;
t1 = Math.sin(lat1) * Math.sin(lat2);
t2 = Math.cos(lat1) * Math.cos(lat2);
t3 = Math.cos(lon1 - lon2);
t4 = t2 * t3;
t5 = t1 + t4;
rad_dist = Math.atan(-t5/Math.sqrt(-t5 * t5 +1)) + 2 * Math.atan(1);
return (rad_dist * 3437.74677 * 1.1508) * 1.6093470878864446;
}
*/
}
if ( !function_exists('json_decode') ){
function json_decode($content, $assoc=false){
require_once 'Services/JSON.php';
if ( $assoc ){
$json = new Services_JSON(SERVICES_JSON_LOOSE_TYPE);
} else {
$json = new Services_JSON;
}
return $json->decode($content);
}
}
if ( !function_exists('json_encode') ){
function json_encode($content){
require_once 'Services/JSON.php';
$json = new Services_JSON;
return $json->encode($content);
}
}
?>