Permalink
Browse files

Merge branch 'bonne' of https://github.com/jasondavies/d3 into release

  • Loading branch information...
2 parents a70975b + 11c1104 commit 6a65451b90edb379d2f374cdb720f5d9fbc1567f @mbostock mbostock committed Sep 27, 2011
Showing with 340 additions and 1 deletion.
  1. +1 −0 Makefile
  2. +64 −0 d3.geo.js
  3. +1 −1 d3.geo.min.js
  4. +161 −0 examples/bonne/bonne.html
  5. +64 −0 src/geo/bonne.js
  6. +49 −0 test/geo/bonne-test.js
View
@@ -184,6 +184,7 @@ d3.geo.js: \
src/geo/geo.js \
src/geo/azimuthal.js \
src/geo/albers.js \
+ src/geo/bonne.js \
src/geo/equirectangular.js \
src/geo/mercator.js \
src/geo/type.js \
View
@@ -207,6 +207,70 @@ d3.geo.albersUsa = function() {
return albersUsa.scale(lower48.scale());
};
+d3.geo.bonne = function() {
+ var origin,
+ scale = 200,
+ translate = [480, 250],
+ parallel, // 90 for Werner, 0 for Sinusoidal
+ p0,
+ ctp0,
+ x0,
+ y0;
+
+ 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;
+ return [
+ scale * x + translate[0],
+ scale * y + translate[1]
+ ];
+ }
+
+ 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);
+ return [
+ (x0 + p * Math.atan2(x, c) / Math.cos(y1)) / d3_geo_radians,
+ y1 / d3_geo_radians
+ ];
+ };
+
+ bonne.parallel = function(x) {
+ if (!arguments.length) return parallel;
+ parallel = +x;
+ ctp0 = 1 / Math.tan(p0 = parallel * 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;
+ return bonne;
+ };
+
+ bonne.scale = function(x) {
+ if (!arguments.length) return scale;
+ scale = +x;
+ return bonne;
+ };
+
+ bonne.translate = function(x) {
+ if (!arguments.length) return translate;
+ translate = [+x[0], +x[1]];
+ return bonne;
+ };
+
+ return bonne.origin([0, 0]).parallel(40);
+};
d3.geo.equirectangular = function() {
var scale = 500,
translate = [480, 250];
View

Large diffs are not rendered by default.

Oops, something went wrong.
View
@@ -0,0 +1,161 @@
+<!DOCTYPE html>
+<html>
+ <head>
+ <meta http-equiv="Content-Type" content="text/html;charset=utf-8">
+ <title>Bonne Projection</title>
+ <script type="text/javascript" src="../../d3.js"></script>
+ <script type="text/javascript" src="../../d3.geo.js"></script>
+ <script type="text/javascript" src="../../lib/jquery/jquery.min.js"></script>
+ <script type="text/javascript" src="../../lib/jquery-ui/jquery-ui.min.js"></script>
+ <style type="text/css">
+
+@import url("../../lib/jquery-ui/jquery-ui.css");
+
+body, .ui-widget {
+ font: 14px Helvetica Neue;
+}
+
+svg {
+ width: 960px;
+ height: 500px;
+ border: solid 1px #ccc;
+ background: #eee;
+}
+
+line {
+ stroke: brown;
+ stroke-dasharray: 4,2;
+}
+
+path {
+ fill: #ccc;
+ stroke: #fff;
+}
+
+div {
+ width: 960px;
+}
+
+ </style>
+ </head>
+ <body>
+ <h3>Bonne Projection</h3>
+ <script type="text/javascript">
+
+// Our projection.
+var xy = d3.geo.bonne().origin([0, 0]),
+ path = d3.geo.path().projection(xy);
+
+var states = d3.select("body")
+ .append("svg:svg")
+ .append("svg:g")
+ .attr("id", "states");
+
+var equator = d3.select("svg")
+ .append("svg:line")
+ .attr("x1", "0%")
+ .attr("x2", "100%");
+
+d3.json("../data/world-countries.json", function(collection) {
+
+ states
+ .selectAll("path")
+ .data(collection.features)
+ .enter().append("svg:path")
+ .attr("d", path)
+ .append("svg:title")
+ .text(function(d) { return d.properties.name; });
+
+ equator
+ .attr("y1", xy([0, 0])[1])
+ .attr("y2", xy([0, 0])[1])
+});
+
+function refresh() {
+ states
+ .selectAll("path")
+ .attr("d", path);
+
+ equator
+ .attr("y1", xy([0, 0])[1])
+ .attr("y2", xy([0, 0])[1])
+
+ d3.select("#scale span")
+ .text(xy.scale());
+ d3.select("#translate-x span")
+ .text(xy.translate()[0]);
+ d3.select("#translate-y span")
+ .text(xy.translate()[1]);
+}
+
+ </script><p>
+ <div id="lon">origin.longitude: <span>0</span></div>
+ <div id="lat">origin.latitude: <span>0</span></div><p>
+ <div id="scale">scale: <span>500</span></div><p>
+ <div id="translate-x">translate.x: <span>480</span></div>
+ <div id="translate-y">translate.y: <span>250</span></div>
+ <script type="text/javascript">
+
+$("#lon").slider({
+ min: -180,
+ max: 180,
+ step: 1e-1,
+ value: 0,
+ slide: function(event, ui) {
+ var origin = xy.origin();
+ origin[0] = ui.value;
+ xy.origin(origin);
+ refresh();
+ }
+});
+
+$("#lat").slider({
+ min: -90,
+ max: 90,
+ step: 1e-1,
+ value: 0,
+ slide: function(event, ui) {
+ var origin = xy.origin();
+ origin[1] = ui.value;
+ xy.origin(origin);
+ refresh();
+ }
+});
+
+$("#scale").slider({
+ min: 0,
+ max: 3000,
+ value: 500,
+ slide: function(event, ui) {
+ xy.scale(ui.value);
+ refresh();
+ }
+});
+
+$("#translate-x").slider({
+ min: -2000,
+ max: 2000,
+ value: 480,
+ slide: function(event, ui) {
+ var translate = xy.translate();
+ translate[0] = ui.value;
+ xy.translate(translate);
+ refresh();
+ }
+});
+
+$("#translate-y").slider({
+ min: -2000,
+ max: 2000,
+ value: 250,
+ slide: function(event, ui) {
+ var translate = xy.translate();
+ translate[1] = ui.value;
+ xy.translate(translate);
+ refresh();
+ }
+});
+
+ </script>
+ </body>
+</html>
View
@@ -0,0 +1,64 @@
+d3.geo.bonne = function() {
+ var origin,
+ scale = 200,
+ translate = [480, 250],
+ parallel, // 90 for Werner, 0 for Sinusoidal
+ p0,
+ ctp0,
+ x0,
+ y0;
+
+ 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;
+ return [
+ scale * x + translate[0],
+ scale * y + translate[1]
+ ];
+ }
+
+ 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);
+ return [
+ (x0 + p * Math.atan2(x, c) / Math.cos(y1)) / d3_geo_radians,
+ y1 / d3_geo_radians
+ ];
+ };
+
+ bonne.parallel = function(x) {
+ if (!arguments.length) return parallel;
+ parallel = +x;
+ ctp0 = 1 / Math.tan(p0 = parallel * 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;
+ return bonne;
+ };
+
+ bonne.scale = function(x) {
+ if (!arguments.length) return scale;
+ scale = +x;
+ return bonne;
+ };
+
+ bonne.translate = function(x) {
+ if (!arguments.length) return translate;
+ translate = [+x[0], +x[1]];
+ return bonne;
+ };
+
+ return bonne.origin([0, 0]).parallel(40);
+};
View
@@ -0,0 +1,49 @@
+require("../env");
+require("../../d3");
+require("../../d3.geo");
+
+var vows = require("vows"),
+ assert = require("assert");
+
+var suite = vows.describe("d3.geo.bonne");
+
+suite.addBatch({
+ "bonne": {
+ topic: function() {
+ return d3.geo.bonne();
+ },
+ "Arctic": function(bonne) {
+ var lonlat = [0, 85],
+ coords = bonne(lonlat);
+ assert.inDelta(coords, [480, 92.920367], 1e-6);
+ assert.inDelta(bonne.invert(coords), lonlat, 1e-6);
+ },
+ "Antarctic": function(bonne) {
+ var lonlat = [0, -85],
+ coords = bonne(lonlat);
+ assert.inDelta(coords, [480, 686.332312], 1e-6);
+ assert.inDelta(bonne.invert(coords), lonlat, 1e-6);
+ },
+ "Hawaii": function(bonne) {
+ var lonlat = [-180, 0],
+ coords = bonne(lonlat);
+ assert.inDelta(coords, [103.604887, -22.895998], 1e-6);
+ assert.inDelta(bonne.invert(coords), lonlat, 1e-6);
+ },
+ "Phillipines": function(bonne) {
+ var lonlat = [180, 0],
+ coords = bonne(lonlat);
+ assert.inDelta(coords, [856.395112, -22.895998], 1e-6);
+ assert.inDelta(bonne.invert(coords), lonlat, 1e-6);
+ },
+ "Inversion works for non-zero translation": function() {
+ var bonne = d3.geo.bonne().translate([123, 99]).scale(100),
+ coords = bonne([0, 85]),
+ lonlat = bonne.invert(coords);
+ assert.inDelta(lonlat[0], 0, 1e-6);
+ assert.inDelta(lonlat[1], 85, 1e-6);
+ }
+ }
+});
+
+suite.export(module);

0 comments on commit 6a65451

Please sign in to comment.