Skip to content
2 changes: 2 additions & 0 deletions draftlogs/7115_change.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
- Update turf.js to v7 [[#7115](https://github.com/plotly/plotly.js/pull/7115)]

113 changes: 62 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": "^7.1.0",
"@turf/bbox": "^7.1.0",
"@turf/centroid": "^7.1.0",
"base64-arraybuffer": "^1.0.2",
"canvas-fit": "^1.5.0",
"color-alpha": "1.0.4",
Expand Down
18 changes: 11 additions & 7 deletions src/lib/geo_location_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

var d3 = require('@plotly/d3');
var countryRegex = require('country-regex');
var turfArea = require('@turf/area');
var turfCentroid = require('@turf/centroid');
var turfBbox = require('@turf/bbox');
var { area: turfArea } = require('@turf/area');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wondering how these imports are transpiled in to the dist?
May I ask you to test the dist from CI on the plotly.py side?

Copy link
Contributor Author

@birkskyum birkskyum Aug 19, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unless minified will they be kept as a destructuring assignment (plotly-geo.js bundle):

var {
  area: turfArea
} = __webpack_require__(9532);

Not sure how to run it in python

var { centroid: turfCentroid } = require('@turf/centroid');
var { bbox: turfBbox } = require('@turf/bbox');

var identity = require('./identity');
var loggers = require('./loggers');
Expand Down 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 Expand Up @@ -291,7 +295,7 @@ function findCentroid(feature) {

for(var i = 0; i < coords.length; i++) {
var polyi = {type: 'Polygon', coordinates: coords[i]};
var area = turfArea.default(polyi);
var area = turfArea(polyi);
if(area > maxArea) {
maxArea = area;
poly = polyi;
Expand All @@ -301,7 +305,7 @@ function findCentroid(feature) {
poly = geometry;
}

return turfCentroid.default(poly).geometry.coordinates;
return turfCentroid(poly).geometry.coordinates;
}

function fetchTraceGeoData(calcData) {
Expand Down Expand Up @@ -362,7 +366,7 @@ function fetchTraceGeoData(calcData) {
// TODO `turf/bbox` gives wrong result when the input feature/geometry
// crosses the anti-meridian. We should try to implement our own bbox logic.
function computeBbox(d) {
return turfBbox.default(d);
return turfBbox(d);
}

module.exports = {
Expand Down
4 changes: 2 additions & 2 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 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
4 changes: 2 additions & 2 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 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