toolkit/javascript/d3/src/geo/bounds.js
changeset 47 c0b4a8b5a012
equal deleted inserted replaced
46:efd9c589177a 47:c0b4a8b5a012
       
     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  */
       
     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 (o.type in d3_geo_boundsTypes) 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   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 
       
    36 function d3_geo_boundsFeature(o, f) {
       
    37   d3_geo_bounds(o.geometry, f);
       
    38 }
       
    39 
       
    40 function 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 
       
    46 function 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 
       
    52 function 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 
       
    60 function 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 
       
    68 function d3_geo_boundsPoint(o, f) {
       
    69   f.apply(null, o.coordinates);
       
    70 }
       
    71 
       
    72 function 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 }