Skip to content

Commit

Permalink
Fix for Bonne projection at 0° parallel.
Browse files Browse the repository at this point in the history
  • Loading branch information
mbostock committed Sep 27, 2011
1 parent 6a65451 commit bbcaa27
Show file tree
Hide file tree
Showing 5 changed files with 161 additions and 84 deletions.
60 changes: 33 additions & 27 deletions d3.geo.js
Original file line number Diff line number Diff line change
Expand Up @@ -208,22 +208,24 @@ d3.geo.albersUsa = function() {
return albersUsa.scale(lower48.scale());
};
d3.geo.bonne = function() {
var origin,
scale = 200,
var scale = 200,
translate = [480, 250],
parallel, // 90 for Werner, 0 for Sinusoidal
p0,
ctp0,
x0,
y0;
x0, // origin longitude in radians
y0, // origin latitude in radians
y1, // parallel latitude in radians
c1; // cot(y1)

function bonne(coordinates) {
var x1 = coordinates[0] * d3_geo_radians - x0,
y1 = coordinates[1] * d3_geo_radians - y0,
p = ctp0 + p0 - y1,
E = x1 * Math.cos(y1) / p,
x = p * Math.sin(E),
y = p * Math.cos(E) - ctp0;
var x = coordinates[0] * d3_geo_radians - x0,
y = coordinates[1] * d3_geo_radians - y0;
if (y1) {
var p = c1 + y1 - y, E = x * Math.cos(y) / p;
x = p * Math.sin(E);
y = p * Math.cos(E) - c1;
} else {
x *= Math.cos(y);
y *= -1;
}
return [
scale * x + translate[0],
scale * y + translate[1]
Expand All @@ -232,28 +234,32 @@ d3.geo.bonne = function() {

bonne.invert = function(coordinates) {
var x = (coordinates[0] - translate[0]) / scale,
y = (coordinates[1] - translate[1]) / scale,
c = ctp0 + y,
p = Math.sqrt(x * x + c * c),
y1 = (ctp0 + p0 - p);
y = (coordinates[1] - translate[1]) / scale;
if (y1) {
var c = c1 + y, p = Math.sqrt(x * x + c * c);
y = c1 + y1 - p;
x = x0 + p * Math.atan2(x, c) / Math.cos(y);
} else {
y *= -1;
x /= Math.cos(y);
}
return [
(x0 + p * Math.atan2(x, c) / Math.cos(y1)) / d3_geo_radians,
y1 / d3_geo_radians
x / d3_geo_radians,
y / d3_geo_radians
];
};

// 90° for Werner, 0° for Sinusoidal
bonne.parallel = function(x) {
if (!arguments.length) return parallel;
parallel = +x;
ctp0 = 1 / Math.tan(p0 = parallel * d3_geo_radians);
if (!arguments.length) return y1 / d3_geo_radians;
c1 = 1 / Math.tan(y1 = x * d3_geo_radians);
return bonne;
};

bonne.origin = function(x) {
if (!arguments.length) return origin;
origin = x;
x0 = origin[0] * d3_geo_radians;
y0 = origin[1] * d3_geo_radians;
if (!arguments.length) return [x0 / d3_geo_radians, y0 / d3_geo_radians];
x0 = x[0] * d3_geo_radians;
y0 = x[1] * d3_geo_radians;
return bonne;
};

Expand All @@ -269,7 +275,7 @@ d3.geo.bonne = function() {
return bonne;
};

return bonne.origin([0, 0]).parallel(40);
return bonne.origin([0, 0]).parallel(45);
};
d3.geo.equirectangular = function() {
var scale = 500,
Expand Down
Loading

0 comments on commit bbcaa27

Please sign in to comment.