Skip to content

Instantly share code, notes, and snippets.

@will83
Last active February 13, 2024 15:20
Show Gist options
  • Save will83/5920606 to your computer and use it in GitHub Desktop.
Save will83/5920606 to your computer and use it in GitHub Desktop.
Fonction PHP permettant la conversion de Lambert93 à WGS84 (selon le fichier fourni par l'IGN).
<?php
function lambert93ToWgs84($x, $y){
$x = number_format($x, 10, '.', '');
$y = number_format($y, 10, '.', '');
$b6 = 6378137.0000;
$b7 = 298.257222101;
$b8 = 1/$b7;
$b9 = 2*$b8-$b8*$b8;
$b10 = sqrt($b9);
$b13 = 3.000000000;
$b14 = 700000.0000;
$b15 = 12655612.0499;
$b16 = 0.7256077650532670;
$b17 = 11754255.426096;
$delx = $x - $b14;
$dely = $y - $b15;
$gamma = atan( -($delx) / $dely );
$r = sqrt(($delx*$delx)+($dely*$dely));
$latiso = log($b17/$r)/$b16;
$sinphiit0 = tanh($latiso+$b10*atanh($b10*sin(1)));
$sinphiit1 = tanh($latiso+$b10*atanh($b10*$sinphiit0));
$sinphiit2 = tanh($latiso+$b10*atanh($b10*$sinphiit1));
$sinphiit3 = tanh($latiso+$b10*atanh($b10*$sinphiit2));
$sinphiit4 = tanh($latiso+$b10*atanh($b10*$sinphiit3));
$sinphiit5 = tanh($latiso+$b10*atanh($b10*$sinphiit4));
$sinphiit6 = tanh($latiso+$b10*atanh($b10*$sinphiit5));
$longrad = $gamma/$b16+$b13/180*pi();
$latrad = asin($sinphiit6);
$long = ($longrad/pi()*180);
$lat = ($latrad/pi()*180);
return array(
'lambert93' => array(
'x' => $x,
'y' => $y
),
'wgs84' => array(
'lat' => $lat,
'long' => $long
)
);
}
// Exemple d'utilisation :
$x = 955502.7409000024;
$y = 6225307.0799999982;
print_r(lambert93ToWgs84($x,$y));
/* Résultat en sortie :
Array
(
[lambert93] => Array
(
[x] => 955502.7409000024
[y] => 6225307.0799999982
)
[wgs84] => Array
(
[lat] => 43.081387131355
[long] => 6.1358573938246
)
)
*/
Copy link

ghost commented Jan 7, 2014

Merci pour ce code.
La fonction carre (ligne 18) ne marche pas chez moi, j'ai remplacé par
$r = sqrt(($delx_$delx)+($dely_$dely));

@ofostier
Copy link

ofostier commented Jan 8, 2014

Great !!!
Merci

j'ai rajouté ceci

function carre ($val){
return $val*$val;
}

@will83
Copy link
Author

will83 commented Jan 31, 2014

Merci pour ce retour, j'ai supprimé la fonction

@arnolanglade
Copy link

La variable $b6 n'est pas utilisée, est ce normal ?

@Grandvizir
Copy link

Merci pour ce code..

@timothylhuillier
Copy link

merci beaucoup

@atogeek
Copy link

atogeek commented Mar 21, 2017

Merci pour ce code, très utile!!!

@bonatoc
Copy link

bonatoc commented Jun 14, 2017

Merci. Un petit apport : parfois les coordonnées passées peuvent être sous forme de strings, ce qui cause une erreur sur number_format() (php 5.3).

			$x = number_format(floatval($x), 10, '.', '');
			$y = number_format(floatval($y), 10, '.', '');

@niquenen
Copy link

niquenen commented Oct 5, 2018

Bonjour, je viens d'utiliser cette fonction pour un projet et j'ai réduit sa taille, j'ai également modifié le tableau retourné.

/**
 * Convert lambert93 to Wgs84
 * @author will83 (https://gist.github.com/will83/5920606)
 * @param  mixed $x [Longitude]
 * @param  mixed $y [Latitude]
 * @return array    [Longitude and latitude in Wgs84]
 */
function lambert93ToWgs84($x, $y)
{
	$b8  = 1 / 298.257222101;
	$b10 = sqrt(2 * $b8 - $b8 * $b8);
	$b16 = 0.7256077650532670;
	$x = number_format(floatval($x), 10, '.', '') - 700000;
	$y = number_format(floatval($y), 10, '.', '') - 12655612.0499;
	$gamma = atan(-$x / $y);
	$latiso = log(11754255.426096 / sqrt(($x * $x) + ($y * $y))) / $b16;
	$sinphiit = tanh($latiso + $b10 * atanh($b10 * sin(1)));

	for ($i = 0; $i != 6 ; $i++) {
		$sinphiit = tanh($latiso + $b10 * atanh($b10 * $sinphiit));
	}
	return (array(
		'longitude' => ($gamma / $b16 + 3 / 180 * pi()) / pi() * 180,
		'latitude' => asin($sinphiit) / pi() * 180
	));
}

@Lulucmy
Copy link

Lulucmy commented Jan 6, 2023

Merci pour la fonction. Une version adaptée en Python de celle de niquenen :

# @will83 // @niquenen (https://gist.github.com/will83/5920606)

from math import *

def Lambert93toWGS84(xC, yC):
    b8 = 1 / 298.257222101
    b10 = sqrt(2 * b8 - b8 * b8)
    b16 = 0.7256077650532670
    x = float(xC.replace(',','.').replace(' ','')) - 700000
    y = float(yC.replace(',','.').replace(' ','')) - 12655612.0499
    gamma = atan((-x)/y)
    latiso = log(11754255.426096/sqrt((x * x) + (y * y))) / b16
    sinphiit = tanh(latiso + b10 * atanh(b10 * sin(1)))
    for i in iter(range(0, 5)):
        sinphiit = tanh(latiso + b10 * atanh(b10 * sinphiit))
    long = (gamma / b16 + 3 / 180 * pi) / pi * 180
    lat = asin(sinphiit) / pi * 180
    return lat, long

@corentinbettiol
Copy link

corentinbettiol commented Feb 13, 2024

Merci pour cette version @Lulucmy !

Si vous ne voulez pas importer toutes les fonctions qui sont dans le module math, vous pouvez rajouter cet import à la place du from math import * :

from math import sqrt, atan, log, tanh, atanh, sin, pi, asin

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment