Skip to content

Instantly share code, notes, and snippets.

@blemoine
Last active May 14, 2024 15:21
Show Gist options
  • Save blemoine/e6045ed93b3d90a52891 to your computer and use it in GitHub Desktop.
Save blemoine/e6045ed93b3d90a52891 to your computer and use it in GitHub Desktop.
Convert Lambert 93 to GPS Coordinates Latitude / Longitude (wgs84)
Math.tanh = Math.tanh || function(x) {
if(x === Infinity) {
return 1;
} else if(x === -Infinity) {
return -1;
} else {
return (Math.exp(x) - Math.exp(-x)) / (Math.exp(x) + Math.exp(-x));
}
};
Math.atanh = Math.atanh || function(x) {
return Math.log((1+x)/(1-x)) / 2;
};
function lambert93toWGPS(lambertE, lambertN) {
var constantes = {
GRS80E: 0.081819191042816,
LONG_0: 3,
XS: 700000,
YS: 12655612.0499,
n: 0.7256077650532670,
C: 11754255.4261
}
var delX = lambertE - constantes.XS;
var delY = lambertN - constantes.YS;
var gamma = Math.atan(-delX / delY);
var R = Math.sqrt(delX * delX + delY * delY);
var latiso = Math.log(constantes.C / R) / constantes.n;
var sinPhiit0 = Math.tanh(latiso + constantes.GRS80E * Math.atanh(constantes.GRS80E * Math.sin(1)));
var sinPhiit1 = Math.tanh(latiso + constantes.GRS80E * Math.atanh(constantes.GRS80E * sinPhiit0));
var sinPhiit2 = Math.tanh(latiso + constantes.GRS80E * Math.atanh(constantes.GRS80E * sinPhiit1));
var sinPhiit3 = Math.tanh(latiso + constantes.GRS80E * Math.atanh(constantes.GRS80E * sinPhiit2));
var sinPhiit4 = Math.tanh(latiso + constantes.GRS80E * Math.atanh(constantes.GRS80E * sinPhiit3));
var sinPhiit5 = Math.tanh(latiso + constantes.GRS80E * Math.atanh(constantes.GRS80E * sinPhiit4));
var sinPhiit6 = Math.tanh(latiso + constantes.GRS80E * Math.atanh(constantes.GRS80E * sinPhiit5));
var longRad = Math.asin(sinPhiit6);
var latRad = gamma / constantes.n + constantes.LONG_0 / 180 * Math.PI;
var longitude = latRad / Math.PI * 180;
var latitude = longRad / Math.PI * 180;
return {longitude: longitude, latitude: latitude};
}
@Thibzzz
Copy link

Thibzzz commented Apr 7, 2021

Hello I've had to make a Lambert CC Zone 49 version today in ES6 so here it is.

Thanks a lot. I grabbed the constants from your link and updated it accordingly.

// Référentiel IAG GRS 80 / Constantes Lambert-CC zone 49
const geodesicReferential = {
  GRS80E: 0.08181919112,
  LONG_0: 3, // OK
  XS: 1700000.00, // OK
  YS: 13754395.745, // OK
  n: 0.75473138518, // OK
  C: 11626445.901, // OK
};

const findGPSFromLambertCC = (lambertE, lambertN) => {
  const delX = lambertE - geodesicReferential.XS;
  const delY = lambertN - geodesicReferential.YS;
  const gamma = Math.atan(-delX / delY);
  const R = Math.sqrt(delX * delX + delY * delY);
  const latiso = Math.log(geodesicReferential.C / R) / geodesicReferential.n;
  const sinPhiit0 = Math.tanh(
    latiso +
      geodesicReferential.GRS80E *
        Math.atanh(geodesicReferential.GRS80E * Math.sin(1))
  );
  const sinPhiit1 = Math.tanh(
    latiso +
      geodesicReferential.GRS80E *
        Math.atanh(geodesicReferential.GRS80E * sinPhiit0)
  );
  const sinPhiit2 = Math.tanh(
    latiso +
      geodesicReferential.GRS80E *
        Math.atanh(geodesicReferential.GRS80E * sinPhiit1)
  );
  const sinPhiit3 = Math.tanh(
    latiso +
      geodesicReferential.GRS80E *
        Math.atanh(geodesicReferential.GRS80E * sinPhiit2)
  );
  const sinPhiit4 = Math.tanh(
    latiso +
      geodesicReferential.GRS80E *
        Math.atanh(geodesicReferential.GRS80E * sinPhiit3)
  );
  const sinPhiit5 = Math.tanh(
    latiso +
      geodesicReferential.GRS80E *
        Math.atanh(geodesicReferential.GRS80E * sinPhiit4)
  );
  const sinPhiit6 = Math.tanh(
    latiso +
      geodesicReferential.GRS80E *
        Math.atanh(geodesicReferential.GRS80E * sinPhiit5)
  );

  const longRad = Math.asin(sinPhiit6);
  const latRad =
    gamma / geodesicReferential.n +
    (geodesicReferential.LONG_0 / 180) * Math.PI;

  const longitude = (latRad / Math.PI) * 180;
  const latitude = (longRad / Math.PI) * 180;

  return { lon: longitude, lat: latitude };
  
};
export { findGPSFromLambertCC, geodesicReferential };

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