-
Notifications
You must be signed in to change notification settings - Fork 1
/
mergeMbtiles.js
91 lines (76 loc) · 2.66 KB
/
mergeMbtiles.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
const fs = require('fs');
const path = require('path');
const syncEach = require('sync-each');
const UtilsMbtiles = require('./utilsMbtiles');
const _f = require('./config');
const hrstart = process.hrtime();
const mbtiles_ori = _f.mbtiles_ori;
const mbtiles_dest = _f.mbtiles_dest;
const pol_cat = _f.pol_cat;
const mbtiles_dest_source = _f.mbtiles_dest_source;
const zoom_levels = _f.zoom_levels;
async function mergeSingleTile(tilezxy, destino_mbt){
return new Promise(async function(resolve, reject) {
let tileId = UtilsMbtiles.createTileId(tilezxy.z, tilezxy.x, tilezxy.y);
const uri_sigleTile = path.join(__dirname, "temp", tileId, tileId+".mbtiles");
const sigleMbtile = await UtilsMbtiles.getMbtile(uri_sigleTile);
tileid = await UtilsMbtiles.replaceTile(sigleMbtile, destino_mbt, tilezxy);
resolve(tileid);
});
}
async function mergeTiles(origen_mbt, destino_mbt, z_levels){
z_levels.forEach(async (item) => {
console.info("zomm",item);
try{
const tiles = await UtilsMbtiles.leerJson('tiles'+item+'.geojson');
syncEach(tiles.features,
function(feat, next){
(async () => {
if (feat !== undefined) {
if(!feat.properties.id){
feat.properties.id = feat.id;
}
const tilezxy = UtilsMbtiles.idTile2ZXY(feat.properties.id);
const isInner = await isInnerTile(tilezxy.z, tilezxy.x, tilezxy.y);
let tileid = null;
if(item <=14){
if(isInner){
tileid = await UtilsMbtiles.replaceTile(origen_mbt, destino_mbt, tilezxy);
}else{
tileid = await mergeSingleTile(tilezxy, destino_mbt);
}
}else{
tileid = await UtilsMbtiles.addNewTile(origen_mbt, destino_mbt, tilezxy);
}
next(null,tileid);
}
})();
},
function(err, transformedItems){
console.log("err0",err);
console.log(transformedItems);
}
);
}catch(err){
console.log(err);
}
});
}
async function mergeMbtiles(uri_origen, uri_destino, uri_clip, z_levels){
const origen = await UtilsMbtiles.getMbtile(uri_origen);
const destino = await UtilsMbtiles.getMbtile(uri_destino);
mergeTiles(origen, destino, z_levels);
}
async function isInnerTile(z, x, y){
try{
const tiles = await UtilsMbtiles.leerJson('tiles'+z+'_inner.geojson');
return UtilsMbtiles.findTileById(z, x, y, tiles);
}catch(err){
return false;
}
}
async function isEdgeTile(z, x, y){
const tiles = await UtilsMbtiles.leerJson('tiles'+z+'_edges.geojson');
return UtilsMbtiles.findTileById(z, x, y, tiles);
}
mergeMbtiles(mbtiles_ori, mbtiles_dest, pol_cat, zoom_levels);