forked from rurseekatze/node-tileserver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
expire-tiles.js
142 lines (128 loc) · 2.93 KB
/
expire-tiles.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/*
node-tileserver Copyright (C) 2014 Alexander Matheisen
This program comes with ABSOLUTELY NO WARRANTY.
This is free software, and you are welcome to redistribute it under certain conditions.
See https://github.com/rurseekatze/node-tileserver for details.
*/
// include necessary modules
os = require('os');
rbush = require('rbush');
assert = require('assert');
url = require("url");
mkdirp = require('mkdirp');
pg = require('pg');
toobusy = require('toobusy-js');
byline = require('byline');
touch = require("touch");
Canvas = require('canvas');
events = require('events');
log4js = require('log4js');
fs = require('graceful-fs');
// load configuraion file
configuration = require('./config.json');
// configure logging
log4js.configure(
{
appenders:
[
{
"type": "logLevelFilter",
"level": "ERROR",
"appender":
{
"type": "file",
"filename": 'expiring.log',
'maxLogSize': 20480,
'backups': 0
}
},
{
"type": "logLevelFilter",
"level": "INFO",
"appender":
{
"type": "console"
}
}
]
});
logger = log4js.getLogger();
logger.setLevel('TRACE');
// load classes
Tile = require('./tile.js');
Tilequeue = require('./queue.js');
if (process.argv[2] && process.argv[2].length > 0 && fs.existsSync(process.argv[2]))
{
expireTileList(process.argv[2], function(err)
{
if (err)
{
console.log('Some problems occurred.');
process.exit(1);
}
else
{
console.log('Finished.');
process.exit(code=0);
}
});
}
else
{
console.log('Given filename invalid or file cannot be found.');
process.exit(code=0);
}
// load an osm2pgsql list of expired tiles and marks all tiles as expired
function expireTileList(filename, callback)
{
console.log('Checking if list of expired tiles exists...');
if (fs.existsSync(filename))
{
console.log('Reading list of expired tiles...');
var stream = byline(fs.createReadStream(filename));
stream.on('data', function(line)
{
var param = line.toString().split("/");
var tile = new Tile(param[0], param[1], param[2]);
tile.expire();
// mark child tiles
var tileset = tile.getChildren();
for (var tilesetIndex=0; tilesetIndex<tileset.length; tilesetIndex++)
{
tileset[tilesetIndex].expire();
tileset[tilesetIndex] = null;
}
// mark parent tiles
var parentTileset = tile.getParents();
for (var parentIndex=0; parentIndex<parentTileset.length; parentIndex++)
{
parentTileset[parentIndex].expire();
parentTileset[parentIndex] = null;
}
});
stream.on('end', function(line)
{
console.log('Expired tiles successfully marked.');
return process.nextTick(function()
{
callback(false);
});
});
stream.on('error', function(line)
{
console.log('Cannot read list of expired tiles. Aborting.');
return process.nextTick(function()
{
callback(true);
});
});
}
else
{
console.log('Cannot find expired-tiles-file. Aborting.');
return process.nextTick(function()
{
callback(true);
});
}
}