-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
190 lines (157 loc) · 5.47 KB
/
server.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
var express = require('express');
var ogr2ogr = require('ogr2ogr');
var fs = require('fs');
var jsonfile = require('jsonfile');
var app = express();
var path = require('path');
var compression = require('compression');
var multer = require('multer');
var zlib = require('zlib');
var tj = require('togeojson'),
fs = require('fs'),
// node doesn't have xml parsing or a dom. use jsdom
jsdom = require('jsdom').jsdom;
var lzma = require('lzma-native').createStream.bind(null, 'aloneEncoder');
var path = './uploads/';
var detectvectorformat = require('./detectvectorformat');
/*
app.get('/', function (req, res) {
res.send('Hello World using express dasdas');
})
*/
var storage = multer.diskStorage({
destination: function (req, file, callback) {
callback(null, './uploads/');
},
filename: function (req, file, callback) {
callback(null, file.originalname);
}
});
app.use(express.static('assets'));
app.use(express.static('uploads'));
app.use('/assets', express.static('assets'));
app.use('/uploads', express.static('uploads'));
app.set('view engine', 'html');
app.engine('html', require('ejs').renderFile);
var upload = multer({ storage : storage })
app.get('/lsiviewer1', function (req, res) {
/*
var shapefile = ogr2ogr('/Users/Manikanta/Desktop/RESEARCH/isprs/SampleData/shapefiles/Andhra_pradesh_region.shp')
.format('GeoJson')
.skipfailures()
.timeout(1000000)
.stream();
var writeStream = fs.createWriteStream('./shapefile.json');
shapefile.pipe(writeStream);
console.log("hi from lsiviewer...!");
writeStream.on('finish', function() {
// do stuff
res.writeHead(200, {"Content-Type": "application/json"});
var obj = fs.createReadStream('./shapefile.json');
obj.pipe(res);
//callbackTest(res, './shapefile.json');
});
*/
res.writeHead(200, {"Content-Type": "application/json"});
var obj = fs.createReadStream('./shapefile.json');
obj.pipe(res);
});
app.get('/', function(req, res) {
res.render('index.html');
});
app.get('/lsiviewer', function(req, res) {
res.render('lsiviewer.html');
});
app.get('/wms', function(req, res) {
res.render('wms.html');
});
app.get('/login', function(req, res) {
res.render('login.html');
});
app.get('/login_success', function(req, res) {
res.render('login_successful.html');
});
// Necessary for file validation
var MIME_TYPES = {
'shapefile' : ['shp', 'shx', 'dbf'],
'gpx' : ['gpx'],
'kml' : ['kml'],
'geojson' : ['geojson'], //geojson,
'json' : ['json'],
'gml' : ['gml']
}
app.post('/uploadFiles', upload.array('files', 4), function(req, res, next) {
// 1. Files written, 2. iterate through files and check if they are same, 3. convert it into json, 4. return json as response
/**
* Things needed :
1. Validation method that takes request files(atleast for 5 formats)
2. Object that represents MIME types of files
3. Method for json conversion taking format as response
*/
// gen filepath
var validated_object = detectvectorformat.validate_uploaded_files(req);
var filepath = null;
for (var key in MIME_TYPES) {
if (key == validated_object.file_type) {
filepath = path + validated_object.file_name + '.' + MIME_TYPES[key][0];
}
}
var file_type = validated_object.file_type;
var file_name = validated_object.file_name;
// 3. Method for Json conversion (files should have same mime)
if (file_type == "kml") {
// Use toGeoJson to convert from kml to geojson
var kml = jsdom(fs.readFileSync(filepath, 'utf8'));
var converted_json = tj.kml(kml);
var converted_with_styles = tj.kml(kml, { styles: true });
jsonfile.writeFile(path + validated_object.file_name + '.json', converted_with_styles, function(err) {
if(err) {
return console.log(err);
}
res.writeHead(200, {"Content-Type": "application/json"});
var obj = fs.createReadStream(path + validated_object.file_name + '.json');
obj.pipe(res);
});
}
if (file_type == "gpx") {
// Use toGeoJson to convert from gpx to geojson
var gpx = jsdom(fs.readFileSync(filepath));
var converted_json = tj.gpx(gpx);
var converted_with_styles = tj.gpx(gpx, { styles: true });
jsonfile.writeFile(path + validated_object.file_name + '.json', converted_with_styles, function(err) {
if(err) {
return console.log(err);
}
res.writeHead(200, {"Content-Type": "application/json"});
var obj = fs.createReadStream(path + validated_object.file_name + '.json');
obj.pipe(res);
});
}
console.log("file type = " + file_type);
if ((file_type == "shapefile" && req.files.length >= 3) || file_type == "gml") {
var geojson = ogr2ogr(filepath)
.format('GeoJson')
.skipfailures()
.timeout(1000000)
.stream();
var writeStream = fs.createWriteStream(path + validated_object.file_name + '.json');
geojson.pipe(writeStream);
var _res = res;
writeStream.on('finish', function() {
// do stuff
var obj = fs.createReadStream(path + validated_object.file_name + '.json');
_res.writeHead(200, {"Content-Type": "application/json", "content-encoding": "deflate" });
obj.pipe(zlib.createDeflate()).pipe(_res);
});
}
if (file_type == "geojson" || file_type == "json") {
res.writeHead(200, {"Content-Type": "application/json"});
var obj = fs.createReadStream(filepath);
obj.pipe(res);
}
});
var server = app.listen(8095, function () {
var host = server.address().address
var port = server.address().port
console.log("Example app listening at http://%s:%s", host, port)
})