Debian Codegraph @

topojson/bin/topojson-svg

raw
1 #!/usr/bin/env node
2
3 var fs = require("fs"),
4 os = require("os"),
5 rw = require("rw").dash,
6 d3 = require("d3"),
7 optimist = require("optimist"),
8 topojson = require("../");
9
10 var argv = optimist
11 .usage("Usage: \033[1mtopojson-svg\033[0m [options] -- file\n\n"
12
13 + "Version: " + topojson.version + "\n\n"
14
15 + "Converts a TopoJSON file to SVG. Assumes the input is already projected\n"
16 + "and simplified as needed; if not, use \033[1mtopojson\033[0m to process the TopoJSON\n"
17 + "file first, and then pass the output to this program.")
18
19 .options("o", {
20 alias: "out",
21 describe: "output SVG file name",
22 default: "-"
23 })
24 .options("width", {
25 describe: "width of the output SVG",
26 default: null
27 })
28 .options("height", {
29 describe: "height of the output SVG",
30 default: null
31 })
32 .options("precision", {
33 describe: "number of digits after the decimal place to output",
34 default: undefined
35 })
36 .options("help", {
37 describe: "display this helpful message",
38 type: "boolean",
39 default: false
40 })
41 .check(function(argv) {
42 if (argv.help) return;
43 if (argv._.length > 1) throw new Error("at most one input file allowed");
44 if (!argv._.length) argv._ = ["-"];
45 })
46 .argv;
47
48 if (argv.help) return optimist.showHelp();
49
50 var precision = Math.pow(10, argv.precision);
51
52 var topology = JSON.parse(rw.readFileSync(argv._[0], "utf8"));
53
54 var path = d3.geo.path().projection(isNaN(precision) ? null : d3.geo.transform({
55 point: function(x, y) {
56 this.stream.point(
57 Math.round(x * precision) / precision,
58 Math.round(y * precision) / precision
59 );
60 }
61 }));
62
63 var out = (argv.o === "-" || argv.o === "/dev/stdout")
64 ? process.stdout
65 : fs.createWriteStream(argv.o, "utf8");
66
67 out.write("<?xml version=\"1.0\" encoding=\"utf-8\"?>" + os.EOL);
68 out.write("<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\" \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">" + os.EOL);
69 out.write("<svg version=\"1.1\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\""
70 + (argv.width == null ? "" : " width=\"" + +argv.width + "\"")
71 + (argv.height == null ? "" : " height=\"" + +argv.height + "\"")
72 + (argv.width == null || argv.height == null ? "" : " viewBox=\"0 0 " + +argv.width + " " + +argv.height + "\"")
73 + ">" + os.EOL);
74
75 for (var key in topology.objects) {
76 out.write(" <g id=\"object-" + quote(key) + "\">" + os.EOL);
77 var object = topology.objects[key];
78 if (object.type === "GeometryCollection") object.geometries.forEach(outputGeometry);
79 else outputGeometry(object);
80 out.write(" </g>" + os.EOL);
81 }
82
83 out.write("</svg>" + os.EOL);
84
85 if (out !== process.stdout) out.end();
86
87 function quote(string) {
88 return string.replace(/"/g, "&quot;");
89 }
90
91 function outputGeometry(geometry) {
92 out.write(" <path");
93 if (geometry.id != null) out.write(" id=\"feature-" + quote(geometry.id + "") + "\"");
94 var d = path(topojson.feature(topology, geometry));
95 out.write((d == null ? "" : " d=\"" + d + "\"") + "></path>" + os.EOL);
96 }