Skip to content

Commit b7b790a

Browse files
committed
throw descriptive error on missing geometry, close #39
1 parent c96d31c commit b7b790a

2 files changed

Lines changed: 21 additions & 0 deletions

File tree

index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ export class VectorTileFeature {
4747
}
4848

4949
loadGeometry() {
50+
if (this._geometry < 0) throw new Error('feature has no geometry');
5051
const pbf = this._pbf;
5152
pbf.pos = this._geometry;
5253

@@ -101,6 +102,7 @@ export class VectorTileFeature {
101102
}
102103

103104
bbox() {
105+
if (this._geometry < 0) throw new Error('feature has no geometry');
104106
const pbf = this._pbf;
105107
pbf.pos = this._geometry;
106108

test/parse.test.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,25 @@ test('Value message with only an unknown field throws rather than looping', () =
263263
assert.throws(() => new VectorTile(new Protobuf(buf)), /unknown feature value/);
264264
});
265265

266+
test('throws a clear error for a feature with no geometry (issue #39)', () => {
267+
const pbf = new Protobuf();
268+
pbf.writeMessage(3, (_, p) => {
269+
p.writeStringField(1, 'layer');
270+
p.writeMessage(2, (_, p) => {
271+
p.writeVarintField(1, 1);
272+
p.writeVarintField(3, 1);
273+
// intentionally no geometry field
274+
}, null);
275+
p.writeVarintField(5, 4096);
276+
}, null);
277+
278+
const tile = new VectorTile(new Protobuf(pbf.finish()));
279+
const feature = tile.layers.layer.feature(0);
280+
assert.throws(() => feature.loadGeometry(), /feature has no geometry/);
281+
assert.throws(() => feature.bbox(), /feature has no geometry/);
282+
assert.throws(() => feature.toGeoJSON(0, 0, 0), /feature has no geometry/);
283+
});
284+
266285
test('does not mutate prototypes via a "__proto__" layer name or property key', () => {
267286
// Hand-build a minimal MVT tile containing a layer named "__proto__"
268287
// with one feature whose properties include a "__proto__" key.

0 commit comments

Comments
 (0)