source: Dev/trunk/d3/src/geo/bounds.js @ 76

Last change on this file since 76 was 76, checked in by fpvanagthoven, 14 years ago

d3

File size: 2.0 KB
RevLine 
[76]1/**
2 * Given a GeoJSON object, returns the corresponding bounding box. The bounding
3 * box is represented by a two-dimensional array: [[left, bottom], [right,
4 * top]], where left is the minimum longitude, bottom is the minimum latitude,
5 * right is maximum longitude, and top is the maximum latitude.
6 */
7d3.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
21function d3_geo_bounds(o, f) {
22  if (o.type in d3_geo_boundsTypes) d3_geo_boundsTypes[o.type](o, f);
23}
24
25var d3_geo_boundsTypes = {
26  Feature: d3_geo_boundsFeature,
27  FeatureCollection: d3_geo_boundsFeatureCollection,
28  LineString: d3_geo_boundsLineString,
29  MultiLineString: d3_geo_boundsMultiLineString,
30  MultiPoint: d3_geo_boundsLineString,
31  MultiPolygon: d3_geo_boundsMultiPolygon,
32  Point: d3_geo_boundsPoint,
33  Polygon: d3_geo_boundsPolygon
34};
35
36function d3_geo_boundsFeature(o, f) {
37  d3_geo_bounds(o.geometry, f);
38}
39
40function d3_geo_boundsFeatureCollection(o, f) {
41  for (var a = o.features, i = 0, n = a.length; i < n; i++) {
42    d3_geo_bounds(a[i].geometry, f);
43  }
44}
45
46function d3_geo_boundsLineString(o, f) {
47  for (var a = o.coordinates, i = 0, n = a.length; i < n; i++) {
48    f.apply(null, a[i]);
49  }
50}
51
52function d3_geo_boundsMultiLineString(o, f) {
53  for (var a = o.coordinates, i = 0, n = a.length; i < n; i++) {
54    for (var b = a[i], j = 0, m = b.length; j < m; j++) {
55      f.apply(null, b[j]);
56    }
57  }
58}
59
60function d3_geo_boundsMultiPolygon(o, f) {
61  for (var a = o.coordinates, i = 0, n = a.length; i < n; i++) {
62    for (var b = a[i][0], j = 0, m = b.length; j < m; j++) {
63      f.apply(null, b[j]);
64    }
65  }
66}
67
68function d3_geo_boundsPoint(o, f) {
69  f.apply(null, o.coordinates);
70}
71
72function d3_geo_boundsPolygon(o, f) {
73  for (var a = o.coordinates[0], i = 0, n = a.length; i < n; i++) {
74    f.apply(null, a[i]);
75  }
76}
Note: See TracBrowser for help on using the repository browser.