Permalink
Browse files

Fix for Bonne projection at 0° parallel.

  • Loading branch information...
1 parent 6a65451 commit bbcaa27732b4d6be464c900344e6da61951e4990 @mbostock mbostock committed Sep 27, 2011
Showing with 161 additions and 84 deletions.
  1. +33 −27 d3.geo.js
  2. +1 −1 d3.geo.min.js
  3. +25 −27 examples/bonne/bonne.html
  4. +33 −27 src/geo/bonne.js
  5. +69 −2 test/geo/bonne-test.js
View
@@ -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]
@@ -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;
};
@@ -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,
Oops, something went wrong.

0 comments on commit bbcaa27

Please sign in to comment.