Skip to content

Commit 604476d

Browse files
committed
Simplify d3.geo.bounds implementation.
Also, add rudimentary test.
1 parent 22fddfb commit 604476d

4 files changed

Lines changed: 69 additions & 143 deletions

File tree

d3.v2.js

Lines changed: 23 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -5656,70 +5656,30 @@
56565656
function d3_path_circle(radius) {
56575657
return "m0," + radius + "a" + radius + "," + radius + " 0 1,1 0," + -2 * radius + "a" + radius + "," + radius + " 0 1,1 0," + +2 * radius + "z";
56585658
}
5659-
d3.geo.bounds = function(feature) {
5660-
var left = Infinity, bottom = Infinity, right = -Infinity, top = -Infinity;
5661-
d3_geo_bounds(feature, function(x, y) {
5662-
if (x < left) left = x;
5663-
if (x > right) right = x;
5664-
if (y < bottom) bottom = y;
5665-
if (y > top) top = y;
5666-
});
5667-
return [ [ left, bottom ], [ right, top ] ];
5668-
};
5669-
function d3_geo_bounds(o, f) {
5670-
if (d3_geo_boundsTypes.hasOwnProperty(o.type)) d3_geo_boundsTypes[o.type](o, f);
5671-
}
5672-
var d3_geo_boundsTypes = {
5673-
Feature: d3_geo_boundsFeature,
5674-
FeatureCollection: d3_geo_boundsFeatureCollection,
5675-
GeometryCollection: d3_geo_boundsGeometryCollection,
5676-
LineString: d3_geo_boundsLineString,
5677-
MultiLineString: d3_geo_boundsMultiLineString,
5678-
MultiPoint: d3_geo_boundsLineString,
5679-
MultiPolygon: d3_geo_boundsMultiPolygon,
5680-
Point: d3_geo_boundsPoint,
5681-
Polygon: d3_geo_boundsPolygon
5682-
};
5683-
function d3_geo_boundsFeature(o, f) {
5684-
d3_geo_bounds(o.geometry, f);
5685-
}
5686-
function d3_geo_boundsFeatureCollection(o, f) {
5687-
for (var a = o.features, i = 0, n = a.length; i < n; i++) {
5688-
d3_geo_bounds(a[i].geometry, f);
5689-
}
5690-
}
5691-
function d3_geo_boundsGeometryCollection(o, f) {
5692-
for (var a = o.geometries, i = 0, n = a.length; i < n; i++) {
5693-
d3_geo_bounds(a[i], f);
5694-
}
5695-
}
5696-
function d3_geo_boundsLineString(o, f) {
5697-
for (var a = o.coordinates, i = 0, n = a.length; i < n; i++) {
5698-
f.apply(null, a[i]);
5699-
}
5700-
}
5701-
function d3_geo_boundsMultiLineString(o, f) {
5702-
for (var a = o.coordinates, i = 0, n = a.length; i < n; i++) {
5703-
for (var b = a[i], j = 0, m = b.length; j < m; j++) {
5704-
f.apply(null, b[j]);
5705-
}
5706-
}
5707-
}
5708-
function d3_geo_boundsMultiPolygon(o, f) {
5709-
for (var a = o.coordinates, i = 0, n = a.length; i < n; i++) {
5710-
for (var b = a[i][0], j = 0, m = b.length; j < m; j++) {
5711-
f.apply(null, b[j]);
5659+
d3.geo.bounds = function() {
5660+
var left, bottom, right, top, recurse = d3_geo_typeRecurse({
5661+
Point: function(o) {
5662+
o = o.coordinates;
5663+
var x = o[0], y = o[1];
5664+
if (x < left) left = x;
5665+
if (x > right) right = x;
5666+
if (y < bottom) bottom = y;
5667+
if (y > top) top = y;
5668+
},
5669+
Polygon: function(o) {
5670+
recurse({
5671+
type: "LineString",
5672+
coordinates: o.coordinates[0]
5673+
});
57125674
}
5713-
}
5714-
}
5715-
function d3_geo_boundsPoint(o, f) {
5716-
f.apply(null, o.coordinates);
5717-
}
5718-
function d3_geo_boundsPolygon(o, f) {
5719-
for (var a = o.coordinates[0], i = 0, n = a.length; i < n; i++) {
5720-
f.apply(null, a[i]);
5721-
}
5722-
}
5675+
});
5676+
return function(object) {
5677+
left = bottom = Infinity;
5678+
right = top = -Infinity;
5679+
recurse(object);
5680+
return [ [ left, bottom ], [ right, top ] ];
5681+
};
5682+
}();
57235683
d3.geo.circle = function() {
57245684
var origin = [ 0, 0 ], degrees = 90 - .01, radians = degrees * d3_geo_radians, arc = d3.geo.greatArc().source(origin).target(d3_identity);
57255685
function circle() {}

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/geo/bounds.js

Lines changed: 22 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -4,80 +4,26 @@
44
* top]], where left is the minimum longitude, bottom is the minimum latitude,
55
* right is maximum longitude, and top is the maximum latitude.
66
*/
7-
d3.geo.bounds = function(feature) {
8-
var left = Infinity,
9-
bottom = Infinity,
10-
right = -Infinity,
11-
top = -Infinity;
12-
d3_geo_bounds(feature, function(x, y) {
13-
if (x < left) left = x;
14-
if (x > right) right = x;
15-
if (y < bottom) bottom = y;
16-
if (y > top) top = y;
17-
});
18-
return [[left, bottom], [right, top]];
19-
};
20-
21-
function d3_geo_bounds(o, f) {
22-
if (d3_geo_boundsTypes.hasOwnProperty(o.type)) d3_geo_boundsTypes[o.type](o, f);
23-
}
24-
25-
var d3_geo_boundsTypes = {
26-
Feature: d3_geo_boundsFeature,
27-
FeatureCollection: d3_geo_boundsFeatureCollection,
28-
GeometryCollection: d3_geo_boundsGeometryCollection,
29-
LineString: d3_geo_boundsLineString,
30-
MultiLineString: d3_geo_boundsMultiLineString,
31-
MultiPoint: d3_geo_boundsLineString,
32-
MultiPolygon: d3_geo_boundsMultiPolygon,
33-
Point: d3_geo_boundsPoint,
34-
Polygon: d3_geo_boundsPolygon
35-
};
36-
37-
function d3_geo_boundsFeature(o, f) {
38-
d3_geo_bounds(o.geometry, f);
39-
}
40-
41-
function d3_geo_boundsFeatureCollection(o, f) {
42-
for (var a = o.features, i = 0, n = a.length; i < n; i++) {
43-
d3_geo_bounds(a[i].geometry, f);
44-
}
45-
}
46-
47-
function d3_geo_boundsGeometryCollection(o, f) {
48-
for (var a = o.geometries, i = 0, n = a.length; i < n; i++) {
49-
d3_geo_bounds(a[i], f);
50-
}
51-
}
52-
53-
function d3_geo_boundsLineString(o, f) {
54-
for (var a = o.coordinates, i = 0, n = a.length; i < n; i++) {
55-
f.apply(null, a[i]);
56-
}
57-
}
58-
59-
function d3_geo_boundsMultiLineString(o, f) {
60-
for (var a = o.coordinates, i = 0, n = a.length; i < n; i++) {
61-
for (var b = a[i], j = 0, m = b.length; j < m; j++) {
62-
f.apply(null, b[j]);
63-
}
64-
}
65-
}
66-
67-
function d3_geo_boundsMultiPolygon(o, f) {
68-
for (var a = o.coordinates, i = 0, n = a.length; i < n; i++) {
69-
for (var b = a[i][0], j = 0, m = b.length; j < m; j++) {
70-
f.apply(null, b[j]);
7+
d3.geo.bounds = (function() {
8+
var left, bottom, right, top,
9+
recurse = d3_geo_typeRecurse({
10+
Point: function(o) {
11+
o = o.coordinates;
12+
var x = o[0], y = o[1];
13+
if (x < left) left = x;
14+
if (x > right) right = x;
15+
if (y < bottom) bottom = y;
16+
if (y > top) top = y;
17+
},
18+
Polygon: function(o) {
19+
// Only check bounds of exterior ring.
20+
recurse({type: "LineString", coordinates: o.coordinates[0]});
7121
}
72-
}
73-
}
74-
75-
function d3_geo_boundsPoint(o, f) {
76-
f.apply(null, o.coordinates);
77-
}
78-
79-
function d3_geo_boundsPolygon(o, f) {
80-
for (var a = o.coordinates[0], i = 0, n = a.length; i < n; i++) {
81-
f.apply(null, a[i]);
82-
}
83-
}
22+
});
23+
return function(object) {
24+
left = bottom = Infinity;
25+
right = top = -Infinity;
26+
recurse(object);
27+
return [[left, bottom], [right, top]];
28+
};
29+
})();

test/geo/bounds-test.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
require("../env");
2+
3+
var vows = require("vows"),
4+
assert = require("assert");
5+
6+
var suite = vows.describe("d3.geo.bounds");
7+
8+
suite.addBatch({
9+
"bounds": {
10+
topic: function() { return d3.geo.bounds; },
11+
"simple": function(bounds) {
12+
assert.deepEqual(bounds({
13+
type: "MultiPoint",
14+
coordinates: [[0, 0], [-1, -1], [-1, 1], [1, 1], [1, -1], [.5, -.5]]
15+
}), [[-1, -1], [1, 1]]);
16+
}
17+
}
18+
});
19+
20+
suite.export(module);

0 commit comments

Comments
 (0)