diff --git a/draftlogs/7115_fix.md b/draftlogs/7115_fix.md new file mode 100644 index 00000000000..03020ae3516 --- /dev/null +++ b/draftlogs/7115_fix.md @@ -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! + \ No newline at end of file diff --git a/package-lock.json b/package-lock.json index d13796b002d..0c6e9dab90b 100644 --- a/package-lock.json +++ b/package-lock.json @@ -13,9 +13,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", @@ -2613,69 +2613,58 @@ } }, "node_modules/@turf/area": { - "version": "6.4.0", - "resolved": "https://registry.npmjs.org/@turf/area/-/area-6.4.0.tgz", - "integrity": "sha512-zJxRZX8UUCHlKuk0oRfYUnEMKw6+TiNDJHJ3R4q7/AC+yz/Wh3m+CkEoTryiXJ8+VoMiPFXrseleUHidfOOZzQ==", + "version": "6.5.0", + "resolved": "https://registry.npmjs.org/@turf/area/-/area-6.5.0.tgz", + "integrity": "sha512-xCZdiuojokLbQ+29qR6qoMD89hv+JAgWjLrwSEWL+3JV8IXKeNFl6XkEJz9HGkVpnXvQKJoRz4/liT+8ZZ5Jyg==", "dependencies": { - "@turf/helpers": "^6.4.0", - "@turf/meta": "^6.4.0" - } - }, - "node_modules/@turf/area/node_modules/@turf/helpers": { - "version": "6.4.0", - "resolved": "https://registry.npmjs.org/@turf/helpers/-/helpers-6.4.0.tgz", - "integrity": "sha512-7vVpWZwHP0Qn8DDSlM++nhs3/6zfPt+GODjvLVZ+sWIG4S3vOtUUOfO5eIjRzxsUHHqhgiIL0QA17u79uLM+mQ==" - }, - "node_modules/@turf/area/node_modules/@turf/meta": { - "version": "6.4.0", - "resolved": "https://registry.npmjs.org/@turf/meta/-/meta-6.4.0.tgz", - "integrity": "sha512-fMra6vMskwz1knn0/tb22ppOeE8CCmpvOvTIxLdV1WYWAoC4bJ4WdXKvZRsJKpHOX5iFehx4DT8aaGdROA4Y3Q==", - "dependencies": { - "@turf/helpers": "^6.4.0" + "@turf/helpers": "^6.5.0", + "@turf/meta": "^6.5.0" + }, + "funding": { + "url": "https://opencollective.com/turf" } }, "node_modules/@turf/bbox": { - "version": "6.4.0", - "resolved": "https://registry.npmjs.org/@turf/bbox/-/bbox-6.4.0.tgz", - "integrity": "sha512-eASq6Op3sGAA293Qo3gTwuQAvdoQCKah4rgA5FZkQ2xvkYquQnkUhV2U18bFgw58aSP1g2Y+rpmSEPHDFX6/6g==", - "dependencies": { - "@turf/helpers": "^6.4.0", - "@turf/meta": "^6.4.0" - } - }, - "node_modules/@turf/bbox/node_modules/@turf/helpers": { - "version": "6.4.0", - "resolved": "https://registry.npmjs.org/@turf/helpers/-/helpers-6.4.0.tgz", - "integrity": "sha512-7vVpWZwHP0Qn8DDSlM++nhs3/6zfPt+GODjvLVZ+sWIG4S3vOtUUOfO5eIjRzxsUHHqhgiIL0QA17u79uLM+mQ==" - }, - "node_modules/@turf/bbox/node_modules/@turf/meta": { - "version": "6.4.0", - "resolved": "https://registry.npmjs.org/@turf/meta/-/meta-6.4.0.tgz", - "integrity": "sha512-fMra6vMskwz1knn0/tb22ppOeE8CCmpvOvTIxLdV1WYWAoC4bJ4WdXKvZRsJKpHOX5iFehx4DT8aaGdROA4Y3Q==", + "version": "6.5.0", + "resolved": "https://registry.npmjs.org/@turf/bbox/-/bbox-6.5.0.tgz", + "integrity": "sha512-RBbLaao5hXTYyyg577iuMtDB8ehxMlUqHEJiMs8jT1GHkFhr6sYre3lmLsPeYEi/ZKj5TP5tt7fkzNdJ4GIVyw==", "dependencies": { - "@turf/helpers": "^6.4.0" + "@turf/helpers": "^6.5.0", + "@turf/meta": "^6.5.0" + }, + "funding": { + "url": "https://opencollective.com/turf" } }, "node_modules/@turf/centroid": { - "version": "6.0.2", - "resolved": "https://registry.npmjs.org/@turf/centroid/-/centroid-6.0.2.tgz", - "integrity": "sha512-auyDauOtC4eddH7GC3CHFTDu2PKhpSeKCRhwhHhXtJqn2dWCJQNIoCeJRmfXRIbzCWhWvgvQafvvhq8HNvmvWw==", + "version": "6.5.0", + "resolved": "https://registry.npmjs.org/@turf/centroid/-/centroid-6.5.0.tgz", + "integrity": "sha512-MwE1oq5E3isewPprEClbfU5pXljIK/GUOMbn22UM3IFPDJX0KeoyLNwghszkdmFp/qMGL/M13MMWvU+GNLXP/A==", "dependencies": { - "@turf/helpers": "6.x", - "@turf/meta": "6.x" + "@turf/helpers": "^6.5.0", + "@turf/meta": "^6.5.0" + }, + "funding": { + "url": "https://opencollective.com/turf" } }, "node_modules/@turf/helpers": { - "version": "6.1.4", - "resolved": "https://registry.npmjs.org/@turf/helpers/-/helpers-6.1.4.tgz", - "integrity": "sha512-vJvrdOZy1ngC7r3MDA7zIGSoIgyrkWcGnNIEaqn/APmw+bVLF2gAW7HIsdTxd12s5wQMqEpqIQrmrbRRZ0xC7g==" + "version": "6.5.0", + "resolved": "https://registry.npmjs.org/@turf/helpers/-/helpers-6.5.0.tgz", + "integrity": "sha512-VbI1dV5bLFzohYYdgqwikdMVpe7pJ9X3E+dlr425wa2/sMJqYDhTO++ec38/pcPvPE6oD9WEEeU3Xu3gza+VPw==", + "funding": { + "url": "https://opencollective.com/turf" + } }, "node_modules/@turf/meta": { - "version": "6.0.2", - "resolved": "https://registry.npmjs.org/@turf/meta/-/meta-6.0.2.tgz", - "integrity": "sha512-VA7HJkx7qF1l3+GNGkDVn2oXy4+QoLP6LktXAaZKjuT1JI0YESat7quUkbCMy4zP9lAUuvS4YMslLyTtr919FA==", + "version": "6.5.0", + "resolved": "https://registry.npmjs.org/@turf/meta/-/meta-6.5.0.tgz", + "integrity": "sha512-RrArvtsV0vdsCBegoBtOalgdSOfkBrTJ07VkpiCnq/491W67hnMWmDu7e6Ztw0C3WldRYTXkg3SumfdzZxLBHA==", "dependencies": { - "@turf/helpers": "6.x" + "@turf/helpers": "^6.5.0" + }, + "funding": { + "url": "https://opencollective.com/turf" } }, "node_modules/@types/bluebird": { diff --git a/package.json b/package.json index f2dfb29212e..5a49e94ea79 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/lib/geo_location_utils.js b/src/lib/geo_location_utils.js index 04771902143..a691e5c3eee 100644 --- a/src/lib/geo_location_utils.js +++ b/src/lib/geo_location_utils.js @@ -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; diff --git a/test/image/baselines/geo_featureidkey.png b/test/image/baselines/geo_featureidkey.png index 761c1ccc24d..ab0735a6b76 100644 Binary files a/test/image/baselines/geo_featureidkey.png and b/test/image/baselines/geo_featureidkey.png differ diff --git a/test/image/baselines/geo_fitbounds-geojson.png b/test/image/baselines/geo_fitbounds-geojson.png index 6578bfc7e5d..f75fc2ea1b0 100644 Binary files a/test/image/baselines/geo_fitbounds-geojson.png and b/test/image/baselines/geo_fitbounds-geojson.png differ diff --git a/test/jasmine/tests/choropleth_test.js b/test/jasmine/tests/choropleth_test.js index 5a813a81ec4..d26468b8fcb 100644 --- a/test/jasmine/tests/choropleth_test.js +++ b/test/jasmine/tests/choropleth_test.js @@ -353,7 +353,7 @@ describe('Test choropleth hover:', function() { fig.data[0].hovertemplate = '%{properties.name}%{ct[0]:.1f} | %{ct[1]:.1f}'; 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); }); }); diff --git a/test/jasmine/tests/choroplethmap_test.js b/test/jasmine/tests/choroplethmap_test.js index acb55cb3a22..5e587505f67 100644 --- a/test/jasmine/tests/choroplethmap_test.js +++ b/test/jasmine/tests/choroplethmap_test.js @@ -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() { @@ -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]] @@ -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]); }); }); @@ -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}] }]; diff --git a/test/jasmine/tests/choroplethmapbox_test.js b/test/jasmine/tests/choroplethmapbox_test.js index df061c8fcd0..c9133894265 100644 --- a/test/jasmine/tests/choroplethmapbox_test.js +++ b/test/jasmine/tests/choroplethmapbox_test.js @@ -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() { @@ -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]] @@ -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]); }); }); @@ -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}] }];