Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert x, y, z to longitude, latitude, altitude in Cesium?

Using three.js library I have assembled a design(plant). That design is contains of so many smaller models which have the reference of position in (x, y, z) from origin (0,0,0). Attached the sample screenshot in the following link

Assembled Model Ref Image

Now I want to load the individual model with its own position into Cesium. When I try to load the directly converting the position (x, y, z) to (north, east, up) the result is not as expected. All the models are scattered.

The functionality which I am trying to achieve is, based on some origin (lon, lat, alt) point, I should position the model into cesium with reference of (x, y, z) relative to cesium coordinates (lon, lat, alt)

E.g.

  • Origin geo-coordinates (ori_lon, ori_lat, ori_alt) => (-106.690647, 36.806761, 0)

  • Model coordinates (m_x, m_y, m_z) => (-150.9, 126.26, 217.7)

  • Expecting Coordinates for Cesium: (ori_lon + m_x, ori_lat + m_y, ori_alt + m_z)

or some algorithm to achieve this.

I have tried with the following article to convert the (x, y, z) to the (long, lat, alt) with some origin (long, lat, alt), but no luck: (

(x, y, z) coordinates >> geo-coordinates

Advice/help to fix the issue.

like image 331
Premkumar Jayaseelan Avatar asked Feb 06 '15 04:02

Premkumar Jayaseelan


2 Answers

EDIT: Since search engines appear to be sending folks here looking for Cartesian-to-cartographic conversions, I'll supply an answer to that here.

The Cartographic.fromCartesian function is the simplest way to perform this conversion. Note that it will return a Cartographic expressed in radians, not degrees. Height is returned in meters.

var cartographic = Cesium.Cartographic.fromCartesian(cartesian);
console.log(
    'lon ' + Cesium.Math.toDegrees(cartographic.longitude) + ', ' +
    'lat ' + Cesium.Math.toDegrees(cartographic.latitude) + ', ' +
    'alt ' + cartographic.height);

Original Answer: If you read the details of the original question here, the asker was attempting to add coordinates together in LLA space, which is incorrect. My original answer here explains how to convert them both to Cartesian space and add the results there.

This can be done with Cesium.Cartesian3.fromDegrees.

var position = Cesium.Cartesian3.fromDegrees(-106.690647, 36.806761, 0);
var offset = new Cesium.Cartesian3(-150.9, 126.26, 217.7);
Cesium.Cartesian3.add(position, offset, position);

Depending on what coordinate frame offset is in, it may need a rotation to apply to the global Cartesian space. For example, if it's East-North-Up, you would use the corresponding function to create and then apply that transformation.

like image 162
emackey Avatar answered Sep 30 '22 13:09

emackey


Cartesian3 to longlat, in case somebody need

function toDegrees(cartesian3Pos) {
  let pos = Cesium.Cartographic.fromCartesian(cartesian3Pos)
  return [pos.longitude / Math.PI * 180, pos.latitude / Math.PI * 180]
}

console.log(toDegrees({
  x: -1681498.1800000381,
  y: 4576516.394998055,
  z: 4098410.649998016
})) // [110.17428132202518, 40.23966923800238]
like image 23
Josh Lin Avatar answered Sep 30 '22 14:09

Josh Lin