🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

Conversion from lat-long to Cartesian coordinates 's result is different with real location

Started by
6 comments, last by nhungntc21 6 years, 1 month ago

Hi there,

I'm trying to put building from real lat-long in my Monogame app (4 buildings in my school, please check the image with the blue circles)

But after some convert method, it went terribly wrong (please check image)

Please  help me find out what's wrong.

My code: 


 public List<float> Gps2Cartesian(float latitude_degree, float longitude_degree, float elevation)
        {
            cartesResult = new List<float>();
            var longtitude_rad = DegreeToRadian(longitude_degree);
            var latitude_rad = DegreeToRadian(latitude_degree);

            var n = a / Math.Sqrt(1 - e2 * Math.Sin(latitude_rad) * Math.Sin(latitude_rad));
            var cartes_X = (n + elevation) * Math.Cos(latitude_rad) * Math.Cos(longtitude_rad);    //ECEF x (z in right - handed system) (m)
            var cartes_Y = (n + elevation) * Math.Cos(latitude_rad) * Math.Sin(longtitude_rad);    //ECEF y (x in right - handed system) (m)
            var cartes_Z = (n * (1 - e2) + elevation) * Math.Sin(latitude_rad);          //ECEF z (y in right-handed system) (m)

            cartesResult.Add((float)cartes_Y);
            cartesResult.Add((float)cartes_Z);
            cartesResult.Add((float)cartes_X);

            return cartesResult;
        }

 

And this is debug result:

Quote

D3      X:5730549      Y:2271896      Z:-162645005

C2      X:5730621      Y:2272063      Z:-162594705

C9      X:5730595      Y:2272005      Z:-1626123

 C1     X:5730521      Y:2272123      Z:-1626228

Thank you !

Hust.PNG

Screenshot_20180429-010149.jpg

Advertisement

Wasnt that that 0.0 angle wasnrepresented by half of image so whenever thise cartesian coord mean its in the center of image since you have 180 deg move in both horizontal directions and 90 in both vert dirs its pretty straight forward, all you have is to define the rectangle sides

I do not understand what purpose your variable n could have. (also e2 - small epsilon i guess)

It should be more simple like this:

 

var cartes_X = elevation * Math.Cos(latitude_rad) * Math.Cos(longtitude_rad); 

var cartes_Y = elevation * Math.Cos(latitude_rad) * Math.Sin(longtitude_rad);

var cartes_Z = elevation * Math.Sin(latitude_rad);

1 hour ago, JoeJ said:

I do not understand what purpose your variable n could have. (also e2 - small epsilon i guess)

It should be more simple like this:

 

var cartes_X = elevation * Math.Cos(latitude_rad) * Math.Cos(longtitude_rad); 

var cartes_Y = elevation * Math.Cos(latitude_rad) * Math.Sin(longtitude_rad);

var cartes_Z = elevation * Math.Sin(latitude_rad);

Those formulas are only valid if you are using a spherical model for Earth. Ellipsoid models do have some number called "N", but I haven't bothered to figure out the details. See this, for example: https://en.wikipedia.org/wiki/Reference_ellipsoid

The OP hasn't posted enough detail for me to be inclined to help.

Thank you for your reply, and I am so sorry that the post missed some details.

Here is all of it, please take a look:

Its declaration (Extensions.cs)


 private static double a = 6378137.0;              //WGS-84 semi-major axis
        private static  double e2 = 6.6943799901377997e-3;  //WGS-84 first eccentricity squared

        List<float> cartesResult;

        public List<float> Gps2Cartesian(float latitude_degree, float longitude_degree, float elevation)
        {
            cartesResult = new List<float>();
            var longtitude_rad = DegreeToRadian(longitude_degree);
            var latitude_rad = DegreeToRadian(latitude_degree);

            var n = a / Math.Sqrt(1 - e2 * Math.Sin(latitude_rad) * Math.Sin(latitude_rad));
            var cartes_X = (n + elevation) * Math.Cos(latitude_rad) * Math.Cos(longtitude_rad);    //ECEF x (z in right - handed system) (m)
            var cartes_Y = (n + elevation) * Math.Cos(latitude_rad) * Math.Sin(longtitude_rad);    //ECEF y (x in right - handed system) (m)
            var cartes_Z = (n * (1 - e2) + elevation) * Math.Sin(latitude_rad);          //ECEF z (y in right-handed system) (m)

            cartesResult.Add((float)cartes_Y);
            cartesResult.Add((float)cartes_Z);
            cartesResult.Add((float)cartes_X);

            return cartesResult;
        }

 

And its usage: (SceneRelated\Hust.cs)

 


 public List<float> Get_C1_Location()
        {
            var result =
            extensions.Gps2Cartesian(Defines.C1_latitude, Defines.C1_longtitude, Defines.C1_elevation);
            System.Diagnostics.Debug.Write($"11111 c1\t X:{result[0]} \t Y:{result[1]} \t Z:{result[2]}");
            return result;
        }

 

Its implementation (SceneRelated\Hust.cs)


  public Hust()
        {
            var c1 = Get_C1_Location();
            C1_location = new Vector3(c1[0], c1[1], c1[2]);
}

Here is my source on Gitlab : Source

 

Thank you for your time 

Here is there real lat-long (from openStreetMap)


  public static float C1_latitude = 21.00699f;
        public static float C1_longtitude = 105.84307f;
        public static float C1_elevation = 15f;
    

        public static float C9_latitude = 21.00586f;
        public static float C9_longtitude = 105.8419f;
        public static float C9_elevation = 12.3f;
   

        public static float D3_latitude = 21.00479f;
        public static float D3_longtitude = 105.84505f;
        public static float D3_elevation = 15f;
    

        public static float C2_latitude = 21.006425f;
        public static float C2_longtitude = 105.8402039f;
        public static float C2_elevation = 11.4f;
   

 

19 hours ago, KKTHXBYE said:

Wasnt that that 0.0 angle wasnrepresented by half of image so whenever thise cartesian coord mean its in the center of image since you have 180 deg move in both horizontal directions and 90 in both vert dirs its pretty straight forward, all you have is to define the rectangle sides

That's right, but my input is latitude, longitude and elevation (real statistics), all I wanted is to convert them to Cartesian, but it went wrong, the camera is looking at C1, so it's displayed center of the phone.

This topic is closed to new replies.

Advertisement