Skip to content

Commit 34fa7ea

Browse files
committed
Don't expose the entire map, just a function.
1 parent 30beedd commit 34fa7ea

8 files changed

Lines changed: 95 additions & 138 deletions

File tree

d3.js

Lines changed: 49 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -5576,12 +5576,10 @@
55765576
}
55775577
}
55785578
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+
};
55855583
}
55865584
var d3_geo_typeDefaults = {
55875585
Feature: function(o) {
@@ -5626,9 +5624,6 @@
56265624
return c && (o = Object.create(o), o.coordinates = c, o);
56275625
},
56285626
Sphere: d3_noop,
5629-
object: function(o) {
5630-
return d3_geo_typeObjects.hasOwnProperty(o.type) ? this[o.type](o) : this.geometry(o);
5631-
},
56325627
geometry: function(o) {
56335628
return d3_geo_typeGeometries.hasOwnProperty(o.type) ? this[o.type](o) : null;
56345629
},
@@ -5750,14 +5745,11 @@
57505745
});
57515746
return function(feature) {
57525747
y1 = x1 = -(x0 = y0 = Infinity);
5753-
bounds.object(feature);
5748+
bounds(feature);
57545749
return [ [ x0, y0 ], [ x1, y1 ] ];
57555750
};
57565751
}
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({
57615753
FeatureCollection: d3_noop,
57625754
GeometryCollection: d3_noop,
57635755
Feature: function(feature) {
@@ -6163,7 +6155,7 @@
61636155
var result = null;
61646156
if (object != result) {
61656157
if (typeof pointRadius === "function") pointCircle = d3_geo_pathCircle(pointRadius.apply(this, arguments));
6166-
pathType.object(object);
6158+
pathType(object);
61676159
if (buffer.length) result = buffer.join(""), buffer = [];
61686160
}
61696161
return result;
@@ -6182,15 +6174,25 @@
61826174
projection.sphere(context);
61836175
}
61846176
});
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({
61866188
Feature: function(feature) {
6187-
return areaType.geometry(feature.geometry);
6189+
return this.geometry(feature.geometry);
61886190
},
61896191
FeatureCollection: function(collection) {
6190-
return d3.sum(collection.features, areaType.Feature);
6192+
return d3.sum(collection.features, this.Feature);
61916193
},
61926194
GeometryCollection: function(collection) {
6193-
return d3.sum(collection.geometries, areaType.geometry);
6195+
return d3.sum(collection.geometries, this.geometry);
61946196
},
61956197
LineString: d3_zero,
61966198
MultiLineString: d3_zero,
@@ -6204,24 +6206,21 @@
62046206
},
62056207
Sphere: sphereArea
62066208
});
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+
};
62166215
}
6217-
path.area = function(object) {
6218-
return areaType.object(object);
6216+
path.bounds = function(object) {
6217+
return (bounds || (bounds = d3_geo_bounds(projection)))(object);
62196218
};
6220-
var centroidType = d3_geo_type({
6219+
path.centroid = d3_geo_type({
62216220
FeatureCollection: d3_noop,
62226221
GeometryCollection: d3_noop,
62236222
Feature: function(feature) {
6224-
return centroidType.geometry(feature.geometry);
6223+
return this.geometry(feature.geometry);
62256224
},
62266225
LineString: weightedCentroid(function(lineString) {
62276226
projection.line(lineString.coordinates, lineCentroidContext);
@@ -6248,19 +6247,6 @@
62486247
projection.sphere(polygonCentroidContext);
62496248
})
62506249
});
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-
};
62646250
path.projection = function(_) {
62656251
if (!arguments.length) return projection;
62666252
projection = _;
@@ -6283,34 +6269,34 @@
62836269
function d3_geo_pathCircle(radius) {
62846270
return "m0," + radius + "a" + radius + "," + radius + " 0 1,1 0," + -2 * radius + "a" + radius + "," + radius + " 0 1,1 0," + +2 * radius + "z";
62856271
}
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({
62906273
Point: d3_zero,
62916274
MultiPoint: d3_zero,
62926275
LineString: d3_zero,
62936276
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);
62966279
},
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);
62996282
},
63006283
Sphere: function() {
63016284
return 4 * π;
63026285
},
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);
63086288
},
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+
});
63116294
},
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+
});
63146300
}
63156301
});
63166302
function d3_geo_areaPolygon(polygon) {
@@ -6480,16 +6466,13 @@
64806466
}
64816467
d3.geo.rotation = function(δλ, δφ, δγ) {
64826468
var rotate = d3_geo_rotation(δλ * d3_radians, δφ * d3_radians, δγ * d3_radians);
6483-
var type = d3_geo_type({
6469+
return d3_geo_type({
64846470
point: function(coordinates) {
64856471
coordinates = rotate(coordinates[0] * d3_radians, coordinates[1] * d3_radians);
64866472
coordinates[0] *= d3_degrees, coordinates[1] *= d3_degrees;
64876473
return coordinates;
64886474
}
64896475
});
6490-
return function(o) {
6491-
return type.object(o);
6492-
};
64936476
};
64946477
function d3_geo_rotation(δλ, δφ, δγ) {
64956478
return δλ ? δφ || δγ ? d3_geo_compose(d3_geo_rotationλ(δλ), d3_geo_rotationφγ(δφ, δγ)) : d3_geo_rotationλ(δλ) : δφ || δγ ? d3_geo_rotationφγ(δφ, δγ) : d3_geo_identityRotation;

d3.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/area.js

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,14 @@
1-
d3.geo.area = function(object) {
2-
return d3_geo_areaType.object(object);
3-
};
4-
5-
var d3_geo_areaType = d3_geo_type({
1+
d3.geo.area = d3_geo_type({
62
Point: d3_zero,
73
MultiPoint: d3_zero,
84
LineString: d3_zero,
95
MultiLineString: d3_zero,
10-
Polygon: function(polygon) { return d3_geo_areaPolygon(polygon.coordinates); },
11-
MultiPolygon: function(multiPolygon) { return d3.sum(multiPolygon.coordinates, d3_geo_areaPolygon); },
6+
Polygon: function(o) { return d3_geo_areaPolygon(o.coordinates); },
7+
MultiPolygon: function(o) { return d3.sum(o.coordinates, d3_geo_areaPolygon); },
128
Sphere: function() { return 4 * π; },
13-
Feature: function(feature) { return d3_geo_areaType.geometry(feature.geometry); },
14-
FeatureCollection: function(collection) { return d3.sum(collection.features, d3_geo_areaType.Feature); },
15-
GeometryCollection: function(collection) { return d3.sum(collection.geometries, d3_geo_areaType.geometry); },
16-
geometry: function(geometry) { return d3_geo_areaType[geometry.type](geometry); }
9+
Feature: function(o) { return this.geometry(o.geometry); },
10+
FeatureCollection: function(o) { var that = this; return d3.sum(o.features, function(o) { return that.Feature(o); }); },
11+
GeometryCollection: function(o) { var that = this; return d3.sum(o.geometries, function(o) { return that.geometry(o); }); }
1712
});
1813

1914
function d3_geo_areaPolygon(polygon) {

src/geo/bounds.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ function d3_geo_bounds(projection) {
1717

1818
return function(feature) {
1919
y1 = x1 = -(x0 = y0 = Infinity);
20-
bounds.object(feature);
20+
bounds(feature);
2121
return [[x0, y0], [x1, y1]];
2222
};
2323
}

src/geo/centroid.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
1-
d3.geo.centroid = function(object) {
2-
return d3_geo_centroidType.object(object);
3-
};
4-
5-
var d3_geo_centroidType = d3_geo_type({
1+
d3.geo.centroid = d3_geo_type({
62

73
FeatureCollection: d3_noop,
84
GeometryCollection: d3_noop,

src/geo/path.js

Lines changed: 27 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ d3.geo.path = function() {
6666
var result = null;
6767
if (object != result) {
6868
if (typeof pointRadius === "function") pointCircle = d3_geo_pathCircle(pointRadius.apply(this, arguments));
69-
pathType.object(object);
69+
pathType(object);
7070
if (buffer.length) result = buffer.join(""), buffer = [];
7171
}
7272
return result;
@@ -79,19 +79,6 @@ d3.geo.path = function() {
7979
Sphere: function() { projection.sphere(context); }
8080
});
8181

82-
var areaType = d3_geo_type({
83-
Feature: function(feature) { return areaType.geometry(feature.geometry); },
84-
FeatureCollection: function(collection) { return d3.sum(collection.features, areaType.Feature); },
85-
GeometryCollection: function(collection) { return d3.sum(collection.geometries, areaType.geometry); },
86-
LineString: d3_zero,
87-
MultiLineString: d3_zero,
88-
MultiPoint: d3_zero,
89-
MultiPolygon: function(multiPolygon) { return d3.sum(multiPolygon.coordinates, polygonArea); },
90-
Point: d3_zero,
91-
Polygon: function(polygon) { return polygonArea(polygon.coordinates); },
92-
Sphere: sphereArea
93-
});
94-
9582
function polygonArea(coordinates) {
9683
area = 0;
9784
projection.polygon(coordinates, areaContext);
@@ -104,12 +91,35 @@ d3.geo.path = function() {
10491
return Math.abs(area) / 2;
10592
}
10693

107-
path.area = function(object) { return areaType.object(object); };
94+
path.area = d3_geo_type({
95+
Feature: function(feature) { return this.geometry(feature.geometry); },
96+
FeatureCollection: function(collection) { return d3.sum(collection.features, this.Feature); },
97+
GeometryCollection: function(collection) { return d3.sum(collection.geometries, this.geometry); },
98+
LineString: d3_zero,
99+
MultiLineString: d3_zero,
100+
MultiPoint: d3_zero,
101+
MultiPolygon: function(multiPolygon) { return d3.sum(multiPolygon.coordinates, polygonArea); },
102+
Point: d3_zero,
103+
Polygon: function(polygon) { return polygonArea(polygon.coordinates); },
104+
Sphere: sphereArea
105+
});
108106

109-
var centroidType = d3_geo_type({
107+
function weightedCentroid(f) {
108+
return function() {
109+
centroidWeight = cx = cy = 0;
110+
f.apply(this, arguments);
111+
return centroidWeight ? [cx / centroidWeight, cy / centroidWeight] : null;
112+
};
113+
}
114+
115+
path.bounds = function(object) {
116+
return (bounds || (bounds = d3_geo_bounds(projection)))(object);
117+
};
118+
119+
path.centroid = d3_geo_type({
110120
FeatureCollection: d3_noop,
111121
GeometryCollection: d3_noop,
112-
Feature: function(feature) { return centroidType.geometry(feature.geometry); },
122+
Feature: function(feature) { return this.geometry(feature.geometry); },
113123

114124
LineString: weightedCentroid(function(lineString) {
115125
projection.line(lineString.coordinates, lineCentroidContext);
@@ -141,22 +151,6 @@ d3.geo.path = function() {
141151
Sphere: weightedCentroid(function() { projection.sphere(polygonCentroidContext); })
142152
});
143153

144-
function weightedCentroid(f) {
145-
return function() {
146-
centroidWeight = cx = cy = 0;
147-
f.apply(this, arguments);
148-
return centroidWeight ? [cx / centroidWeight, cy / centroidWeight] : null;
149-
};
150-
}
151-
152-
path.bounds = function(object) {
153-
return (bounds || (bounds = d3_geo_bounds(projection)))(object);
154-
};
155-
156-
path.centroid = function(object) {
157-
return centroidType.object(object);
158-
};
159-
160154
path.projection = function(_) {
161155
if (!arguments.length) return projection;
162156
projection = _;

src/geo/rotation.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,13 @@
11
d3.geo.rotation = function(δλ, δφ, δγ) {
22
var rotate = d3_geo_rotation(δλ * d3_radians, δφ * d3_radians, δγ * d3_radians);
33

4-
var type = d3_geo_type({
4+
return d3_geo_type({
55
point: function(coordinates) {
66
coordinates = rotate(coordinates[0] * d3_radians, coordinates[1] * d3_radians);
77
coordinates[0] *= d3_degrees, coordinates[1] *= d3_degrees;
88
return coordinates;
99
}
1010
});
11-
12-
return function(o) {
13-
return type.object(o);
14-
};
1511
};
1612

1713
// Note: |δλ| and |δφ| must be < 2π

src/geo/type.js

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
// TODO optimized path when no object creation is desired?
2-
// TODO return bound object function, rather than a map of types?
32

43
function d3_geo_type(types) {
5-
for (var type in d3_geo_typeDefaults) {
6-
if (!(type in types)) {
7-
types[type] = d3_geo_typeDefaults[type];
8-
}
9-
}
10-
return types;
4+
for (var t in d3_geo_typeDefaults) if (!(t in types)) types[t] = d3_geo_typeDefaults[t];
5+
return function(o) {
6+
return d3_geo_typeObjects.hasOwnProperty(o.type)
7+
? types[o.type](o)
8+
: types.geometry(o);
9+
};
1110
}
1211

1312
var d3_geo_typeDefaults = {
@@ -64,12 +63,6 @@ var d3_geo_typeDefaults = {
6463

6564
Sphere: d3_noop,
6665

67-
object: function(o) {
68-
return d3_geo_typeObjects.hasOwnProperty(o.type)
69-
? this[o.type](o)
70-
: this.geometry(o);
71-
},
72-
7366
geometry: function(o) {
7467
return d3_geo_typeGeometries.hasOwnProperty(o.type)
7568
? this[o.type](o)

0 commit comments

Comments
 (0)