Skip to content

Commit 634293a

Browse files
authored
Merge pull request #7115 from birkskyum/bump-turf
Fix centroid calculation. Bump turf to v6.5
2 parents fe1750d + a6e151a commit 634293a

File tree

9 files changed

+59
-64
lines changed

9 files changed

+59
-64
lines changed

draftlogs/7115_fix.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
- Fix centroid calculation in turf [[#7115](https://github.com/plotly/plotly.js/pull/7115)], with thanks to @birkskyum for the contribution!
2+

package-lock.json

Lines changed: 40 additions & 51 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,9 @@
7171
"@plotly/d3-sankey": "0.7.2",
7272
"@plotly/d3-sankey-circular": "0.33.1",
7373
"@plotly/mapbox-gl": "1.13.4",
74-
"@turf/area": "^6.4.0",
75-
"@turf/bbox": "^6.4.0",
76-
"@turf/centroid": "^6.0.2",
74+
"@turf/area": "^6.5.0",
75+
"@turf/bbox": "^6.5.0",
76+
"@turf/centroid": "^6.5.0",
7777
"base64-arraybuffer": "^1.0.2",
7878
"canvas-fit": "^1.5.0",
7979
"color-alpha": "1.0.4",

src/lib/geo_location_utils.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,11 @@ function extractTraceFeature(calcTrace) {
226226
};
227227

228228
// Compute centroid, add it to the properties
229-
fOut.properties.ct = findCentroid(fOut);
229+
if (fOut.geometry.coordinates.length > 0) {
230+
fOut.properties.ct = findCentroid(fOut);
231+
} else {
232+
fOut.properties.ct = [NaN, NaN];
233+
}
230234

231235
// Mutate in in/out features into calcdata
232236
cdi.fIn = fIn;
6 Bytes
Loading
71 Bytes
Loading

test/jasmine/tests/choropleth_test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,7 @@ describe('Test choropleth hover:', function() {
353353
fig.data[0].hovertemplate = '%{properties.name}<extra>%{ct[0]:.1f} | %{ct[1]:.1f}</extra>';
354354
fig.layout.geo.projection = {scale: 20};
355355

356-
run(hasCssTransform, [300, 200], fig, ['New York', '-75.1 | 42.6'])
356+
run(hasCssTransform, [300, 200], fig, ['New York', '-75.2 | 42.6'])
357357
.then(done, done.fail);
358358
});
359359
});

test/jasmine/tests/choroplethmap_test.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -489,7 +489,7 @@ describe('Test choroplethmap convert:', function() {
489489
var opts = convertModule.convert(calcTrace);
490490

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

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

513513
expect(opts.geojson.features[0].geometry.coordinates).toBe(coordsIn);
514-
expect(calcTrace[0].ct).toEqual([28.75, 30]);
514+
expect(calcTrace[0].ct).toEqual([29, 31]);
515515
});
516516
});
517517

@@ -639,7 +639,7 @@ describe('Test choroplethmap hover:', function() {
639639
return fig;
640640
},
641641
nums: '### 100',
642-
name: '-86.7 | 32.0 ###',
642+
name: '-86.7 | 31.9 ###',
643643
evtPts: [{location: 100, z: 10, pointNumber: 0, curveNumber: 0}]
644644
}];
645645

test/jasmine/tests/choroplethmapbox_test.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -490,7 +490,7 @@ describe('Test choroplethmapbox convert:', function() {
490490
var opts = convertModule.convert(calcTrace);
491491

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

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

514514
expect(opts.geojson.features[0].geometry.coordinates).toBe(coordsIn);
515-
expect(calcTrace[0].ct).toEqual([28.75, 30]);
515+
expect(calcTrace[0].ct).toEqual([29, 31]);
516516
});
517517
});
518518

@@ -640,7 +640,7 @@ describe('Test choroplethmapbox hover:', function() {
640640
return fig;
641641
},
642642
nums: '### 100',
643-
name: '-86.7 | 32.0 ###',
643+
name: '-86.7 | 31.9 ###',
644644
evtPts: [{location: 100, z: 10, pointNumber: 0, curveNumber: 0}]
645645
}];
646646

0 commit comments

Comments
 (0)