|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +const program = require("commander"); |
| 4 | +const osmsm = require("./lib"); |
| 5 | +const package = require("../package.json"); |
| 6 | + |
| 7 | +program |
| 8 | + .version(package.version) |
| 9 | + .name("osmsm") |
| 10 | + .usage( |
| 11 | + "[options]\nGenerate an image of a geojson with a background map layer" |
| 12 | + ) |
| 13 | + .option("-g, --geojson <string>", "Geojson object to be rendered") |
| 14 | + .option( |
| 15 | + "-H, --height <number>", |
| 16 | + "Height in pixels of the returned img", |
| 17 | + Number, |
| 18 | + 600 |
| 19 | + ) |
| 20 | + .option( |
| 21 | + "-W, --width <number>", |
| 22 | + "Width in pixels of the returned img", |
| 23 | + Number, |
| 24 | + 800 |
| 25 | + ) |
| 26 | + .option( |
| 27 | + "-c, --center <lon,lat>", |
| 28 | + "Center of the map (default: center of geojson or '-57.9524339,-34.921779'" |
| 29 | + ) |
| 30 | + .option( |
| 31 | + "-z, --zoom <number>", |
| 32 | + "Zoomlevel of the map (default: vectorserverUrl ? 12 : 20)", |
| 33 | + Number |
| 34 | + ) |
| 35 | + .option("-Z, --maxZoom <number>", "Maximum zoomlevel of the map", Number, 17) |
| 36 | + .option( |
| 37 | + "-A, --attribution <string>", |
| 38 | + "Attribution legend watermark", |
| 39 | + "osm-static-maps / © OpenStreetMap contributors" |
| 40 | + ) |
| 41 | + .option( |
| 42 | + "-t, --tileserverUrl <string>", |
| 43 | + "Url of a tileserver", |
| 44 | + "http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png" |
| 45 | + ) |
| 46 | + .option( |
| 47 | + "-m, --vectorserverUrl <string>", |
| 48 | + "Url of a vector tile server (MVT style.json)" |
| 49 | + ) |
| 50 | + .option( |
| 51 | + "-M, --vectorserverToken <string>", |
| 52 | + "Token of the vector tile server (MVT)" |
| 53 | + ) |
| 54 | + .option( |
| 55 | + "-D, --renderToHtml", |
| 56 | + "Returns html of the webpage containing the leaflet map (instead of a binary image)", |
| 57 | + false |
| 58 | + ) |
| 59 | + .option("-F, --type <jpeg|png>", "Format of the image returned", "png") |
| 60 | + .option( |
| 61 | + "-q, --quality <number>", |
| 62 | + "Quality of the image returned (only for -F=jpeg)", |
| 63 | + Number, |
| 64 | + 100 |
| 65 | + ) |
| 66 | + .option( |
| 67 | + "-x, --imagemin", |
| 68 | + "Apply lossless compression with optipng/jpegtran", |
| 69 | + false |
| 70 | + ) |
| 71 | + .option("-X, --oxipng", "Apply lossless compression with oxipng", false) |
| 72 | + .option( |
| 73 | + "-a, --arrows", |
| 74 | + "Render arrows to show the direction of linestrings", |
| 75 | + false |
| 76 | + ) |
| 77 | + .option( |
| 78 | + "-s, --scale [json string]", |
| 79 | + "Enable render a scale ruler (see options in https://leafletjs.com/reference-1.6.0.html#control-scale-option)", |
| 80 | + false |
| 81 | + ) |
| 82 | + .option( |
| 83 | + "-k, --markerIconOptions <json string>", |
| 84 | + "Set marker icon options (see doc in https://leafletjs.com/reference-1.6.0.html#icon-option)" |
| 85 | + ); |
| 86 | + |
| 87 | +program.on("--help", function() { |
| 88 | + process.stdout.write("\n"); |
| 89 | + process.stdout.write("Examples: \n"); |
| 90 | + process.stdout.write( |
| 91 | + ` $ osmsm -g '{"type":"Point","coordinates":[-105.01621,39.57422]}'\n` |
| 92 | + ); |
| 93 | + process.stdout.write( |
| 94 | + ` $ osmsm -g '[{"type":"Feature","properties":{"party":"Republican"},"geometry":{"type":"Polygon","coordinates":[[[-104.05,48.99],[-97.22,48.98],[-96.58,45.94],[-104.03,45.94],[-104.05,48.99]]]}},{"type":"Feature","properties":{"party":"Democrat"},"geometry":{"type":"Polygon","coordinates":[[[-109.05,41.00],[-102.06,40.99],[-102.03,36.99],[-109.04,36.99],[-109.05,41.00]]]}}]' --height=300 --width=300\n` |
| 95 | + ); |
| 96 | + process.stdout.write("\n"); |
| 97 | +}); |
| 98 | + |
| 99 | +program.parse(process.argv); |
| 100 | + |
| 101 | +const opts = program.opts(); |
| 102 | + |
| 103 | +// DEBUG |
| 104 | +// process.stderr.write(JSON.stringify(opts, undefined, 2)); |
| 105 | +// process.stderr.write("\n"); |
| 106 | + |
| 107 | +if (!["png", "jpeg"].includes(opts.type)) { |
| 108 | + process.stderr.write('-F|--type can only have the values "png" or "jpeg"\n'); |
| 109 | + process.exit(2); |
| 110 | +} |
| 111 | + |
| 112 | +osmsm(opts) |
| 113 | + .then(v => { |
| 114 | + process.stdout.write(v); |
| 115 | + process.stdout.end(); |
| 116 | + process.exit(0); |
| 117 | + }) |
| 118 | + .catch(e => { |
| 119 | + process.stderr.write(e.toString()); |
| 120 | + process.exit(1); |
| 121 | + }); |
0 commit comments