Permalink
Browse files

Add support for Point and MultiPoint geometries.

  • Loading branch information...
1 parent e551cb8 commit 9e50072119bd6ea5ad0ffc3083cc6a333a3f5210 @mbostock mbostock committed Dec 29, 2012
Showing with 43 additions and 9 deletions.
  1. +18 −3 lib/topojson/topology.js
  2. +1 −1 package.json
  3. +13 −0 test/topology-test.js
  4. +11 −5 topojson.js
@@ -98,7 +98,7 @@ module.exports = function(objects, options) {
type: "GeometryCollection",
geometries: collection.features
.map(this.Feature, this)
- .filter(function(geometry) { return geometry.arcs.length; })
+ .filter(function(geometry) { return (geometry.arcs || geometry.coordinates).length; })
};
},
@@ -107,7 +107,7 @@ module.exports = function(objects, options) {
type: "GeometryCollection",
geometries: collection.geometries
.map(this.geometry, this)
- .filter(function(geometry) { return geometry.arcs.length; })
+ .filter(function(geometry) { return (geometry.arcs || geometry.coordinates).length; })
};
},
@@ -133,6 +133,14 @@ module.exports = function(objects, options) {
lineString.arcs = lineOpen(lineString.coordinates);
},
+ MultiPoint: function(multiPoint) {
+ multiPoint.coordinates = multiPoint.coordinates.map(fixedPoint);
+ },
+
+ Point: function(point) {
+ point.coordinates = fixedPoint(point.coordinates);
+ },
+
geometry: function(geometry) {
this.defaults.geometry.call(this, geometry);
geometry.id = id(geometry);
@@ -149,11 +157,18 @@ module.exports = function(objects, options) {
}
}
- delete geometry.coordinates;
+ if (geometry.arcs) delete geometry.coordinates;
return geometry;
}
});
+ function fixedPoint(coordinates) {
+ return [
+ (coordinates[0] - x0) * kx | 0,
+ (coordinates[1] - y0) * ky | 0
+ ];
+ }
+
function lineClosed(points) {
return line(points, false);
}
View
@@ -1,6 +1,6 @@
{
"name": "topojson",
- "version": "0.0.4",
+ "version": "0.0.5",
"description": "An extension to GeoJSON that encodes topology.",
"keywords": [
"geojson",
View
@@ -101,6 +101,19 @@ suite.addBatch({
assert.deepEqual(topology.arcs[0], [[0, 0], [1, 0], [0, 1], [-1, 0], [0, -1]]);
},
+ // TopoJSON uses integers with for points, also. However, there’s no delta-
+ // encoding, even for MultiPoints. And, unlike other geometry objects,
+ // points are still defined with coordinates rather than arcs.
+ "points coordinates are integers with delta encoding": function() {
+ var topology = topojson.topology({foo: {type: "Point", coordinates: [1/8, 1/16]}, bar: {type: "Point", coordinates: [1/2, 1/4]}}, {quantization: 2});
+ assert.deepEqual(topology.arcs, []);
+ assert.deepEqual(topology.objects.foo, {type: "Point", coordinates: [0, 0]});
+ assert.deepEqual(topology.objects.bar, {type: "Point", coordinates: [1, 1]});
+ var topology = topojson.topology({foo: {type: "MultiPoint", coordinates: [[1/8, 1/16], [1/2, 1/4]]}}, {quantization: 2});
+ assert.deepEqual(topology.arcs, []);
+ assert.deepEqual(topology.objects.foo, {type: "MultiPoint", coordinates: [[0, 0], [1, 1]]});
+ },
+
// GeoJSON inputs are in floating point format, so some error may creep in
// that prevents you from using exact match to determine shared points. The
// default quantization, 1e4, allows for 10,000 differentiable points in
View
@@ -161,6 +161,10 @@ topojson = (function() {
if (i < 0) reverse(points, n);
}
+ function point(coordinates) {
+ return [coordinates[0] * kx + dx, coordinates[1] * ky + dy];
+ }
+
function line(arcs) {
var points = [];
for (var i = 0, n = arcs.length; i < n; ++i) arc(arcs[i], points);
@@ -173,15 +177,17 @@ topojson = (function() {
function geometry(o) {
o = Object.create(o);
- o.coordinates = geometryType[o.type](o.arcs);
+ o.coordinates = geometryType[o.type](o);
return o;
}
var geometryType = {
- LineString: line,
- MultiLineString: polygon,
- Polygon: polygon,
- MultiPolygon: function(arcs) { return arcs.map(polygon); }
+ Point: function(o) { return point(o.coordinates); },
+ MultiPoint: function(o) { return o.coordinates.map(point); },
+ LineString: function(o) { return line(o.arcs); },
+ MultiLineString: function(o) { return polygon(o.arcs); },
+ Polygon: function(o) { return polygon(o.arcs); },
+ MultiPolygon: function(o) { return o.arcs.map(polygon); }
};
return o.type === "GeometryCollection"

0 comments on commit 9e50072

Please sign in to comment.