-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
340 lines (230 loc) · 8.67 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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
const { execSync, exec } = require('child_process');
const {long2tile, lat2tile, tile2long, tile2lat} = require('xyztile')
const fse = require('fs-extra')
var GDAL_INFO = "D:\\OSGeo4W64\\bin\\gdalinfo.exe"
var GDAL_TRANSLATE = "D:\\OSGeo4W64\\bin\\gdal_translate.exe"
// var TIFF_PATH = "D:\\_temp\\istan_rect\\t_1.tif"
var TIFF_PATH = "D:\\_temp\\istan_rect\\testshape.tif"
var ROOT_FOLDER = 'D:\\_temp\\istan_rect\\istanbul_rect\\'
var MAX_LEVEL = 16
var TIF_EXTEND
var startZoom = 16
var abort = false
var randomDebug = Math.round(Math.random() * 9999)
var debugPath = `D://debug.txt`
async function start() {
let header = 'tile_x;tile_y;tile_z;geom'
// fse.ensureFileSync(debugPath)
// fse.appendFileSync(debugPath, header + '\n')
let command = [
GDAL_INFO,
TIFF_PATH,
"-json"
]
exec(command.join(' '), (err, stdout, stderr) => {
if (err) {
console.log("\033[31m ERROR \033");
console.error(err);
return;
}
let infoData = JSON.parse(stdout)
let cornerCoordinates = infoData.cornerCoordinates
let coordinates = infoData.wgs84Extent.coordinates
let upperLeft = coordinates[0][0]
let lowerLeft = coordinates[0][1]
let lowerRight = coordinates[0][2]
let upperRight = coordinates[0][3]
// console.log(`WGS84 Extent : ${upperLeft} ${lowerLeft} ${lowerRight} ${upperRight}`);
let ulX = upperLeft[0]
let ulY = upperLeft[1]
let lrX = lowerRight[0]
let lrY = lowerRight[1]
TIF_EXTEND = {
west : ulX,
east : lrX,
north : ulY,
south : lrY,
}
var level = startZoom
let tileCountX = zoomToTileCount(level)
let tileCountY = zoomToTileCount(level) / 2
let tileStartX = long2tile(ulX, level)
let tileEndX = long2tile(lrX, level) + 1
let tileStartY = lat2tile(ulY, level) + 1
let tileEndY = lat2tile(lrY,level)
console.log(TIF_EXTEND);
let pStart = {x: tileStartX , y:tileStartY}
let pEnd = {x: tileEndX , y: tileEndY}
var levels = {}
levels[level] = []
for (let i = tileStartX; i <= tileEndX; i++) {
for (let j = tileEndY; j <= tileStartY; j++) {
let xt = tile2long(i, level)
let yt = tile2lat(j, level)
let ext = tile2long(i + 1, level)
let eyt = tile2lat(j + 1, level)
let o = {
west : xt,
east : ext,
north : yt,
south : eyt,
x : long2tile(xt, level),
y : lat2tile(yt, level),
level : level,
}
levels[level].push(o)
}
}
createTiles(levels, level)
var fullData = []
for (const key in levels) {
const l = levels[key]
for (let i = 0; i < l.length; i++) {
const item = l[i];
if(isIntersect(item)) {
fullData.push(item)
boundsToWKT(item)
}
}
}
console.log('Total Tile Length : ' , fullData.length);
startProgress(fullData)
})
}
start();
function isIntersect(item) {
if(item.west < TIF_EXTEND.west && item.east < TIF_EXTEND.west) {
return false
}
if(item.east > TIF_EXTEND.east && item.west > TIF_EXTEND.east) {
return false
}
if(item.north > TIF_EXTEND.north && item.south > TIF_EXTEND.north) {
return false
}
if(item.south < TIF_EXTEND.south && item.north < TIF_EXTEND.south) {
return false
}
return true
}
async function startProgress(fullData) {
const time = process.hrtime();
var size = 100
for (let i = 0; i < fullData.length; i+=size) {
let sizes = []
for (let j = i; j < i + size; j++) {
const element = fullData[j]
sizes.push(extractTiff(element))
}
// const element = fullData[i]
// let a = await extractTiff(element)
let data = await Promise.all(sizes)
if(abort) {
break
}
process.stdout.write("Loading : %" + Math.round((i / fullData.length) * 100) + '\r' + 'processed : ' + i + '\n');
}
const diff = process.hrtime(time);
const NS_PER_SEC = 1e9;
console.log(`Benchmark took ${diff[0] * NS_PER_SEC + diff[1]} nanoseconds`);
}
function extractTiff(data) {
return new Promise((resolve, reject) => {
let west = data.west
let east = data.east
let north = data.north
let south = data.south
var EXPORT_PATH = ROOT_FOLDER + data.level + '\\' + data.x + '\\'
fse.ensureDirSync(EXPORT_PATH)
EXPORT_PATH = EXPORT_PATH + data.y + '.tif'
if(fse.existsSync(EXPORT_PATH)) {
resolve(1)
return
}
let boundBox = west + ' ' + south + ' ' + east + ' ' + north
let command = [
GDAL_TRANSLATE,
"-of GTiff",
`-projwin ${boundBox}`,
'-outsize 256 256',
TIFF_PATH,
EXPORT_PATH
]
exec(command.join(' '), function (err, stdout, stderr) {
if(err) {
abort = true
reject()
console.error('Error : ' , err);
return
}
let infoCommand = [
GDAL_INFO,
EXPORT_PATH,
"-json",
"-mm",
// "-approx_stats",
// "-norat",
// "-stats"
]
exec(infoCommand.join(' '), function (err, stdout, stderr) {
if(err) {
abort = true
reject()
console.error('Error : ' , err);
return
}
let infoData = JSON.parse(stdout)
if(infoData.bands.length === 0 || (infoData.bands[0].computedMin === undefined && infoData.bands[0].computedMax === undefined )) {
fse.removeSync(EXPORT_PATH)
// fse.removeSync(EXPORT_PATH + ".aux.xml")
}
resolve(2)
})
})
})
}
function createTiles(levels, currentLevel) {
// if(currentLevel >= 18) return
if(currentLevel >= MAX_LEVEL) return
let ar = levels[currentLevel]
for (let i = 0; i < ar.length; i++) {
const e = ar[i];
let centerX = e.west + ((e.east - e.west) / 2)
let centerY = e.south + ((e.north - e.south) / 2)
let newLevel = currentLevel + 1
if(levels[newLevel] === undefined){
levels[newLevel] = []
}
levels[newLevel].push( {west : e.west , east : centerX, north : e.north, south : centerY, x : long2tile(e.west, newLevel), y : lat2tile(e.north, newLevel), level : newLevel }) // up left
levels[newLevel].push( {west : e.west , east : centerX, north : centerY, south : e.south, x : long2tile(e.west, newLevel), y : lat2tile(centerY, newLevel), level : newLevel }) // down left
levels[newLevel].push( {west : centerX, east : e.east, north : e.north, south : centerY, x : long2tile(centerX, newLevel), y : lat2tile(e.north, newLevel), level : newLevel })
levels[newLevel].push( {west : centerX, east : e.east, north : centerY, south : e.south, x : long2tile(centerX, newLevel), y : lat2tile(centerY, newLevel), level : newLevel })
}
createTiles(levels, currentLevel + 1)
}
function boundsToWKT(item) {
let west = item.west
let east = item.east
let north = item.north
let south = item.south
let x = item.x
let y = item.y
let z = item.level
let p1 = { x: west, y : north}
let p2 = { x: east, y : north}
let p3 = { x: east, y : south}
let p4 = { x: west, y : south}
let wktTXT = wkt(p1, p2, p3, p4)
// fse.ensureFileSync(debugPath)
// fse.appendFileSync(debugPath, x +';'+ y +';'+ z + ';'+ wktTXT + '\n')
}
function wkt(p1, p2, p3, p4) {
var str = `POLYGON((${p1.x} ${p1.y},${p2.x} ${p2.y},${p3.x} ${p3.y},${p4.x} ${p4.y},${p1.x} ${p1.y}))`
// console.log(str);
return str
}
function zoomToTileCount(zoom) {
if(zoom === 0) return 1
let tileCount = 1 << zoom
return tileCount
}