Several methods of obtaining latitude and longitude by Cesium

Posted by Rederick on Wed, 10 Nov 2021 19:53:06 +0100

Several methods of obtaining latitude and longitude by Cesium

1 get latitude and longitude

This article uses the left mouse click event as an example. If necessary, please refer to other events to obtain the screen coordinate Cartesian2 object.

1.1 obtain the longitude and latitude of points on the ellipsoid (points on the ellipsoid)

let handler = new Cesium.ScreenSpaceEventHandler(viewer.scene.canvas);
handler.setInputAction(function(event) {
    let cartesian = viewer.camera.pickEllipsoid(event.position);
    let cartographic = Cesium.Cartographic.fromCartesian(cartesian);
    let lng = Cesium.Math.toDegrees(cartographic.longitude); // longitude
    let lat = Cesium.Math.toDegrees(cartographic.latitude); // latitude
    let alt = cartographic.height; // Height, ellipsoid height is always equal to 0
    let coordinate = {
        longitude: Number(lng.toFixed(6)),
        latitude: Number(lat.toFixed(6)),
        altitude: Number(alt.toFixed(2))
    };
    console.log(coordinate);
}, Cesium.ScreenSpaceEventType.LEFT_CLICK);

1.2 obtain the longitude and latitude of points on the ground surface (points on the terrain)

let handler = new Cesium.ScreenSpaceEventHandler(viewer.scene.canvas);
handler.setInputAction(function(event){
    let ray = viewer.camera.getPickRay(event.position);
    let cartesian = viewer.scene.globe.pick(ray, viewer.scene);
    let cartographic = Cesium.Cartographic.fromCartesian(cartesian);
    let lng = Cesium.Math.toDegrees(cartographic.longitude); // longitude
    let lat = Cesium.Math.toDegrees(cartographic.latitude); // latitude
    let alt = cartographic.height; // height
    let coordinate = {
        longitude: Number(lng.toFixed(6)),
        latitude: Number(lat.toFixed(6)),
        altitude: Number(alt.toFixed(2))
    };
    console.log(coordinate);
}, Cesium.ScreenSpaceEventType.LEFT_CLICK);

1.3 obtain the longitude and latitude of points in the scene (points on the model)

let handler = new Cesium.ScreenSpaceEventHandler(viewer.scene.canvas);
handler.setInputAction(function (event) {
    let cartesian = viewer.scene.pickPosition(event.position);
    let cartographic = Cesium.Cartographic.fromCartesian(cartesian);
    let lng = Cesium.Math.toDegrees(cartographic.longitude); // longitude
    let lat = Cesium.Math.toDegrees(cartographic.latitude); // latitude
    let alt = cartographic.height; // height
    let coordinate = {
        longitude: Number(lng.toFixed(6)),
        latitude: Number(lat.toFixed(6)),
        altitude: Number(alt.toFixed(2))
    };
    console.log(coordinate);
}, Cesium.ScreenSpaceEventType.LEFT_CLICK);

2. Possible cesium API s

2.1 function: camera pick ellipse (windowposition, ellipse, result)

[official interpretation]
Pick an ellipsoid or map.
[Chinese translation]
Select an ellipsoid or map. If an ellipsoid or map is selected, the points on the ellipsoid or map are returned in world coordinates. undefined if no ellipsoid or mapping is selected.
[reference]
windowPosition, type: Cartesian 2, x and y coordinates of a pixel.
Ellipsoid (optional, default: Ellipsoid.WGS84), type: ellipsoid, ellipsoid to be selected.
Result (optional), type: Cartesian3, the object on which the result is stored.
[reference]
Type: Cartesian3, ellipsoid or point on map.

2.2 function: camera getpickray (windowposition, result)

[official interpretation]
Create a ray from the camera position through the pixel at windowPosition in world coordinates.
[Chinese translation]
In world coordinates, a ray is created from the camera position through the pixels of the window position.
[reference]
windowPosition, type: Cartesian 2, x and y coordinates of a pixel.
Result (optional), type: Ray, the object on which the result is stored.
[reference]
Type: Ray, returns the Cartesian coordinates and direction of the Ray.

2.3 function: globe pick (ray, scene, result)

[official interpretation]
Find an intersection between a ray and the globe surface that was rendered. The ray must be given in world coordinates.
[Chinese translation]
Find the intersection between the ray and the surface of the rendered earth sphere. The ray must be given in world coordinates. If no intersection is found, undefined is returned.
[reference]
Ray, type: ray, ray of test intersection.
Scene, type: scene, scene object.
Result (optional), type: Cartesian, the object on which the result is stored.
[reference]
Type: Cartesian, Cartesian coordinates of intersection points.

2.4 function: scene pickposition (windowposition, result)

[official interpretation]
Returns the cartesian position reconstructed from the depth buffer and window position.
The position reconstructed from the depth buffer in 2D may be slightly different from those reconstructed in 3D and Columbus view. This is caused by the difference in the distribution of depth values of perspective and orthographic projection.
Set Scene#pickTranslucentDepth to true to include the depth of translucent primitives; otherwise, this essentially picks through translucent primitives.
[Chinese translation]
Returns the Cartesian coordinates reconstructed by the depth buffer and the window position.
The reconstruction of this position in the depth buffer of 2D view may be slightly different from that in 3D view and Columbus view (i.e. 2.5D view), which is caused by the different depth value distribution of perspective and orthophoto.
If you need to include transparent primitives, set scene #picktransparentdepth to true. Otherwise, this pick will pass through transparent primitives.
[reference]
windowPosition, type: Cartesian2, the window coordinate of the selected operation.
Result (optional), type: Cartesian3, the object on which the result is stored.
[reference]
Type: Cartesian, reconstructed Cartesian coordinates.
[throw exception]
Developer error: development exception. Selecting from depth buffer is not supported. Please check pickPositionSupported.

2.5 static function: cartographic-cesium.cartographic.from Cartesian (Cartesian, ellipsoid, result)

[official interpretation]
Creates a new Cartographic instance from a Cartesian position. The values in the resulting object will be in radians.
[Chinese translation]
Create a new map instance from Cartesian coordinates. The values in the result object are in radians. undefined if the Cartesian coordinate is at the center of the ellipsoid (that is, Cesium.Cartesian.ZERO).
[reference]
cartesian, type: cartesian 3, cartesian coordinates to be converted to map representation.
Ellipsoid (optional, default: Ellipsoid.WGS84), type: ellipsoid, the ellipsoid of the coordinate.
Result (optional), type: Cartographic, the object on which the result is stored.
[reference]
Type: Cartographic, created map instance.

Topics: gis