forked from calvinmetcalf/shapefile-js
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
180 lines (176 loc) · 4.48 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
'use strict';
let proj4 = require('proj4');
if (proj4.default) {
proj4 = proj4.default;
}
const unzip = require('./unzip');
const binaryAjax = require('./binaryajax');
const parseShp = require('./parseShp');
const parseDbf = require('parsedbf');
const Promise = require('lie');
const Cache = require('lru-cache');
const Buffer = require('buffer').Buffer;
const URL = global.URL;
const cache = new Cache({
max: 20
});
function toBuffer (b) {
if (!b) {
throw new Error('forgot to pass buffer');
}
if (Buffer.isBuffer(b)) {
return b;
}
if (b instanceof global.ArrayBuffer) {
return Buffer.from(b);
}
if (b.buffer instanceof global.ArrayBuffer) {
if (b.BYTES_PER_ELEMENT === 1) {
return Buffer.from(b);
}
return Buffer.from(b.buffer);
}
}
function shp (base, whiteList) {
if (typeof base === 'string' && cache.has(base)) {
return Promise.resolve(cache.get(base));
}
return shp.getShapefile(base, whiteList).then(function (resp) {
if (typeof base === 'string') {
cache.set(base, resp);
}
return resp;
});
}
shp.combine = function ([shp, dbf]) {
const out = {};
out.type = 'FeatureCollection';
out.features = [];
let i = 0;
const len = shp.length;
if (!dbf) {
dbf = [];
}
while (i < len) {
out.features.push({
type: 'Feature',
geometry: shp[i],
properties: dbf[i] || {}
});
i++;
}
return out;
};
shp.parseZip = async function (buffer, whiteList) {
let key;
buffer = toBuffer(buffer);
const zip = await unzip(buffer);
const names = [];
whiteList = whiteList || [];
for (key in zip) {
if (key.indexOf('__MACOSX') !== -1) {
continue;
}
if (key.slice(-3).toLowerCase() === 'shp') {
names.push(key.slice(0, -4));
zip[key.slice(0, -3) + key.slice(-3).toLowerCase()] = zip[key];
} else if (key.slice(-3).toLowerCase() === 'prj') {
zip[key.slice(0, -3) + key.slice(-3).toLowerCase()] = proj4(zip[key]);
} else if (key.slice(-4).toLowerCase() === 'json' || whiteList.indexOf(key.split('.').pop()) > -1) {
names.push(key.slice(0, -3) + key.slice(-3).toLowerCase());
} else if (key.slice(-3).toLowerCase() === 'dbf' || key.slice(-3).toLowerCase() === 'cpg') {
zip[key.slice(0, -3) + key.slice(-3).toLowerCase()] = zip[key];
}
}
if (!names.length) {
throw new Error('no layers founds');
}
const geojson = names.map(function (name) {
let parsed, dbf;
const lastDotIdx = name.lastIndexOf('.');
if (lastDotIdx > -1 && name.slice(lastDotIdx).indexOf('json') > -1) {
parsed = JSON.parse(zip[name]);
parsed.fileName = name.slice(0, lastDotIdx);
} else if (whiteList.indexOf(name.slice(lastDotIdx + 1)) > -1) {
parsed = zip[name];
parsed.fileName = name;
} else {
if (zip[name + '.dbf']) {
dbf = parseDbf(zip[name + '.dbf'], zip[name + '.cpg']);
}
parsed = shp.combine([parseShp(zip[name + '.shp'], zip[name + '.prj']), dbf]);
parsed.fileName = name;
}
return parsed;
});
if (geojson.length === 1) {
return geojson[0];
} else {
return geojson;
}
};
async function getZip (base, whiteList) {
const a = await binaryAjax(base);
return shp.parseZip(a, whiteList);
}
const handleShp = async (base) => {
const args = await Promise.all([
binaryAjax(base, 'shp'),
binaryAjax(base, 'prj')
]);
let prj = false;
try {
if (args[1]) {
prj = proj4(args[1]);
}
} catch (e) {
prj = false;
}
return parseShp(args[0], prj);
};
const handleDbf = async (base) => {
const [dbf, cpg] = await Promise.all([
binaryAjax(base, 'dbf'),
binaryAjax(base, 'cpg')
]);
if (!dbf) {
return;
}
return parseDbf(dbf, cpg);
};
const checkSuffix = (base, suffix) => {
const url = new URL(base);
return url.pathname.slice(-4).toLowerCase() === suffix;
};
shp.getShapefile = async function (base, whiteList) {
if (typeof base !== 'string') {
return shp.parseZip(base);
}
if (checkSuffix(base, '.zip')) {
return getZip(base, whiteList);
}
const results = await Promise.all([
handleShp(base),
handleDbf(base)
]);
return shp.combine(results);
};
shp.parseShp = function (shp, prj) {
shp = toBuffer(shp);
if (Buffer.isBuffer(prj)) {
prj = prj.toString();
}
if (typeof prj === 'string') {
try {
prj = proj4(prj);
} catch (e) {
prj = false;
}
}
return parseShp(shp, prj);
};
shp.parseDbf = function (dbf, cpg) {
dbf = toBuffer(dbf);
return parseDbf(dbf, cpg);
};
module.exports = shp;