Skip to content

Commit 8f78039

Browse files
committed
Rename equirect to equirectangular; add test.
1 parent 93a0a0c commit 8f78039

6 files changed

Lines changed: 124 additions & 27 deletions

File tree

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,7 @@ d3.geo.js: \
182182
src/geo/geo.js \
183183
src/geo/azimuthal.js \
184184
src/geo/albers.js \
185+
src/geo/equirectangular.js \
185186
src/geo/mercator.js \
186187
src/geo/path.js \
187188
src/geo/bounds.js \

d3.geo.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,42 @@ d3.geo.albersUsa = function() {
197197
};
198198

199199
var d3_radians = Math.PI / 180;
200+
d3.geo.equirectangular = function() {
201+
var scale = 500,
202+
translate = [480, 250];
203+
204+
function equirectangular(coordinates) {
205+
var x = coordinates[0] / 360,
206+
y = -coordinates[1] / 360;
207+
return [
208+
scale * x + translate[0],
209+
scale * y + translate[1]
210+
];
211+
}
212+
213+
equirectangular.invert = function(coordinates) {
214+
var x = (coordinates[0] - translate[0]) / scale,
215+
y = (coordinates[1] - translate[1]) / scale;
216+
return [
217+
360 * x,
218+
-360 * y
219+
];
220+
};
221+
222+
equirectangular.scale = function(x) {
223+
if (!arguments.length) return scale;
224+
scale = +x;
225+
return equirectangular;
226+
};
227+
228+
equirectangular.translate = function(x) {
229+
if (!arguments.length) return translate;
230+
translate = [+x[0], +x[1]];
231+
return equirectangular;
232+
};
233+
234+
return equirectangular;
235+
};
200236
d3.geo.mercator = function() {
201237
var scale = 500,
202238
translate = [480, 250];

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.

src/geo/equirect.js

Lines changed: 0 additions & 26 deletions
This file was deleted.

src/geo/equirectangular.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
d3.geo.equirectangular = function() {
2+
var scale = 500,
3+
translate = [480, 250];
4+
5+
function equirectangular(coordinates) {
6+
var x = coordinates[0] / 360,
7+
y = -coordinates[1] / 360;
8+
return [
9+
scale * x + translate[0],
10+
scale * y + translate[1]
11+
];
12+
}
13+
14+
equirectangular.invert = function(coordinates) {
15+
var x = (coordinates[0] - translate[0]) / scale,
16+
y = (coordinates[1] - translate[1]) / scale;
17+
return [
18+
360 * x,
19+
-360 * y
20+
];
21+
};
22+
23+
equirectangular.scale = function(x) {
24+
if (!arguments.length) return scale;
25+
scale = +x;
26+
return equirectangular;
27+
};
28+
29+
equirectangular.translate = function(x) {
30+
if (!arguments.length) return translate;
31+
translate = [+x[0], +x[1]];
32+
return equirectangular;
33+
};
34+
35+
return equirectangular;
36+
};

test/geo/equirectangular-test.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
require("../env");
2+
require("../../d3");
3+
require("../../d3.geo");
4+
5+
var vows = require("vows"),
6+
assert = require("assert");
7+
8+
var suite = vows.describe("d3.geo.equirectangular");
9+
10+
suite.addBatch({
11+
"equirectangular": {
12+
topic: function() {
13+
return d3.geo.equirectangular();
14+
},
15+
16+
"scale": {
17+
"defaults to 500": function(projection) {
18+
assert.equal(projection.scale(), 500);
19+
},
20+
"is coerced to a number": function(projection) {
21+
assert.strictEqual(projection.scale("400"), projection);
22+
assert.strictEqual(projection.scale(), 400);
23+
projection.scale(500);
24+
}
25+
},
26+
27+
"translate": {
28+
"defaults to [480, 250]": function(projection) {
29+
assert.deepEqual(projection.translate(), [480, 250]);
30+
},
31+
"is coerced to two numbers": function(projection) {
32+
assert.strictEqual(projection.translate(["23", "141"]), projection);
33+
assert.strictEqual(projection.translate()[0], 23);
34+
assert.strictEqual(projection.translate()[1], 141);
35+
projection.translate([480, 250]);
36+
}
37+
},
38+
39+
"of San Francisco, CA": {
40+
"is at location [-122.446, 37.767]": function(projection) {
41+
assert.inDelta(projection.invert([310, 198]), [-122.446, 37.767], .5);
42+
},
43+
"is at point [310, 198]": function(projection) {
44+
assert.inDelta(projection([-122.446, 37.767]), [310, 198], .5);
45+
}
46+
}
47+
}
48+
});
49+
50+
suite.export(module);

0 commit comments

Comments
 (0)