|
5576 | 5576 | } |
5577 | 5577 | } |
5578 | 5578 | function d3_geo_type(types) { |
5579 | | - for (var type in d3_geo_typeDefaults) { |
5580 | | - if (!(type in types)) { |
5581 | | - types[type] = d3_geo_typeDefaults[type]; |
5582 | | - } |
5583 | | - } |
5584 | | - return types; |
| 5579 | + for (var t in d3_geo_typeDefaults) if (!(t in types)) types[t] = d3_geo_typeDefaults[t]; |
| 5580 | + return function(o) { |
| 5581 | + return d3_geo_typeObjects.hasOwnProperty(o.type) ? types[o.type](o) : types.geometry(o); |
| 5582 | + }; |
5585 | 5583 | } |
5586 | 5584 | var d3_geo_typeDefaults = { |
5587 | 5585 | Feature: function(o) { |
|
5626 | 5624 | return c && (o = Object.create(o), o.coordinates = c, o); |
5627 | 5625 | }, |
5628 | 5626 | Sphere: d3_noop, |
5629 | | - object: function(o) { |
5630 | | - return d3_geo_typeObjects.hasOwnProperty(o.type) ? this[o.type](o) : this.geometry(o); |
5631 | | - }, |
5632 | 5627 | geometry: function(o) { |
5633 | 5628 | return d3_geo_typeGeometries.hasOwnProperty(o.type) ? this[o.type](o) : null; |
5634 | 5629 | }, |
|
5750 | 5745 | }); |
5751 | 5746 | return function(feature) { |
5752 | 5747 | y1 = x1 = -(x0 = y0 = Infinity); |
5753 | | - bounds.object(feature); |
| 5748 | + bounds(feature); |
5754 | 5749 | return [ [ x0, y0 ], [ x1, y1 ] ]; |
5755 | 5750 | }; |
5756 | 5751 | } |
5757 | | - d3.geo.centroid = function(object) { |
5758 | | - return d3_geo_centroidType.object(object); |
5759 | | - }; |
5760 | | - var d3_geo_centroidType = d3_geo_type({ |
| 5752 | + d3.geo.centroid = d3_geo_type({ |
5761 | 5753 | FeatureCollection: d3_noop, |
5762 | 5754 | GeometryCollection: d3_noop, |
5763 | 5755 | Feature: function(feature) { |
|
6163 | 6155 | var result = null; |
6164 | 6156 | if (object != result) { |
6165 | 6157 | if (typeof pointRadius === "function") pointCircle = d3_geo_pathCircle(pointRadius.apply(this, arguments)); |
6166 | | - pathType.object(object); |
| 6158 | + pathType(object); |
6167 | 6159 | if (buffer.length) result = buffer.join(""), buffer = []; |
6168 | 6160 | } |
6169 | 6161 | return result; |
|
6182 | 6174 | projection.sphere(context); |
6183 | 6175 | } |
6184 | 6176 | }); |
6185 | | - var areaType = d3_geo_type({ |
| 6177 | + function polygonArea(coordinates) { |
| 6178 | + area = 0; |
| 6179 | + projection.polygon(coordinates, areaContext); |
| 6180 | + return Math.abs(area) / 2; |
| 6181 | + } |
| 6182 | + function sphereArea() { |
| 6183 | + area = 0; |
| 6184 | + projection.sphere(areaContext); |
| 6185 | + return Math.abs(area) / 2; |
| 6186 | + } |
| 6187 | + path.area = d3_geo_type({ |
6186 | 6188 | Feature: function(feature) { |
6187 | | - return areaType.geometry(feature.geometry); |
| 6189 | + return this.geometry(feature.geometry); |
6188 | 6190 | }, |
6189 | 6191 | FeatureCollection: function(collection) { |
6190 | | - return d3.sum(collection.features, areaType.Feature); |
| 6192 | + return d3.sum(collection.features, this.Feature); |
6191 | 6193 | }, |
6192 | 6194 | GeometryCollection: function(collection) { |
6193 | | - return d3.sum(collection.geometries, areaType.geometry); |
| 6195 | + return d3.sum(collection.geometries, this.geometry); |
6194 | 6196 | }, |
6195 | 6197 | LineString: d3_zero, |
6196 | 6198 | MultiLineString: d3_zero, |
|
6204 | 6206 | }, |
6205 | 6207 | Sphere: sphereArea |
6206 | 6208 | }); |
6207 | | - function polygonArea(coordinates) { |
6208 | | - area = 0; |
6209 | | - projection.polygon(coordinates, areaContext); |
6210 | | - return Math.abs(area) / 2; |
6211 | | - } |
6212 | | - function sphereArea() { |
6213 | | - area = 0; |
6214 | | - projection.sphere(areaContext); |
6215 | | - return Math.abs(area) / 2; |
| 6209 | + function weightedCentroid(f) { |
| 6210 | + return function() { |
| 6211 | + centroidWeight = cx = cy = 0; |
| 6212 | + f.apply(this, arguments); |
| 6213 | + return centroidWeight ? [ cx / centroidWeight, cy / centroidWeight ] : null; |
| 6214 | + }; |
6216 | 6215 | } |
6217 | | - path.area = function(object) { |
6218 | | - return areaType.object(object); |
| 6216 | + path.bounds = function(object) { |
| 6217 | + return (bounds || (bounds = d3_geo_bounds(projection)))(object); |
6219 | 6218 | }; |
6220 | | - var centroidType = d3_geo_type({ |
| 6219 | + path.centroid = d3_geo_type({ |
6221 | 6220 | FeatureCollection: d3_noop, |
6222 | 6221 | GeometryCollection: d3_noop, |
6223 | 6222 | Feature: function(feature) { |
6224 | | - return centroidType.geometry(feature.geometry); |
| 6223 | + return this.geometry(feature.geometry); |
6225 | 6224 | }, |
6226 | 6225 | LineString: weightedCentroid(function(lineString) { |
6227 | 6226 | projection.line(lineString.coordinates, lineCentroidContext); |
|
6248 | 6247 | projection.sphere(polygonCentroidContext); |
6249 | 6248 | }) |
6250 | 6249 | }); |
6251 | | - function weightedCentroid(f) { |
6252 | | - return function() { |
6253 | | - centroidWeight = cx = cy = 0; |
6254 | | - f.apply(this, arguments); |
6255 | | - return centroidWeight ? [ cx / centroidWeight, cy / centroidWeight ] : null; |
6256 | | - }; |
6257 | | - } |
6258 | | - path.bounds = function(object) { |
6259 | | - return (bounds || (bounds = d3_geo_bounds(projection)))(object); |
6260 | | - }; |
6261 | | - path.centroid = function(object) { |
6262 | | - return centroidType.object(object); |
6263 | | - }; |
6264 | 6250 | path.projection = function(_) { |
6265 | 6251 | if (!arguments.length) return projection; |
6266 | 6252 | projection = _; |
|
6283 | 6269 | function d3_geo_pathCircle(radius) { |
6284 | 6270 | return "m0," + radius + "a" + radius + "," + radius + " 0 1,1 0," + -2 * radius + "a" + radius + "," + radius + " 0 1,1 0," + +2 * radius + "z"; |
6285 | 6271 | } |
6286 | | - d3.geo.area = function(object) { |
6287 | | - return d3_geo_areaType.object(object); |
6288 | | - }; |
6289 | | - var d3_geo_areaType = d3_geo_type({ |
| 6272 | + d3.geo.area = d3_geo_type({ |
6290 | 6273 | Point: d3_zero, |
6291 | 6274 | MultiPoint: d3_zero, |
6292 | 6275 | LineString: d3_zero, |
6293 | 6276 | MultiLineString: d3_zero, |
6294 | | - Polygon: function(polygon) { |
6295 | | - return d3_geo_areaPolygon(polygon.coordinates); |
| 6277 | + Polygon: function(o) { |
| 6278 | + return d3_geo_areaPolygon(o.coordinates); |
6296 | 6279 | }, |
6297 | | - MultiPolygon: function(multiPolygon) { |
6298 | | - return d3.sum(multiPolygon.coordinates, d3_geo_areaPolygon); |
| 6280 | + MultiPolygon: function(o) { |
| 6281 | + return d3.sum(o.coordinates, d3_geo_areaPolygon); |
6299 | 6282 | }, |
6300 | 6283 | Sphere: function() { |
6301 | 6284 | return 4 * π; |
6302 | 6285 | }, |
6303 | | - Feature: function(feature) { |
6304 | | - return d3_geo_areaType.geometry(feature.geometry); |
6305 | | - }, |
6306 | | - FeatureCollection: function(collection) { |
6307 | | - return d3.sum(collection.features, d3_geo_areaType.Feature); |
| 6286 | + Feature: function(o) { |
| 6287 | + return this.geometry(o.geometry); |
6308 | 6288 | }, |
6309 | | - GeometryCollection: function(collection) { |
6310 | | - return d3.sum(collection.geometries, d3_geo_areaType.geometry); |
| 6289 | + FeatureCollection: function(o) { |
| 6290 | + var that = this; |
| 6291 | + return d3.sum(o.features, function(o) { |
| 6292 | + return that.Feature(o); |
| 6293 | + }); |
6311 | 6294 | }, |
6312 | | - geometry: function(geometry) { |
6313 | | - return d3_geo_areaType[geometry.type](geometry); |
| 6295 | + GeometryCollection: function(o) { |
| 6296 | + var that = this; |
| 6297 | + return d3.sum(o.geometries, function(o) { |
| 6298 | + return that.geometry(o); |
| 6299 | + }); |
6314 | 6300 | } |
6315 | 6301 | }); |
6316 | 6302 | function d3_geo_areaPolygon(polygon) { |
|
6480 | 6466 | } |
6481 | 6467 | d3.geo.rotation = function(δλ, δφ, δγ) { |
6482 | 6468 | var rotate = d3_geo_rotation(δλ * d3_radians, δφ * d3_radians, δγ * d3_radians); |
6483 | | - var type = d3_geo_type({ |
| 6469 | + return d3_geo_type({ |
6484 | 6470 | point: function(coordinates) { |
6485 | 6471 | coordinates = rotate(coordinates[0] * d3_radians, coordinates[1] * d3_radians); |
6486 | 6472 | coordinates[0] *= d3_degrees, coordinates[1] *= d3_degrees; |
6487 | 6473 | return coordinates; |
6488 | 6474 | } |
6489 | 6475 | }); |
6490 | | - return function(o) { |
6491 | | - return type.object(o); |
6492 | | - }; |
6493 | 6476 | }; |
6494 | 6477 | function d3_geo_rotation(δλ, δφ, δγ) { |
6495 | 6478 | return δλ ? δφ || δγ ? d3_geo_compose(d3_geo_rotationλ(δλ), d3_geo_rotationφγ(δφ, δγ)) : d3_geo_rotationλ(δλ) : δφ || δγ ? d3_geo_rotationφγ(δφ, δγ) : d3_geo_identityRotation; |
|
0 commit comments