Skip to content

Commit

Permalink
added logic of metadata.json
Browse files Browse the repository at this point in the history
  • Loading branch information
Jin IGARASHI committed Jul 29, 2020
1 parent cbc87a0 commit 916fc84
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 1 deletion.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@watergis/mbtiles2pbf",
"version": "0.2.0",
"version": "0.2.1",
"description": "This module will extract pbf files from mbtile by using mbutil.",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down
14 changes: 14 additions & 0 deletions src/mbtiles2pbf.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ class mbtiles2pbf {
const count = db.prepare('SELECT count(*) FROM tiles').get()[
'count(*)'
];

let c = 0;
for (const r of db.prepare('SELECT * FROM tiles').iterate()) {
const buf = zlib.unzipSync(r.tile_data);
Expand All @@ -58,6 +59,19 @@ class mbtiles2pbf {
fs.writeFileSync(`${this.dist}/${z}/${x}/${y}${this.ext}`, buf);
this.report(++c, count, `${this.dist}/${z}/${x}/${y}${this.ext}`);
}

let metadata: { [key: string]: string } = {};
var rows: Array<{ [key: string]: string }> = db
.prepare('SELECT name, value FROM metadata')
.all();
rows.forEach((row: { [key: string]: string }) => {
metadata[row.name] = row.value;
});
fs.writeFileSync(
`${this.dist}/metadata.json`,
JSON.stringify(metadata, null, 4)
);

db.close();
resolve(c);
} catch (err) {
Expand Down
3 changes: 3 additions & 0 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import { Mbtiles2Pbf, FileExtension } from '../src/index';
import rimraf from 'rimraf';
import fs from 'fs-extra';

const src = __dirname + '/test.mbtiles';
const dist = __dirname + '/tiles';
Expand All @@ -11,12 +12,14 @@ describe('extract vector tiles', (): void => {
const mbtile2pbf = new Mbtiles2Pbf(src, dist, FileExtension.PBF);
const res = await mbtile2pbf.run();
expect(res).toBe(36);
expect(fs.existsSync(dist+'/metadata.json')).toBeTruthy();
rimraf.sync(dist);
});
test('extract to mvt', async () => {
const mbtile2pbf = new Mbtiles2Pbf(src, dist, FileExtension.MVT);
const res = await mbtile2pbf.run();
expect(res).toBe(36);
expect(fs.existsSync(dist+'/metadata.json')).toBeTruthy();
rimraf.sync(dist);
});
});

0 comments on commit 916fc84

Please sign in to comment.