Three latitude and longitude to world coordinate algorithm

1. Introduction

Use Three to create a three-dimensional earth (sphere), convert the latitude and longitude into Cartesian coordinates for display on the sphere.

Second, the process

1、SphereGeometry 生成

The constructor of SphereGeometry is as follows

SphereGeometry(radius : Float, widthSegments : Integer, heightSegments : Integer, phiStart : Float, phiLength : Float, thetaStart : Float, thetaLength : Float)
radius — 球体半径,默认为1。
widthSegments — 水平分段数(沿着经线分段),最小值为3,默认值为8。
heightSegments — 垂直分段数(沿着纬线分段),最小值为2,默认值为6。
phiStart — 指定水平(经线)起始角度,默认值为0。。
phiLength — 指定水平(经线)扫描角度的大小,默认值为 Math.PI * 2。
thetaStart — 指定垂直(纬线)起始角度,默认值为0。
thetaLength — 指定垂直(纬线)扫描角度大小,默认值为 Math.PI。

Mainly look at phiStart and thetaStart, which are the horizontal and vertical starting points of the sphere, respectively. The horizontal starting point is the negative half axis of the X axis, and the vertical starting point is the positive half axis of the Y axis, as shown in the figure, which means that if the sphere is mapped, the left half of the texture is the negative half of the X axis corresponding to the starting point. Axis, the upper side of the texture corresponds to the positive semi-axis of the Y axis. After knowing this, you can map the sphere.

2. Spherical texture

The texture uses the world map of Mercator projection

After the map is pasted, it shows that the left half corresponds to the negative semi-axis of the x-axis of the sphere, as shown in the figure, here you need to pay attention to the position of the prime meridian (ie the Mercator zero-degree line)

3. Longitude and latitude to world coordinates (Cartesian coordinates)

Before calculating, you need to understand the meaning of latitude and longitude, as shown in the figure below

Starting from the meridian, turn A (50) degrees to the right to reach 50 degrees east longitude, and then use the equator as the starting point to turn B (40) degrees upwards to reach 40 degrees north latitude. After understanding how longitude and latitude are calculated, you can start to derive The formula is, set the radius of the sphere to R, R * sin (B) can calculate the Y-axis coordinate of the point. R * cos(B) Project the point to the plane formed by the ZX axis to get the point A1, A1 * COS(A) can get the Z axis coordinate, and A1 * sin(A) can get the X axis coordinate. code show as below

function getPosition(longitude, latitude, radius) {
    var lg = THREE.Math.degToRad(longitude);
    var lt = THREE.Math.degToRad(latitude);
    var temp = radius * Math.cos(lt);
    var x = temp * Math.sin(lg);
    var y = radius * Math.sin(lt);
    var z = temp * Math.cos(lg);
    return {
        x: x,
        y: y,
        z: z
    }
}

Enter the latitude and longitude coordinates (50, 40). The results are shown in the figure. Some people may have questions. The actual effect of the point position is very different from the example picture above. For this problem, you need to look back at the original The position of the meridian is calculated. The calculation of the algorithm is based on the positive semi-axis of the Z axis as the starting point. In fact, the original meridian after the mapping is on the positive semi-axis of the X axis, with a difference of 90 degrees between them, so in the calculation Before you need to add 90 to the longitude for calculation.

Input (140, 40), the output result is as shown in the figure, the current point is the correct point

Guess you like

Origin blog.csdn.net/oneKnow/article/details/106891238