Skip to content

Commit 77adc10

Browse files
committed
Checkpoint integration of geo/projection plugin.
This is still a work in progress, but it's a start.
1 parent 8f5ede3 commit 77adc10

31 files changed

Lines changed: 860 additions & 782 deletions

Makefile

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -183,17 +183,26 @@ d3.layout.js: \
183183

184184
d3.geo.js: \
185185
src/geo/geo.js \
186-
src/geo/azimuthal.js \
186+
src/geo/albers-usa.js \
187187
src/geo/albers.js \
188-
src/geo/bonne.js \
189-
src/geo/equirectangular.js \
190-
src/geo/mercator.js \
191-
src/geo/type.js \
192-
src/geo/path.js \
188+
src/geo/azimuthal-equal-area.js \
189+
src/geo/azimuthal-equidistant.js \
193190
src/geo/bounds.js \
194191
src/geo/circle.js \
192+
src/geo/compose.js \
193+
src/geo/equirectangular.js \
194+
src/geo/gnomonic.js \
195+
src/geo/graticule.js \
195196
src/geo/greatArc.js \
196-
src/geo/greatCircle.js
197+
src/geo/greatCircle.js \
198+
src/geo/mercator.js \
199+
src/geo/orthographic.js \
200+
src/geo/path.js \
201+
src/geo/projection.js \
202+
src/geo/rotation.js \
203+
src/geo/stereographic.js \
204+
src/geo/azimuthal.js \
205+
src/geo/type.js
197206

198207
d3.dsv.js: \
199208
src/dsv/dsv.js \

d3.v2.js

Lines changed: 421 additions & 328 deletions
Large diffs are not rendered by default.

d3.v2.min.js

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/core/core.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
11
d3 = {version: "2.10.2"}; // semver
2+
3+
var π = Math.PI;

src/core/ease.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ function d3_ease_poly(e) {
9595
}
9696

9797
function d3_ease_sin(t) {
98-
return 1 - Math.cos(t * Math.PI / 2);
98+
return 1 - Math.cos(t * π / 2);
9999
}
100100

101101
function d3_ease_exp(t) {
@@ -110,9 +110,9 @@ function d3_ease_elastic(a, p) {
110110
var s;
111111
if (arguments.length < 2) p = 0.45;
112112
if (arguments.length < 1) { a = 1; s = p / 4; }
113-
else s = p / (2 * Math.PI) * Math.asin(1 / a);
113+
else s = p / (2 * π) * Math.asin(1 / a);
114114
return function(t) {
115-
return 1 + a * Math.pow(2, 10 * -t) * Math.sin((t - s) * 2 * Math.PI / p);
115+
return 1 + a * Math.pow(2, 10 * -t) * Math.sin((t - s) * 2 * π / p);
116116
};
117117
}
118118

src/core/hcl.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,5 +33,5 @@ d3_Hcl.prototype.toString = function() {
3333
};
3434

3535
function d3_hcl_lab(h, c, l) {
36-
return d3_lab(l, Math.cos(h *= Math.PI / 180) * c, Math.sin(h) * c);
36+
return d3_lab(l, Math.cos(h *= π / 180) * c, Math.sin(h) * c);
3737
}

src/core/lab.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ function d3_lab_rgb(l, a, b) {
5555
}
5656

5757
function d3_lab_hcl(l, a, b) {
58-
return d3_hcl(Math.atan2(b, a) / Math.PI * 180, Math.sqrt(a * a + b * b), l);
58+
return d3_hcl(Math.atan2(b, a) / π * 180, Math.sqrt(a * a + b * b), l);
5959
}
6060

6161
function d3_lab_xyz(x) {

src/core/transform.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,5 +56,5 @@ function d3_transformCombine(a, b, k) {
5656
return a;
5757
}
5858

59-
var d3_transformDegrees = 180 / Math.PI,
59+
var d3_transformDegrees = 180 / π,
6060
d3_transformIdentity = {a: 1, b: 0, c: 0, d: 1, e: 0, f: 0};

src/geo/albers-usa.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// A composite projection for the United States, 960x500. The set of standard
2+
// parallels for each region comes from USGS, which is published here:
3+
// http://egsc.usgs.gov/isb/pubs/MapProjections/projections.html#albers
4+
// TODO allow the composite projection to be rescaled?
5+
d3.geo.albersUsa = function() {
6+
var lower48 = d3.geo.albers();
7+
8+
var alaska = d3.geo.albers()
9+
.origin([-160, 60])
10+
.parallels([55, 65]);
11+
12+
var hawaii = d3.geo.albers()
13+
.origin([-160, 20])
14+
.parallels([8, 18]);
15+
16+
var puertoRico = d3.geo.albers()
17+
.origin([-60, 10])
18+
.parallels([8, 18]);
19+
20+
function albersUsa(coordinates) {
21+
var lon = coordinates[0],
22+
lat = coordinates[1];
23+
return (lat > 50 ? alaska
24+
: lon < -140 ? hawaii
25+
: lat < 21 ? puertoRico
26+
: lower48)(coordinates);
27+
}
28+
29+
albersUsa.scale = function(x) {
30+
if (!arguments.length) return lower48.scale();
31+
lower48.scale(x);
32+
alaska.scale(x * .6);
33+
hawaii.scale(x);
34+
puertoRico.scale(x * 1.5);
35+
return albersUsa.translate(lower48.translate());
36+
};
37+
38+
albersUsa.translate = function(x) {
39+
if (!arguments.length) return lower48.translate();
40+
var dz = lower48.scale(),
41+
dx = x[0],
42+
dy = x[1];
43+
lower48.translate(x);
44+
alaska.translate([dx - .40 * dz, dy + .17 * dz]);
45+
hawaii.translate([dx - .19 * dz, dy + .20 * dz]);
46+
puertoRico.translate([dx + .58 * dz, dy + .43 * dz]);
47+
return albersUsa;
48+
};
49+
50+
return albersUsa.scale(lower48.scale());
51+
};

src/geo/albers.js

Lines changed: 41 additions & 110 deletions
Original file line numberDiff line numberDiff line change
@@ -1,126 +1,57 @@
1-
// Derived from Tom Carden's Albers implementation for Protovis.
2-
// http://gist.github.com/476238
3-
// http://mathworld.wolfram.com/AlbersEqual-AreaConicProjection.html
4-
5-
d3.geo.albers = function() {
6-
var origin = [-98, 38],
7-
parallels = [29.5, 45.5],
8-
scale = 1000,
9-
translate = [480, 250],
10-
lng0, // d3_geo_radians * origin[0]
11-
n,
12-
C,
13-
p0;
14-
15-
function albers(coordinates) {
16-
var t = n * (d3_geo_radians * coordinates[0] - lng0),
17-
p = Math.sqrt(C - 2 * n * Math.sin(d3_geo_radians * coordinates[1])) / n;
1+
function d3_geo_albers(φ0, φ1) {
2+
var sinφ0 = Math.sin(φ0),
3+
n = (sinφ0 + Math.sin(φ1)) / 2,
4+
C = 1 + sinφ0 * (2 * n - sinφ0),
5+
ρ0 = Math.sqrt(C) / n;
6+
7+
function albers(λ, φ) {
8+
var ρ = Math.sqrt(C - 2 * n * Math.sin(φ)) / n;
189
return [
19-
scale * p * Math.sin(t) + translate[0],
20-
scale * (p * Math.cos(t) - p0) + translate[1]
10+
ρ * Math.sin(n * λ),
11+
ρ0 - ρ * Math.cos(n * λ)
2112
];
2213
}
2314

24-
albers.invert = function(coordinates) {
25-
var x = (coordinates[0] - translate[0]) / scale,
26-
y = (coordinates[1] - translate[1]) / scale,
27-
p0y = p0 + y,
28-
t = Math.atan2(x, p0y),
29-
p = Math.sqrt(x * x + p0y * p0y);
15+
albers.invert = function(x, y) {
16+
var ρ0_y = ρ0 - y;
3017
return [
31-
(lng0 + t / n) / d3_geo_radians,
32-
Math.asin((C - p * p * n * n) / (2 * n)) / d3_geo_radians
18+
Math.atan2(x, ρ0_y) / n,
19+
Math.asin((C - (x * x + ρ0_y * ρ0_y) * n * n) / (2 * n))
3320
];
3421
};
3522

36-
function reload() {
37-
var phi1 = d3_geo_radians * parallels[0],
38-
phi2 = d3_geo_radians * parallels[1],
39-
lat0 = d3_geo_radians * origin[1],
40-
s = Math.sin(phi1),
41-
c = Math.cos(phi1);
42-
lng0 = d3_geo_radians * origin[0];
43-
n = .5 * (s + Math.sin(phi2));
44-
C = c * c + 2 * n * s;
45-
p0 = Math.sqrt(C - 2 * n * Math.sin(lat0)) / n;
46-
return albers;
47-
}
48-
49-
albers.origin = function(x) {
50-
if (!arguments.length) return origin;
51-
origin = [+x[0], +x[1]];
52-
return reload();
53-
};
54-
55-
albers.parallels = function(x) {
56-
if (!arguments.length) return parallels;
57-
parallels = [+x[0], +x[1]];
58-
return reload();
59-
};
60-
61-
albers.scale = function(x) {
62-
if (!arguments.length) return scale;
63-
scale = +x;
64-
return albers;
65-
};
23+
return albers;
24+
}
6625

67-
albers.translate = function(x) {
68-
if (!arguments.length) return translate;
69-
translate = [+x[0], +x[1]];
70-
return albers;
26+
d3.geo.albers = function() {
27+
var p = d3_geo_doubleParallelProjection(d3_geo_albers)
28+
.rotate([98, 0])
29+
.center([0, 38])
30+
.parallels([29.5, 45.5])
31+
.scale(1000);
32+
33+
// Deprecated; use projection.rotate and projection.center instead.
34+
p.origin = function(_) {
35+
var rotate = p.rotate(),
36+
center = p.center();
37+
if (!arguments.length) return [-rotate[0], center[1]];
38+
return p.rotate([-_[0], rotate[1]]).center([center[0], _[1]]);
7139
};
7240

73-
return reload();
41+
return p;
7442
};
7543

76-
// A composite projection for the United States, 960x500. The set of standard
77-
// parallels for each region comes from USGS, which is published here:
78-
// http://egsc.usgs.gov/isb/pubs/MapProjections/projections.html#albers
79-
// TODO allow the composite projection to be rescaled?
80-
d3.geo.albersUsa = function() {
81-
var lower48 = d3.geo.albers();
82-
83-
var alaska = d3.geo.albers()
84-
.origin([-160, 60])
85-
.parallels([55, 65]);
86-
87-
var hawaii = d3.geo.albers()
88-
.origin([-160, 20])
89-
.parallels([8, 18]);
44+
// TODO Expose this API? Rename conicProjection?
45+
function d3_geo_doubleParallelProjection(projectAt) {
46+
var φ0 = 0,
47+
φ1 = π / 3,
48+
m = d3_geo_projectionMutator(projectAt),
49+
p = m(φ0, φ1);
9050

91-
var puertoRico = d3.geo.albers()
92-
.origin([-60, 10])
93-
.parallels([8, 18]);
94-
95-
function albersUsa(coordinates) {
96-
var lon = coordinates[0],
97-
lat = coordinates[1];
98-
return (lat > 50 ? alaska
99-
: lon < -140 ? hawaii
100-
: lat < 21 ? puertoRico
101-
: lower48)(coordinates);
102-
}
103-
104-
albersUsa.scale = function(x) {
105-
if (!arguments.length) return lower48.scale();
106-
lower48.scale(x);
107-
alaska.scale(x * .6);
108-
hawaii.scale(x);
109-
puertoRico.scale(x * 1.5);
110-
return albersUsa.translate(lower48.translate());
111-
};
112-
113-
albersUsa.translate = function(x) {
114-
if (!arguments.length) return lower48.translate();
115-
var dz = lower48.scale() / 1000,
116-
dx = x[0],
117-
dy = x[1];
118-
lower48.translate(x);
119-
alaska.translate([dx - 400 * dz, dy + 170 * dz]);
120-
hawaii.translate([dx - 190 * dz, dy + 200 * dz]);
121-
puertoRico.translate([dx + 580 * dz, dy + 430 * dz]);
122-
return albersUsa;
51+
p.parallels = function(_) {
52+
if (!arguments.length) return [φ0 / π * 180, φ1 / π * 180];
53+
return m(φ0 = _[0] * π / 180, φ1 = _[1] * π / 180);
12354
};
12455

125-
return albersUsa.scale(lower48.scale());
126-
};
56+
return p;
57+
}

0 commit comments

Comments
 (0)