-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
256 lines (231 loc) · 9.65 KB
/
index.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
const { BoundingBox } = require('@ngageoint/geopackage'),
chalk = require('chalk'),
xyzTileUtils = require('./lib/xyz-tile-utils');
let proj4 = require('proj4');
proj4 = 'default' in proj4 ? proj4['default'] : proj4; // Module loading hack
const WEB_MERCATOR_MAX_LAT_RANGE = 85.0511287798066;
const WEB_MERCATOR_MIN_LAT_RANGE = -85.05112877980659;
function outputTileTableInfo(geoPackage, dao) {
const info = geoPackage.getInfoForTable(dao);
console.log('\n' + chalk.blue(info.tableName + ' Tile Table Information'));
console.log(chalk.blue('Total Tiles: ') + info.count);
console.log(chalk.blue('Zoom Levels: ') + info.zoomLevels);
console.log(chalk.blue('Min Zoom: ') + info.minZoom);
console.log(chalk.blue('Max Zoom: ') + info.maxZoom);
console.log('\n' + chalk.green('Tile Matrix Set Bounds'));
console.log(chalk.green('SRS ID: ') + info.tileMatrixSet.srsId);
console.log(chalk.green('Min X: ') + info.tileMatrixSet.minX);
console.log(chalk.green('Min Y : ') + info.tileMatrixSet.minY);
console.log(chalk.green('Max X: ') + info.tileMatrixSet.maxX);
console.log(chalk.green('Max Y: ') + info.tileMatrixSet.maxY);
console.log('\n\t' + chalk.green('Tile Matrix Spatial Reference System'));
console.log('\t' + chalk.green('SRS Name: ') + info.srs.count);
console.log('\t' + chalk.green('SRS ID: ') + info.srs.id);
console.log('\t' + chalk.green('Organization: ') + info.srs.organization);
console.log('\t' + chalk.green('Coordsys ID: ') + info.srs.organization_coordsys_id);
console.log('\t' + chalk.green('Definition: ') + info.srs.definition);
console.log('\t' + chalk.green('Description: ') + info.srs.description);
console.log('\n' + chalk.cyan('Contents'));
console.log(chalk.cyan('Table Name: ') + info.contents.tableName);
console.log(chalk.cyan('Data Type: ') + info.contents.dataType);
console.log(chalk.cyan('Identifier: ') + info.contents.identifier);
console.log(chalk.cyan('Description: ') + info.contents.description);
console.log(chalk.cyan('Last Change: ') + info.contents.lastChange);
console.log(chalk.cyan('Min X: ') + info.contents.minX);
console.log(chalk.cyan('Min Y : ') + info.contents.minY);
console.log(chalk.cyan('Max X: ') + info.contents.maxX);
console.log(chalk.cyan('Max Y: ') + info.contents.maxY);
console.log('\n\t' + chalk.cyan('Contents Spatial Reference System'));
console.log('\t' + chalk.cyan('SRS Name: ') + info.contents.srs.count);
console.log('\t' + chalk.cyan('SRS ID: ') + info.contents.srs.id);
console.log('\t' + chalk.cyan('Organization: ') + info.contents.srs.organization);
console.log('\t' + chalk.cyan('Coordsys ID: ') + info.contents.srs.organization_coordsys_id);
console.log('\t' + chalk.cyan('Definition: ') + info.contents.srs.definition);
console.log('\t' + chalk.cyan('Description: ') + info.contents.srs.description);
return info;
}
function copyFeatures(options) {
const inputGeoPackage = options.inputGeoPackage;
const outputGeoPackage = options.outputGeoPackage;
const table = options.tableName;
let featureDao = inputGeoPackage.getFeatureDao(table);
const featureTable = featureDao.getFeatureTable();
const geometryColumns = inputGeoPackage.geometryColumnsDao.queryForTableName(table);
const boundingBox = featureDao.getBoundingBox();
const srsId = featureDao.srs.srs_id;
const columns = featureTable.getUserColumns().getColumns();
outputGeoPackage.createFeatureTable(table, geometryColumns, columns, boundingBox, srsId);
const outputFeatureDao = outputGeoPackage.getFeatureDao(table);
featureDao = inputGeoPackage.getFeatureDao(table);
const iterator = featureDao.queryForAll();
for (const feature of iterator) {
outputFeatureDao.create(featureDao.createObject(feature));
}
return outputFeatureDao;
}
function indexTable(featureDao, progress) {
// eslint-disable-next-line @typescript-eslint/no-empty-function
progress = progress || function() {};
return featureDao.featureTableIndex.rtreeIndex.create(progress);
}
function tile2lat(y, z) {
const n = Math.PI - (2 * Math.PI * y) / Math.pow(2, z);
return (180 / Math.PI) * Math.atan(0.5 * (Math.exp(n) - Math.exp(-n)));
}
function processTileTable(options) {
const inputGeoPackage = options.inputGeoPackage;
const outputGeoPackage = options.outputGeoPackage;
const tableDao = options.tableDao;
const outputTableName = options.tableName;
// eslint-disable-next-line @typescript-eslint/no-empty-function
const progress = options.progress || function() {};
const info = outputTileTableInfo(inputGeoPackage, tableDao);
const minZoom = info.minZoom;
const maxZoom = info.maxZoom;
const projection = info.contents.srs.organization.toUpperCase() + ':' + info.contents.srs.organization_coordsys_id;
const tableName = outputTableName || info.tableName;
const sw = proj4(projection, 'EPSG:4326', [info.contents.minX, info.contents.minY]);
const ne = proj4(projection, 'EPSG:4326', [info.contents.maxX, info.contents.maxY]);
const totalCount = xyzTileUtils.tileCountInExtent([sw[0], Math.max(sw[1], WEB_MERCATOR_MIN_LAT_RANGE), ne[0], Math.min(ne[1], WEB_MERCATOR_MAX_LAT_RANGE)], minZoom, maxZoom);
progress({
count: 0,
totalCount: totalCount,
layer: tableDao.table_name,
});
const tileMatrixSetBoundingBox = new BoundingBox(
-20037508.342789244,
20037508.342789244,
-20037508.342789244,
20037508.342789244,
);
const tileMatrixSetSrsId = 3857;
if (sw[1] < tile2lat(1, 0)) {
sw[1] = tile2lat(1, 0);
}
if (ne[1] > tile2lat(0, 0)) {
ne[1] = tile2lat(0, 0);
}
const sw3857 = proj4('EPSG:4326', 'EPSG:3857', sw);
const ne3857 = proj4('EPSG:4326', 'EPSG:3857', ne);
let tilesProcessedCount = 0;
const contentsBoundingBox = new BoundingBox(sw3857[0], ne3857[0], sw3857[1], ne3857[1]);
const contentsSrsId = 3857;
const tileMatrixSet = outputGeoPackage.createTileTableWithTableName(
tableName,
contentsBoundingBox,
contentsSrsId,
tileMatrixSetBoundingBox,
tileMatrixSetSrsId,
);
outputGeoPackage.createStandardWebMercatorTileMatrix(tileMatrixSetBoundingBox, tileMatrixSet, minZoom, maxZoom);
const targetTableDao = outputGeoPackage.getTileDao(tableName);
const tileRow = targetTableDao.newRow()
return xyzTileUtils.iterateAllTilesInExtent(
[sw[0], sw[1], ne[0], ne[1]],
minZoom,
maxZoom,
tableDao,
async function processTileCallback(x, y, z, retriever) {
const tileData = await retriever.getTile(x, y, z);
if (tileData != null) {
const bytes = Buffer.from(tileData.split(',')[1], 'base64');
if (bytes != null) {
tileRow.resetId();
tileRow.tileData = bytes;
tileRow.tileRow = y;
tileRow.tileColumn = x;
tileRow.zoomLevel = z;
targetTableDao.create(tileRow)
}
}
tilesProcessedCount++;
if (tilesProcessedCount % Math.ceil(totalCount * 0.05) === 0) {
progress({
count: tilesProcessedCount,
totalCount: totalCount,
layer: tableDao.table_name,
});
}
},
);
}
function optimize(options) {
const inputGeoPackage = options.inputGeoPackage;
const outputGeoPackage = options.outputGeoPackage;
const same = options.same;
// eslint-disable-next-line @typescript-eslint/no-empty-function
const progress = options.progress || function() {};
const tileTables = inputGeoPackage.getTileTables() || [];
const featureTables = inputGeoPackage.getFeatureTables() || [];
outputGeoPackage.spatialReferenceSystemDao.createWebMercator();
if (!same) {
return tileTables
.reduce(function(sequence, table) {
return sequence.then(function() {
const tileDao = inputGeoPackage.getTileDao(table);
return processTileTable({
inputGeoPackage: inputGeoPackage,
outputGeoPackage: outputGeoPackage,
tableDao: tileDao,
tableName: table,
});
});
}, Promise.resolve())
.then(function() {
return featureTables.reduce(function(sequence, table) {
return sequence.then(function() {
const featureDao = copyFeatures({
inputGeoPackage: inputGeoPackage,
outputGeoPackage: outputGeoPackage,
tableName: table,
});
indexTable(featureDao, progress);
});
}, Promise.resolve());
});
} else {
return tileTables
.reduce(function(sequence, table) {
return sequence.then(function() {
const tileDao = inputGeoPackage.getTileDao(table);
let count = 1;
let name = 'tiles';
while (tileTables.indexOf(name) !== -1) {
name = 'tiles' + '_' + count;
count++;
}
return processTileTable({
inputGeoPackage: inputGeoPackage,
outputGeoPackage: outputGeoPackage,
tableDao: tileDao,
tableName: name,
progress: progress,
})
.then(function() {
// delete the original table
const tileDao = outputGeoPackage.getTileDao(table);
return tileDao.dropTable();
})
.then(function(dropResult) {
const tileDao = outputGeoPackage.getTileDao(name);
return tileDao.rename(table);
// rename the new table
});
});
}, Promise.resolve())
.then(function() {
return featureTables.reduce(function(sequence, table) {
return sequence.then(function() {
const featureDao = inputGeoPackage.getFeatureDao(table);
return indexTable(featureDao, progress);
});
}, Promise.resolve());
});
}
}
module.exports = {
optimize,
processTileTable,
copyFeatures,
indexTable,
};