Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CesiumJS - Distance Between Two Points

My goal is to calculate the distance between two Cesium entities in kilometers. As a bonus, I eventually want to be able to measure their distance in pixels.

I have a bunch of placemarks in KML format like this:

<Placemark>
  <name>Place</name>
  <Point><coordinates>48.655,-31.175</coordinates></Point>
  <styleUrl>#style</styleUrl>
  <ExtendedData>
     ...
  </ExtendedData>
</Placemark>

I am importing them into Cesium like so:

viewer.dataSources.add(Cesium.KmlDataSource.load('./data.kml', options)).then(function(dataSource) {
  var entities = dataSource.entities._entities._array;

I have attempted to create new Cartesian3 objects of entities I care about, but the x, y, and z values I get from the entity object are in the hundreds of thousands. The latitude and longitude from my KML are nowhere to be found in the entity objects.

If I do create Cartesian3 objects and compute the distance like so:

var distance = Cesium.Cartesian3.distance(firstPoint, secondPoint);

it returns numbers in the millions. I have evaluated the distance between multiple points this way and when I compare those values returned to the result of an online calculator which returns the actual value in kilometers, the differences in the distances are not linear (some of the distances returned by Cesium are 900 times the actual distance and some are 700 times the actual distance).

I hope that is enough to receive help. I am not sure where to start fixing this. Any help would be appreciated. Thank you.

like image 464
Jeff Diederiks Avatar asked May 09 '16 17:05

Jeff Diederiks


Video Answer


1 Answers

A couple of things are going on here. The Cesium.Cartesian3 class holds meters, so it is correct to divide by 1000 to get km, but that's not the full story. Cartesian3s are positions on a 3D globe, and if you compute a simple Cartesian.distance between two of them on opposite sides of that globe, you'll get the Cartesian linear distance, as in the length of a line that cuts through the middle of the globe to get from one to the other, rather than traveling around the surface of the globe to get to the far side.

To get the distance you actually want -- the distance of a line that follows the curvature of the surface of the Earth -- check out the answer to Cesium JS Line Length on GIS SE.

like image 128
emackey Avatar answered Sep 20 '22 23:09

emackey