diff --git a/packages/turf-unkink-polygon/index.js b/packages/turf-unkink-polygon/index.js index 8a2c5e5cfc..5d67fd083d 100644 --- a/packages/turf-unkink-polygon/index.js +++ b/packages/turf-unkink-polygon/index.js @@ -1,7 +1,10 @@ +var meta = require('@turf/meta'); +var helpers = require('@turf/helpers'); var simplepolygon = require('simplepolygon'); -var flatten = require('@turf/flatten'); -var featureEach = require('@turf/meta').featureEach; -var featureCollection = require('@turf/helpers').featureCollection; +var flattenEach = meta.flattenEach; +var featureEach = meta.featureEach; +var polygon = helpers.polygon; +var featureCollection = helpers.featureCollection; /** * Takes a kinked polygon and returns a feature collection of polygons that have no kinks. @@ -19,23 +22,12 @@ var featureCollection = require('@turf/helpers').featureCollection; * var addToMap = [poly, result] */ module.exports = function (geojson) { - var results = featureCollection([]); - - // Handles FeatureCollection & Feature - featureEach(geojson, function (feature) { - - // Handle MultiPolygons as Feature or FeatureCollection - if (feature.geometry.type === 'MultiPolygon') { feature = flatten(feature); } - - // Store simple polygons in results - featureEach(feature, function (polygon) { - var simple = simplepolygon(polygon); - - featureEach(simple, function (poly) { - poly.properties = (polygon.properties) ? polygon.properties : {}; - results.features.push(poly); - }); + var features = []; + flattenEach(geojson, function (feature) { + if (feature.geometry.type !== 'Polygon') return; + featureEach(simplepolygon(feature), function (poly) { + features.push(polygon(poly.geometry.coordinates, feature.properties)); }); }); - return results; + return featureCollection(features); }; diff --git a/packages/turf-unkink-polygon/package.json b/packages/turf-unkink-polygon/package.json index 1f585abbaa..a9fe825c54 100644 --- a/packages/turf-unkink-polygon/package.json +++ b/packages/turf-unkink-polygon/package.json @@ -36,9 +36,8 @@ "write-json-file": "^2.0.0" }, "dependencies": { - "@turf/flatten": "^4.6.0", "@turf/helpers": "^4.6.0", "@turf/meta": "^4.6.0", - "simplepolygon": "1.2.0" + "simplepolygon": "1.2.1" } } diff --git a/packages/turf-unkink-polygon/test.js b/packages/turf-unkink-polygon/test.js index 9615e742ec..f5b87dcd0f 100644 --- a/packages/turf-unkink-polygon/test.js +++ b/packages/turf-unkink-polygon/test.js @@ -20,7 +20,7 @@ test('unkink-polygon', t => { for (const {filename, geojson} of fixtures) { const unkinked = colorize(unkink(geojson)); - if (process.env.REGEN) { write.sync(directories.out + filename, unkinked); } + if (process.env.REGEN) write.sync(directories.out + filename, unkinked); const expected = load.sync(directories.out + filename); t.deepEquals(unkinked, expected, path.parse(filename).name); @@ -39,16 +39,16 @@ test('unkink-polygon -- throws', t => { t.end(); }); -function colorize(features, colors = ['#F00', '#00F'], width = 6) { +function colorize(features, colors = ['#F00', '#00F', '#0F0', '#F0F', '#FFF'], width = 6) { const results = []; featureEach(features, (feature, index) => { const color = colors[index % colors.length]; - feature.properties = { + feature.properties = Object.assign({ stroke: color, fill: color, 'stroke-width': width, - 'fill-opacity': 0.1 - }; + 'fill-opacity': 0.5 + }, feature.properties); results.push(feature); }); return featureCollection(results); diff --git a/packages/turf-unkink-polygon/test/in/complex.geojson b/packages/turf-unkink-polygon/test/in/complex.geojson index 03a765866a..bb9580c484 100644 --- a/packages/turf-unkink-polygon/test/in/complex.geojson +++ b/packages/turf-unkink-polygon/test/in/complex.geojson @@ -1,6 +1,8 @@ { "type": "Feature", - "properties": {}, + "properties": { + "foo": "bar" + }, "geometry": { "type": "Polygon", "coordinates": [ diff --git a/packages/turf-unkink-polygon/test/out/complex.geojson b/packages/turf-unkink-polygon/test/out/complex.geojson index d62d57e145..55a0b91ee7 100644 --- a/packages/turf-unkink-polygon/test/out/complex.geojson +++ b/packages/turf-unkink-polygon/test/out/complex.geojson @@ -7,7 +7,8 @@ "stroke": "#F00", "fill": "#F00", "stroke-width": 6, - "fill-opacity": 0.1 + "fill-opacity": 0.5, + "foo": "bar" }, "geometry": { "type": "Polygon", @@ -43,7 +44,8 @@ "stroke": "#00F", "fill": "#00F", "stroke-width": 6, - "fill-opacity": 0.1 + "fill-opacity": 0.5, + "foo": "bar" }, "geometry": { "type": "Polygon", @@ -148,10 +150,11 @@ { "type": "Feature", "properties": { - "stroke": "#F00", - "fill": "#F00", + "stroke": "#0F0", + "fill": "#0F0", "stroke-width": 6, - "fill-opacity": 0.1 + "fill-opacity": 0.5, + "foo": "bar" }, "geometry": { "type": "Polygon", @@ -228,10 +231,11 @@ { "type": "Feature", "properties": { - "stroke": "#00F", - "fill": "#00F", + "stroke": "#F0F", + "fill": "#F0F", "stroke-width": 6, - "fill-opacity": 0.1 + "fill-opacity": 0.5, + "foo": "bar" }, "geometry": { "type": "Polygon", diff --git a/packages/turf-unkink-polygon/test/out/hourglass.geojson b/packages/turf-unkink-polygon/test/out/hourglass.geojson index e99ef88434..1ba0aa5e7d 100644 --- a/packages/turf-unkink-polygon/test/out/hourglass.geojson +++ b/packages/turf-unkink-polygon/test/out/hourglass.geojson @@ -4,10 +4,10 @@ { "type": "Feature", "properties": { - "stroke": "#F00", + "stroke": "#ff0000", "fill": "#F00", "stroke-width": 6, - "fill-opacity": 0.1 + "fill-opacity": 0.5 }, "geometry": { "type": "Polygon", @@ -36,10 +36,10 @@ { "type": "Feature", "properties": { - "stroke": "#00F", + "stroke": "#ff0000", "fill": "#00F", "stroke-width": 6, - "fill-opacity": 0.1 + "fill-opacity": 0.5 }, "geometry": { "type": "Polygon", diff --git a/packages/turf-unkink-polygon/test/out/hourglassFeatureCollection.geojson b/packages/turf-unkink-polygon/test/out/hourglassFeatureCollection.geojson index feed0d3fea..c12807f16a 100644 --- a/packages/turf-unkink-polygon/test/out/hourglassFeatureCollection.geojson +++ b/packages/turf-unkink-polygon/test/out/hourglassFeatureCollection.geojson @@ -4,10 +4,10 @@ { "type": "Feature", "properties": { - "stroke": "#F00", + "stroke": "#ff0000", "fill": "#F00", "stroke-width": 6, - "fill-opacity": 0.1 + "fill-opacity": 0.5 }, "geometry": { "type": "Polygon", @@ -36,10 +36,10 @@ { "type": "Feature", "properties": { - "stroke": "#00F", + "stroke": "#ff0000", "fill": "#00F", "stroke-width": 6, - "fill-opacity": 0.1 + "fill-opacity": 0.5 }, "geometry": { "type": "Polygon", @@ -68,10 +68,10 @@ { "type": "Feature", "properties": { - "stroke": "#F00", - "fill": "#F00", + "stroke": "#0000ff", + "fill": "#0F0", "stroke-width": 6, - "fill-opacity": 0.1 + "fill-opacity": 0.5 }, "geometry": { "type": "Polygon", @@ -100,10 +100,10 @@ { "type": "Feature", "properties": { - "stroke": "#00F", - "fill": "#00F", + "stroke": "#0000ff", + "fill": "#F0F", "stroke-width": 6, - "fill-opacity": 0.1 + "fill-opacity": 0.5 }, "geometry": { "type": "Polygon", diff --git a/packages/turf-unkink-polygon/test/out/hourglassFeatureCollectionMultiPolygon.geojson b/packages/turf-unkink-polygon/test/out/hourglassFeatureCollectionMultiPolygon.geojson index ab5cd164ae..6c92313506 100644 --- a/packages/turf-unkink-polygon/test/out/hourglassFeatureCollectionMultiPolygon.geojson +++ b/packages/turf-unkink-polygon/test/out/hourglassFeatureCollectionMultiPolygon.geojson @@ -4,10 +4,10 @@ { "type": "Feature", "properties": { - "stroke": "#F00", + "stroke": "#ff0000", "fill": "#F00", "stroke-width": 6, - "fill-opacity": 0.1 + "fill-opacity": 0.5 }, "geometry": { "type": "Polygon", @@ -36,10 +36,10 @@ { "type": "Feature", "properties": { - "stroke": "#00F", + "stroke": "#ff0000", "fill": "#00F", "stroke-width": 6, - "fill-opacity": 0.1 + "fill-opacity": 0.5 }, "geometry": { "type": "Polygon", @@ -68,10 +68,10 @@ { "type": "Feature", "properties": { - "stroke": "#F00", - "fill": "#F00", + "stroke": "#ff0000", + "fill": "#0F0", "stroke-width": 6, - "fill-opacity": 0.1 + "fill-opacity": 0.5 }, "geometry": { "type": "Polygon", @@ -100,10 +100,10 @@ { "type": "Feature", "properties": { - "stroke": "#00F", - "fill": "#00F", + "stroke": "#ff0000", + "fill": "#F0F", "stroke-width": 6, - "fill-opacity": 0.1 + "fill-opacity": 0.5 }, "geometry": { "type": "Polygon", @@ -132,10 +132,10 @@ { "type": "Feature", "properties": { - "stroke": "#F00", - "fill": "#F00", + "stroke": "#0000ff", + "fill": "#FFF", "stroke-width": 6, - "fill-opacity": 0.1 + "fill-opacity": 0.5 }, "geometry": { "type": "Polygon", @@ -164,10 +164,10 @@ { "type": "Feature", "properties": { - "stroke": "#00F", - "fill": "#00F", + "stroke": "#0000ff", + "fill": "#F00", "stroke-width": 6, - "fill-opacity": 0.1 + "fill-opacity": 0.5 }, "geometry": { "type": "Polygon", diff --git a/packages/turf-unkink-polygon/test/out/hourglassMultiPolygon.geojson b/packages/turf-unkink-polygon/test/out/hourglassMultiPolygon.geojson index feed0d3fea..cae2e351f7 100644 --- a/packages/turf-unkink-polygon/test/out/hourglassMultiPolygon.geojson +++ b/packages/turf-unkink-polygon/test/out/hourglassMultiPolygon.geojson @@ -4,10 +4,10 @@ { "type": "Feature", "properties": { - "stroke": "#F00", + "stroke": "#ff0000", "fill": "#F00", "stroke-width": 6, - "fill-opacity": 0.1 + "fill-opacity": 0.5 }, "geometry": { "type": "Polygon", @@ -36,10 +36,10 @@ { "type": "Feature", "properties": { - "stroke": "#00F", + "stroke": "#ff0000", "fill": "#00F", "stroke-width": 6, - "fill-opacity": 0.1 + "fill-opacity": 0.5 }, "geometry": { "type": "Polygon", @@ -68,10 +68,10 @@ { "type": "Feature", "properties": { - "stroke": "#F00", - "fill": "#F00", + "stroke": "#ff0000", + "fill": "#0F0", "stroke-width": 6, - "fill-opacity": 0.1 + "fill-opacity": 0.5 }, "geometry": { "type": "Polygon", @@ -100,10 +100,10 @@ { "type": "Feature", "properties": { - "stroke": "#00F", - "fill": "#00F", + "stroke": "#ff0000", + "fill": "#F0F", "stroke-width": 6, - "fill-opacity": 0.1 + "fill-opacity": 0.5 }, "geometry": { "type": "Polygon", diff --git a/packages/turf-unkink-polygon/test/out/polygon-with-holes.geojson b/packages/turf-unkink-polygon/test/out/polygon-with-holes.geojson index 30852349eb..3bb49b3478 100644 --- a/packages/turf-unkink-polygon/test/out/polygon-with-holes.geojson +++ b/packages/turf-unkink-polygon/test/out/polygon-with-holes.geojson @@ -7,7 +7,7 @@ "stroke": "#F00", "fill": "#F00", "stroke-width": 6, - "fill-opacity": 0.1 + "fill-opacity": 0.5 }, "geometry": { "type": "Polygon", @@ -43,7 +43,7 @@ "stroke": "#00F", "fill": "#00F", "stroke-width": 6, - "fill-opacity": 0.1 + "fill-opacity": 0.5 }, "geometry": { "type": "Polygon", @@ -76,10 +76,10 @@ { "type": "Feature", "properties": { - "stroke": "#F00", - "fill": "#F00", + "stroke": "#0F0", + "fill": "#0F0", "stroke-width": 6, - "fill-opacity": 0.1 + "fill-opacity": 0.5 }, "geometry": { "type": "Polygon", @@ -112,10 +112,10 @@ { "type": "Feature", "properties": { - "stroke": "#00F", - "fill": "#00F", + "stroke": "#F0F", + "fill": "#F0F", "stroke-width": 6, - "fill-opacity": 0.1 + "fill-opacity": 0.5 }, "geometry": { "type": "Polygon", @@ -148,10 +148,10 @@ { "type": "Feature", "properties": { - "stroke": "#F00", - "fill": "#F00", + "stroke": "#FFF", + "fill": "#FFF", "stroke-width": 6, - "fill-opacity": 0.1 + "fill-opacity": 0.5 }, "geometry": { "type": "Polygon", diff --git a/packages/turf-unkink-polygon/test/out/polygon.geojson b/packages/turf-unkink-polygon/test/out/polygon.geojson index f19e014ff0..a09393dfb2 100644 --- a/packages/turf-unkink-polygon/test/out/polygon.geojson +++ b/packages/turf-unkink-polygon/test/out/polygon.geojson @@ -4,10 +4,10 @@ { "type": "Feature", "properties": { - "stroke": "#F00", + "stroke": "#ff0000", "fill": "#F00", "stroke-width": 6, - "fill-opacity": 0.1 + "fill-opacity": 0.5 }, "geometry": { "type": "Polygon", diff --git a/packages/turf-unkink-polygon/yarn.lock b/packages/turf-unkink-polygon/yarn.lock index 0344172e87..62ec76f103 100644 --- a/packages/turf-unkink-polygon/yarn.lock +++ b/packages/turf-unkink-polygon/yarn.lock @@ -19,6 +19,10 @@ version "3.13.0" resolved "https://registry.yarnpkg.com/@turf/helpers/-/helpers-3.13.0.tgz#d06078a1464cf56cdb7ea624ea1e13a71b88b806" +"@turf/helpers@^4.6.0": + version "4.6.0" + resolved "https://registry.yarnpkg.com/@turf/helpers/-/helpers-4.6.0.tgz#12398733b8ae28420df6166fa44c7ee8ffd6414c" + "@turf/inside@^3.14.0": version "3.14.0" resolved "https://registry.yarnpkg.com/@turf/inside/-/inside-3.14.0.tgz#d6b6af55882cbdb8f9a558dca98689c67bd3c590" @@ -43,6 +47,10 @@ version "3.14.0" resolved "https://registry.yarnpkg.com/@turf/meta/-/meta-3.14.0.tgz#8d3050c1a0f44bf406a633b6bd28c510f7bcee27" +"@turf/meta@^4.6.0": + version "4.6.0" + resolved "https://registry.yarnpkg.com/@turf/meta/-/meta-4.6.0.tgz#0d3f9a218e58d1c5e5deedf467c3321dd61203f3" + "@turf/within@^3.13.0": version "3.14.0" resolved "https://registry.yarnpkg.com/@turf/within/-/within-3.14.0.tgz#891a578323c292b9792269032dd74870e0e14c53" @@ -138,7 +146,7 @@ function-bind@^1.0.2, function-bind@^1.1.0, function-bind@~1.1.0: version "1.1.0" resolved "https://registry.yarnpkg.com/function-bind/-/function-bind-1.1.0.tgz#16176714c801798e4e8f2cf7f7529467bb4a5771" -geojson-polygon-self-intersections@^1.1.1: +geojson-polygon-self-intersections@^1.1.2: version "1.1.2" resolved "https://registry.yarnpkg.com/geojson-polygon-self-intersections/-/geojson-polygon-self-intersections-1.1.2.tgz#bc49ce3be2175005802ad6f3c231dbdd7c65777b" dependencies: @@ -301,16 +309,16 @@ resumer@~0.0.0: dependencies: through "~2.3.4" -simplepolygon@1.2.0: - version "1.2.0" - resolved "https://registry.yarnpkg.com/simplepolygon/-/simplepolygon-1.2.0.tgz#46260a48bbe467e2a7a10bcb65ecd0e3ddbb2eb9" +simplepolygon@1.2.1: + version "1.2.1" + resolved "https://registry.yarnpkg.com/simplepolygon/-/simplepolygon-1.2.1.tgz#48250a6a853275e96e42c7105f507da00dc67d64" dependencies: "@turf/area" "^3.13.0" "@turf/helpers" "^3.13.0" "@turf/inside" "^4.5.2" "@turf/within" "^3.13.0" debug "^2.6.3" - geojson-polygon-self-intersections "^1.1.1" + geojson-polygon-self-intersections "^1.1.2" rbush "^2.0.1" slide@^1.1.5: