From 500814db3c3889ce4c39ca42f6ddd067d925297d Mon Sep 17 00:00:00 2001 From: mike-000 <49240900+mike-000@users.noreply.github.com> Date: Thu, 14 Jan 2021 11:11:01 +0000 Subject: [PATCH 1/2] handle null geometry --- src/ol/format/TopoJSON.js | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/src/ol/format/TopoJSON.js b/src/ol/format/TopoJSON.js index 2b40927e07..0c3178c053 100644 --- a/src/ol/format/TopoJSON.js +++ b/src/ol/format/TopoJSON.js @@ -368,18 +368,18 @@ function readFeatureFromGeometry( name, opt_options ) { - let geometry; + let geometry = null; const type = object.type; - const geometryReader = GEOMETRY_READERS[type]; - if (type === 'Point' || type === 'MultiPoint') { - geometry = geometryReader(object, scale, translate); - } else { - geometry = geometryReader(object, arcs); + if (type) { + const geometryReader = GEOMETRY_READERS[type]; + if (type === 'Point' || type === 'MultiPoint') { + geometry = geometryReader(object, scale, translate); + } else { + geometry = geometryReader(object, arcs); + } + geometry = transformGeometryWithOptions(geometry, false, opt_options); } - const feature = new Feature(); - feature.setGeometry( - transformGeometryWithOptions(geometry, false, opt_options) - ); + const feature = new Feature({geometry: geometry}); if (object.id !== undefined) { feature.setId(object.id); } From 50c99330e9b6c21bd7fe85165cd5f92dca941dac Mon Sep 17 00:00:00 2001 From: mike-000 <49240900+mike-000@users.noreply.github.com> Date: Thu, 14 Jan 2021 11:25:07 +0000 Subject: [PATCH 2/2] test null geometry --- test/spec/ol/format/topojson.test.js | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/test/spec/ol/format/topojson.test.js b/test/spec/ol/format/topojson.test.js index 5ac1afd8e1..bee50e6992 100644 --- a/test/spec/ol/format/topojson.test.js +++ b/test/spec/ol/format/topojson.test.js @@ -48,6 +48,19 @@ const zeroId = { }, }; +const nullGeometry = { + type: 'Topology', + objects: { + foobar: { + type: null, + properties: { + prop0: 'value0', + }, + id: 533, + }, + }, +}; + describe('ol.format.TopoJSON', function () { let format; before(function () { @@ -93,6 +106,17 @@ describe('ol.format.TopoJSON', function () { expect(feature).to.be.a(Feature); expect(feature.getId()).to.be(0); }); + + it('can read a feature with null geometry', function () { + const features = format.readFeaturesFromObject(nullGeometry); + expect(features).to.have.length(1); + + const feature = features[0]; + expect(feature).to.be.a(Feature); + expect(feature.getGeometry()).to.be(null); + expect(feature.getId()).to.be(533); + expect(feature.get('prop0')).to.be('value0'); + }); }); describe('#readFeatures()', function () {