-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
149 lines (148 loc) · 3.64 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
const shpWritter = require('./shp');
const DbfWritter = require('./dbf');
const stream = require('readable-stream');
const pump = require('pump');
const debug = require('debug')('shp-write-stream:index')
class Input extends stream.Writable {
constructor(schema, createStream, opts, cb) {
super({
objectMode: true
});
if (typeof opts === 'function') {
cb = opts;
opts = {};
}
if (typeof opts.preferPoint === 'undefined') {
this.preferPoint = true;
} else {
this.preferPoint = !!opts.preferPoint;
}
if (Array.isArray(schema)) {
this.schema = {
point: schema,
multipoint: schema,
line: schema,
polygon: schema
}
} else {
this.schema = schema
}
this.schema = schema;
this.createStream = createStream;
this.cb = cb;
this.cbCalled = false;
this.toClose = [];
this.typeStreams = new Map();
this.inProgress = 0;
this.out = {};
this.on('finish', ()=>{
for (let stream of this.toClose) {
stream.end();
}
});
}
_write(chunk, _, next) {
const geom = chunk.geometry;
const properties = chunk.properties;
const {dbf,shp} = this.getStream(geom.type);
if (!dbf || !shp) {
return next();
}
let otherDone = false;
function done(err) {
if (err) {
return next(err);
}
if (otherDone) {
next();
} else {
otherDone = true;
}
}
shp.write(geom, done);
dbf.write(properties, done);
}
getStream(type) {
let shpType;
switch (type) {
case 'Point':
if (this.preferPoint) {
shpType = 'point';
} else {
shpType = 'multipoint';
}
break;
case 'MultiPoint':
shpType = 'multipoint';
break;
case 'LineString':
case 'MultiLineString':
shpType = 'line';
break;
case 'Polygon':
case 'MultiPolygon':
shpType = 'polygon';
break;
default:
return {};
}
if (this.typeStreams.has(shpType)) {
return this.typeStreams.get(shpType);
}
if (!this.schema[shpType]) {
throw new Error('no schema for type')
}
const dbf = new DbfWritter(this.schema[shpType]);
const {shp, shx} = shpWritter(shpType);
const dbfOut = this.createStream(shpType, 'dbf');
const shpOut = this.createStream(shpType, 'shp');
const shxOut = this.createStream(shpType, 'shx');
this.handlePiping(dbf, dbfOut, shpType, 'dbf');
this.handlePiping(shp, shpOut, shpType, 'shp');
this.handlePiping(shx, shxOut);
const out = {dbf, shp, shx};
this.typeStreams.set(shpType, out);
return out;
}
callCB(err, data) {
if (this.cbCalled) {
return;
}
this.cbCalled = true;
let cb = this.cb;
cb(err, data);
}
maybeFinish() {
if (this.inProgress) {
return;
}
this.callCB(null, this.out);
}
handlePiping(from, to, type, file) {
this.toClose.push(from);
this.inProgress++;
pump(from, to, err=>{
this.inProgress--;
debug(`done, inProgress: ${this.inProgress}, err: ${err}`)
if (err) {
return this.callCB(err);
}
if (type) {
if (!this.out[type]) {
this.out[type] = {};
}
if (file === 'shp') {
let {shp, shx} = from.generateHeader();
this.out[type].shp = shp;
this.out[type].shx = shx;
} else if (file === 'dbf') {
this.out[type].dbf = from.generateHeader();
}
}
this.maybeFinish();
})
}
}
module.exports = function (schema, createStream, cb) {
return new Input(schema, createStream ,cb);
}