Debian Codegraph @

topojson/bin/topojson-geojson

raw
1 #!/usr/bin/env node
2
3 var path = require("path"),
4 rw = require("rw").dash,
5 optimist = require("optimist"),
6 type = require("../lib/topojson/type"),
7 topojson = require("../");
8
9 var argv = optimist
10 .usage("Usage: \033[1mtopojson-geojson\033[0m [options] [file …]\n\n"
11
12 + "Converts the specified input TopoJSON objects to GeoJSON.")
13
14 .options("o", {
15 alias: "out",
16 describe: "output GeoJSON directory",
17 default: process.cwd()
18 })
19 .options("precision", {
20 describe: "number of digits after the decimal place to output",
21 default: undefined
22 })
23 .options("id-property", {
24 describe: "demote geometry id to specified feature property",
25 default: null
26 })
27 .options("help", {
28 describe: "display this helpful message",
29 type: "boolean",
30 default: false
31 })
32 .check(function(argv) {
33 if (argv.help) return;
34 if (argv._.length > 1) throw new Error("at most one input file allowed");
35 if (!argv._.length) argv._ = ["-"];
36 })
37 .argv;
38
39 if (argv.help) return optimist.showHelp();
40
41 var precision = Math.pow(10, argv.precision), round = isNaN(precision)
42 ? function(x) { return x; }
43 : function(x) { return Math.round(x * precision) / precision; };
44
45 // Convert TopoJSON back to GeoJSON.
46 var topology = JSON.parse(rw.readFileSync(argv._[0], "utf8"));
47 for (var key in topology.objects) {
48 var object = topojson.feature(topology, topology.objects[key]);
49
50 type({
51 Feature: function(feature) {
52 if (argv["id-property"] != null) {
53 feature.properties[argv["id-property"]] = feature.id;
54 delete feature.id;
55 }
56 return this.defaults.Feature.call(this, feature);
57 },
58 point: function(point) {
59 point[0] = round(point[0]);
60 point[1] = round(point[1]);
61 }
62 }).object(object);
63
64 rw.writeFileSync(path.join(argv.o, key + ".json"), JSON.stringify(object), "utf8");
65 }