Skip to content

Fix centroid calculation. Bump turf to v6.5 #7115

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 15 commits into from
Aug 19, 2024
2 changes: 2 additions & 0 deletions draftlogs/7115_fix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
- Fix centroid calculation in turf [[#7115](https://github.com/plotly/plotly.js/pull/7115)], with thanks to @birkskyum for the contribution!

91 changes: 40 additions & 51 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,9 @@
"@plotly/d3-sankey": "0.7.2",
"@plotly/d3-sankey-circular": "0.33.1",
"@plotly/mapbox-gl": "1.13.4",
"@turf/area": "^6.4.0",
"@turf/bbox": "^6.4.0",
"@turf/centroid": "^6.0.2",
"@turf/area": "^6.5.0",
"@turf/bbox": "^6.5.0",
"@turf/centroid": "^6.5.0",
"base64-arraybuffer": "^1.0.2",
"canvas-fit": "^1.5.0",
"color-alpha": "1.0.4",
Expand Down
6 changes: 5 additions & 1 deletion src/lib/geo_location_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,11 @@ function extractTraceFeature(calcTrace) {
};

// Compute centroid, add it to the properties
fOut.properties.ct = findCentroid(fOut);
if (fOut.geometry.coordinates.length > 0) {
fOut.properties.ct = findCentroid(fOut);
} else {
fOut.properties.ct = [NaN, NaN];
}

// Mutate in in/out features into calcdata
cdi.fIn = fIn;
Expand Down
Binary file modified test/image/baselines/geo_featureidkey.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/image/baselines/geo_fitbounds-geojson.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion test/jasmine/tests/choropleth_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,7 @@ describe('Test choropleth hover:', function() {
fig.data[0].hovertemplate = '%{properties.name}<extra>%{ct[0]:.1f} | %{ct[1]:.1f}</extra>';
fig.layout.geo.projection = {scale: 20};

run(hasCssTransform, [300, 200], fig, ['New York', '-75.1 | 42.6'])
run(hasCssTransform, [300, 200], fig, ['New York', '-75.2 | 42.6'])
.then(done, done.fail);
});
});
Expand Down
8 changes: 4 additions & 4 deletions test/jasmine/tests/choroplethmap_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -489,7 +489,7 @@ describe('Test choroplethmap convert:', function() {
var opts = convertModule.convert(calcTrace);

expect(opts.geojson.features[0].geometry.coordinates).toBe(coordsIn);
expect(calcTrace[0].ct).toEqual([100.4, 0.4]);
expect(calcTrace[0].ct).toEqual([100.5, 0.5]);
});

it('should find correct centroid (multi-polygon case)', function() {
Expand All @@ -498,7 +498,7 @@ describe('Test choroplethmap convert:', function() {
var coordsIn = [
[
// this one has the bigger area
[[30, 20], [45, 40], [10, 40], [30, 20]]
[[30, 20], [47, 40], [10, 33], [30, 20]]
],
[
[[15, 5], [40, 10], [10, 20], [5, 10], [15, 5]]
Expand All @@ -511,7 +511,7 @@ describe('Test choroplethmap convert:', function() {
var opts = convertModule.convert(calcTrace);

expect(opts.geojson.features[0].geometry.coordinates).toBe(coordsIn);
expect(calcTrace[0].ct).toEqual([28.75, 30]);
expect(calcTrace[0].ct).toEqual([29, 31]);
});
});

Expand Down Expand Up @@ -639,7 +639,7 @@ describe('Test choroplethmap hover:', function() {
return fig;
},
nums: '### 100',
name: '-86.7 | 32.0 ###',
name: '-86.7 | 31.9 ###',
evtPts: [{location: 100, z: 10, pointNumber: 0, curveNumber: 0}]
}];

Expand Down
8 changes: 4 additions & 4 deletions test/jasmine/tests/choroplethmapbox_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -490,7 +490,7 @@ describe('Test choroplethmapbox convert:', function() {
var opts = convertModule.convert(calcTrace);

expect(opts.geojson.features[0].geometry.coordinates).toBe(coordsIn);
expect(calcTrace[0].ct).toEqual([100.4, 0.4]);
expect(calcTrace[0].ct).toEqual([100.5, 0.5]);
});

it('should find correct centroid (multi-polygon case)', function() {
Expand All @@ -499,7 +499,7 @@ describe('Test choroplethmapbox convert:', function() {
var coordsIn = [
[
// this one has the bigger area
[[30, 20], [45, 40], [10, 40], [30, 20]]
[[30, 20], [47, 40], [10, 33], [30, 20]]
],
[
[[15, 5], [40, 10], [10, 20], [5, 10], [15, 5]]
Expand All @@ -512,7 +512,7 @@ describe('Test choroplethmapbox convert:', function() {
var opts = convertModule.convert(calcTrace);

expect(opts.geojson.features[0].geometry.coordinates).toBe(coordsIn);
expect(calcTrace[0].ct).toEqual([28.75, 30]);
expect(calcTrace[0].ct).toEqual([29, 31]);
});
});

Expand Down Expand Up @@ -640,7 +640,7 @@ describe('Test choroplethmapbox hover:', function() {
return fig;
},
nums: '### 100',
name: '-86.7 | 32.0 ###',
name: '-86.7 | 31.9 ###',
evtPts: [{location: 100, z: 10, pointNumber: 0, curveNumber: 0}]
}];

Expand Down