25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 
 

97 satır
4.3 KiB

  1. #!/usr/bin/env node
  2. import {EOL} from "os";
  3. import {program} from "commander";
  4. import {geoPath} from "d3-geo";
  5. import {readFileSync} from "fs";
  6. import {dirname, resolve} from "path";
  7. import {fileURLToPath} from "url";
  8. import {geoQuantize} from "../src/index.js";
  9. import read from "./read.js";
  10. import write from "./write.js";
  11. const version = JSON.parse(readFileSync(resolve(dirname(fileURLToPath(import.meta.url)), "../package.json"))).version;
  12. const options = program
  13. .version(version)
  14. .usage("[options] [file]")
  15. .description("Convert GeoJSON to SVG.")
  16. .option("-o, --out <file>", "output file name; defaults to “-” for stdout", "-")
  17. .option("-w, --width <value>", "output width", 960)
  18. .option("-h, --height <value>", "output height", 500)
  19. .option("-p, --precision <value>", "number of output digits after the decimal point", 6)
  20. .option("-n, --newline-delimited", "accept newline-delimited JSON")
  21. .option("--fill <value>", "default fill", "none")
  22. .option("--stroke <value>", "default stroke", "black")
  23. .option("-r, --radius <value>", "default point radius", 4.5)
  24. .parse(process.argv)
  25. .opts();
  26. if (program.args.length === 0) program.args[0] = "-";
  27. else if (program.args.length !== 1) {
  28. console.error();
  29. console.error(" error: multiple input files");
  30. console.error();
  31. process.exit(1);
  32. }
  33. var reader = read(program.args[0], options.newlineDelimited, transform).then(end),
  34. writer = write(options.out),
  35. path = geoPath().pointRadius(function(d) { var p = d.properties, v; return p && (v = p["point-radius"] || p.pointRadius) != null ? v : options.radius; }),
  36. render = options.precision == null ? path : function(d) { return path(geoQuantize(d, options.precision)); };
  37. reader.catch(error => {
  38. console.error(error.stack);
  39. });
  40. writer.write("<?xml version=\"1.0\" encoding=\"utf-8\"?>" + EOL
  41. + "<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\" \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">" + EOL
  42. + "<!-- Generated by geo2svg " + version + ". https://d3js.org/d3-geo-projection/ -->" + EOL
  43. + "<svg version=\"1.1\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\""
  44. + " width=\"" + +options.width + "\""
  45. + " height=\"" + +options.height + "\""
  46. + " viewBox=\"0 0 " + +options.width + " " + +options.height + "\""
  47. + (options.fill != "black" ? " fill=\"" + attr(options.fill) + "\"" : "")
  48. + (options.stroke != "none" ? " stroke=\"" + attr(options.stroke) + "\"" : "")
  49. + ">" + EOL);
  50. function transform(d) {
  51. var p = d.properties, v;
  52. return writer.write(" <path"
  53. + ((v = d.id) != null ? " id=\"" + attr(v + "") + "\"" : "")
  54. + (p ? ((v = p["fill"]) != null ? " fill=\"" + attr(v + "") + "\"" : "")
  55. + ((v = p["fill-rule"] || p.fillRule) != null ? " fill-rule=\"" + attr(v + "") + "\"" : "")
  56. + ((v = p["fill-opacity"] || p.fillOpacity) != null ? " fill-opacity=\"" + attr(v + "") + "\"" : "")
  57. + ((v = p["stroke"]) != null ? " stroke=\"" + attr(v + "") + "\"" : "")
  58. + ((v = p["stroke-width"] || p.strokeWidth) != null ? " stroke-width=\"" + attr(v + "") + "\"" : "")
  59. + ((v = p["stroke-linecap"] || p.strokeLinecap) != null ? " stroke-linecap=\"" + attr(v + "") + "\"" : "")
  60. + ((v = p["stroke-linejoin"] || p.strokeLinejoin) != null ? " stroke-linejoin=\"" + attr(v + "") + "\"" : "")
  61. + ((v = p["stroke-miterlimit"] || p.strokeMiterlimit) != null ? " stroke-miterlimit=\"" + attr(v + "") + "\"" : "")
  62. + ((v = p["stroke-dasharray"] || p.strokeDasharray) != null ? " stroke-dasharray=\"" + attr(v + "") + "\"" : "")
  63. + ((v = p["stroke-dashoffset"] || p.strokeDashoffset) != null ? " stroke-dashoffset=\"" + attr(v + "") + "\"" : "")
  64. + ((v = p["stroke-opacity"] || p.strokeOpacity) != null ? " stroke-opacity=\"" + attr(v + "") + "\"" : "")
  65. : "")
  66. + (v = render(d), v ? " d=\"" + v + "\"" : "")
  67. + ">"
  68. + ((v = p && p["title"]) != null ? "<title>" + text(v + "") + "</title>" : "")
  69. + "</path>" + EOL);
  70. }
  71. function end() {
  72. return writer.write("</svg>" + EOL);
  73. }
  74. function text(string) {
  75. return string.replace(/[&<>]/g, function(character) {
  76. switch (character) {
  77. case "&": return "&amp;";
  78. case "<": return "&lt;";
  79. case ">": return "&gt;";
  80. }
  81. });
  82. }
  83. function attr(string) {
  84. return string.replace(/"/g, "&quot;");
  85. }