Flater - 5 months ago 38

C# Question

I'm trying to merge two databases for consolidating two clients' websites. However, Client A has been using regular Lat/Lon pairs for geolocation, while Client B is using Lambert 72 (X/Y) coordinates.

I've built a script that should convert these coordinates (as I'm not sure which coordinates will be used in the final merged database, I'm trying converting them either way).

I took some snippets from here: http://zoologie.umh.ac.be/tc/algorithms.aspx

Please note that all coordinates mentioned below point to locations in Belgium.

I'm converting some coordinates to see if the calculations are correct, but the coordinates I'm getting seem to be way off. For reference, the center of Belgium is roughly (North 50.84323737103243, East 4.355735778808594), so I'd expect all coordinates to be close to these values.

I converted the Lambert 72 value (X: 151488250, Y: 170492909) to a Lat/Lon pair, but the result is: (-87.538.... , -50.724....) which is way off from the expected values.

If I convert full circle (Lambert->LatLon->Lambert and vice versa), I get the same result values as I entered, so I know my conversions are at least consistent and the conversions are perfect inversions of one another.

I tried some online converter tools as well, and they give me the same (-87.538.... , -50.724....) result.

Since multiple sources yield the same results, and my conversions are correct inversions of eachother, I'm figuring the calculations themselves are correct, but the resulting values still need to be converted/offset further?

I consider myself to be sufficient in algebra, but cartographic projections completely elude me.

Can someone please shed some light on this?

**Extra Info**

- I hope I posted this in the correct forum. I'm not really sure where to put this as this is a mix of geography, mathematics and coding/conversion...
- The mentioned Lambert coordinates (X: 151488250, Y: 170492909) point to a location in Brussels, so the Lat/Lon result should be
*very*near to (North 50.84323737103243, East 4.355735778808594). - Please find my conversion functions below:

`public static Lambert72 LatLon_To_Lambert72(LatLon latlon)`

{

var lat = latlon.Lat;

var lng = latlon.Lon;

double LongRef = 0.076042943;

//=4°21'24"983

double bLamb = 6378388 * (1 - (1 / 297));

double aCarre = Math.Pow(6378388, 2);

double eCarre = (aCarre - Math.Pow(bLamb, 2)) / aCarre;

double KLamb = 11565915.812935;

double nLamb = 0.7716421928;

double eLamb = Math.Sqrt(eCarre);

double eSur2 = eLamb / 2;

//conversion to radians

lat = (Math.PI / 180) * lat;

lng = (Math.PI / 180) * lng;

double eSinLatitude = eLamb * Math.Sin(lat);

double TanZDemi = (Math.Tan((Math.PI / 4) - (lat / 2))) * (Math.Pow(((1 + (eSinLatitude)) / (1 - (eSinLatitude))), (eSur2)));

double RLamb = KLamb * (Math.Pow((TanZDemi), nLamb));

double Teta = nLamb * (lng - LongRef);

double x = 0;

double y = 0;

x = 150000 + 0.01256 + RLamb * Math.Sin(Teta - 0.000142043);

y = 5400000 + 88.4378 - RLamb * Math.Cos(Teta - 0.000142043);

return new Lambert72(x, y);

}

public static LatLon Lambert72_To_LatLon(Lambert72 lb72)

{

double X = lb72.X;

double Y = lb72.Y;

double LongRef = 0.076042943;

//=4°21'24"983

double nLamb = 0.7716421928;

double aCarre = Math.Pow(6378388, 2);

double bLamb = 6378388 * (1 - (1 / 297));

double eCarre = (aCarre - Math.Pow(bLamb, 2)) / aCarre;

double KLamb = 11565915.812935;

double eLamb = Math.Sqrt(eCarre);

double eSur2 = eLamb / 2;

double Tan1 = (X - 150000.01256) / (5400088.4378 - Y);

double Lambda = LongRef + (1 / nLamb) * (0.000142043 + Math.Atan(Tan1));

double RLamb = Math.Sqrt(Math.Pow((X - 150000.01256), 2) + Math.Pow((5400088.4378 - Y), 2));

double TanZDemi = Math.Pow((RLamb / KLamb), (1 / nLamb));

double Lati1 = 2 * Math.Atan(TanZDemi);

double eSin = 0;

double Mult1 = 0;

double Mult2 = 0;

double Mult = 0;

double LatiN = 0;

double Diff = 0;

double lat = 0;

double lng = 0;

do {

eSin = eLamb * Math.Sin(Lati1);

Mult1 = 1 - eSin;

Mult2 = 1 + eSin;

Mult = Math.Pow((Mult1 / Mult2), (eLamb / 2));

LatiN = (Math.PI / 2) - (2 * (Math.Atan(TanZDemi * Mult)));

Diff = LatiN - Lati1;

Lati1 = LatiN;

} while (Math.Abs(Diff) > 2.77777E-08);

lat = (LatiN * 180) / Math.PI;

lng = (Lambda * 180) / Math.PI;

return new LatLon(lat, lng);

}

Answer

I am the author or the page you mention in your post. I don't know if you have resolved your problem but the Lambert coordinates you give are not correct. I think that you have to divide them by 1000. That gives x=151488.250 and y=170492.909 which are possible coordinates and corresponding to a street in... Brussels. Be careful to the choice of the datum when converting to and from lat/lng values.