Skip to content

Commit a32fd8c

Browse files
committed
fuck it, you can omit the dbf
1 parent 8cc8283 commit a32fd8c

File tree

8 files changed

+69
-37
lines changed

8 files changed

+69
-37
lines changed

lib/binaryajax-fetch.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ module.exports = async function binaryAjax (_url, type) {
1919
const parsed = await resp.arrayBuffer();
2020
return Buffer.from(parsed);
2121
} catch (e) {
22-
if (isOptionalTxt) {
22+
console.log('ERROR', e, type);
23+
if (isOptionalTxt || type === 'dbf') {
2324
return false;
2425
}
2526
throw e;

lib/binaryajax.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ function binaryAjax (_url, type) {
2828
});
2929
res.on('end', function () {
3030
if (res.statusCode > 399) {
31-
if (type === 'prj' || type === 'cpg') {
31+
if (type === 'prj' || type === 'cpg' || type === 'dbf') {
3232
return resolve(false);
3333
} else {
3434
return reject(new Error(Buffer.concat(buffers, len).toString()));

lib/index.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,17 +45,20 @@ function shp (base, whiteList) {
4545
return resp;
4646
});
4747
}
48-
shp.combine = function (arr) {
48+
shp.combine = function ([shp, dbf]) {
4949
const out = {};
5050
out.type = 'FeatureCollection';
5151
out.features = [];
5252
let i = 0;
53-
const len = arr[0].length;
53+
const len = shp.length;
54+
if (!dbf) {
55+
dbf = [];
56+
}
5457
while (i < len) {
5558
out.features.push({
5659
type: 'Feature',
57-
geometry: arr[0][i],
58-
properties: arr[1][i]
60+
geometry: shp[i],
61+
properties: dbf[i] || {}
5962
});
6063
i++;
6164
}
@@ -134,6 +137,9 @@ const handleDbf = async (base) => {
134137
binaryAjax(base, 'dbf'),
135138
binaryAjax(base, 'cpg')
136139
]);
140+
if (!dbf) {
141+
return;
142+
}
137143
return parseDbf(dbf, cpg);
138144
};
139145
const checkSuffix = (base, suffix) => {

test/bundle.js

Lines changed: 34 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3493,7 +3493,8 @@ module.exports = async function binaryAjax (_url, type) {
34933493
const parsed = await resp.arrayBuffer();
34943494
return Buffer.from(parsed);
34953495
} catch (e) {
3496-
if (isOptionalTxt) {
3496+
console.log('ERROR', e, type);
3497+
if (isOptionalTxt || type === 'dbf') {
34973498
return false;
34983499
}
34993500
throw e;
@@ -3564,17 +3565,20 @@ function shp (base, whiteList) {
35643565
return resp;
35653566
});
35663567
}
3567-
shp.combine = function (arr) {
3568+
shp.combine = function ([shp, dbf]) {
35683569
const out = {};
35693570
out.type = 'FeatureCollection';
35703571
out.features = [];
35713572
let i = 0;
3572-
const len = arr[0].length;
3573+
const len = shp.length;
3574+
if (!dbf) {
3575+
dbf = [];
3576+
}
35733577
while (i < len) {
35743578
out.features.push({
35753579
type: 'Feature',
3576-
geometry: arr[0][i],
3577-
properties: arr[1][i]
3580+
geometry: shp[i],
3581+
properties: dbf[i] || {}
35783582
});
35793583
i++;
35803584
}
@@ -3653,6 +3657,9 @@ const handleDbf = async (base) => {
36533657
binaryAjax(base, 'dbf'),
36543658
binaryAjax(base, 'cpg')
36553659
]);
3660+
if (!dbf) {
3661+
return;
3662+
}
36563663
return parseDbf(dbf, cpg);
36573664
};
36583665
const checkSuffix = (base, suffix) => {
@@ -21135,16 +21142,11 @@ describe('Shp', function () {
2113521142
});
2113621143
});
2113721144
describe('county zipped', function () {
21138-
const pandr = shp('http://localhost:3000/test/data/counties.zip');
21139-
it('should have the right keys', function () {
21140-
return pandr.should.eventually.contain.keys('type', 'features');
21141-
});
21142-
it('should be the right type', function () {
21143-
return pandr.should.eventually.have.property('type', 'FeatureCollection');
21144-
});
21145-
it('should have the right number of features', function () {
21146-
return pandr.then(function (a) { return a.features; }).should.eventually.have.length(14);
21147-
});
21145+
return shp('http://localhost:3000/test/data/counties.zip').then(thing => {
21146+
thing.should.contain.keys('type', 'features');
21147+
thing.should.have.property('type', 'FeatureCollection');
21148+
return thing.features;
21149+
}).should.eventually.have.length(14);
2114821150
});
2114921151
describe('trains zipped', function () {
2115021152
const pandr = shp('http://localhost:3000/test/data/train_stations.zip');
@@ -21357,9 +21359,23 @@ describe('Shp', function () {
2135721359
return item.features.length;
2135821360
}).should.eventually.equal(3);
2135921361
});
21360-
it('file too long', function(){
21361-
return shp('http://localhost:3000/test/data/ipra_dresden_polygon');
21362-
});
21362+
it('file too long', function () {
21363+
return shp('http://localhost:3000/test/data/ipra_dresden_polygon');
21364+
});
21365+
it('should handle missing dbf', function () {
21366+
return shp('http://localhost:3000/test/data/no-dbf').then(thing => {
21367+
thing.should.contain.keys('type', 'features');
21368+
thing.should.have.property('type', 'FeatureCollection');
21369+
return thing.features;
21370+
}).should.eventually.have.length(14);
21371+
});
21372+
it('should handle missing dbf in a zip', function () {
21373+
return shp('http://localhost:3000/test/data/no-dbf.zip').then(thing => {
21374+
thing.should.contain.keys('type', 'features');
21375+
thing.should.have.property('type', 'FeatureCollection');
21376+
return thing.features;
21377+
}).should.eventually.have.length(14);
21378+
});
2136321379
});
2136421380
});
2136521381

test/data/no-dbf.sbx

132 Bytes
Binary file not shown.

test/data/no-dbf.shp

622 KB
Binary file not shown.

test/data/no-dbf.zip

475 KB
Binary file not shown.

test/test.js

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -79,16 +79,11 @@ describe('Shp', function () {
7979
});
8080
});
8181
describe('county zipped', function () {
82-
const pandr = shp('http://localhost:3000/test/data/counties.zip');
83-
it('should have the right keys', function () {
84-
return pandr.should.eventually.contain.keys('type', 'features');
85-
});
86-
it('should be the right type', function () {
87-
return pandr.should.eventually.have.property('type', 'FeatureCollection');
88-
});
89-
it('should have the right number of features', function () {
90-
return pandr.then(function (a) { return a.features; }).should.eventually.have.length(14);
91-
});
82+
return shp('http://localhost:3000/test/data/counties.zip').then(thing => {
83+
thing.should.contain.keys('type', 'features');
84+
thing.should.have.property('type', 'FeatureCollection');
85+
return thing.features;
86+
}).should.eventually.have.length(14);
9287
});
9388
describe('trains zipped', function () {
9489
const pandr = shp('http://localhost:3000/test/data/train_stations.zip');
@@ -301,8 +296,22 @@ describe('Shp', function () {
301296
return item.features.length;
302297
}).should.eventually.equal(3);
303298
});
304-
it('file too long', function(){
305-
return shp('http://localhost:3000/test/data/ipra_dresden_polygon');
306-
});
299+
it('file too long', function () {
300+
return shp('http://localhost:3000/test/data/ipra_dresden_polygon');
301+
});
302+
it('should handle missing dbf', function () {
303+
return shp('http://localhost:3000/test/data/no-dbf').then(thing => {
304+
thing.should.contain.keys('type', 'features');
305+
thing.should.have.property('type', 'FeatureCollection');
306+
return thing.features;
307+
}).should.eventually.have.length(14);
308+
});
309+
it('should handle missing dbf in a zip', function () {
310+
return shp('http://localhost:3000/test/data/no-dbf.zip').then(thing => {
311+
thing.should.contain.keys('type', 'features');
312+
thing.should.have.property('type', 'FeatureCollection');
313+
return thing.features;
314+
}).should.eventually.have.length(14);
315+
});
307316
});
308317
});

0 commit comments

Comments
 (0)