Skip to content

Commit 6994640

Browse files
committed
Add d3.geo.stream.
This will replace d3_geo_type and make projection faster.
1 parent 876a654 commit 6994640

5 files changed

Lines changed: 159 additions & 4 deletions

File tree

Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,8 @@ d3.layout.js: \
180180

181181
d3.geo.js: \
182182
src/geo/geo.js \
183+
src/geo/stream.js \
184+
src/geo/stream-listener.js \
183185
src/geo/spherical.js \
184186
src/geo/cartesian.js \
185187
src/geo/type.js \

d3.js

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5333,6 +5333,79 @@
53335333
d3.csv = d3_dsv(",", "text/csv");
53345334
d3.tsv = d3_dsv(" ", "text/tab-separated-values");
53355335
d3.geo = {};
5336+
d3.geo.stream = function(object, listener) {
5337+
if (d3_geo_streamObjectType.hasOwnProperty(object.type)) {
5338+
d3_geo_streamObjectType[object.type](object, listener);
5339+
} else {
5340+
d3_geo_streamGeometry(object, listener);
5341+
}
5342+
};
5343+
function d3_geo_streamGeometry(geometry, listener) {
5344+
if (d3_geo_streamGeometryType.hasOwnProperty(geometry.type)) {
5345+
d3_geo_streamGeometryType[geometry.type](geometry, listener);
5346+
}
5347+
}
5348+
var d3_geo_streamObjectType = {
5349+
Feature: function(feature, listener) {
5350+
d3_geo_streamGeometry(feature.geometry, listener);
5351+
},
5352+
FeatureCollection: function(object, listener) {
5353+
var features = object.features, i = -1, n = features.length;
5354+
while (++i < n) d3_geo_streamGeometry(features[i].geometry, listener);
5355+
},
5356+
GeometryCollection: function(object, listener) {
5357+
var geometries = object.geometries, i = -1, n = geometries.length;
5358+
while (++i < n) d3_geo_streamGeometry(geometries[i], listener);
5359+
}
5360+
};
5361+
var d3_geo_streamGeometryType = {
5362+
Sphere: function(object, listener) {
5363+
listener.sphere();
5364+
},
5365+
Point: function(object, listener) {
5366+
var coordinate = object.coordinates;
5367+
listener.point(coordinate[0], coordinate[1]);
5368+
},
5369+
MultiPoint: function(object, listener) {
5370+
var coordinates = object.coordinates, i = -1, n = coordinates.length, coordinate;
5371+
while (++i < n) coordinate = coordinates[i], listener.point(coordinate[0], coordinate[1]);
5372+
},
5373+
LineString: function(object, listener) {
5374+
d3_geo_streamLine(object.coordinates, listener);
5375+
},
5376+
MultiLineString: function(object, listener) {
5377+
var coordinates = object.coordinates, i = -1, n = coordinates.length;
5378+
while (++i < n) d3_geo_streamLine(coordinates[i], listener);
5379+
},
5380+
Polygon: function(object, listener) {
5381+
d3_geo_streamPolygon(object.coordinates, listener);
5382+
},
5383+
MultiPolygon: function(object, listener) {
5384+
var coordinates = object.coordinates, i = -1, n = coordinates.length;
5385+
while (++i < n) d3_geo_streamPolygon(coordinates[i], listener);
5386+
}
5387+
};
5388+
function d3_geo_streamLine(coordinates, listener) {
5389+
var i = -1, n = coordinates.length, coordinate;
5390+
listener.lineStart();
5391+
while (++i < n) coordinate = coordinates[i], listener.point(coordinate[0], coordinate[1]);
5392+
listener.lineEnd();
5393+
}
5394+
function d3_geo_streamPolygon(coordinates, listener) {
5395+
var i = -1, n = coordinates.length;
5396+
listener.polygonStart();
5397+
while (++i < n) d3_geo_streamLine(coordinates[i], listener);
5398+
listener.polygonEnd();
5399+
}
5400+
var d3_geo_streamListener = d3.geo.streamListener = function() {};
5401+
d3_geo_streamListener.prototype = {
5402+
sphere: d3_noop,
5403+
point: d3_noop,
5404+
lineStart: d3_noop,
5405+
lineEnd: d3_noop,
5406+
polygonStart: d3_noop,
5407+
polygonEnd: d3_noop
5408+
};
53365409
function d3_geo_spherical(cartesian) {
53375410
return [ Math.atan2(cartesian[1], cartesian[0]), Math.asin(Math.max(-1, Math.min(1, cartesian[2]))) ];
53385411
}

d3.min.js

Lines changed: 5 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/geo/stream-listener.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
var d3_geo_streamListener = d3.geo.streamListener = function() {};
2+
3+
d3_geo_streamListener.prototype = {
4+
sphere: d3_noop,
5+
point: d3_noop,
6+
lineStart: d3_noop,
7+
lineEnd: d3_noop,
8+
polygonStart: d3_noop,
9+
polygonEnd: d3_noop
10+
};

src/geo/stream.js

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
d3.geo.stream = function(object, listener) {
2+
if (d3_geo_streamObjectType.hasOwnProperty(object.type)) {
3+
d3_geo_streamObjectType[object.type](object, listener);
4+
} else {
5+
d3_geo_streamGeometry(object, listener);
6+
}
7+
};
8+
9+
function d3_geo_streamGeometry(geometry, listener) {
10+
if (d3_geo_streamGeometryType.hasOwnProperty(geometry.type)) {
11+
d3_geo_streamGeometryType[geometry.type](geometry, listener);
12+
}
13+
}
14+
15+
var d3_geo_streamObjectType = {
16+
Feature: function(feature, listener) {
17+
d3_geo_streamGeometry(feature.geometry, listener);
18+
},
19+
FeatureCollection: function(object, listener) {
20+
var features = object.features, i = -1, n = features.length;
21+
while (++i < n) d3_geo_streamGeometry(features[i].geometry, listener);
22+
},
23+
GeometryCollection: function(object, listener) {
24+
var geometries = object.geometries, i = -1, n = geometries.length;
25+
while (++i < n) d3_geo_streamGeometry(geometries[i], listener);
26+
}
27+
};
28+
29+
var d3_geo_streamGeometryType = {
30+
Sphere: function(object, listener) {
31+
listener.sphere();
32+
},
33+
Point: function(object, listener) {
34+
var coordinate = object.coordinates;
35+
listener.point(coordinate[0], coordinate[1]);
36+
},
37+
MultiPoint: function(object, listener) {
38+
var coordinates = object.coordinates, i = -1, n = coordinates.length, coordinate;
39+
while (++i < n) coordinate = coordinates[i], listener.point(coordinate[0], coordinate[1]);
40+
},
41+
LineString: function(object, listener) {
42+
d3_geo_streamLine(object.coordinates, listener);
43+
},
44+
MultiLineString: function(object, listener) {
45+
var coordinates = object.coordinates, i = -1, n = coordinates.length;
46+
while (++i < n) d3_geo_streamLine(coordinates[i], listener);
47+
},
48+
Polygon: function(object, listener) {
49+
d3_geo_streamPolygon(object.coordinates, listener);
50+
},
51+
MultiPolygon: function(object, listener) {
52+
var coordinates = object.coordinates, i = -1, n = coordinates.length;
53+
while (++i < n) d3_geo_streamPolygon(coordinates[i], listener);
54+
}
55+
};
56+
57+
function d3_geo_streamLine(coordinates, listener) {
58+
var i = -1, n = coordinates.length, coordinate;
59+
listener.lineStart();
60+
while (++i < n) coordinate = coordinates[i], listener.point(coordinate[0], coordinate[1]);
61+
listener.lineEnd();
62+
}
63+
64+
function d3_geo_streamPolygon(coordinates, listener) {
65+
var i = -1, n = coordinates.length;
66+
listener.polygonStart();
67+
while (++i < n) d3_geo_streamLine(coordinates[i], listener);
68+
listener.polygonEnd();
69+
}

0 commit comments

Comments
 (0)