Skip to content

Commit

Permalink
polygon-smooth: fix crash with MultiPolygons (#2323) (#2324)
Browse files Browse the repository at this point in the history
* Fix polygon-smooth was crashing when dealing with MultiPolygons including more than one polygon with hole(s) (#2323)

* Move rollup.config in folder so the package can be used with https://gitpkg.vercel.app/

* Add the files required for using the forked package

* Make the fork usable

* Remove changes to rollup and and ts

Co-authored-by: Rowan Winsemius <[email protected]>
  • Loading branch information
Pitouli and rowanwins authored Oct 6, 2022
1 parent 81bb457 commit 9e20dd5
Show file tree
Hide file tree
Showing 3 changed files with 131 additions and 49 deletions.
92 changes: 43 additions & 49 deletions packages/turf-polygon-smooth/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ function polygonSmooth(inputPolys, options) {
case "Polygon":
outCoords = [[]];
for (var i = 0; i < iterations; i++) {
tempOutput = [[]];
tempOutput = [];
poly = geom;
if (i > 0) poly = polygon(outCoords).geometry;
processPolygon(poly, tempOutput);
Expand All @@ -45,7 +45,7 @@ function polygonSmooth(inputPolys, options) {
case "MultiPolygon":
outCoords = [[[]]];
for (var y = 0; y < iterations; y++) {
tempOutput = [[[]]];
tempOutput = [];
poly = geom;
if (y > 0) poly = multiPolygon(outCoords).geometry;
processMultiPolygon(poly, tempOutput);
Expand All @@ -66,8 +66,8 @@ function polygonSmooth(inputPolys, options) {
* @private
*/
function processPolygon(poly, tempOutput) {
var prevGeomIndex = 0;
var subtractCoordIndex = 0;
var previousCoord = null;
var previousGeometryIndex = null;

coordEach(
poly,
Expand All @@ -78,27 +78,26 @@ function processPolygon(poly, tempOutput) {
multiFeatureIndex,
geometryIndex
) {
if (geometryIndex > prevGeomIndex) {
prevGeomIndex = geometryIndex;
subtractCoordIndex = coordIndex;
if (previousGeometryIndex !== geometryIndex) {
tempOutput.push([]);
} else {
var p0x = previousCoord[0];
var p0y = previousCoord[1];
var p1x = currentCoord[0];
var p1y = currentCoord[1];
tempOutput[geometryIndex].push([
0.75 * p0x + 0.25 * p1x,
0.75 * p0y + 0.25 * p1y,
]);
tempOutput[geometryIndex].push([
0.25 * p0x + 0.75 * p1x,
0.25 * p0y + 0.75 * p1y,
]);
}
var realCoordIndex = coordIndex - subtractCoordIndex;
var p1 = poly.coordinates[geometryIndex][realCoordIndex + 1];
var p0x = currentCoord[0];
var p0y = currentCoord[1];
var p1x = p1[0];
var p1y = p1[1];
tempOutput[geometryIndex].push([
0.75 * p0x + 0.25 * p1x,
0.75 * p0y + 0.25 * p1y,
]);
tempOutput[geometryIndex].push([
0.25 * p0x + 0.75 * p1x,
0.25 * p0y + 0.75 * p1y,
]);
previousCoord = currentCoord;
previousGeometryIndex = geometryIndex;
},
true
false
);
tempOutput.forEach(function (ring) {
ring.push(ring[0]);
Expand All @@ -111,9 +110,9 @@ function processPolygon(poly, tempOutput) {
* @private
*/
function processMultiPolygon(poly, tempOutput) {
var prevGeomIndex = 0;
var subtractCoordIndex = 0;
var prevMultiIndex = 0;
var previousCoord = null;
var previousMultiFeatureIndex = null;
var previousGeometryIndex = null;

coordEach(
poly,
Expand All @@ -124,35 +123,30 @@ function processMultiPolygon(poly, tempOutput) {
multiFeatureIndex,
geometryIndex
) {
if (multiFeatureIndex > prevMultiIndex) {
prevMultiIndex = multiFeatureIndex;
subtractCoordIndex = coordIndex;
if (previousMultiFeatureIndex !== multiFeatureIndex) {
tempOutput.push([[]]);
}
if (geometryIndex > prevGeomIndex) {
prevGeomIndex = geometryIndex;
subtractCoordIndex = coordIndex;
} else if (previousGeometryIndex !== geometryIndex) {
tempOutput[multiFeatureIndex].push([]);
} else {
var p0x = previousCoord[0];
var p0y = previousCoord[1];
var p1x = currentCoord[0];
var p1y = currentCoord[1];
tempOutput[multiFeatureIndex][geometryIndex].push([
0.75 * p0x + 0.25 * p1x,
0.75 * p0y + 0.25 * p1y,
]);
tempOutput[multiFeatureIndex][geometryIndex].push([
0.25 * p0x + 0.75 * p1x,
0.25 * p0y + 0.75 * p1y,
]);
}
var realCoordIndex = coordIndex - subtractCoordIndex;
var p1 =
poly.coordinates[multiFeatureIndex][geometryIndex][realCoordIndex + 1];
var p0x = currentCoord[0];
var p0y = currentCoord[1];
var p1x = p1[0];
var p1y = p1[1];
tempOutput[multiFeatureIndex][geometryIndex].push([
0.75 * p0x + 0.25 * p1x,
0.75 * p0y + 0.25 * p1y,
]);
tempOutput[multiFeatureIndex][geometryIndex].push([
0.25 * p0x + 0.75 * p1x,
0.25 * p0y + 0.75 * p1y,
]);
previousCoord = currentCoord;
previousMultiFeatureIndex = multiFeatureIndex;
previousGeometryIndex = geometryIndex;
},
true
false
);

tempOutput.forEach(function (poly) {
poly.forEach(function (ring) {
ring.push(ring[0]);
Expand Down
16 changes: 16 additions & 0 deletions packages/turf-polygon-smooth/test/in/multipolygonWithHole.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,22 @@
[100.2, 0.8],
[100.2, 0.2]
]
],
[
[
[105.0, 0.0],
[106.0, 0.0],
[106.0, 1.0],
[105.0, 1.0],
[105.0, 0.0]
],
[
[105.2, 0.2],
[105.8, 0.2],
[105.8, 0.8],
[105.2, 0.8],
[105.2, 0.2]
]
]
]
}
72 changes: 72 additions & 0 deletions packages/turf-polygon-smooth/test/out/multipolygonWithHole.json
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,78 @@
[100.396875, 0.20937500000000003],
[100.46249999999999, 0.2]
]
],
[
[
[105.4375, 0],
[105.5625, 0],
[105.671875, 0.015625],
[105.765625, 0.046875],
[105.84375, 0.09375],
[105.90625, 0.15625],
[105.953125, 0.234375],
[105.984375, 0.328125],
[106, 0.4375],
[106, 0.5625],
[105.984375, 0.671875],
[105.953125, 0.765625],
[105.90625, 0.84375],
[105.84375, 0.90625],
[105.765625, 0.953125],
[105.671875, 0.984375],
[105.5625, 1],
[105.4375, 1],
[105.328125, 0.984375],
[105.234375, 0.953125],
[105.15625, 0.90625],
[105.09375, 0.84375],
[105.046875, 0.765625],
[105.015625, 0.671875],
[105, 0.5625],
[105, 0.4375],
[105.015625, 0.328125],
[105.046875, 0.234375],
[105.09375, 0.15625],
[105.15625, 0.09375],
[105.234375, 0.046875],
[105.328125, 0.015625],
[105.4375, 0]
],
[
[105.46249999999999, 0.2],
[105.53750000000001, 0.2],
[105.603125, 0.20937500000000003],
[105.659375, 0.22812500000000002],
[105.70625, 0.25625000000000003],
[105.74374999999999, 0.29375],
[105.771875, 0.340625],
[105.79062499999999, 0.39687500000000003],
[105.8, 0.4625],
[105.8, 0.5375000000000001],
[105.79062499999999, 0.603125],
[105.771875, 0.6593750000000002],
[105.74374999999999, 0.7062500000000002],
[105.70625, 0.7437500000000001],
[105.659375, 0.7718750000000001],
[105.603125, 0.7906250000000001],
[105.53750000000001, 0.8],
[105.46249999999999, 0.8],
[105.396875, 0.7906250000000001],
[105.340625, 0.7718750000000001],
[105.29375, 0.7437500000000001],
[105.25625000000001, 0.7062500000000002],
[105.228125, 0.6593750000000002],
[105.20937500000001, 0.603125],
[105.2, 0.5375000000000001],
[105.2, 0.4625],
[105.20937500000001, 0.39687500000000003],
[105.228125, 0.340625],
[105.25625000000001, 0.29375],
[105.29375, 0.25625000000000003],
[105.340625, 0.22812500000000002],
[105.396875, 0.20937500000000003],
[105.46249999999999, 0.2]
]
]
]
}
Expand Down

0 comments on commit 9e20dd5

Please sign in to comment.