Handle multipolygons with empty polygons

This commit is contained in:
Andreas Hocevar
2022-07-20 13:18:41 +02:00
parent 14041e490f
commit c8508bcf97
4 changed files with 58 additions and 7 deletions

View File

@@ -94,6 +94,9 @@ export function deflateMultiCoordinatesArray(
stride, stride,
endss[i] endss[i]
); );
if (ends.length === 0) {
ends[0] = offset;
}
endss[i++] = ends; endss[i++] = ends;
offset = ends[ends.length - 1]; offset = ends[ends.length - 1];
} }

View File

@@ -79,13 +79,16 @@ export function inflateMultiCoordinatesArray(
let i = 0; let i = 0;
for (let j = 0, jj = endss.length; j < jj; ++j) { for (let j = 0, jj = endss.length; j < jj; ++j) {
const ends = endss[j]; const ends = endss[j];
coordinatesss[i++] = inflateCoordinatesArray( coordinatesss[i++] =
flatCoordinates, ends.length === 1 && ends[0] === offset
offset, ? []
ends, : inflateCoordinatesArray(
stride, flatCoordinates,
coordinatesss[i] offset,
); ends,
stride,
coordinatesss[i]
);
offset = ends[ends.length - 1]; offset = ends[ends.length - 1];
} }
coordinatesss.length = i; coordinatesss.length = i;

View File

@@ -516,6 +516,26 @@ describe('ol/format/GeoJSON.js', function () {
expect(array[1]).to.be.a(LineString); expect(array[1]).to.be.a(LineString);
expect(array[1].getLayout()).to.eql('XY'); 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 () { describe('#readProjection', function () {
@@ -1025,5 +1045,28 @@ describe('ol/format/GeoJSON.js', function () {
[43, 39], [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,
});
});
}); });
}); });

View File

@@ -322,6 +322,7 @@ describe('ol/geom/MultiPolygon.js', function () {
[cw, ccw], [cw, ccw],
[cw2, ccw2], [cw2, ccw2],
]); ]);
const withEmptyPolygon = new MultiPolygon([[ccw], []]);
it('returns coordinates as they were constructed', function () { it('returns coordinates as they were constructed', function () {
expect(right.getCoordinates()).to.eql([ expect(right.getCoordinates()).to.eql([
@@ -332,6 +333,7 @@ describe('ol/geom/MultiPolygon.js', function () {
[cw, ccw], [cw, ccw],
[cw2, ccw2], [cw2, ccw2],
]); ]);
expect(withEmptyPolygon.getCoordinates()).to.eql([[ccw], []]);
}); });
it('can return coordinates with right-hand orientation', function () { it('can return coordinates with right-hand orientation', function () {