Skip to content

Commit

Permalink
Reinstate d3.geo.greatCircle.polyline.
Browse files Browse the repository at this point in the history
For backwards compatibility.
  • Loading branch information
jasondavies committed Sep 12, 2011
1 parent 2a6c2db commit 15dda93
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 61 deletions.
52 changes: 26 additions & 26 deletions d3.geo.js
Original file line number Diff line number Diff line change
Expand Up @@ -622,15 +622,22 @@ function d3_geo_boundsPolygon(o, f) {
}
// From http://williams.best.vwh.net/avform.htm#Intermediate
d3.geo.greatCircle = function() {
var coordinates = Object,
var source = d3_geo_greatCircleSource,
target = d3_geo_greatCircleTarget,
coordinates = Object, // for use with polyline
precision = 1,
radius = d3_geo_earthRadius;
// TODO: breakAtDateLine?

function greatCircle(d, i) {
return d3_geo_greatCirclePath(coordinates.call(this, d, i), precision);
return d3_geo_greatCirclePath([
source.call(this, d, i), target.call(this, d, i)], precision);
}

greatCircle.polyline = function(d, i) {
return d3_geo_greatCirclePath(coordinates.call(this, d, i), precision);
};

greatCircle.coordinates = function(x) {
if (!arguments.length) return coordinates;
coordinates = x;
Expand All @@ -651,29 +658,16 @@ d3.geo.greatCircle = function() {

// Haversine formula for great-circle distance.
greatCircle.distance = function(d, i) {
d = coordinates.call(this, d, i);
if (d.length < 2) return NaN;

var from = d[0],
to,
var from = source.call(this, d, i),
to = target.call(this, d, i),
x0 = from[0] * d3_radians,
y0 = from[1] * d3_radians,
n = d.length,
i = 0,
s = 0;

while (++i < n) {
to = d[i];
var x1 = to[0] * d3_radians,
y1 = to[1] * d3_radians,
sy = Math.sin((y1 - y0) / 2),
sx = Math.sin((x1 - x0) / 2),
a = sy * sy + Math.cos(y0) * Math.cos(y1) * sx * sx;
x0 = x1;
y0 = y1;
s += radius * 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
}
return s;
x1 = to[0] * d3_radians,
y1 = to[1] * d3_radians,
sy = Math.sin((y1 - y0) / 2),
sx = Math.sin((x1 - x0) / 2),
a = sy * sy + Math.cos(y0) * Math.cos(y1) * sx * sx;
return radius * 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
};

return greatCircle;
Expand Down Expand Up @@ -727,6 +721,14 @@ function d3_geo_greatCirclePath(coordinates, precision) {

return path;
}

function d3_geo_greatCircleSource(d) {
return d.source;
}

function d3_geo_greatCircleTarget(d) {
return d.target;
}
d3.geo.clip = function() {
var origin = [0, 0],
angle = 90,
Expand Down Expand Up @@ -790,9 +792,7 @@ d3.geo.clip = function() {
return clip;
}

var d3_geo_clipGreatCircle = d3.geo.greatCircle().coordinates(function(d) {
return [d.source, d.target];
});
var d3_geo_clipGreatCircle = d3.geo.greatCircle();

function d3_geo_clipIntersect(from, to, f) {
var x0 = from[0] * d3_radians,
Expand Down
2 changes: 1 addition & 1 deletion d3.geo.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion examples/azimuthal/azimuthal.html
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ <h3>Azimuthal Projection</h3>
$("#mode").change(function() {
var mode = $(this).val();
xy.mode(mode);
path.clip(mode === "gnomonic" ? clip : circle);
path.clip(mode === "gnomonic" ? clip : circle.polyline);
refresh(500);
});

Expand Down
2 changes: 1 addition & 1 deletion examples/azimuthal/azimuthal.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
var xy = d3.geo.azimuthal().scale(240).mode("stereographic"),
clip = d3.geo.clip().angle(89),
circle = d3.geo.greatCircle().precision(10).coordinates(clip),
path = d3.geo.path().projection(xy).clip(circle),
path = d3.geo.path().projection(xy).clip(circle.polyline),
svg = d3.select("body").append("svg:svg");

d3.json("../data/world-countries.json", function(collection) {
Expand Down
4 changes: 1 addition & 3 deletions src/geo/clip.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,7 @@ d3.geo.clip = function() {
return clip;
}

var d3_geo_clipGreatCircle = d3.geo.greatCircle().coordinates(function(d) {
return [d.source, d.target];
});
var d3_geo_clipGreatCircle = d3.geo.greatCircle();

function d3_geo_clipIntersect(from, to, f) {
var x0 = from[0] * d3_radians,
Expand Down
48 changes: 25 additions & 23 deletions src/geo/greatCircle.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
// From http://williams.best.vwh.net/avform.htm#Intermediate
d3.geo.greatCircle = function() {
var coordinates = Object,
var source = d3_geo_greatCircleSource,
target = d3_geo_greatCircleTarget,
coordinates = Object, // for use with polyline
precision = 1,
radius = d3_geo_earthRadius;
// TODO: breakAtDateLine?

function greatCircle(d, i) {
return d3_geo_greatCirclePath(coordinates.call(this, d, i), precision);
return d3_geo_greatCirclePath([
source.call(this, d, i), target.call(this, d, i)], precision);
}

greatCircle.polyline = function(d, i) {
return d3_geo_greatCirclePath(coordinates.call(this, d, i), precision);
};

greatCircle.coordinates = function(x) {
if (!arguments.length) return coordinates;
coordinates = x;
Expand All @@ -29,29 +36,16 @@ d3.geo.greatCircle = function() {

// Haversine formula for great-circle distance.
greatCircle.distance = function(d, i) {
d = coordinates.call(this, d, i);
if (d.length < 2) return NaN;

var from = d[0],
to,
var from = source.call(this, d, i),
to = target.call(this, d, i),
x0 = from[0] * d3_radians,
y0 = from[1] * d3_radians,
n = d.length,
i = 0,
s = 0;

while (++i < n) {
to = d[i];
var x1 = to[0] * d3_radians,
y1 = to[1] * d3_radians,
sy = Math.sin((y1 - y0) / 2),
sx = Math.sin((x1 - x0) / 2),
a = sy * sy + Math.cos(y0) * Math.cos(y1) * sx * sx;
x0 = x1;
y0 = y1;
s += radius * 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
}
return s;
x1 = to[0] * d3_radians,
y1 = to[1] * d3_radians,
sy = Math.sin((y1 - y0) / 2),
sx = Math.sin((x1 - x0) / 2),
a = sy * sy + Math.cos(y0) * Math.cos(y1) * sx * sx;
return radius * 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
};

return greatCircle;
Expand Down Expand Up @@ -105,3 +99,11 @@ function d3_geo_greatCirclePath(coordinates, precision) {

return path;
}

function d3_geo_greatCircleSource(d) {
return d.source;
}

function d3_geo_greatCircleTarget(d) {
return d.target;
}
12 changes: 6 additions & 6 deletions test/geo/greatCircle-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ suite.addBatch({
return d3.geo.greatCircle().precision(7.5);
},
"distance": function(circle) {
assert.equal(circle.distance([[0, 0], [0, 0]]), 0);
assert.inDelta(circle.distance([
[118 + 24 / 60, 33 + 57 / 60],
[ 73 + 47 / 60, 40 + 38 / 60]
]), 3973, .5);
assert.equal(circle.distance({source: [0, 0], target: [0, 0]}), 0);
assert.inDelta(circle.distance({
source: [118 + 24 / 60, 33 + 57 / 60],
target: [ 73 + 47 / 60, 40 + 38 / 60]
}), 3973, .5);
},
"geodesic": function(circle) {
assert.inDelta(circle([[5, 52], [-120, 37]]), [
assert.inDelta(circle.polyline([[5, 52], [-120, 37]]), [
[ 5, 52 ],
[ -3.805036, 57.05083],
[ -15.122869, 61.30118],
Expand Down

0 comments on commit 15dda93

Please sign in to comment.