Skip to content

Commit 508ae65

Browse files
committed
Add d3.geo.simplify.
Assumes that the third dimension (z) of each coordinate represents the importance of that coordinate, and implements a filter stream that skips coordinates that fail to meet the minimum importance threshold.
1 parent 36e5526 commit 508ae65

7 files changed

Lines changed: 97 additions & 82 deletions

File tree

d3.js

Lines changed: 49 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -2249,12 +2249,11 @@ d3 = function() {
22492249
listener.sphere();
22502250
},
22512251
Point: function(object, listener) {
2252-
var coordinate = object.coordinates;
2253-
listener.point(coordinate[0], coordinate[1]);
2252+
listener.point.apply(listener, object.coordinates);
22542253
},
22552254
MultiPoint: function(object, listener) {
2256-
var coordinates = object.coordinates, i = -1, n = coordinates.length, coordinate;
2257-
while (++i < n) coordinate = coordinates[i], listener.point(coordinate[0], coordinate[1]);
2255+
var coordinates = object.coordinates, i = -1, n = coordinates.length;
2256+
while (++i < n) listener.point.apply(listener, coordinates[i]);
22582257
},
22592258
LineString: function(object, listener) {
22602259
d3_geo_streamLine(object.coordinates, listener, 0);
@@ -2276,9 +2275,9 @@ d3 = function() {
22762275
}
22772276
};
22782277
function d3_geo_streamLine(coordinates, listener, closed) {
2279-
var i = -1, n = coordinates.length - closed, coordinate;
2278+
var i = -1, n = coordinates.length - closed;
22802279
listener.lineStart();
2281-
while (++i < n) coordinate = coordinates[i], listener.point(coordinate[0], coordinate[1]);
2280+
while (++i < n) listener.point.apply(listener, coordinates[i]);
22822281
listener.lineEnd();
22832282
}
22842283
function d3_geo_streamPolygon(coordinates, listener) {
@@ -2287,6 +2286,26 @@ d3 = function() {
22872286
while (++i < n) d3_geo_streamLine(coordinates[i], listener, 1);
22882287
listener.polygonEnd();
22892288
}
2289+
function d3_geo_streamTransform(stream, point) {
2290+
return {
2291+
point: point,
2292+
sphere: function() {
2293+
stream.sphere();
2294+
},
2295+
lineStart: function() {
2296+
stream.lineStart();
2297+
},
2298+
lineEnd: function() {
2299+
stream.lineEnd();
2300+
},
2301+
polygonStart: function() {
2302+
stream.polygonStart();
2303+
},
2304+
polygonEnd: function() {
2305+
stream.polygonEnd();
2306+
}
2307+
};
2308+
}
22902309
d3.geo.area = function(object) {
22912310
d3_geo_areaSum = 0;
22922311
d3.geo.stream(object, d3_geo_area);
@@ -3556,31 +3575,13 @@ d3 = function() {
35563575
return path.projection(d3.geo.albersUsa()).context(null);
35573576
};
35583577
function d3_geo_pathProjectStream(project) {
3559-
var resample = d3_geo_resample(function(λ, φ) {
3560-
return project([ λ * d3_degrees, φ * d3_degrees ]);
3578+
var resample = d3_geo_resample(function(x, y) {
3579+
return project([ x * d3_degrees, y * d3_degrees ]);
35613580
});
35623581
return function(stream) {
3563-
stream = resample(stream);
3564-
return {
3565-
point: function(λ, φ) {
3566-
stream.point(λ * d3_radians, φ * d3_radians);
3567-
},
3568-
sphere: function() {
3569-
stream.sphere();
3570-
},
3571-
lineStart: function() {
3572-
stream.lineStart();
3573-
},
3574-
lineEnd: function() {
3575-
stream.lineEnd();
3576-
},
3577-
polygonStart: function() {
3578-
stream.polygonStart();
3579-
},
3580-
polygonEnd: function() {
3581-
stream.polygonEnd();
3582-
}
3583-
};
3582+
return d3_geo_streamTransform(stream = resample(stream), function(x, y) {
3583+
stream.point(x * d3_radians, y * d3_radians);
3584+
});
35843585
};
35853586
}
35863587
d3.geo.projection = d3_geo_projection;
@@ -3663,27 +3664,10 @@ d3 = function() {
36633664
};
36643665
}
36653666
function d3_geo_projectionRadiansRotate(rotate, stream) {
3666-
return {
3667-
point: function(x, y) {
3668-
y = rotate(x * d3_radians, y * d3_radians), x = y[0];
3669-
stream.point(x > π ? x - 2 * π : x < -π ? x + 2 * π : x, y[1]);
3670-
},
3671-
sphere: function() {
3672-
stream.sphere();
3673-
},
3674-
lineStart: function() {
3675-
stream.lineStart();
3676-
},
3677-
lineEnd: function() {
3678-
stream.lineEnd();
3679-
},
3680-
polygonStart: function() {
3681-
stream.polygonStart();
3682-
},
3683-
polygonEnd: function() {
3684-
stream.polygonEnd();
3685-
}
3686-
};
3667+
return d3_geo_streamTransform(stream, function(x, y) {
3668+
y = rotate(x * d3_radians, y * d3_radians), x = y[0];
3669+
stream.point(x > π ? x - 2 * π : x < -π ? x + 2 * π : x, y[1]);
3670+
});
36873671
}
36883672
function d3_geo_equirectangular(λ, φ) {
36893673
return [ λ, φ ];
@@ -3953,6 +3937,21 @@ d3 = function() {
39533937
λ0 = λ, sinφ0 = sinφ, cosφ0 = cosφ;
39543938
}
39553939
}
3940+
d3.geo.simplify = function() {
3941+
var importance = 1, simplify = {
3942+
stream: function(stream) {
3943+
return d3_geo_streamTransform(stream, function(x, y, z) {
3944+
if (z >= importance) stream.point(x, y);
3945+
});
3946+
},
3947+
importance: function(_) {
3948+
if (!arguments.length) return importance;
3949+
importance = +_;
3950+
return simplify;
3951+
}
3952+
};
3953+
return simplify;
3954+
};
39563955
function d3_geo_azimuthal(scale, angle) {
39573956
function azimuthal(λ, φ) {
39583957
var cosλ = Math.cos(λ), cosφ = Math.cos(φ), k = scale(cosλ * cosφ);

d3.min.js

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

src/geo/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import "path-context";
1616
import "projection";
1717
import "rotation";
1818
import "stream";
19+
import "simplify";
1920
import "albers";
2021
import "albers-usa";
2122
import "azimuthal";

src/geo/path.js

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -82,16 +82,10 @@ d3.geo.path = function() {
8282
};
8383

8484
function d3_geo_pathProjectStream(project) {
85-
var resample = d3_geo_resample(function(λ, φ) { return project([λ * d3_degrees, φ * d3_degrees]); });
85+
var resample = d3_geo_resample(function(x, y) { return project([x * d3_degrees, y * d3_degrees]); });
8686
return function(stream) {
87-
stream = resample(stream);
88-
return {
89-
point: function(λ, φ) { stream.point(λ * d3_radians, φ * d3_radians); },
90-
sphere: function() { stream.sphere(); },
91-
lineStart: function() { stream.lineStart(); },
92-
lineEnd: function() { stream.lineEnd(); },
93-
polygonStart: function() { stream.polygonStart(); },
94-
polygonEnd: function() { stream.polygonEnd(); }
95-
};
87+
return d3_geo_streamTransform(stream = resample(stream), function(x, y) {
88+
stream.point(x * d3_radians, y * d3_radians);
89+
});
9690
};
9791
}

src/geo/projection.js

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -115,15 +115,8 @@ function d3_geo_projectionMutator(projectAt) {
115115
}
116116

117117
function d3_geo_projectionRadiansRotate(rotate, stream) {
118-
return {
119-
point: function(x, y) {
120-
y = rotate(x * d3_radians, y * d3_radians), x = y[0];
121-
stream.point(x > π ? x - 2 * π : x < -π ? x + 2 * π : x, y[1]);
122-
},
123-
sphere: function() { stream.sphere(); },
124-
lineStart: function() { stream.lineStart(); },
125-
lineEnd: function() { stream.lineEnd(); },
126-
polygonStart: function() { stream.polygonStart(); },
127-
polygonEnd: function() { stream.polygonEnd(); }
128-
};
118+
return d3_geo_streamTransform(stream, function(x, y) {
119+
y = rotate(x * d3_radians, y * d3_radians), x = y[0];
120+
stream.point(x > π ? x - 2 * π : x < -π ? x + 2 * π : x, y[1]);
121+
});
129122
}

src/geo/simplify.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import "geo";
2+
import "stream";
3+
4+
d3.geo.simplify = function() {
5+
var importance = 1, simplify = {
6+
stream: function(stream) {
7+
return d3_geo_streamTransform(stream, function(x, y, z) {
8+
if (z >= importance) stream.point(x, y);
9+
});
10+
},
11+
importance: function(_) {
12+
if (!arguments.length) return importance;
13+
importance = +_;
14+
return simplify;
15+
}
16+
};
17+
return simplify;
18+
};

src/geo/stream.js

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,11 @@ var d3_geo_streamGeometryType = {
2929
listener.sphere();
3030
},
3131
Point: function(object, listener) {
32-
var coordinate = object.coordinates;
33-
listener.point(coordinate[0], coordinate[1]);
32+
listener.point.apply(listener, object.coordinates);
3433
},
3534
MultiPoint: function(object, listener) {
36-
var coordinates = object.coordinates, i = -1, n = coordinates.length, coordinate;
37-
while (++i < n) coordinate = coordinates[i], listener.point(coordinate[0], coordinate[1]);
35+
var coordinates = object.coordinates, i = -1, n = coordinates.length;
36+
while (++i < n) listener.point.apply(listener, coordinates[i]);
3837
},
3938
LineString: function(object, listener) {
4039
d3_geo_streamLine(object.coordinates, listener, 0);
@@ -57,9 +56,9 @@ var d3_geo_streamGeometryType = {
5756
};
5857

5958
function d3_geo_streamLine(coordinates, listener, closed) {
60-
var i = -1, n = coordinates.length - closed, coordinate;
59+
var i = -1, n = coordinates.length - closed;
6160
listener.lineStart();
62-
while (++i < n) coordinate = coordinates[i], listener.point(coordinate[0], coordinate[1]);
61+
while (++i < n) listener.point.apply(listener, coordinates[i]);
6362
listener.lineEnd();
6463
}
6564

@@ -69,3 +68,14 @@ function d3_geo_streamPolygon(coordinates, listener) {
6968
while (++i < n) d3_geo_streamLine(coordinates[i], listener, 1);
7069
listener.polygonEnd();
7170
}
71+
72+
function d3_geo_streamTransform(stream, point) {
73+
return {
74+
point: point,
75+
sphere: function() { stream.sphere(); },
76+
lineStart: function() { stream.lineStart(); },
77+
lineEnd: function() { stream.lineEnd(); },
78+
polygonStart: function() { stream.polygonStart(); },
79+
polygonEnd: function() { stream.polygonEnd(); }
80+
};
81+
}

0 commit comments

Comments
 (0)