-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
60 lines (50 loc) · 1.69 KB
/
server.js
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
const express = require('express');
const app = express();
const fs = require('fs');
const request = require('request');
const mkdirp = require('mkdirp');
const url = "http://c.tile.openstreetmap.org"
const LIMIT_ZOOM = 9
const START_ZOOM = 9
const START_X = 312
const START_Y = 290
app.get('/', function (req, res) {
return res.status(200).send('google offline image crawler ready to serve');
});
app.get('/GET/offlineImage', (req, res) => {
/* 設定起始位置 */
getGoogleOfflineImage(START_ZOOM, START_X, START_Y, res)
})
function getGoogleOfflineImage(zoom, x, y, res) {
let reqUrl = `${url}/${zoom}/${x}/${y}.png`
if(!fs.existsSync(`./countries/${zoom}/${x}`)){
mkdirp(`./countries/${zoom}/${x}`)
}
request
.get(reqUrl)
.on('response', response => {
const image = fs.createWriteStream(`./countries/${zoom}/${x}/${y}.png`)
let imageStream = response.pipe(image)
imageStream.on('finish', () => {
console.log(`save ${zoom}/${x}/${y}.png okay`)
if(zoom == 0 && x == 0 && y == 0){
getGoogleOfflineImage(zoom + 1, 0, 0, res)
}
else if(zoom == LIMIT_ZOOM && x == Math.pow(2, LIMIT_ZOOM) - 1 && y == Math.pow(2, LIMIT_ZOOM)){
res.status(200).send(`save google offline image okay`)
}
else if(y != Math.pow(2, zoom) - 1){
getGoogleOfflineImage(zoom, x, y + 1, res)
}
else if(x != Math.pow(2, zoom) - 1 && y == Math.pow(2, zoom) - 1){
getGoogleOfflineImage(zoom, x + 1, 0, res)
}
else if(zoom != LIMIT_ZOOM && x == Math.pow(2, zoom) - 1 && y== Math.pow(2, zoom) - 1){
getGoogleOfflineImage(zoom + 1, 0, 0, res)
}
})
})
}
app.listen(process.env.PORT || 3000, function () {
console.log('google offline image crawler ready');
});