Skip to content

Commit c7be968

Browse files
committed
Fallback for projections that don't stream.
1 parent 8e8699b commit c7be968

4 files changed

Lines changed: 66 additions & 75 deletions

File tree

d3.js

Lines changed: 36 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -5795,12 +5795,6 @@
57955795
var lon = point[0], lat = point[1];
57965796
return lat > 50 ? alaska : lon < -140 ? hawaii : lat < 21 ? puertoRico : lower48;
57975797
}
5798-
var projectResample = d3_geo_resample(function(λ, φ) {
5799-
return albersUsa([ λ * d3_degrees, φ * d3_degrees ]);
5800-
});
5801-
albersUsa.stream = function(listener) {
5802-
return d3_geo_albersUsaRadians(projectResample(listener));
5803-
};
58045798
albersUsa.scale = function(x) {
58055799
if (!arguments.length) return lower48.scale();
58065800
lower48.scale(x);
@@ -5820,28 +5814,6 @@
58205814
};
58215815
return albersUsa.scale(lower48.scale());
58225816
};
5823-
function d3_geo_albersUsaRadians(stream) {
5824-
return {
5825-
point: function(λ, φ) {
5826-
stream.point(λ * d3_radians, φ * d3_radians);
5827-
},
5828-
sphere: function() {
5829-
stream.sphere();
5830-
},
5831-
lineStart: function() {
5832-
stream.lineStart();
5833-
},
5834-
lineEnd: function() {
5835-
stream.lineEnd();
5836-
},
5837-
polygonStart: function() {
5838-
stream.polygonStart();
5839-
},
5840-
polygonEnd: function() {
5841-
stream.polygonEnd();
5842-
}
5843-
};
5844-
}
58455817
function d3_geo_albers(φ0, φ1) {
58465818
var sinφ0 = Math.sin(φ0), n = (sinφ0 + Math.sin(φ1)) / 2, C = 1 + sinφ0 * (2 * n - sinφ0), ρ0 = Math.sqrt(C) / n;
58475819
function albers(λ, φ) {
@@ -6254,44 +6226,72 @@
62546226
return d3_geo_projection(d3_geo_orthographic);
62556227
}).raw = d3_geo_orthographic;
62566228
d3.geo.path = function() {
6257-
var pointRadius = 4.5, projection = d3.geo.albersUsa(), context, stream = new d3_geo_pathBuffer();
6229+
var pointRadius = 4.5, projection, context, projectStream, contextStream;
62586230
function path(object) {
6259-
if (object) d3.geo.stream(object, projection.stream(stream.pointRadius(typeof pointRadius === "function" ? +pointRadius.apply(this, arguments) : pointRadius)));
6260-
return stream.result();
6231+
if (object) d3.geo.stream(object, projectStream(contextStream.pointRadius(typeof pointRadius === "function" ? +pointRadius.apply(this, arguments) : pointRadius)));
6232+
return contextStream.result();
62616233
}
62626234
path.area = function(object) {
62636235
d3_geo_pathAreaSum = 0;
6264-
d3.geo.stream(object, projection.stream(d3_geo_pathArea));
6236+
d3.geo.stream(object, projectStream(d3_geo_pathArea));
62656237
return d3_geo_pathAreaSum;
62666238
};
62676239
path.centroid = function(object) {
62686240
d3_geo_centroidDimension = d3_geo_centroidX = d3_geo_centroidY = d3_geo_centroidZ = 0;
6269-
d3.geo.stream(object, projection.stream(d3_geo_pathCentroid));
6241+
d3.geo.stream(object, projectStream(d3_geo_pathCentroid));
62706242
return d3_geo_centroidZ ? [ d3_geo_centroidX / d3_geo_centroidZ, d3_geo_centroidY / d3_geo_centroidZ ] : undefined;
62716243
};
62726244
path.bounds = function(object) {
62736245
return d3_geo_bounds(projection)(object);
62746246
};
62756247
path.projection = function(_) {
62766248
if (!arguments.length) return projection;
6277-
projection = _;
6249+
projectStream = (projection = _).stream ? projection.stream : d3_geo_pathProjectStream(projection);
62786250
return path;
62796251
};
62806252
path.context = function(_) {
62816253
if (!arguments.length) return context;
6282-
stream = (context = _) == null ? new d3_geo_pathBuffer() : new d3_geo_pathContext(_);
6254+
contextStream = (context = _) == null ? new d3_geo_pathBuffer() : new d3_geo_pathContext(_);
62836255
return path;
62846256
};
62856257
path.pointRadius = function(_) {
62866258
if (!arguments.length) return pointRadius;
62876259
pointRadius = typeof _ === "function" ? _ : +_;
62886260
return path;
62896261
};
6290-
return path;
6262+
return path.projection(d3.geo.albersUsa()).context(null);
62916263
};
62926264
function d3_geo_pathCircle(radius) {
62936265
return "m0," + radius + "a" + radius + "," + radius + " 0 1,1 0," + -2 * radius + "a" + radius + "," + radius + " 0 1,1 0," + +2 * radius + "z";
62946266
}
6267+
function d3_geo_pathProjectStream(project) {
6268+
var resample = d3_geo_resample(function(λ, φ) {
6269+
return project([ λ * d3_degrees, φ * d3_degrees ]);
6270+
});
6271+
return function(stream) {
6272+
stream = resample(stream);
6273+
return {
6274+
point: function(λ, φ) {
6275+
stream.point(λ * d3_radians, φ * d3_radians);
6276+
},
6277+
sphere: function() {
6278+
stream.sphere();
6279+
},
6280+
lineStart: function() {
6281+
stream.lineStart();
6282+
},
6283+
lineEnd: function() {
6284+
stream.lineEnd();
6285+
},
6286+
polygonStart: function() {
6287+
stream.polygonStart();
6288+
},
6289+
polygonEnd: function() {
6290+
stream.polygonEnd();
6291+
}
6292+
};
6293+
};
6294+
}
62956295
function d3_geo_pathBuffer() {
62966296
var pointCircle = d3_geo_pathCircle(4.5), buffer = [];
62976297
var stream = {

d3.min.js

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

src/geo/albers-usa.js

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,6 @@ d3.geo.albersUsa = function() {
3434
: lower48;
3535
}
3636

37-
var projectResample = d3_geo_resample(function(λ, φ) {
38-
return albersUsa([λ * d3_degrees, φ * d3_degrees]);
39-
});
40-
41-
albersUsa.stream = function(listener) {
42-
return d3_geo_albersUsaRadians(projectResample(listener));
43-
};
44-
4537
albersUsa.scale = function(x) {
4638
if (!arguments.length) return lower48.scale();
4739
lower48.scale(x);
@@ -65,16 +57,3 @@ d3.geo.albersUsa = function() {
6557

6658
return albersUsa.scale(lower48.scale());
6759
};
68-
69-
function d3_geo_albersUsaRadians(stream) {
70-
return {
71-
point: function(λ, φ) {
72-
stream.point(λ * d3_radians, φ * d3_radians);
73-
},
74-
sphere: function() { stream.sphere(); },
75-
lineStart: function() { stream.lineStart(); },
76-
lineEnd: function() { stream.lineEnd(); },
77-
polygonStart: function() { stream.polygonStart(); },
78-
polygonEnd: function() { stream.polygonEnd(); }
79-
};
80-
}

src/geo/path.js

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,30 @@
1-
// TODO fallback for projections that don't implement stream?
21
// TODO better encapsulation for d3_geo_pathArea; move to area.js
32
// TODO better encapsulation for d3_geo_pathCentroid; move to centroid.js
43

54
d3.geo.path = function() {
65
var pointRadius = 4.5,
7-
projection = d3.geo.albersUsa(),
6+
projection,
87
context,
9-
stream = new d3_geo_pathBuffer;
8+
projectStream,
9+
contextStream;
1010

1111
function path(object) {
12-
if (object) d3.geo.stream(object, projection.stream(stream.pointRadius(
13-
typeof pointRadius === "function"
14-
? +pointRadius.apply(this, arguments)
15-
: pointRadius
16-
)));
17-
return stream.result();
12+
if (object) d3.geo.stream(object, projectStream(
13+
contextStream.pointRadius(typeof pointRadius === "function"
14+
? +pointRadius.apply(this, arguments)
15+
: pointRadius)));
16+
return contextStream.result();
1817
}
1918

2019
path.area = function(object) {
2120
d3_geo_pathAreaSum = 0;
22-
d3.geo.stream(object, projection.stream(d3_geo_pathArea));
21+
d3.geo.stream(object, projectStream(d3_geo_pathArea));
2322
return d3_geo_pathAreaSum;
2423
};
2524

2625
path.centroid = function(object) {
2726
d3_geo_centroidDimension = d3_geo_centroidX = d3_geo_centroidY = d3_geo_centroidZ = 0;
28-
d3.geo.stream(object, projection.stream(d3_geo_pathCentroid));
27+
d3.geo.stream(object, projectStream(d3_geo_pathCentroid));
2928
return d3_geo_centroidZ ? [d3_geo_centroidX / d3_geo_centroidZ, d3_geo_centroidY / d3_geo_centroidZ] : undefined;
3029
};
3130

@@ -35,15 +34,13 @@ d3.geo.path = function() {
3534

3635
path.projection = function(_) {
3736
if (!arguments.length) return projection;
38-
projection = _;
37+
projectStream = (projection = _).stream ? projection.stream : d3_geo_pathProjectStream(projection);
3938
return path;
4039
};
4140

4241
path.context = function(_) {
4342
if (!arguments.length) return context;
44-
stream = (context = _) == null
45-
? new d3_geo_pathBuffer
46-
: new d3_geo_pathContext(_);
43+
contextStream = (context = _) == null ? new d3_geo_pathBuffer : new d3_geo_pathContext(_);
4744
return path;
4845
};
4946

@@ -53,7 +50,7 @@ d3.geo.path = function() {
5350
return path;
5451
};
5552

56-
return path;
53+
return path.projection(d3.geo.albersUsa()).context(null);
5754
};
5855

5956
function d3_geo_pathCircle(radius) {
@@ -62,3 +59,18 @@ function d3_geo_pathCircle(radius) {
6259
+ "a" + radius + "," + radius + " 0 1,1 0," + (+2 * radius)
6360
+ "z";
6461
}
62+
63+
function d3_geo_pathProjectStream(project) {
64+
var resample = d3_geo_resample(function(λ, φ) { return project([λ * d3_degrees, φ * d3_degrees]); });
65+
return function(stream) {
66+
stream = resample(stream);
67+
return {
68+
point: function(λ, φ) { stream.point(λ * d3_radians, φ * d3_radians); },
69+
sphere: function() { stream.sphere(); },
70+
lineStart: function() { stream.lineStart(); },
71+
lineEnd: function() { stream.lineEnd(); },
72+
polygonStart: function() { stream.polygonStart(); },
73+
polygonEnd: function() { stream.polygonEnd(); }
74+
};
75+
};
76+
}

0 commit comments

Comments
 (0)