Add ol.Sphere#interpolate #1873

Merged
merged 2 commits into from Mar 18, 2014

2 participants

@twpayne

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).

@tschaub tschaub commented on an outdated diff Mar 18, 2014
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.
@tschaub
OpenLayers member
tschaub added a note Mar 18, 2014

How about 'Fraction' instead?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@tschaub tschaub commented on an outdated diff Mar 18, 2014
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));
@tschaub
OpenLayers member
tschaub added a note Mar 18, 2014

Minor optimization in reusing cosD and sinD.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@tschaub
OpenLayers member

A couple minor comments above. Looks like a nice addition. Please merge.

@tschaub
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.

@twpayne

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

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

@tschaub
OpenLayers member

Looks great.

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

1 check passed

Details default The Travis CI build passed
@twpayne twpayne deleted the twpayne:sphere-interpolate branch Mar 18, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment