-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIF_tile_server.js
More file actions
338 lines (197 loc) · 8.33 KB
/
IF_tile_server.js
File metadata and controls
338 lines (197 loc) · 8.33 KB
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
var fs = require('fs');
var im = require('imagemagick'); //must also install imagemagick package on server /!\
var async = require('async');
var moment = require('moment');
var morgan = require('morgan');
var connectBusboy = require('connect-busboy');
var bodyParser = require('body-parser');
var express = require('express'),
app = module.exports.app = express();
app.use(express.static(__dirname + '/maps'));
//===== PASSPORT =====//
// set up our express application
app.use(morgan('dev')); // log every request to the console
// app.use(cookieParser()); // read cookies (needed for auth)
// app.use(bodyParser.urlencoded({
// extended: true
// })); // get information from html forms
// app.use(bodyParser.json({
// extended: true
// })); // get information from html forms
//===================//
app.use(connectBusboy());
// var parameters = [''];
// var express = require('express');
// var app = express();
// app.use(express.logger('dev'));
// app.use(express.bodyParser());
// app.use(app.router);
var sys = require('sys')
//var exec = require('child_process').exec;
// function puts(error, stdout, stderr) { sys.puts(stdout) }
// exec("gdal_translate -of VRT -a_srs EPSG:4326 -gcp 0 0 -73.99749 40.75683 -gcp 0 10000 -73.99749 40.7428 -gcp 9529 0 -73.98472 40.75683 -gcp 9529 10000 -73.98472 40.7428 ./tilers_tools/map_to_tiles2.png ./map_vrts/newtesting2.vrt", puts, function(res){
// console.log(res);
// });
// pull in image with coordinates
// measure image size
// plugin here:
var copyright = 'Interface Foundry PBC';
// var mapName = 'incoming';
// var nw_pixel_lng = 0;
// var nw_pixel_lat = 0;
// var nw_loc_lng = -73.99749;
// var nw_loc_lat = 40.75683;
// var sw_pixel_lng = 0;
// var sw_pixel_lat = 10000;
// var sw_loc_lng = -73.99749;
// var sw_loc_lat = 40.7428;
// var ne_pixel_lng = 9529;
// var ne_pixel_lat = 0;
// var ne_loc_lng = -73.98472;
// var ne_loc_lat = 40.75683;
// var se_pixel_lng = 9529;
// var se_pixel_lat = 10000;
// var se_loc_lng = -73.98472;
// var se_loc_lat = 40.7428;
// //generate unique temp image path hash
// var tempIMG = './tilers_tools/map_to_tiles2.png';
// //use same name for vrt
// var tempVRT = './maps/newtesting9.vrt';
// var tempVRT2 = './maps/newtesting10.vrt';
var exec = require('child_process').exec;
app.post('/api/upload', function (req, res) {
//console.log('asdf334');
// buildMap();
// function buildMap(){
//saveImage(req, gotImageID);
saveImage(req, res);
// var gotImageID = function(data) {
// console.log('got data: '+data);
// SEND BACK TO IF-root
// }
});
function saveImage(req, res){
//console.log(req.body);
var fstream;
try {
req.pipe(req.busboy);
try {
req.busboy.on('file', function (fieldname, file, filename) {
// fieldname = fieldname.replace(/%22/g, '"'); //fixing weird angular %22 for " thing
// var coordinates = JSON.parse(fieldname); //incoming box coordinates
var fileName = filename.substr(0, filename.lastIndexOf('.')) || filename;
var fileType = filename.split('.').pop();
while (1) {
var fileNumber = Math.floor((Math.random()*100000000)+1); //generate random file name
var fileNumber_str = fileNumber.toString();
var current = fileNumber_str + '.' + fileType;
//checking for existing file, if unique, write to dir
if (fs.existsSync("temp_img_uploads/" + current)) {
continue; //if there are max # of files in the dir this will infinite loop...
}
else {
console.log('writing new path');
var newPath = "temp_img_uploads/" + current;
fstream = fs.createWriteStream(newPath);
file.pipe(fstream);
fstream.on('close', function () {
console.log('saved image as ' + current);
console.log(fieldname);
processImage(current, res, fieldname);
});
break;
}
}
});
}
catch(err){
console.log('bad map image upload ',err);
}
}
catch(err){
console.log('bad map image upload ',err);
//res.send(500);
}
}
function processImage(id, res, coordinates){
im.identify('temp_img_uploads/' + id, function(err, features){
if (err) throw err
buildTiles(id,res,coordinates,features)
// { format: 'JPEG', width: 3904, height: 2622, depth: 8 }
})
// IF MAP DIR ALREADY EXISTS, then DELETE AND REWRITE WITH NEW ONE
}
function buildTiles(id,res,coordinatesString,size){
console.log('building tiles');
console.log(coordinatesString);
//var coordinates = coordinatesString;
coordinatesString = coordinatesString.replace(/%22/g, '"'); //fixing weird angular %22 for " thing
var coordinates = JSON.parse(coordinatesString); //incoming box coordinates
//console.log(coordinates);
var mapImage = 'temp_img_uploads/' + id;
var worldMapVRT = './maps/'+coordinates.localMapID+'.vrt';
var worldMapVRT2 ='./maps/'+coordinates.localMapID + '_warped.vrt';
var nw_pixel_lng = 0;
var nw_pixel_lat = 0;
var nw_loc_lng = coordinates.nw_loc_lng;
var nw_loc_lat = coordinates.nw_loc_lat;
var sw_pixel_lng = 0;
var sw_pixel_lat = size.height;
var sw_loc_lng = coordinates.sw_loc_lng;
var sw_loc_lat = coordinates.sw_loc_lat;
var ne_pixel_lng = size.width;
var ne_pixel_lat = 0;
var ne_loc_lng = coordinates.ne_loc_lng;
var ne_loc_lat = coordinates.ne_loc_lat;
var se_pixel_lng = size.width;
var se_pixel_lat = size.height;
var se_loc_lng = coordinates.se_loc_lng;
var se_loc_lat = coordinates.se_loc_lat;
//console.log('process '+id);
console.log('localMapID:');
console.log(coordinates.localMapID);
if (coordinates.localMapID){
//console.log('gdal_translate -of VRT -a_srs EPSG:4326 -gcp '+ nw_pixel_lng +' '+ nw_pixel_lat +' '+ nw_loc_lng +' '+ nw_loc_lat +' -gcp '+ sw_pixel_lng +' '+ sw_pixel_lat +' '+ sw_loc_lng +' '+ sw_loc_lat +' -gcp '+ ne_pixel_lng +' '+ ne_pixel_lat +' '+ ne_loc_lng +' '+ ne_loc_lat +' -gcp '+ se_pixel_lng +' '+ se_pixel_lat +' '+ se_loc_lng +' '+ se_loc_lat +' '+ mapImage +' '+ worldMapVRT + '');
//build VRT file from image pixels && location coordinates
exec('gdal_translate -of VRT -a_srs EPSG:4326 -gcp '+ nw_pixel_lng +' '+ nw_pixel_lat +' '+ nw_loc_lng +' '+ nw_loc_lat +' -gcp '+ sw_pixel_lng +' '+ sw_pixel_lat +' '+ sw_loc_lng +' '+ sw_loc_lat +' -gcp '+ ne_pixel_lng +' '+ ne_pixel_lat +' '+ ne_loc_lng +' '+ ne_loc_lat +' -gcp '+ se_pixel_lng +' '+ se_pixel_lat +' '+ se_loc_lng +' '+ se_loc_lat +' '+ mapImage +' '+ worldMapVRT + '', function(err, stdout, stderr) {
// React to callback
console.log(stderr);
console.log(stdout);
//warp VRT to earth curvature
exec('gdalwarp -of VRT -t_srs EPSG:4326 '+worldMapVRT+' '+worldMapVRT2+'', function(err2, stdout2, stderr2) {
console.log(stderr2);
console.log(stdout2);
//build tiles from warped VRT
//add in -w none
exec('gdal2tiles.py -k -n '+worldMapVRT2+' '+__dirname+'/maps/'+worldMapVRT2, function(err3, stdout3, stderr3) {
console.log(stderr3);
console.log(stdout3);
var zoomLevels = [];
var files = fs.readdirSync('./maps/maps/'+coordinates.localMapID + '_warped.vrt');
for (var i in files) {
if(/^\d+$/.test(files[i])) { //sort for whole numbers in files (zooms)
zoomLevels.push(files[i]);
}
}
var mapResponse = {
mapURL: coordinates.localMapID + '_warped.vrt',
zooms: zoomLevels,
worldID: coordinates.worldID
};
var map_text = JSON.stringify(mapResponse);
res.send(map_text);
//delete temp img and vrt
//remove temp map file
fs.unlink(__dirname + '/temp_img_uploads/' + id, function (err) {
if (err) throw err;
console.log('successfully deleted '+__dirname + '/temp_img_uploads/' + id);
});
});
});
});
}
// res.send('asdf');
}
app.listen(3000, function() {
console.log("Illya casting tile build! on 3000 ~ ~ ♡");
});