This repository was archived by the owner on Nov 22, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathindex.js
More file actions
67 lines (40 loc) · 1.17 KB
/
index.js
File metadata and controls
67 lines (40 loc) · 1.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
const getPixels = require('get-pixels');
const getRandomInt = require('random-in-range');
// https://stackoverflow.com/a/36002376/922323
function pxToLatLon(pixelX, pixelY, width, height) {
return {
latitude: parseFloat((((pixelY / (height / 180)) - 90) / -1).toFixed(1)),
longitude: parseFloat(((pixelX / (width / 360)) - 180).toFixed(1)),
};
}
function getRandomCoordOverLand(pixels) {
let width = pixels.shape[0];
let height = pixels.shape[1];
let randX = getRandomInt(0, width);
let randY = getRandomInt(0, height);
// console.log('got pixels', pixels.get(randX, randY, 0), randX, randY)
if (pixels.get(randX, randY, 0) != 0) {
return getRandomCoordOverLand(pixels);
} else {
return pxToLatLon(
randX,
randY,
width,
height
);
}
}
function run(options = {}) {
map = (options.map || `${__dirname}/map.png`);
return new Promise((fulfilled, rejected) => {
getPixels(map, function(err, pixels) {
if (err) {
return rejected(new Error('Bad image path'));
} else {
result = getRandomCoordOverLand(pixels);
fulfilled(result);
}
});
});
}
module.exports = run;