Skip to content

Commit fc981a6

Browse files
committed
Add new CLI, refactor directory structure
1 parent 309679e commit fc981a6

File tree

5 files changed

+128
-3
lines changed

5 files changed

+128
-3
lines changed

package.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
"url": "http://github.com/jperelli/osm-static-maps.git"
1616
},
1717
"dependencies": {
18+
"commander": "4.1.1",
1819
"express": "4.17.1",
1920
"handlebars": "4.7.3",
2021
"imagemin": "7.0.1",
@@ -34,7 +35,10 @@
3435
"dev": "nodemon -e js,html sample-server.js",
3536
"installandstartdev": "npm install && npm run dev"
3637
},
37-
"main": "./lib/lib.js",
38+
"main": "./src/lib.js",
39+
"bin": {
40+
"osmsm": "./src/cli.js"
41+
},
3842
"devDependencies": {
3943
"nodemon": "2.0.2"
4044
}

src/cli.js

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
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+
});

lib/lib.d.ts renamed to src/lib.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ interface OsmStaticMapsOptions {
1616
height?: number;
1717

1818
/**
19-
* height in pixels of the returned img
19+
* width in pixels of the returned img
2020
* @defaultValue `800`
2121
*/
2222
width?: number;
@@ -40,7 +40,7 @@ interface OsmStaticMapsOptions {
4040
maxZoom?: number;
4141

4242
/**
43-
* attribution legend
43+
* attribution legend watermark
4444
* @defaultValue `'osm-static-maps / © OpenStreetMap contributors'`
4545
*/
4646
attribution?: string;
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)