Skip to content

Commit afe60a1

Browse files
committed
Add azimuthal "equalarea" projection mode.
1 parent 1e017e6 commit afe60a1

5 files changed

Lines changed: 46 additions & 4 deletions

File tree

d3.geo.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
var d3_radians = Math.PI / 180;
44
// TODO clip input coordinates on opposite hemisphere
55
d3.geo.azimuthal = function() {
6-
var mode = "orthographic", // or stereographic, gnomonic or equidistant
6+
var mode = "orthographic", // or stereographic, gnomonic, equidistant or equalarea
77
origin,
88
scale = 200,
99
translate = [480, 250],
@@ -24,6 +24,7 @@ d3.geo.azimuthal = function() {
2424
k = mode === "stereographic" ? 1 / (1 + cc)
2525
: mode === "gnomonic" ? 1 / cc
2626
: mode === "equidistant" ? (c = Math.acos(cc), c / Math.sin(c))
27+
: mode === "equalarea" ? Math.sqrt(2 / (1 + cc))
2728
: 1,
2829
x = k * cy1 * sx1,
2930
y = k * (sy0 * cy1 * cx1 - cy0 * sy1);
@@ -40,6 +41,7 @@ d3.geo.azimuthal = function() {
4041
c = mode === "stereographic" ? 2 * Math.atan(p)
4142
: mode === "gnomonic" ? Math.atan(p)
4243
: mode === "equidistant" ? p
44+
: mode === "equalarea" ? 2 * Math.asin(.5 * p)
4345
: Math.asin(p),
4446
sc = Math.sin(c),
4547
cc = Math.cos(c);

d3.geo.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/azimuthal/azimuthal.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
var xy = d3.geo.azimuthal().scale(240).mode("stereographic"),
1+
var xy = d3.geo.azimuthal().scale(240).mode("equalarea"),
22
path = d3.geo.path().projection(xy),
33
svg = d3.select("body").append("svg:svg");
44

src/geo/azimuthal.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// TODO clip input coordinates on opposite hemisphere
22
d3.geo.azimuthal = function() {
3-
var mode = "orthographic", // or stereographic, gnomonic or equidistant
3+
var mode = "orthographic", // or stereographic, gnomonic, equidistant or equalarea
44
origin,
55
scale = 200,
66
translate = [480, 250],
@@ -21,6 +21,7 @@ d3.geo.azimuthal = function() {
2121
k = mode === "stereographic" ? 1 / (1 + cc)
2222
: mode === "gnomonic" ? 1 / cc
2323
: mode === "equidistant" ? (c = Math.acos(cc), c / Math.sin(c))
24+
: mode === "equalarea" ? Math.sqrt(2 / (1 + cc))
2425
: 1,
2526
x = k * cy1 * sx1,
2627
y = k * (sy0 * cy1 * cx1 - cy0 * sy1);
@@ -37,6 +38,7 @@ d3.geo.azimuthal = function() {
3738
c = mode === "stereographic" ? 2 * Math.atan(p)
3839
: mode === "gnomonic" ? Math.atan(p)
3940
: mode === "equidistant" ? p
41+
: mode === "equalarea" ? 2 * Math.asin(.5 * p)
4042
: Math.asin(p),
4143
sc = Math.sin(c),
4244
cc = Math.cos(c);

test/geo/azimuthal-test.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,44 @@ suite.addBatch({
177177
assert.inDelta(lonlat[0], 0, 1e-6);
178178
assert.inDelta(lonlat[1], 85, 1e-6);
179179
}
180+
},
181+
"azimuthal.equalarea": {
182+
topic: function() {
183+
return d3.geo.azimuthal().mode("equalarea").translate([0, 0]).scale(100);
184+
},
185+
"Arctic": function(azimuthal) {
186+
var coords = azimuthal([0, 85]);
187+
assert.inDelta(coords[0], 0, 1e-6);
188+
assert.inDelta(coords[1], -135.118041, 1e-6);
189+
var lonlat = azimuthal.invert(coords);
190+
assert.inDelta(lonlat[0], 0, 1e-6);
191+
assert.inDelta(lonlat[1], 85, 1e-6);
192+
},
193+
"Antarctic": function(azimuthal) {
194+
var coords = azimuthal([0, -85]);
195+
assert.inDelta(coords[0], 0, 1e-6);
196+
assert.inDelta(coords[1], 135.118041, 1e-6);
197+
var lonlat = azimuthal.invert(coords);
198+
assert.inDelta(lonlat[0], 0, 1e-6);
199+
assert.inDelta(lonlat[1], -85, 1e-6);
200+
},
201+
"Hawaii": function(azimuthal) {
202+
var coords = azimuthal([-180, 0]);
203+
assert.equal(coords[0], -Infinity);
204+
assert.isTrue(isNaN(coords[1]));
205+
},
206+
"Phillipines": function(azimuthal) {
207+
var coords = azimuthal([180, 0]);
208+
assert.equal(coords[0], Infinity);
209+
assert.isTrue(isNaN(coords[1]));
210+
},
211+
"Inversion works for non-zero translation": function() {
212+
var azimuthal = d3.geo.azimuthal().mode("stereographic").translate([123, 99]).scale(100),
213+
coords = azimuthal([0, 85]),
214+
lonlat = azimuthal.invert(coords);
215+
assert.inDelta(lonlat[0], 0, 1e-6);
216+
assert.inDelta(lonlat[1], 85, 1e-6);
217+
}
180218
}
181219
});
182220

0 commit comments

Comments
 (0)