Skip to content

Commit 22fddfb

Browse files
committed
Add d3.geo.path.bounds.
This computes the bounding box for a given GeoJSON object using the current projection.
1 parent 38a6c30 commit 22fddfb

4 files changed

Lines changed: 139 additions & 4 deletions

File tree

d3.v2.js

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5430,6 +5430,53 @@
54305430
return object && types.hasOwnProperty(object.type) ? types[object.type](object) : defaultValue;
54315431
};
54325432
}
5433+
function d3_geo_typeRecurse(types, defaultValue) {
5434+
var type = d3_geo_type(types, defaultValue), defaults = {
5435+
Feature: function(o) {
5436+
type(o.geometry);
5437+
},
5438+
FeatureCollection: function(o) {
5439+
for (var a = o.features, i = 0, n = a.length; i < n; i++) {
5440+
type(a[i].geometry);
5441+
}
5442+
},
5443+
GeometryCollection: function(o) {
5444+
for (var a = o.geometries, i = 0, n = a.length; i < n; i++) {
5445+
type(a[i]);
5446+
}
5447+
},
5448+
LineString: multiPoint,
5449+
MultiPoint: multiPoint,
5450+
MultiLineString: multiLineString,
5451+
Polygon: multiLineString,
5452+
MultiPolygon: function(o) {
5453+
for (var a = o.coordinates, i = 0, n = a.length; i < n; i++) {
5454+
type({
5455+
type: "Polygon",
5456+
coordinates: a[i]
5457+
});
5458+
}
5459+
}
5460+
};
5461+
for (var k in defaults) if (!types.hasOwnProperty(k)) types[k] = defaults[k];
5462+
return type;
5463+
function multiPoint(o) {
5464+
for (var a = o.coordinates, i = 0, n = a.length; i < n; i++) {
5465+
type({
5466+
type: "Point",
5467+
coordinates: a[i]
5468+
});
5469+
}
5470+
}
5471+
function multiLineString(o) {
5472+
for (var a = o.coordinates, i = 0, n = a.length; i < n; i++) {
5473+
type({
5474+
type: "LineString",
5475+
coordinates: a[i]
5476+
});
5477+
}
5478+
}
5479+
}
54335480
d3.geo.path = function() {
54345481
var pointRadius = 4.5, pointCircle = d3_path_circle(pointRadius), projection = d3.geo.albersUsa(), buffer = [];
54355482
function path(d, i) {
@@ -5570,6 +5617,29 @@
55705617
function area(coordinates) {
55715618
return Math.abs(d3.geom.polygon(coordinates.map(projection)).area());
55725619
}
5620+
path.bounds = function() {
5621+
var x0, x1, y0, y1, recurse = d3_geo_typeRecurse({
5622+
Point: function(o) {
5623+
var p = projection(o.coordinates), x = p[0], y = p[1];
5624+
if (x < x0) x0 = x;
5625+
if (x > x1) x1 = x;
5626+
if (y < y0) y0 = y;
5627+
if (y > y1) y1 = y;
5628+
},
5629+
Polygon: function(o) {
5630+
recurse({
5631+
type: "LineString",
5632+
coordinates: o.coordinates[0]
5633+
});
5634+
}
5635+
});
5636+
return function(object) {
5637+
x0 = y0 = Infinity;
5638+
x1 = y1 = -Infinity;
5639+
recurse(object);
5640+
return [ [ x0, y0 ], [ x1, y1 ] ];
5641+
};
5642+
}();
55735643
path.projection = function(x) {
55745644
projection = x;
55755645
return path;

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/path.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,31 @@ d3.geo.path = function() {
235235
return Math.abs(d3.geom.polygon(coordinates.map(projection)).area());
236236
}
237237

238+
path.bounds = (function() {
239+
var x0, x1, y0, y1,
240+
recurse = d3_geo_typeRecurse({
241+
Point: function(o) {
242+
var p = projection(o.coordinates),
243+
x = p[0],
244+
y = p[1];
245+
if (x < x0) x0 = x;
246+
if (x > x1) x1 = x;
247+
if (y < y0) y0 = y;
248+
if (y > y1) y1 = y;
249+
},
250+
Polygon: function(o) {
251+
// Only check bounds of exterior ring.
252+
recurse({type: "LineString", coordinates: o.coordinates[0]});
253+
}
254+
});
255+
return function(object) {
256+
x0 = y0 = Infinity;
257+
x1 = y1 = -Infinity;
258+
recurse(object);
259+
return [[x0, y0], [x1, y1]];
260+
};
261+
})();
262+
238263
path.projection = function(x) {
239264
projection = x;
240265
return path;

src/geo/type.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,43 @@ function d3_geo_type(types, defaultValue) {
33
return object && types.hasOwnProperty(object.type) ? types[object.type](object) : defaultValue;
44
};
55
}
6+
7+
function d3_geo_typeRecurse(types, defaultValue) {
8+
var type = d3_geo_type(types, defaultValue),
9+
defaults = {
10+
Feature: function(o) { type(o.geometry); },
11+
FeatureCollection: function(o) {
12+
for (var a = o.features, i = 0, n = a.length; i < n; i++) {
13+
type(a[i].geometry);
14+
}
15+
},
16+
GeometryCollection: function(o) {
17+
for (var a = o.geometries, i = 0, n = a.length; i < n; i++) {
18+
type(a[i]);
19+
}
20+
},
21+
LineString: multiPoint,
22+
MultiPoint: multiPoint,
23+
MultiLineString: multiLineString,
24+
Polygon: multiLineString,
25+
MultiPolygon: function(o) {
26+
for (var a = o.coordinates, i = 0, n = a.length; i < n; i++) {
27+
type({type: "Polygon", coordinates: a[i]});
28+
}
29+
}
30+
};
31+
for (var k in defaults) if (!types.hasOwnProperty(k)) types[k] = defaults[k];
32+
return type;
33+
34+
function multiPoint(o) {
35+
for (var a = o.coordinates, i = 0, n = a.length; i < n; i++) {
36+
type({type: "Point", coordinates: a[i]});
37+
}
38+
}
39+
40+
function multiLineString(o) {
41+
for (var a = o.coordinates, i = 0, n = a.length; i < n; i++) {
42+
type({type: "LineString", coordinates: a[i]});
43+
}
44+
}
45+
}

0 commit comments

Comments
 (0)