[Unity] Coordinate conversion latitude and longitude method (application)

[Unity] Coordinate conversion latitude and longitude method (application)

Solve the problem of conversion between latitude and longitude coordinates and unity coordinates in the map. Using the method of linear transformation can theoretically solve the problem of coordinate transformation in a small range.

I have written [Unity] coordinate conversion latitude and longitude method (principle ) before. In actual use, because the steps are relatively cumbersome, based on the previous method, I will explain the usage method in detail.

Case resource download address

1. Steps to use

  • binding script
  • Establish control origin and registration points
  • get transformed binomial
  • Coordinate conversion method

1. Binding script

After importing the resource pack, you can see the directory structure

  • The use of coordinate transformation under Examples is a demonstration demo

  • Under Transfromation is the function script

Just drag ChangeMatrixMgr under any GameObject in the scene. In the demo, it is placed under the empty object GameManager.

2. Establish control origin and registration point

(1) Establish control origin

Find the Piont under Prefab in the directory, drag it into the scene, and place it in a special and easy-to-identify position. (The control point should be placed in the middle area of ​​the map as much as possible)

Just find a website for obtaining latitude and longitude on the Internet, which is used to obtain latitude and longitude.

Longitude and latitude query positioning pick coordinate system

Select the image map and find the location of the Piont on the map

Here you can get the latitude and longitude of the point. Fill in the latitude and longitude into the LonLatPoint in the ChangePoint component under Piont, fill in the latitude for X, and fill in the longitude for Y

Drag the control origin Piont into the OriginPoint in ChangeMatrixMgr (the red box is put into the control point)

(2) Establish registration points

The establishment of registration points is the same as that of control points, and they are placed in the GruberPoints value after establishment.

Note: Create at least two registration points

3. Obtain the transformed binomial

Once the registration points and control origins are established, the transformed binomial can be created

Read the latitude and longitude coordinates and local coordinates corresponding to the registration point into an array

  List<DoubleVector2> LonLatPoints;//经纬度坐标点数组
  List<DoubleVector2> LocalPoints;//本地坐标点数组  
void ExtractionCoordinate()
    {
        LonLatPoints = new List<DoubleVector2>();
        LocalPoints = new List<DoubleVector2>();
        int length = GruberPoints.Length;
        for (int i = 0; i < length; i++)
        {
            LonLatPoints.Add(GruberPoints[i].LonLatPoint - OriginPoint.LonLatPoint);
            LocalPoints.Add(GruberPoints[i].LocalPoint - OriginPoint.LocalPoint);
        }
    }

create binomial

    public DoubleVector4 ToLonLatMatrix;//转经纬度二项式
    public DoubleVector4 ToLocalMatrix;//转本地坐标二项式
    void CreateMatrix()
    {
        ExtractionCoordinate();
        changeMatrixUtil = new ChangeMatrixUtil(LonLatPoints, LocalPoints, errorValue);
        ToLonLatMatrix = changeMatrixUtil.ToLonLatMatrix;
        ToLocalMatrix = changeMatrixUtil.ToLocalMatrix;
    }

The value of the binomial only needs to be created once and can be reused later.

The value can be copied and assigned separately, and the registration point can be removed after the value is assigned. The above two methods, ExtractionCoordinate and CreateMatrix, do not need to be called.

As long as the binomial has a value, re-running does not affect it.

4. Coordinate mutual conversion method

        /// <summary>
        /// 变换坐标
        /// </summary>
        /// <param name="Vector">要转换的坐标</param>
        /// <param name="ChangeMatrix">变换行列式</param>
        /// <param name="BZero">变换后原点坐标</param>
        /// <param name="FZero">要转换的原点坐标</param>
        /// <returns></returns>
        public static DoubleVector2 GetChangeVector(DoubleVector2 Vector, DoubleVector4 ChangeMatrix, DoubleVector2 BZero, DoubleVector2 FZero)

demo

  DoubleVector2 lonLatPoint= DoubleVector2.GetChangeVector(test.LocalPoint, ToLonLatMatrix, OriginPoint.LonLatPoint, OriginPoint.LocalPoint);

Latitude and longitude to local

The first parameter is passed in the latitude and longitude coordinates;

The second parameter is passed to the binomial converted to local coordinates;

The third parameter passes in the control origin of the local coordinates;

The fourth parameter is passed in the latitude and longitude coordinates to control the origin

Local latitude and longitude

The first parameter passes in the local coordinates;

The second parameter is passed to the binomial of the latitude and longitude coordinates;

The third parameter is passed in the latitude and longitude coordinates to control the origin;

The fourth parameter passes in the control origin of the local coordinates

2. Expansion

1. The cause of the error

The earth is an irregular sphere. Projecting a sphere onto a plane cannot be orthogonally projected. Deformation will inevitably cause a position shift. Different projection methods will produce different projected coordinate systems, such as Gaussian projection and Mercator projection . , UTM projection and other planar coordinate systems.
What we obtain may also be the latitude and longitude coordinates of the geodetic coordinate system (such as WGS_84, Xi'an 80, GCJ-02). The geodetic coordinate system is an abstract spatial coordinate system. Due to the different reference ellipsoids, the same longitude and latitude are in different geodetic coordinate systems The lower position is also different, and the gap is huge.
If we do registration based on the geodetic coordinate system, since our scene is planar and the two geodetic coordinate systems are three-dimensional, the error will increase as the range distance increases.

2. Reasons for reporting NaN calculation errors

When there are too many registration points, the position sum of the registration points is wrong, or the coordinate system of the registration points is inconsistent, which will cause the binomial to be unable to be calculated.

Solution:

  1. Reduce the number of registration points
  2. Modify the ErrorValue parameter under ChangeMatrixMgr
  3. check registration points

Guess you like

Origin blog.csdn.net/dxs1990/article/details/132366537