# openlayers/ol3

Merged
merged 2 commits into from Mar 18, 2014

### 2 participants

This PR adds a method to interpolate along a great circle passing though two points, which will be helpful in the construction of geodesic lines.

Note that this PR also removes the tests which use poorly defined points (e.g. -90 lat, 180 long).

 twpayne `Remove tests for spherical geometry on poorly defined points` `088a748`
src/ol/sphere/sphere.js
 @@ -140,6 +140,41 @@ ol.Sphere.prototype.haversineDistance = function(c1, c2) { /** + * Returns the point at fraction f along the segment of the great circle passing + * through c1 and c2. + * + * @param {ol.Coordinate} c1 Coordinate 1. + * @param {ol.Coordinate} c2 Coordinate 2. + * @param {number} f F.
src/ol/sphere/sphere.js
 + var sinLat1 = Math.sin(lat1); + var cosLat2 = Math.cos(lat2); + var sinLat2 = Math.sin(lat2); + var d = sinLat1 * sinLat2 + cosLat1 * cosLat2 * Math.cos(lon2 - lon1); + if (d < 1) { + d = f * Math.acos(d); + } else { + d = 0.0; + } + var y = Math.sin(lon2 - lon1) * cosLat2; + var x = cosLat1 * sinLat2 - sinLat1 * cosLat2 * Math.cos(lon2 - lon1); + var theta = Math.atan2(y, x); + var lat = Math.asin(sinLat1 * Math.cos(d) + + cosLat1 * Math.sin(d) * Math.cos(theta)); + var lon = lon1 + Math.atan2(Math.sin(theta) * Math.sin(d) * cosLat1, + Math.cos(d) - sinLat1 * Math.sin(lat));
 OpenLayers member tschaub added a note Mar 18, 2014 Minor optimization in reusing `cosD` and `sinD`. to join this conversation on GitHub. Already have an account? Sign in to comment
OpenLayers member

OpenLayers member

It also strikes me that it would be nice to have an efficient way to generate multiple interpolated points along a great circle arc without unnecessary recalculations. If we do want to support that, this could perhaps be refactored.

Thanks for the comments. I'll update the PR appropriately.

For generating geodesic geometries, @fredj and I had a chat about this earlier, and I think we have a reasonable solution. I'll open a separate PR for discussion.

 twpayne `Add ol.Sphere#interpolate` `0b5f0d0`

I've updated the PR to include the improved argument name and a couple of optimisations.

OpenLayers member

Looks great.

merged commit `1fc1cd0` into openlayers:master Mar 18, 2014

#### 1 check passed

Details default The Travis CI build passed
deleted the twpayne:sphere-interpolate branch Mar 18, 2014