@turf/polygon-smooth
Advanced tools
Comparing version 6.2.0-alpha.2 to 6.2.0-alpha.3
@@ -22,69 +22,85 @@ import { coordEach, geomEach } from '@turf/meta'; | ||
function polygonSmooth(inputPolys, options) { | ||
var outPolys = []; | ||
// Optional parameters | ||
var iterations = options.iterations || 1; | ||
if (!inputPolys) throw new Error('inputPolys is required'); | ||
var outPolys = []; | ||
// Optional parameters | ||
var iterations = options.iterations || 1; | ||
if (!inputPolys) throw new Error("inputPolys is required"); | ||
geomEach(inputPolys, function (geom, geomIndex, properties) { | ||
var outCoords; | ||
var poly; | ||
var tempOutput; | ||
geomEach(inputPolys, function (geom, geomIndex, properties) { | ||
var outCoords; | ||
var poly; | ||
var tempOutput; | ||
switch (geom.type) { | ||
case 'Polygon': | ||
outCoords = [[]]; | ||
for (var i = 0; i < iterations; i++) { | ||
tempOutput = [[]]; | ||
poly = geom; | ||
if (i > 0) poly = polygon(outCoords).geometry; | ||
processPolygon(poly, tempOutput); | ||
outCoords = tempOutput.slice(0); | ||
} | ||
outPolys.push(polygon(outCoords, properties)); | ||
break; | ||
case 'MultiPolygon': | ||
outCoords = [[[]]]; | ||
for (var y = 0; y < iterations; y++) { | ||
tempOutput = [[[]]]; | ||
poly = geom; | ||
if (y > 0) poly = multiPolygon(outCoords).geometry; | ||
processMultiPolygon(poly, tempOutput); | ||
outCoords = tempOutput.slice(0); | ||
} | ||
outPolys.push(multiPolygon(outCoords, properties)); | ||
break; | ||
default: | ||
throw new Error('geometry is invalid, must be Polygon or MultiPolygon'); | ||
switch (geom.type) { | ||
case "Polygon": | ||
outCoords = [[]]; | ||
for (var i = 0; i < iterations; i++) { | ||
tempOutput = [[]]; | ||
poly = geom; | ||
if (i > 0) poly = polygon(outCoords).geometry; | ||
processPolygon(poly, tempOutput); | ||
outCoords = tempOutput.slice(0); | ||
} | ||
}); | ||
return featureCollection(outPolys); | ||
outPolys.push(polygon(outCoords, properties)); | ||
break; | ||
case "MultiPolygon": | ||
outCoords = [[[]]]; | ||
for (var y = 0; y < iterations; y++) { | ||
tempOutput = [[[]]]; | ||
poly = geom; | ||
if (y > 0) poly = multiPolygon(outCoords).geometry; | ||
processMultiPolygon(poly, tempOutput); | ||
outCoords = tempOutput.slice(0); | ||
} | ||
outPolys.push(multiPolygon(outCoords, properties)); | ||
break; | ||
default: | ||
throw new Error("geometry is invalid, must be Polygon or MultiPolygon"); | ||
} | ||
}); | ||
return featureCollection(outPolys); | ||
} | ||
/** | ||
* @param {poly} poly to process | ||
* @param {poly} tempOutput to place the results in | ||
* @private | ||
* @param {poly} poly to process | ||
* @param {poly} tempOutput to place the results in | ||
* @private | ||
*/ | ||
function processPolygon(poly, tempOutput) { | ||
var prevGeomIndex = 0; | ||
var subtractCoordIndex = 0; | ||
var prevGeomIndex = 0; | ||
var subtractCoordIndex = 0; | ||
coordEach(poly, function (currentCoord, coordIndex, featureIndex, multiFeatureIndex, geometryIndex) { | ||
if (geometryIndex > prevGeomIndex) { | ||
prevGeomIndex = geometryIndex; | ||
subtractCoordIndex = coordIndex; | ||
tempOutput.push([]); | ||
} | ||
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]); | ||
}, true); | ||
tempOutput.forEach(function (ring) { | ||
ring.push(ring[0]); | ||
}); | ||
coordEach( | ||
poly, | ||
function ( | ||
currentCoord, | ||
coordIndex, | ||
featureIndex, | ||
multiFeatureIndex, | ||
geometryIndex | ||
) { | ||
if (geometryIndex > prevGeomIndex) { | ||
prevGeomIndex = geometryIndex; | ||
subtractCoordIndex = coordIndex; | ||
tempOutput.push([]); | ||
} | ||
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, | ||
]); | ||
}, | ||
true | ||
); | ||
tempOutput.forEach(function (ring) { | ||
ring.push(ring[0]); | ||
}); | ||
} | ||
@@ -98,34 +114,51 @@ | ||
function processMultiPolygon(poly, tempOutput) { | ||
var prevGeomIndex = 0; | ||
var subtractCoordIndex = 0; | ||
var prevMultiIndex = 0; | ||
var prevGeomIndex = 0; | ||
var subtractCoordIndex = 0; | ||
var prevMultiIndex = 0; | ||
coordEach(poly, function (currentCoord, coordIndex, featureIndex, multiFeatureIndex, geometryIndex) { | ||
if (multiFeatureIndex > prevMultiIndex) { | ||
prevMultiIndex = multiFeatureIndex; | ||
subtractCoordIndex = coordIndex; | ||
tempOutput.push([[]]); | ||
} | ||
if (geometryIndex > prevGeomIndex) { | ||
prevGeomIndex = geometryIndex; | ||
subtractCoordIndex = coordIndex; | ||
tempOutput[multiFeatureIndex].push([]); | ||
} | ||
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]); | ||
}, true); | ||
coordEach( | ||
poly, | ||
function ( | ||
currentCoord, | ||
coordIndex, | ||
featureIndex, | ||
multiFeatureIndex, | ||
geometryIndex | ||
) { | ||
if (multiFeatureIndex > prevMultiIndex) { | ||
prevMultiIndex = multiFeatureIndex; | ||
subtractCoordIndex = coordIndex; | ||
tempOutput.push([[]]); | ||
} | ||
if (geometryIndex > prevGeomIndex) { | ||
prevGeomIndex = geometryIndex; | ||
subtractCoordIndex = coordIndex; | ||
tempOutput[multiFeatureIndex].push([]); | ||
} | ||
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, | ||
]); | ||
}, | ||
true | ||
); | ||
tempOutput.forEach(function (poly) { | ||
poly.forEach(function (ring) { | ||
ring.push(ring[0]); | ||
}); | ||
tempOutput.forEach(function (poly) { | ||
poly.forEach(function (ring) { | ||
ring.push(ring[0]); | ||
}); | ||
}); | ||
} | ||
export default polygonSmooth; |
@@ -24,69 +24,85 @@ 'use strict'; | ||
function polygonSmooth(inputPolys, options) { | ||
var outPolys = []; | ||
// Optional parameters | ||
var iterations = options.iterations || 1; | ||
if (!inputPolys) throw new Error('inputPolys is required'); | ||
var outPolys = []; | ||
// Optional parameters | ||
var iterations = options.iterations || 1; | ||
if (!inputPolys) throw new Error("inputPolys is required"); | ||
meta.geomEach(inputPolys, function (geom, geomIndex, properties) { | ||
var outCoords; | ||
var poly; | ||
var tempOutput; | ||
meta.geomEach(inputPolys, function (geom, geomIndex, properties) { | ||
var outCoords; | ||
var poly; | ||
var tempOutput; | ||
switch (geom.type) { | ||
case 'Polygon': | ||
outCoords = [[]]; | ||
for (var i = 0; i < iterations; i++) { | ||
tempOutput = [[]]; | ||
poly = geom; | ||
if (i > 0) poly = helpers.polygon(outCoords).geometry; | ||
processPolygon(poly, tempOutput); | ||
outCoords = tempOutput.slice(0); | ||
} | ||
outPolys.push(helpers.polygon(outCoords, properties)); | ||
break; | ||
case 'MultiPolygon': | ||
outCoords = [[[]]]; | ||
for (var y = 0; y < iterations; y++) { | ||
tempOutput = [[[]]]; | ||
poly = geom; | ||
if (y > 0) poly = helpers.multiPolygon(outCoords).geometry; | ||
processMultiPolygon(poly, tempOutput); | ||
outCoords = tempOutput.slice(0); | ||
} | ||
outPolys.push(helpers.multiPolygon(outCoords, properties)); | ||
break; | ||
default: | ||
throw new Error('geometry is invalid, must be Polygon or MultiPolygon'); | ||
switch (geom.type) { | ||
case "Polygon": | ||
outCoords = [[]]; | ||
for (var i = 0; i < iterations; i++) { | ||
tempOutput = [[]]; | ||
poly = geom; | ||
if (i > 0) poly = helpers.polygon(outCoords).geometry; | ||
processPolygon(poly, tempOutput); | ||
outCoords = tempOutput.slice(0); | ||
} | ||
}); | ||
return helpers.featureCollection(outPolys); | ||
outPolys.push(helpers.polygon(outCoords, properties)); | ||
break; | ||
case "MultiPolygon": | ||
outCoords = [[[]]]; | ||
for (var y = 0; y < iterations; y++) { | ||
tempOutput = [[[]]]; | ||
poly = geom; | ||
if (y > 0) poly = helpers.multiPolygon(outCoords).geometry; | ||
processMultiPolygon(poly, tempOutput); | ||
outCoords = tempOutput.slice(0); | ||
} | ||
outPolys.push(helpers.multiPolygon(outCoords, properties)); | ||
break; | ||
default: | ||
throw new Error("geometry is invalid, must be Polygon or MultiPolygon"); | ||
} | ||
}); | ||
return helpers.featureCollection(outPolys); | ||
} | ||
/** | ||
* @param {poly} poly to process | ||
* @param {poly} tempOutput to place the results in | ||
* @private | ||
* @param {poly} poly to process | ||
* @param {poly} tempOutput to place the results in | ||
* @private | ||
*/ | ||
function processPolygon(poly, tempOutput) { | ||
var prevGeomIndex = 0; | ||
var subtractCoordIndex = 0; | ||
var prevGeomIndex = 0; | ||
var subtractCoordIndex = 0; | ||
meta.coordEach(poly, function (currentCoord, coordIndex, featureIndex, multiFeatureIndex, geometryIndex) { | ||
if (geometryIndex > prevGeomIndex) { | ||
prevGeomIndex = geometryIndex; | ||
subtractCoordIndex = coordIndex; | ||
tempOutput.push([]); | ||
} | ||
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]); | ||
}, true); | ||
tempOutput.forEach(function (ring) { | ||
ring.push(ring[0]); | ||
}); | ||
meta.coordEach( | ||
poly, | ||
function ( | ||
currentCoord, | ||
coordIndex, | ||
featureIndex, | ||
multiFeatureIndex, | ||
geometryIndex | ||
) { | ||
if (geometryIndex > prevGeomIndex) { | ||
prevGeomIndex = geometryIndex; | ||
subtractCoordIndex = coordIndex; | ||
tempOutput.push([]); | ||
} | ||
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, | ||
]); | ||
}, | ||
true | ||
); | ||
tempOutput.forEach(function (ring) { | ||
ring.push(ring[0]); | ||
}); | ||
} | ||
@@ -100,35 +116,51 @@ | ||
function processMultiPolygon(poly, tempOutput) { | ||
var prevGeomIndex = 0; | ||
var subtractCoordIndex = 0; | ||
var prevMultiIndex = 0; | ||
var prevGeomIndex = 0; | ||
var subtractCoordIndex = 0; | ||
var prevMultiIndex = 0; | ||
meta.coordEach(poly, function (currentCoord, coordIndex, featureIndex, multiFeatureIndex, geometryIndex) { | ||
if (multiFeatureIndex > prevMultiIndex) { | ||
prevMultiIndex = multiFeatureIndex; | ||
subtractCoordIndex = coordIndex; | ||
tempOutput.push([[]]); | ||
} | ||
if (geometryIndex > prevGeomIndex) { | ||
prevGeomIndex = geometryIndex; | ||
subtractCoordIndex = coordIndex; | ||
tempOutput[multiFeatureIndex].push([]); | ||
} | ||
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]); | ||
}, true); | ||
meta.coordEach( | ||
poly, | ||
function ( | ||
currentCoord, | ||
coordIndex, | ||
featureIndex, | ||
multiFeatureIndex, | ||
geometryIndex | ||
) { | ||
if (multiFeatureIndex > prevMultiIndex) { | ||
prevMultiIndex = multiFeatureIndex; | ||
subtractCoordIndex = coordIndex; | ||
tempOutput.push([[]]); | ||
} | ||
if (geometryIndex > prevGeomIndex) { | ||
prevGeomIndex = geometryIndex; | ||
subtractCoordIndex = coordIndex; | ||
tempOutput[multiFeatureIndex].push([]); | ||
} | ||
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, | ||
]); | ||
}, | ||
true | ||
); | ||
tempOutput.forEach(function (poly) { | ||
poly.forEach(function (ring) { | ||
ring.push(ring[0]); | ||
}); | ||
tempOutput.forEach(function (poly) { | ||
poly.forEach(function (ring) { | ||
ring.push(ring[0]); | ||
}); | ||
}); | ||
} | ||
module.exports = polygonSmooth; | ||
module.exports.default = polygonSmooth; |
@@ -1,2 +0,7 @@ | ||
import { Feature, FeatureCollection, Polygon, MultiPolygon } from '@turf/helpers' | ||
import { | ||
Feature, | ||
FeatureCollection, | ||
Polygon, | ||
MultiPolygon, | ||
} from "@turf/helpers"; | ||
@@ -7,6 +12,6 @@ /** | ||
export default function <T extends Polygon | MultiPolygon>( | ||
polygon: FeatureCollection<T> | Feature<T> | T, | ||
options?: { | ||
iterations?: number | ||
} | ||
polygon: FeatureCollection<T> | Feature<T> | T, | ||
options?: { | ||
iterations?: number; | ||
} | ||
): FeatureCollection<T>; |
{ | ||
"name": "@turf/polygon-smooth", | ||
"version": "6.2.0-alpha.2", | ||
"version": "6.2.0-alpha.3", | ||
"description": "turf polygon smooth module", | ||
@@ -28,2 +28,6 @@ "author": "Turf Authors", | ||
"module": "dist/es/index.js", | ||
"exports": { | ||
"import": "./dist/es/index.js", | ||
"require": "./dist/js/index.js" | ||
}, | ||
"types": "index.d.ts", | ||
@@ -36,10 +40,9 @@ "sideEffects": false, | ||
"scripts": { | ||
"bench": "npm-run-all prepare bench:run", | ||
"bench:run": "node bench.js", | ||
"bench": "node -r esm bench.js", | ||
"build": "rollup -c ../../rollup.config.js && echo '{\"type\":\"module\"}' > dist/es/package.json", | ||
"docs": "node ../../scripts/generate-readmes", | ||
"posttest": "node -r esm ../../scripts/validate-es5-dependencies.js", | ||
"prepare": "rollup -c ../../rollup.config.js", | ||
"test": "npm-run-all prepare test:*", | ||
"test": "npm-run-all test:*", | ||
"test:tape": "node -r esm test.js", | ||
"test:types": "tsc --noEmit types.ts" | ||
"test:types": "tsc --esModuleInterop --noEmit types.ts" | ||
}, | ||
@@ -56,6 +59,6 @@ "devDependencies": { | ||
"dependencies": { | ||
"@turf/helpers": "^6.2.0-alpha.2", | ||
"@turf/meta": "^6.2.0-alpha.2" | ||
"@turf/helpers": "^6.2.0-alpha.3", | ||
"@turf/meta": "^6.2.0-alpha.3" | ||
}, | ||
"gitHead": "23d5cb91d77e0c1e2e903a2252f525797f1d0d09" | ||
"gitHead": "dce9edfc705352e8cb9e0083c9330ba0e8d77409" | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
9
564
24477
Updated@turf/helpers@^6.2.0-alpha.3
Updated@turf/meta@^6.2.0-alpha.3