Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions draftlogs/7921_fix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- Prevent degenerate MultiPolygon features from aborting choropleth rendering [[#7921](https://github.com/plotly/plotly.js/pull/7921)]
3 changes: 3 additions & 0 deletions src/lib/geo_location_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,9 @@ function findCentroid(feature) {
poly = geometry;
}

// Degenerate MultiPolygons may not contain a positive-area polygon.
if (!poly) return [NaN, NaN];

return turfCentroid(poly).geometry.coordinates;
}

Expand Down
65 changes: 64 additions & 1 deletion test/jasmine/tests/lib_geo_location_utils_test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,67 @@
const { getFitboundsLonRange, unwrapLonRange, doesCrossAntiMeridian } = require('../../../src/lib/geo_location_utils');
const {
extractTraceFeature,
getFitboundsLonRange,
unwrapLonRange,
doesCrossAntiMeridian
} = require('../../../src/lib/geo_location_utils');

describe('Test geo_location_utils.extractTraceFeature', () => {
it('keeps degenerate MultiPolygons without affecting valid features', () => {
const trace = {
_length: 2,
geojson: {
type: 'FeatureCollection',
features: [
{
type: 'Feature',
id: 'degenerate',
geometry: {
type: 'MultiPolygon',
coordinates: [
[
[
[0, 0],
[1, 1],
[0, 0],
[0, 0]
]
]
]
}
},
{
type: 'Feature',
id: 'valid',
geometry: {
type: 'Polygon',
coordinates: [
[
[0, 0],
[0, 1],
[1, 1],
[1, 0],
[0, 0]
]
]
}
}
]
}
};
const calcTrace = [
{ loc: 'degenerate', trace },
{ loc: 'valid', trace }
];

const features = extractTraceFeature(calcTrace);

expect(features.length).toBe(2);
expect(features[0].id).toBe('degenerate');
expect(features[0].properties.ct.every(Number.isNaN)).toBe(true);
expect(features[1].id).toBe('valid');
expect(features[1].properties.ct.every(Number.isFinite)).toBe(true);
});
});

describe('Test geo_location_utils.getFitboundsLonRange', () => {
it('returns the compact crossing range when point data straddles the antimeridian', () => {
Expand Down