Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 
 

75 righe
2.5 KiB

  1. #!/usr/bin/env node
  2. import {program} from "commander";
  3. import {readFileSync} from "fs";
  4. import {dirname, resolve} from "path";
  5. import {fileURLToPath} from "url";
  6. import {createContext, Script} from "vm";
  7. import * as d3Geo from "d3-geo";
  8. import * as d3GeoProjection from "../src/index.js";
  9. import {geoProject} from "../src/index.js";
  10. import read from "./read.js";
  11. import write from "./write.js";
  12. const version = JSON.parse(readFileSync(resolve(dirname(fileURLToPath(import.meta.url)), "../package.json"))).version;
  13. const d3 = {...d3Geo, ...d3GeoProjection};
  14. const options = program
  15. .version(version)
  16. .usage("[options] <projection> [file]")
  17. .description("Transform GeoJSON, such as to project from spherical to planar coordinates.")
  18. .option("-o, --out <file>", "output file name; defaults to “-” for stdout", "-")
  19. .option("-p, --precision <value>", "number of output digits after the decimal point")
  20. .option("-n, --newline-delimited", "use newline-delimited JSON")
  21. .option("-r, --require <[name=]module>", "require (import) a module", require, [])
  22. .parse(process.argv)
  23. .opts();
  24. if (program.args.length < 1) {
  25. console.error();
  26. console.error(" error: missing projection");
  27. console.error();
  28. process.exit(1);
  29. } else if (program.args.length > 2) {
  30. console.error();
  31. console.error(" error: multiple input files");
  32. console.error();
  33. process.exit(1);
  34. } else if (program.args.length === 1) {
  35. program.args.push("-");
  36. }
  37. var reader = projection().then(project => read(program.args[1], options.newlineDelimited, project)).then(end),
  38. writer = write(options.out);
  39. reader.catch(error => {
  40. console.error(error.stack);
  41. });
  42. async function projection() {
  43. const sandbox = {d3, d: undefined, i: undefined};
  44. for (const [name, module] of await Promise.all(options.require)) {
  45. sandbox[name] = sandbox[name] ? {...sandbox[name], ...module} : module;
  46. }
  47. const context = createContext(sandbox);
  48. const projection = new Script("(" + program.args[0] + ")");
  49. return function project(d, i) {
  50. sandbox.d = d, sandbox.i = i;
  51. d = geoProject(d, projection.runInContext(context));
  52. if (options.precision != null) d = geoQuantize(d, options.precision);
  53. return writer.write(JSON.stringify(d) + "\n");
  54. };
  55. }
  56. function end() {
  57. return writer.end();
  58. }
  59. function require(module, requires) {
  60. var i = module.indexOf("="), name = module;
  61. if (i >= 0) name = module.slice(0, i), module = module.slice(i + 1);
  62. requires.push(import(module).then(module => [name, module]));
  63. return requires;
  64. }