diff --git a/src/ol/geom/flat/deflate.js b/src/ol/geom/flat/deflate.js index 882f95f1e9..42ae0f43fc 100644 --- a/src/ol/geom/flat/deflate.js +++ b/src/ol/geom/flat/deflate.js @@ -94,6 +94,9 @@ export function deflateMultiCoordinatesArray( stride, endss[i] ); + if (ends.length === 0) { + ends[0] = offset; + } endss[i++] = ends; offset = ends[ends.length - 1]; } diff --git a/src/ol/geom/flat/inflate.js b/src/ol/geom/flat/inflate.js index 774a813fc9..1db29689e2 100644 --- a/src/ol/geom/flat/inflate.js +++ b/src/ol/geom/flat/inflate.js @@ -79,13 +79,16 @@ export function inflateMultiCoordinatesArray( let i = 0; for (let j = 0, jj = endss.length; j < jj; ++j) { const ends = endss[j]; - coordinatesss[i++] = inflateCoordinatesArray( - flatCoordinates, - offset, - ends, - stride, - coordinatesss[i] - ); + coordinatesss[i++] = + ends.length === 1 && ends[0] === offset + ? [] + : inflateCoordinatesArray( + flatCoordinates, + offset, + ends, + stride, + coordinatesss[i] + ); offset = ends[ends.length - 1]; } coordinatesss.length = i; diff --git a/test/node/ol/format/GeoJSON.test.js b/test/node/ol/format/GeoJSON.test.js index 16522d4aeb..7c127006ba 100644 --- a/test/node/ol/format/GeoJSON.test.js +++ b/test/node/ol/format/GeoJSON.test.js @@ -516,6 +516,26 @@ describe('ol/format/GeoJSON.js', function () { expect(array[1]).to.be.a(LineString); expect(array[1].getLayout()).to.eql('XY'); }); + + it('works with empty coordinate arrays', function () { + const coordinates = [ + [ + [ + [1, 2], + [3, 4], + [5, 6], + [1, 2], + ], + ], + [], + ]; + const geojson = { + type: 'MultiPolygon', + coordinates: coordinates, + }; + const geometry = format.readGeometry(geojson); + expect(geometry.getCoordinates()).to.eql(coordinates); + }); }); describe('#readProjection', function () { @@ -1025,5 +1045,28 @@ describe('ol/format/GeoJSON.js', function () { [43, 39], ]); }); + + it('works with empty coordinate arrays', function () { + const coordinates = [ + [ + [ + [1, 2], + [3, 4], + [5, 6], + [1, 2], + ], + ], + [], + ]; + const geometry = new MultiPolygon([ + new Polygon(coordinates[0]), + new Polygon(coordinates[1]), + ]); + const geojson = format.writeGeometryObject(geometry); + expect(geojson).to.eql({ + type: 'MultiPolygon', + coordinates: coordinates, + }); + }); }); }); diff --git a/test/node/ol/geom/MultiPolygon.test.js b/test/node/ol/geom/MultiPolygon.test.js index 722da6c708..0f12ac80e0 100644 --- a/test/node/ol/geom/MultiPolygon.test.js +++ b/test/node/ol/geom/MultiPolygon.test.js @@ -322,6 +322,7 @@ describe('ol/geom/MultiPolygon.js', function () { [cw, ccw], [cw2, ccw2], ]); + const withEmptyPolygon = new MultiPolygon([[ccw], []]); it('returns coordinates as they were constructed', function () { expect(right.getCoordinates()).to.eql([ @@ -332,6 +333,7 @@ describe('ol/geom/MultiPolygon.js', function () { [cw, ccw], [cw2, ccw2], ]); + expect(withEmptyPolygon.getCoordinates()).to.eql([[ccw], []]); }); it('can return coordinates with right-hand orientation', function () {