#!/usr/bin/env node var fs = require("fs"), os = require("os"), rw = require("rw").dash, d3 = require("d3"), optimist = require("optimist"), topojson = require("../"); var argv = optimist .usage("Usage: \033[1mtopojson-svg\033[0m [options] -- file\n\n" + "Version: " + topojson.version + "\n\n" + "Converts a TopoJSON file to SVG. Assumes the input is already projected\n" + "and simplified as needed; if not, use \033[1mtopojson\033[0m to process the TopoJSON\n" + "file first, and then pass the output to this program.") .options("o", { alias: "out", describe: "output SVG file name", default: "-" }) .options("width", { describe: "width of the output SVG", default: null }) .options("height", { describe: "height of the output SVG", default: null }) .options("precision", { describe: "number of digits after the decimal place to output", default: undefined }) .options("help", { describe: "display this helpful message", type: "boolean", default: false }) .check(function(argv) { if (argv.help) return; if (argv._.length > 1) throw new Error("at most one input file allowed"); if (!argv._.length) argv._ = ["-"]; }) .argv; if (argv.help) return optimist.showHelp(); var precision = Math.pow(10, argv.precision); var topology = JSON.parse(rw.readFileSync(argv._[0], "utf8")); var path = d3.geo.path().projection(isNaN(precision) ? null : d3.geo.transform({ point: function(x, y) { this.stream.point( Math.round(x * precision) / precision, Math.round(y * precision) / precision ); } })); var out = (argv.o === "-" || argv.o === "/dev/stdout") ? process.stdout : fs.createWriteStream(argv.o, "utf8"); out.write("" + os.EOL); out.write("" + os.EOL); out.write("" + os.EOL); for (var key in topology.objects) { out.write(" " + os.EOL); var object = topology.objects[key]; if (object.type === "GeometryCollection") object.geometries.forEach(outputGeometry); else outputGeometry(object); out.write(" " + os.EOL); } out.write("" + os.EOL); if (out !== process.stdout) out.end(); function quote(string) { return string.replace(/"/g, """); } function outputGeometry(geometry) { out.write(" " + os.EOL); }