-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
357 lines (352 loc) · 9.69 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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
'use strict';
require('colors');
var fs = require('fs');
var path = require('path');
var JsonStream = require('jsonstream3');
var csv = require('csv-parser');
var stream = require('readable-stream');
var Transform = stream.Transform;
var PassThrough = stream.PassThrough;
var shapefile = require('shp-stream').reader;
var yauzl = require('yauzl');
var Proj4Geojson = require('proj4geojson');
var Kml = require('kml-stream');
var wkx = require('wkx');
var easyTypes = ['.geojson', '.kml', '.csv', '.json'];
var allTypes = easyTypes.concat('.shp');
var debug = require('debug')('to-geojson-stream');
module.exports = function () {
var shpnameResolve, shpnameReject;
var filename = new Promise(function (yes, no) {
shpnameResolve = yes;
shpnameReject = no;
});
var stream;
var argv = getArgv();
var createStream = makeStream.bind(null, argv, shpnameResolve, shpnameReject);
return {
stream: function() {
if (!stream) {
stream = createStream();
}
return stream;
},
get filename() {
if (!stream) {
stream = createStream();
}
return filename;
},
args: argv
};
};
function getArgv() {
return require('yargs')
.alias('f', 'file')
.describe('f', 'specify file to upload'.yellow)
.alias('n', 'name')
.describe('n', 'upload from stdin as file'.yellow).alias('u', 'user')
.help('h', 'Show Help'.yellow)
.alias('h', 'help');
}
function makeStream(args, shpnameResolve, shpnameReject) {
var argv = args.argv;
if (!argv.f && !argv.n && !argv._[0]) {
process.stdout.write('name or file is required'.red);
process.stdout.write('\n');
process.exit(4); // eslint-disable-line no-process-exit
}
var fileName = argv.f || argv._[0];
var name;
if (fileName) {
fileName = path.resolve(fileName);
name = path.basename(fileName);
}
if (argv.n) {
name = argv.n;
if (!fileName) {
var ext = path.extname(name);
if (ext === '.shp' || ext === '.zip' || ext === '.kmz') {
process.stdout.write(('must use full path with ' + ext).red);
process.stdout.write('\n');
process.exit(12);// eslint-disable-line no-process-exit
}
}
}
var tempFilename = path.basename(argv.n || argv._[0] || argv.f);
if (path.extname(tempFilename) !== '.zip') {
shpnameResolve(tempFilename);
}
return getMiddleStream(fileName || name);
function toGeoJson() {
return new Transform({
objectMode: true,
transform: function (chunk, _, next) {
var out = {
type: 'Feature',
properties: chunk,
geometry: null
};
if (typeof chunk.lat === 'number' && (typeof chunk.lon === 'number' || typeof chunk.lng === 'number')) {
out.geometry = {
type: 'point',
coordinates: [chunk.lat, chunk.lon || chunk.lng]
};
} else if (typeof chunk.x === 'number' && typeof chunk.y === 'number') {
out.geometry = {
type: 'point',
coordinates: [chunk.x, chunk.y]
};
} else if (chunk.the_geom && chunk.the_geom_webmercator) {
// probably from carto
try {
out.geometry = wkx.Geometry.parse(Buffer.from(chunk.the_geom, 'hex')).toGeoJSON();
} catch (e) {
debug(e);
}
}
this.push(out);
next();
}
});
}
function getStream(thing) {
if (thing) {
return thing;
}
if (fileName) {
return fs.createReadStream(fileName);
}
return process.stdin;
}
function unzipKmz() {
var out = new PassThrough();
yauzl.open(fileName, {autoClose: false}, function (err, zipfile){
if (err) {
return out.emit('error', err);
}
zipfile.on('entry', function (entry) {
if (/\.kml$/.test(entry.fileName)) {
zipfile.openReadStream(entry, function (err, readStream) {
if (err) {
return out.emit('error', err);
}
readStream.pipe(out);
});
}
});
});
return out;
}
function unzipZip() {
var out = new PassThrough({
objectMode: true
});
yauzl.open(fileName, {autoClose: false}, function (err, zipfile){
if (err) {
return out.emit('error', err);
}
var files = new Map();
zipfile.on('entry', function (entry) {
if (/\/$/.test(entry.fileName) || /^__MACOSX/.test(entry.fileName)) {
// directory file names end with '/'
return;
}
files.set(entry.fileName, entry);
});
zipfile.on('end', function () {
finishUp(files, out, zipfile);
});
});
return out;
}
function toArray(thing) {
var out = [];
for (let value of thing) {
out.push(value);
}
return out;
}
function finishUp(files, out, zipfile) {
var keys = toArray(files.keys());
var primary;
if (argv.n) {
let re = new RegExp(argv.n.replace(/\./g, '\\.') + '$');
primary = keys.filter(function (item) {
return item.toLowerCase().match(re);
})[0];
}
if (!primary) {
primary = keys.filter(function (item) {
return allTypes.indexOf(path.extname(item) > -1);
})[0];
}
if (!primary) {
shpnameReject(new Error('name not found'));
zipfile.close();
process.stdout.write('\nnot valid file inside zip'.red);
process.stdout.write('\n');
process.exit(15); // eslint-disable-line no-process-exit
}
shpnameResolve(primary);
var ext = path.extname(primary);
if (easyTypes.indexOf(ext) > -1) {
return zipfile.openReadStream(files.get(primary), function (err, stream) {
if (err) {
return out.emit('error', err);
}
getMiddleStream(primary, stream).pipe(out);
zipfile.close();
});
}
if (ext !== '.shp') {
zipfile.close();
process.stdout.write(('\ninvalid type ' + ext).red);
process.stdout.write('\n');
process.exit(16); // eslint-disable-line no-process-exit
}
getShapeBits(primary, files, zipfile, function (err, res) {
if (err) {
return out.emit('error', err);
}
var shpStream = shapefile({
shp: res.shp,
dbf: res.dbf
}).createReadStream();
if (res.prj) {
shpStream.pipe(transformStream(res.prj, true)).pipe(out);
} else {
shpStream.pipe(out);
}
zipfile.close();
});
}
function getShapeBits(primary, files, zipfile, cb) {
var done = 0;
var out = {};
var e;
var base = path.join(path.dirname(primary), path.basename(primary, '.shp'));
if (files.has(base + '.prj')) {
zipfile.openReadStream(files.get(base + '.prj'), function (err, stream) {
if (e) {
return;
}
if (err) {
cb(err);
e = true;
return;
}
var prj = '';
stream.on('data', function (d) {
prj += d.toString();
}).on('end', function () {
if (e) {
return;
}
out.prj = prj;
done++;
maybeFinish();
});
});
} else {
done++;
}
if (files.has(base + '.dbf')) {
zipfile.openReadStream(files.get(base + '.dbf'), function (err, stream) {
if (e) {
return;
}
if (err) {
cb(err);
e = true;
return;
}
out.dbf = stream;
done++;
maybeFinish();
});
} else {
e = new Error('must include dbf');
return cb(e);
}
zipfile.openReadStream(files.get(primary), function (err, stream) {
if (e) {
return;
}
if (err) {
cb(err);
e = true;
return;
}
out.shp = stream;
done++;
maybeFinish();
});
function maybeFinish() {
if (done === 3 && !e) {
cb(null, out);
}
}
}
function getMiddleStream(name, thing) {
var ext = path.extname(name);
switch(ext) {
case '.geojson':
return getStream(thing).pipe(JsonStream.parse('features.*'));
case '.kml':
return getStream(thing).pipe(new Kml());
case '.kmz':
return unzipKmz().pipe(new Kml());
case '.csv':
return getStream(thing).pipe(csv()).pipe(toGeoJson());
case '.json':
return getStream(thing).pipe(JsonStream.parse('*')).pipe(toGeoJson());
case '.shp':
var dbf = path.join(path.dirname(name), path.basename(name, '.shp') + '.dbf');
return shapefile({
shp: fileName,
dbf: dbf
}).createReadStream().pipe(transformStream(path.join(path.dirname(name), path.basename(name, '.shp') + '.prj')));
case '.zip':
return unzipZip();
default:
process.stdout.write(('\nunknown file type: ' + ext).red);
process.stdout.write('\n');
process.exit(9);// eslint-disable-line no-process-exit
}
}
function makeObject(path, noFile) {
if (noFile) {
return Promise.resolve(new Proj4Geojson(path));
}
return new Promise(function (yes) {
fs.readFile(path, {encoding: 'utf8'}, function (err, file) {
if (err) {
return yes({
feature: function (thing) {
return thing;
}
});
}
yes(new Proj4Geojson(file));
});
});
}
function transformStream(path, noFile) {
var obj = makeObject(path, noFile);
return new Transform({
objectMode: true,
transform: function (chunk, _, next) {
var self = this;
if (!chunk.geometry) {
this.push(chunk);
return next();
}
obj.then(function (transformer) {
self.push(transformer.feature(chunk));
next();
}).catch(next);
}
});
}
}