mapcachetools 1.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md ADDED
@@ -0,0 +1,45 @@
1
+ # MAP Cache Tool
2
+
3
+ This tool download any area from map provider and store it as images in a folder.
4
+ You can use these images with a library like [Leafet](https://leafletjs.com/ "Leafet") to make your own map server easily without the need to download a local OpenStreetMap server.
5
+
6
+
7
+ ## Usage
8
+
9
+ Download area from 58.5500977,-4.5231876 to 58.5501,-4.5231976 with maximum zoom in of 20 and minimum zoom in of 0 i.e. whole world zoom, and store images in folder called image_folder
10
+
11
+
12
+ downloadmaps --lat1=58.5500977 --lng1=-4.5231876 --lat2=58.5501 --lng2=-4.5231976 --zin=20 --zout=0 --folder=./image_folder
13
+
14
+ ## Map Providers
15
+
16
+ The application supports [openstreetmap.org](https://www.openstreetmap.org/#map=7/26.805/30.246 "openstreetmap.org")
17
+
18
+ ```bash
19
+ downloadmaps --lat1=58.5500977 --lng1=-4.5231876 --lat2=58.5501 --lng2=-4.5231976 --zin=20 --folder=./out
20
+
21
+ ```
22
+
23
+ and [api.mapbox.com](https://api.mapbox.com "api.mapbox.com"). You need to have a **TOKEN** for using [api.mapbox.com](https://api.mapbox.com "api.mapbox.com").
24
+
25
+ ```bash
26
+ downloadmaps --lat1=58.5500977 --lng1=-4.5231876 --lat2=58.5501 --lng2=-4.5231976 --zin=20 --folder=./out --provider=1 --token=pk.eyJ1IjoibSahlZm59IiwiYSI6ImNrZW84Nm9rYTA2ZWgycv9mdmNscmFxYzcifQ.c-z43FdasErPzKhbQ
27
+ ```
28
+
29
+ ## Sample
30
+
31
+ folder ./site contains a folder called **cachedMaps** you can put your maps into it and browse them.
32
+
33
+
34
+
35
+ cd ./site
36
+ http-server .
37
+
38
+
39
+
40
+
41
+
42
+ ### Disclaimer
43
+ Please make sure to review any terms and conditions of map providers you want to use. Author assumes no liability for any incidental, consequential or other liability from the use of this product.
44
+
45
+
package/REALKEY.txt ADDED
@@ -0,0 +1 @@
1
+ const TOKEN = "pk.eyJ1IjoiaHNhYWQiLCJhIjoiY2tqZnIwNXRuMndvdTJ4cnV0ODQ4djZ3NiJ9.LKojA3YMrG34L93jRThEGQ"
@@ -0,0 +1,377 @@
1
+ /*
2
+ Download images sequentially
3
+ */
4
+ "use strict";
5
+ const v_pjson = require("./package.json");
6
+ const c_args = require("./helpers/hlp_args.js");
7
+ const c_colors = require("./helpers/js_colors.js").Colors;
8
+
9
+ const fs = require("fs");
10
+ const axios = require("axios");
11
+
12
+ const EARTH_RADIUS = 6378137;
13
+ const MAX_LATITUDE = 85.0511287798;
14
+ const R_MINOR = 6356752.314245179;
15
+ const TOKEN = "GET YOUR TOKEN";
16
+ var map_provider = 0;
17
+ var myArgs = c_args.getArgs();
18
+
19
+ /*
20
+ * @namespace Projection
21
+ * @projection L.Projection.SphericalMercator
22
+ *
23
+ * Spherical Mercator projection — the most common projection for online maps,
24
+ * used by almost all free and commercial tile providers. Assumes that Earth is
25
+ * a sphere. Used by the `EPSG:3857` CRS.
26
+ */
27
+ function project(lat, lng) {
28
+ var d = Math.PI / 180,
29
+ max = MAX_LATITUDE,
30
+ lat = Math.max(Math.min(max, lat), -max),
31
+ sin = Math.sin(lat * d);
32
+
33
+ return {
34
+ x: EARTH_RADIUS * lng * d,
35
+ y: (EARTH_RADIUS * Math.log((1 + sin) / (1 - sin))) / 2,
36
+ };
37
+ }
38
+
39
+ function unproject(point) {
40
+ var d = 180 / Math.PI;
41
+
42
+ return {
43
+ lat: (2 * Math.atan(Math.exp(point.y / EARTH_RADIUS)) - Math.PI / 2) * d,
44
+ lng: (point.x * d) / EARTH_RADIUS,
45
+ };
46
+ }
47
+
48
+ function zoomScale(zoom) {
49
+ return 256 * Math.pow(2, zoom);
50
+ }
51
+
52
+ function transform(point, scale) {
53
+ scale = scale || 1;
54
+ point.x = scale * (2.495320233665337e-8 * point.x + 0.5);
55
+ point.y = scale * (-2.495320233665337e-8 * point.y + 0.5);
56
+ return point;
57
+ }
58
+
59
+ function fn_convertFromLngLatToPoints(lat1, lng1, lat2, lng2, zoom) {
60
+ // order location
61
+ if (lng1 > lng2) {
62
+ var t = lng2;
63
+ lng2 = lng1;
64
+ lng1 = t;
65
+ }
66
+
67
+ if (lat1 > lat2) {
68
+ var t = lat2;
69
+ lat2 = lat1;
70
+ lat1 = t;
71
+ }
72
+
73
+ // convert to points
74
+ var point1 = project(lat1, lng1);
75
+ var point2 = project(lat2, lng2);
76
+
77
+ var scaledZoom = zoomScale(zoom);
78
+ point1 = transform(point1, scaledZoom);
79
+ point2 = transform(point2, scaledZoom);
80
+
81
+ // convert to integer
82
+ point1.x = Math.floor(point1.x / 256);
83
+ point1.y = Math.floor(point1.y / 256);
84
+ point2.x = Math.floor(point2.x / 256);
85
+ point2.y = Math.floor(point2.y / 256);
86
+
87
+ // sort
88
+ if (point1.y > point2.y) {
89
+ var t = point2.y;
90
+ point2.y = point1.y;
91
+ point1.y = t;
92
+ }
93
+
94
+ var point = [];
95
+
96
+ point.push(point1);
97
+ point.push(point2);
98
+
99
+ return point;
100
+ }
101
+
102
+ /* ============================================================
103
+ Function: Download Image
104
+ ============================================================ */
105
+
106
+ const download_image = (url, image_path) =>
107
+ axios({
108
+ url,
109
+ responseType: "stream",
110
+ }).then(
111
+ (response) =>
112
+ new Promise((resolve, reject) => {
113
+ response.data
114
+ .pipe(fs.createWriteStream(image_path))
115
+ .on("finish", () => resolve())
116
+ .on("error", (e) => reject(e));
117
+ })
118
+ ).catch((error) => {
119
+ console.log(error.message);
120
+ });;
121
+
122
+ function fn_download_images(point1, point2, zoom) {
123
+ (async () => {
124
+ var url, filename;
125
+ var totalImages =
126
+ (1 + (point2.x + 1) - point1.x) * (1 + (point2.y + 1) - point1.y);
127
+ var START_FROM = 0;
128
+
129
+ for (var j, c = 0, i = point1.x; i <= point2.x + 1; ++i)
130
+ for (j = point1.y; j <= point2.y + 1; ++j, ++c) {
131
+ if (c >= START_FROM) {
132
+ /*
133
+ You can edit URL here to download from any provider.
134
+ */
135
+ if (map_provider==1)
136
+ {
137
+ // MAPBOX API
138
+ url =
139
+ "https://api.mapbox.com/styles/v1/mapbox/satellite-v9/tiles/" +
140
+ zoom +
141
+ "/" +
142
+ i +
143
+ "/" +
144
+ j +
145
+ "?access_token=" +
146
+ TOKEN;
147
+ }
148
+ else
149
+ {
150
+ // Open Street Map
151
+ // URL https://tile.openstreetmap.org/{z}/{x}/{y}.png
152
+ url =
153
+ "https://tile.openstreetmap.org/" +
154
+ zoom +
155
+ "/" +
156
+ i +
157
+ "/" +
158
+ j +
159
+ ".png";
160
+ }
161
+ filename = folder + "/" + i + "_" + j + "_" + zoom + ".jpeg";
162
+ if (!fs.existsSync(filename)) {
163
+ await download_image(url, filename);
164
+ }
165
+ else
166
+ {
167
+ console.log ("image no. " + c + " already exists.");
168
+ }
169
+ console.log(c + " of " + totalImages + ":" + url);
170
+ }
171
+ }
172
+ })();
173
+ }
174
+
175
+ function fn_handle_arguments() {
176
+ myArgs = c_args.getArgs();
177
+ console.log(JSON.stringify(myArgs));
178
+ var error = false;
179
+ if (
180
+ myArgs.hasOwnProperty("version") === true ||
181
+ myArgs.hasOwnProperty("v") === true
182
+ ) {
183
+ console.log(
184
+ c_colors.BSuccess +
185
+ "MAP Cache version" +
186
+ c_colors.FgYellow +
187
+ JSON.stringify(v_pjson.version) +
188
+ c_colors.Reset
189
+ );
190
+ console.log(
191
+ c_colors.BSuccess +
192
+ "--lat1" +
193
+ c_colors.FgWhite +
194
+ " for Start latitude. " +
195
+ c_colors.FgYellow +
196
+ " Example --lat1=-54.652332555" +
197
+ c_colors.Reset
198
+ );
199
+ console.log(
200
+ c_colors.BSuccess +
201
+ "--lng1" +
202
+ c_colors.FgWhite +
203
+ " for start longitude. " +
204
+ c_colors.FgYellow +
205
+ " Example --lng1=-54.652332555" +
206
+ c_colors.Reset
207
+ );
208
+ console.log(
209
+ c_colors.BSuccess +
210
+ "--lat2" +
211
+ c_colors.FgWhite +
212
+ " for end latitude. " +
213
+ c_colors.FgYellow +
214
+ " Example --lat2=-54.652332555" +
215
+ c_colors.Reset
216
+ );
217
+ console.log(
218
+ c_colors.BSuccess +
219
+ "--lng2" +
220
+ c_colors.FgWhite +
221
+ " for end longitude. " +
222
+ c_colors.FgYellow +
223
+ " Example --lng2=-54.652332555" +
224
+ c_colors.Reset
225
+ );
226
+ console.log(
227
+ c_colors.BSuccess +
228
+ "--zout" +
229
+ c_colors.FgWhite +
230
+ " for maximum zoom in. You need to check provider normally up to 20" +
231
+ c_colors.FgYellow +
232
+ " Example --zout=2 " +
233
+ c_colors.Reset
234
+ );
235
+ console.log(
236
+ c_colors.BSuccess +
237
+ "--zin" +
238
+ c_colors.FgWhite +
239
+ " for maximum zoom out. can be as low as 0. " +
240
+ c_colors.FgYellow +
241
+ " Example --zin=10 " +
242
+ c_colors.Reset
243
+ );
244
+ console.log(
245
+ c_colors.BSuccess +
246
+ "--folder" +
247
+ c_colors.FgWhite +
248
+ " folder to store images in. " +
249
+ c_colors.FgYellow +
250
+ " Example --folder=./out " +
251
+ c_colors.Reset
252
+ );
253
+ console.log(
254
+ c_colors.BSuccess +
255
+ "--provider" +
256
+ c_colors.FgWhite +
257
+ " if equal to 1 then use https://api.mapbox.com else use tile.openstreetmap.org. " +
258
+ c_colors.FgYellow +
259
+ " Example --folder=./out " +
260
+ c_colors.Reset
261
+ );
262
+ console.log(
263
+ c_colors.BSuccess +
264
+ "--token" +
265
+ c_colors.FgWhite +
266
+ " Required only with https://api.mapbox.com " +
267
+ c_colors.FgYellow +
268
+ " Example --folder=pk.eyJ1IjoibZglZm55IiwiYSI698mNrZW84Nm9rYTA2ZWgycm9mdmNscmFxYzcifQ.c-zxDZXCthXmRsErPzKhbQ " +
269
+ c_colors.Reset
270
+ );
271
+ process.exit();
272
+ }
273
+ if (myArgs.hasOwnProperty("lat1") !== true) {
274
+ error = true;
275
+ console.log(
276
+ c_colors.BError +
277
+ "Missing start latitude. " +
278
+ c_colors.FgYellow +
279
+ " Example --lat1=-54.652332555" +
280
+ c_colors.Reset
281
+ );
282
+ }
283
+ if (myArgs.hasOwnProperty("lng1") !== true) {
284
+ error = true;
285
+ console.log(
286
+ c_colors.BError +
287
+ "Missing start longitude. " +
288
+ c_colors.FgYellow +
289
+ " Example --lng1=-54.652332555" +
290
+ c_colors.Reset
291
+ );
292
+ }
293
+ if (myArgs.hasOwnProperty("lat2") !== true) {
294
+ error = true;
295
+ console.log(
296
+ c_colors.BError +
297
+ "Missing end latitude. " +
298
+ c_colors.FgYellow +
299
+ " Example --lat2=-54.652332555" +
300
+ c_colors.Reset
301
+ );
302
+ }
303
+ if (myArgs.hasOwnProperty("lng2") !== true) {
304
+ error = true;
305
+ console.log(
306
+ c_colors.BError +
307
+ "Missing end longitude. " +
308
+ c_colors.FgYellow +
309
+ " Example --lng2=-54.652332555" +
310
+ c_colors.Reset
311
+ );
312
+ }
313
+ if (myArgs.hasOwnProperty("zout") !== true) {
314
+ myArgs.zout = 0;
315
+ console.log(
316
+ c_colors.FgCyan +
317
+ "Missing zoom out max. Use zero as a default. " +
318
+ c_colors.FgYellow +
319
+ " Example --zout=0 " +
320
+ c_colors.Reset
321
+ );
322
+ }
323
+ if (myArgs.hasOwnProperty("zin") !== true) {
324
+ error = true;
325
+ console.log(
326
+ c_colors.BError +
327
+ "Missing zoom in max between 18 & 2. " +
328
+ c_colors.FgYellow +
329
+ " Example --zin=10 " +
330
+ c_colors.Reset
331
+ );
332
+ }
333
+ if (myArgs.hasOwnProperty("folder") !== true) {
334
+ error = true;
335
+ console.log(
336
+ c_colors.BError +
337
+ "Missing output folder. " +
338
+ c_colors.FgYellow +
339
+ " Example --folder=./out " +
340
+ c_colors.Reset
341
+ );
342
+ }
343
+ if (myArgs.hasOwnProperty("provider") === true) {
344
+ if (myArgs.provider == 1)
345
+ {
346
+ map_provider = 1;
347
+ if (myArgs.hasOwnProperty("token") !== true) {
348
+ error = true;
349
+ console.log(
350
+ c_colors.BError +
351
+ "Missing TOKEN file for https://api.mapbox.com. " +
352
+ c_colors.FgYellow +
353
+ " Example --token=pk.eyJ1IjoibZglZm55IiwiYSI698mNrZW84Nm9rYTA2ZWgycm9mdmNscmFxYzcifQ.c-zxDZXCthXmRsErPzKhbQ " +
354
+ c_colors.Reset
355
+ );
356
+ }
357
+ }
358
+ }
359
+
360
+ if (map_provider)
361
+ if (error === true) process.exit(0);
362
+ }
363
+
364
+ /* ============================================================
365
+ Download Images in Order
366
+ ============================================================ */
367
+
368
+ fn_handle_arguments();
369
+ var folder = myArgs.folder;
370
+ for (var zoom = myArgs.zout; zoom <= myArgs.zin; ++zoom) {
371
+ var points = fn_convertFromLngLatToPoints(myArgs.lat1, myArgs.lng1, myArgs.lat2, myArgs.lng2, zoom);
372
+
373
+ var point1 = points[0];
374
+ var point2 = points[1];
375
+
376
+ fn_download_images(point1, point2, zoom);
377
+ }
@@ -0,0 +1,26 @@
1
+ "use strict";
2
+
3
+
4
+ exports.getArgs = function getArgs () {
5
+ //https://stackoverflow.com/questions/4351521/how-do-i-pass-command-line-arguments-to-a-node-js-program
6
+ //node test.js -D --name=Hello
7
+
8
+ const args = {}
9
+ process.argv
10
+ .slice(2, process.argv.length)
11
+ .forEach( arg => {
12
+ // long arg
13
+ if (arg.slice(0,2) === '--') {
14
+ const longArg = arg.split('=')
15
+ args[longArg[0].slice(2,longArg[0].length)] = longArg[1]
16
+ }
17
+ // flags
18
+ else if (arg[0] === '-') {
19
+ const flags = arg.slice(1,arg.length).split('')
20
+ flags.forEach(flag => {
21
+ args[flag] = true
22
+ })
23
+ }
24
+ })
25
+ return args
26
+ }
@@ -0,0 +1,33 @@
1
+ const c_Colors =
2
+ {
3
+ Reset : "\x1b[0m",
4
+ Bright : "\x1b[1m",
5
+ Dim : "\x1b[2m",
6
+ Underscore : "\x1b[4m",
7
+ Blink : "\x1b[5m",
8
+ Reverse : "\x1b[7m",
9
+ Hidden : "\x1b[8m",
10
+
11
+ FgBlack : "\x1b[30m",
12
+ Error : "\x1b[31m",
13
+ BError : "\x1b[1;31m",
14
+ Success : "\x1b[32m",
15
+ BSuccess : "\x1b[1;32m",
16
+ FgYellow : "\x1b[33m",
17
+ BFgYellow : "\x1b[1;33m",
18
+ Log : "\x1b[34m",
19
+ FgMagenta : "\x1b[35m",
20
+ FgCyan : "\x1b[36m",
21
+ FgWhite : "\x1b[37m",
22
+ Info : "\x1b[33m",
23
+ BgBlack : "\x1b[40m",
24
+ BgRed : "\x1b[41m",
25
+ BgGreen : "\x1b[42m",
26
+ BgYellow : "\x1b[43m",
27
+ BgBlue : "\x1b[44m",
28
+ BgMagenta : "\x1b[45m",
29
+ BgCyan : "\x1b[46m",
30
+ BgWhite : "\x1b[47m"
31
+ }
32
+
33
+ exports.Colors = c_Colors;
package/package.json ADDED
@@ -0,0 +1,23 @@
1
+ {
2
+ "name": "mapcachetools",
3
+ "version": "1.0.1",
4
+ "description": "This tool downloads images from a map provider and cache it locally on your device. Please check terms and condition of your map provider. The tool is provided as is with no responsibility from author. Please Use IT AT YOUR OWN RISK.",
5
+ "main": "downloadmaps.js",
6
+ "scripts": {
7
+ "start": "node ./bin/downloadmaps.js"
8
+ },
9
+ "bin": {
10
+ "mapcache": "downloadmaps.js"
11
+ },
12
+ "keywords": [
13
+ "map",
14
+ "local server",
15
+ "cache"
16
+ ],
17
+ "author": "Mohammad Said Hefny - rcmobilestuff <rcmobilestuff@gmail.com> (http://www.andruav.com)",
18
+ "license": "ISC",
19
+ "dependencies": {
20
+ "axios": "^0.21.1",
21
+ "request": "^2.88.2"
22
+ }
23
+ }