-
Notifications
You must be signed in to change notification settings - Fork 1
/
dump-mbtiles-metadata.js
executable file
·81 lines (73 loc) · 2.11 KB
/
dump-mbtiles-metadata.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
#!/usr/bin/env node
/*
Reads all metadata from a directory structure containing .mbtiles and outputs metadata in JSON format.
*/
const sqlite = require("better-sqlite3");
const fs = require("fs");
var path = require("path");
function sqliteQuery(mbtilesPath, sql) {
try {
const db = new sqlite(mbtilesPath);
const rows = db.prepare(sql).all();
return rows;
} catch (error) {
console.error(`Error reading ${mbtilesPath}: ${error.message}`);
return { error: error.message };
}
}
function readMbtilesMetadata(mbtilesPath) {
const sql = "SELECT * FROM metadata";
const rows = sqliteQuery(mbtilesPath, sql);
return rows.reduce((acc, row) => {
acc[row.name] = row.value;
return acc;
}, {});
}
function readSpatialiteMetadata(spatialitePath) {
const sql = "select * from vector_layers_statistics";
return sqliteQuery(spatialitePath, sql)[0];
}
function dumpAllMetadata(basePath, mbtilesPath, acc = {}) {
return fs
.readdirSync(mbtilesPath)
.map(file => path.join(mbtilesPath, file))
.reduce((acc, file) => {
const stat = fs.statSync(file);
if (stat.isDirectory()) {
dumpAllMetadata(basePath, file, acc);
return acc;
}
const fn = path.basename(file);
let rec = {
size: stat.size,
mtime: stat.mtime
};
rec = Object.assign(rec, readMetadata(file));
const relPath = path.relative(basePath, file);
const dir = path.dirname(relPath);
acc[dir] = acc[dir] || {};
acc[dir][fn] = rec;
return acc;
}, acc);
}
try {
if (process.argv.length === 3) {
const sourcePath = process.argv[2];
const meta = dumpAllMetadata(sourcePath, sourcePath);
console.log(JSON.stringify(meta));
} else throw new Error("Usage: node dump-mbtiles-metadata.js <directory>");
} catch (error) {
console.error(error);
process.exit(1);
}
function readMetadata(file) {
try {
const ext = path.extname(file);
if (ext === ".mbtiles") return readMbtilesMetadata(file);
if (ext === ".sqlite")
// Spatialite
return readSpatialiteMetadata(file);
} catch (e) {
console.error(e);
}
}