hunterInt - 1 year ago 104
Node.js Question

# Polar to Cartesian coordinates function not outputting the correct data

I have a flight path with lat/long and elevation and I need to convert this to cartesin X,Y,Z for cesium.js.
I am running into the wall trying to convert this because I don't seem to be getting the right results from my function.

``````var R = 6371;
function polarToCartesian(latitude, longitude, elevation){
x = (R+elevation) * math.cos(latitude) * math.cos(longitude);
y = (R+elevation) * math.cos(latitude) * math.sin(longitude);
z = (R+elevation) * math.sin(latitude);

var ar = [x,y,z];
return  ar;
}
``````

I must not either have the correct formula for polar to cartesian or I don't have the correct radius of earth. I found somewhere that my radius should be 6371 but can't seem to find that same SO question for reference.

I am partialy checking if my code is correct by manually adding up the radius of the earth + altitude of the flight path at a given location and seeing if this equals the length of my x,y,z vector.

For example: x,y,z
`(3689.2472215653725,3183.2401988117012,13306.90338789763)`

is outputted when I give my function this

``````-93.028,44.6942,7800
``````

lat,long,elevation

Could someone point me to find the right js code to accomplish this conversion?

You should be using Cesium's built-in functions for this. See `Cartesian3.fromDegrees` and `Cartesian3.fromDegreesArray`.
``````var result = Cesium.Cartesian3.fromDegrees(latitude, longitude, elevation);