diff --git a/packages/turf-mask/index.d.ts b/packages/turf-mask/index.d.ts index 2ae6d3a5ce..ea89a7f8e2 100644 --- a/packages/turf-mask/index.d.ts +++ b/packages/turf-mask/index.d.ts @@ -1,10 +1,13 @@ /// -type Polygon = GeoJSON.Feature | GeoJSON.Polygon; +type Geom = GeoJSON.Polygon | GeoJSON.MultiPolygon; +type Poly = GeoJSON.FeatureCollection | GeoJSON.Feature | Geom; +type Polygon = GeoJSON.Feature; +type Mask = GeoJSON.Feature | GeoJSON.Polygon; /** * http://turfjs.org/docs/#mask */ -declare function mask(poly: Polygon, mask?: Polygon): Polygon; +declare function mask(poly: Poly, mask?: Mask): Polygon; declare namespace mask {} export = mask; diff --git a/packages/turf-mask/index.js b/packages/turf-mask/index.js index 931efab672..1af1a289df 100644 --- a/packages/turf-mask/index.js +++ b/packages/turf-mask/index.js @@ -2,7 +2,7 @@ var rbush = require('rbush'); var union = require('@turf/union'); var helpers = require('@turf/helpers'); var turfBBox = require('@turf/bbox'); -var featureEach = require('@turf/meta').featureEach; +var flattenEach = require('@turf/meta').flattenEach; /** * Takes any type of {@link Polygon|polygon} and an optional mask and returns a {@link Polygon|polygon} exterior ring with holes. @@ -51,11 +51,11 @@ function buildMask(maskPolygon, polygonOuters, polygonInners) { var coordinates = []; coordinates.push(maskPolygon.geometry.coordinates[0]); - featureEach(polygonOuters, function (feature) { + flattenEach(polygonOuters, function (feature) { coordinates.push(feature.geometry.coordinates[0]); }); - featureEach(polygonInners, function (feature) { + flattenEach(polygonInners, function (feature) { coordinates.push(feature.geometry.coordinates[0]); }); return helpers.polygon(coordinates); @@ -71,38 +71,18 @@ function buildMask(maskPolygon, polygonOuters, polygonInners) { function separatePolygons(polygon) { var outers = []; var inners = []; - featureEach(polygon, function (multiFeature) { - if (multiFeature.geometry.type === 'MultiPolygon') { - multiFeature = flattenMultiPolygon(multiFeature); - } - featureEach(multiFeature, function (feature) { - var coordinates = feature.geometry.coordinates; - var featureOuter = coordinates[0]; - var featureInner = coordinates.slice(1); - outers.push(helpers.polygon([featureOuter])); - featureInner.forEach(function (inner) { - inners.push(helpers.polygon([inner])); - }); + flattenEach(polygon, function (feature) { + var coordinates = feature.geometry.coordinates; + var featureOuter = coordinates[0]; + var featureInner = coordinates.slice(1); + outers.push(helpers.polygon([featureOuter])); + featureInner.forEach(function (inner) { + inners.push(helpers.polygon([inner])); }); }); return [helpers.featureCollection(outers), helpers.featureCollection(inners)]; } -/** - * Flatten MultiPolygon - * - * @private - * @param {Feature} multiPolygon GeoJSON Feature - * @returns {FeatureCollection} Feature Collection - */ -function flattenMultiPolygon(multiPolygon) { - var polygons = []; - multiPolygon.geometry.coordinates.forEach(function (coordinates) { - polygons.push(helpers.polygon(coordinates)); - }); - return helpers.featureCollection(polygons); -} - /** * Create Mask Coordinates * @@ -130,7 +110,7 @@ function unionPolygons(polygons) { var results = []; var removed = {}; - featureEach(polygons, function (currentFeature, currentIndex) { + flattenEach(polygons, function (currentFeature, currentIndex) { // Exclude any removed features if (removed[currentIndex]) return true; @@ -187,7 +167,7 @@ function filterByIndex(a, b) { function createIndex(features) { var tree = rbush(); var load = []; - featureEach(features, function (feature, index) { + flattenEach(features, function (feature, index) { var bbox = turfBBox(feature); load.push({ minX: bbox[0], diff --git a/packages/turf-mask/test/in/multi-polygon.geojson b/packages/turf-mask/test/in/multi-polygon.geojson index f0ae5c948d..7d64e27055 100644 --- a/packages/turf-mask/test/in/multi-polygon.geojson +++ b/packages/turf-mask/test/in/multi-polygon.geojson @@ -2,61 +2,509 @@ "type": "FeatureCollection", "features": [ { - "type": "Feature", - "properties": {}, - "geometry": { - "type": "MultiPolygon", - "coordinates": [ + "type": "Feature", + "geometry": { + "type": "MultiPolygon", + "coordinates": [ + [ [ [ - [ - 111.796875, - -38.13455657705412 - ], - [ - 129.0234375, - -38.13455657705412 - ], - [ - 129.0234375, - -20.13847031245114 - ], - [ - 111.796875, - -20.13847031245114 - ], - [ - 111.796875, - -38.13455657705412 - ] + 174.8257084, + -36.8869965 + ], + [ + 174.8276109, + -36.887539 + ], + [ + 174.8278042, + -36.8875561 + ], + [ + 174.8296591, + -36.8874264 + ], + [ + 174.8311438, + -36.8878347 + ], + [ + 174.8314673, + -36.8878689 + ], + [ + 174.8336846, + -36.8865436 + ], + [ + 174.8341202, + -36.8862468 + ], + [ + 174.834388, + -36.885941 + ], + [ + 174.8354962, + -36.8846273 + ], + [ + 174.8356801, + -36.8844927 + ], + [ + 174.8367548, + -36.8837641 + ], + [ + 174.8373321, + -36.8831729 + ], + [ + 174.8373394, + -36.8830989 + ], + [ + 174.8369142, + -36.88283 + ], + [ + 174.8367485, + -36.8829628 + ], + [ + 174.836428, + -36.8830636 + ], + [ + 174.8340081, + -36.8834433 + ], + [ + 174.8332402, + -36.8837406 + ], + [ + 174.8321967, + -36.8848087 + ], + [ + 174.8315144, + -36.8849051 + ], + [ + 174.8311496, + -36.8842651 + ], + [ + 174.8319151, + -36.8828293 + ], + [ + 174.8321192, + -36.8820636 + ], + [ + 174.8324179, + -36.8806023 + ], + [ + 174.8325982, + -36.8803963 + ], + [ + 174.8346312, + -36.8804379 + ], + [ + 174.8346495, + -36.8804375 + ], + [ + 174.8348716, + -36.8800115 + ], + [ + 174.8349119, + -36.8798696 + ], + [ + 174.8347595, + -36.8798408 + ], + [ + 174.8342416, + -36.8795988 + ], + [ + 174.8328699, + -36.878784 + ], + [ + 174.8331132, + -36.8782542 + ], + [ + 174.8333146, + -36.878309 + ], + [ + 174.8337539, + -36.8777781 + ], + [ + 174.8343234, + -36.8772656 + ], + [ + 174.8347697, + -36.8769085 + ], + [ + 174.8349499, + -36.8768261 + ], + [ + 174.8351645, + -36.8767575 + ], + [ + 174.8353877, + -36.8766751 + ], + [ + 174.8355399, + -36.8764449 + ], + [ + 174.8357051, + -36.8761931 + ], + [ + 174.8357668, + -36.876134 + ], + [ + 174.8356795, + -36.8760915 + ], + [ + 174.8353743, + -36.8758963 + ], + [ + 174.8352751, + -36.8760453 + ], + [ + 174.8347802, + -36.8758729 + ], + [ + 174.8346298, + -36.8755945 + ], + [ + 174.8350415, + -36.8755096 + ], + [ + 174.8350096, + -36.8753835 + ], + [ + 174.8350022, + -36.8752449 + ], + [ + 174.8350149, + -36.875154 + ], + [ + 174.8345392, + -36.8752539 + ], + [ + 174.8345787, + -36.8753974 + ], + [ + 174.8343983, + -36.8756894 + ], + [ + 174.833726, + -36.8754489 + ], + [ + 174.8331871, + -36.8758692 + ], + [ + 174.8329038, + -36.8761774 + ], + [ + 174.8325172, + -36.8766213 + ], + [ + 174.8322845, + -36.8767656 + ], + [ + 174.8322194, + -36.876702 + ], + [ + 174.8320384, + -36.8768042 + ], + [ + 174.8319464, + -36.8770175 + ], + [ + 174.8315081, + -36.8771832 + ], + [ + 174.8313365, + -36.8770253 + ], + [ + 174.8312077, + -36.8771008 + ], + [ + 174.8312421, + -36.8772587 + ], + [ + 174.8309073, + -36.8774235 + ], + [ + 174.8305018, + -36.8776747 + ], + [ + 174.8302808, + -36.8777874 + ], + [ + 174.8291478, + -36.8782886 + ], + [ + 174.8291478, + -36.8783915 + ], + [ + 174.8291735, + -36.8785151 + ], + [ + 174.8290963, + -36.8787142 + ], + [ + 174.829062, + -36.8788584 + ], + [ + 174.8293809, + -36.8815138 + ], + [ + 174.8294238, + -36.8818914 + ], + [ + 174.8291049, + -36.8820989 + ], + [ + 174.8290019, + -36.8821676 + ], + [ + 174.829225, + -36.8827923 + ], + [ + 174.829535, + -36.8828012 + ], + [ + 174.8295855, + -36.8828026 + ], + [ + 174.8294482, + -36.8835612 + ], + [ + 174.8290133, + -36.8842478 + ], + [ + 174.828547, + -36.8850853 + ], + [ + 174.8285298, + -36.8854835 + ], + [ + 174.8285583, + -36.8859898 + ], + [ + 174.827812, + -36.8862177 + ], + [ + 174.8272938, + -36.8863759 + ], + [ + 174.8268918, + -36.8864866 + ], + [ + 174.8265485, + -36.8865621 + ], + [ + 174.8263768, + -36.8866582 + ], + [ + 174.8261688, + -36.886697 + ], + [ + 174.8261552, + -36.8866995 + ], + [ + 174.8261193, + -36.8867062 + ], + [ + 174.8259044, + -36.8868107 + ], + [ + 174.8257084, + -36.8869965 ] - ], + ] + ], + [ [ [ - [ - 135.52734375, - -36.3151251474805 - ], - [ - 153.80859375, - -36.3151251474805 - ], - [ - 153.80859375, - -18.145851771694467 - ], - [ - 135.52734375, - -18.145851771694467 - ], - [ - 135.52734375, - -36.3151251474805 - ] + 174.8347149, + -36.8815236 + ], + [ + 174.8347774, + -36.881634 + ], + [ + 174.8347409, + -36.8819051 + ], + [ + 174.8350392, + -36.8821049 + ], + [ + 174.8355443, + -36.8821803 + ], + [ + 174.8362531, + -36.881711 + ], + [ + 174.8371047, + -36.8807158 + ], + [ + 174.8376266, + -36.8798969 + ], + [ + 174.8384676, + -36.878055 + ], + [ + 174.837355, + -36.8776834 + ], + [ + 174.8369669, + -36.8778472 + ], + [ + 174.8360445, + -36.8784047 + ], + [ + 174.8360549, + -36.8784168 + ], + [ + 174.8357777, + -36.8790427 + ], + [ + 174.8357201, + -36.8790301 + ], + [ + 174.8355874, + -36.8793891 + ], + [ + 174.8354683, + -36.8795601 + ], + [ + 174.8355479, + -36.8795814 + ], + [ + 174.8353415, + -36.8797934 + ], + [ + 174.8351245, + -36.8798054 + ], + [ + 174.8350004, + -36.8802174 + ], + [ + 174.8347295, + -36.8806857 + ], + [ + 174.835116, + -36.8807968 + ], + [ + 174.8347149, + -36.8815236 ] ] ] - } + ] + }, + "properties": {} } ] -} \ No newline at end of file +} diff --git a/packages/turf-mask/test/out/multi-polygon.geojson b/packages/turf-mask/test/out/multi-polygon.geojson index ce5cf403cd..7416f6924d 100644 --- a/packages/turf-mask/test/out/multi-polygon.geojson +++ b/packages/turf-mask/test/out/multi-polygon.geojson @@ -1,75 +1,523 @@ { - "type": "Feature", - "properties": {}, - "geometry": { - "type": "Polygon", - "coordinates": [ - [ - [ - 180, - 90 - ], - [ - -180, - 90 - ], - [ - -180, - -90 - ], - [ - 180, - -90 - ], - [ - 180, - 90 - ] - ], - [ - [ - 111.796875, - -38.13455657705412 - ], - [ - 129.0234375, - -38.13455657705412 - ], - [ - 129.0234375, - -20.13847031245114 - ], - [ - 111.796875, - -20.13847031245114 - ], - [ - 111.796875, - -38.13455657705412 - ] - ], - [ - [ - 135.52734375, - -36.3151251474805 - ], - [ - 153.80859375, - -36.3151251474805 - ], - [ - 153.80859375, - -18.145851771694467 - ], - [ - 135.52734375, - -18.145851771694467 - ], - [ - 135.52734375, - -36.3151251474805 - ] - ] - ] - } + "type": "Feature", + "geometry": { + "type": "Polygon", + "coordinates": [ + [ + [ + 180, + 90 + ], + [ + -180, + 90 + ], + [ + -180, + -90 + ], + [ + 180, + -90 + ], + [ + 180, + 90 + ] + ], + [ + [ + 174.8347149, + -36.8815236 + ], + [ + 174.835116, + -36.8807968 + ], + [ + 174.8347295, + -36.8806857 + ], + [ + 174.8350004, + -36.8802174 + ], + [ + 174.8351245, + -36.8798054 + ], + [ + 174.8353415, + -36.8797934 + ], + [ + 174.8355479, + -36.8795814 + ], + [ + 174.8354683, + -36.8795601 + ], + [ + 174.8355874, + -36.8793891 + ], + [ + 174.8357201, + -36.8790301 + ], + [ + 174.8357777, + -36.8790427 + ], + [ + 174.8360549, + -36.8784168 + ], + [ + 174.8360445, + -36.8784047 + ], + [ + 174.8369669, + -36.8778472 + ], + [ + 174.837355, + -36.8776834 + ], + [ + 174.8384676, + -36.878055 + ], + [ + 174.8376266, + -36.8798969 + ], + [ + 174.8371047, + -36.8807158 + ], + [ + 174.8362531, + -36.881711 + ], + [ + 174.8355443, + -36.8821803 + ], + [ + 174.8350392, + -36.8821049 + ], + [ + 174.8347409, + -36.8819051 + ], + [ + 174.8347774, + -36.881634 + ], + [ + 174.8347149, + -36.8815236 + ] + ], + [ + [ + 174.8257084, + -36.8869965 + ], + [ + 174.8259044, + -36.8868107 + ], + [ + 174.8261193, + -36.8867062 + ], + [ + 174.8261552, + -36.8866995 + ], + [ + 174.8261688, + -36.886697 + ], + [ + 174.8263768, + -36.8866582 + ], + [ + 174.8265485, + -36.8865621 + ], + [ + 174.8268918, + -36.8864866 + ], + [ + 174.8272938, + -36.8863759 + ], + [ + 174.827812, + -36.8862177 + ], + [ + 174.8285583, + -36.8859898 + ], + [ + 174.8285298, + -36.8854835 + ], + [ + 174.828547, + -36.8850853 + ], + [ + 174.8290133, + -36.8842478 + ], + [ + 174.8294482, + -36.8835612 + ], + [ + 174.8295855, + -36.8828026 + ], + [ + 174.829535, + -36.8828012 + ], + [ + 174.829225, + -36.8827923 + ], + [ + 174.8290019, + -36.8821676 + ], + [ + 174.8291049, + -36.8820989 + ], + [ + 174.8294238, + -36.8818914 + ], + [ + 174.8293809, + -36.8815138 + ], + [ + 174.829062, + -36.8788584 + ], + [ + 174.8290963, + -36.8787142 + ], + [ + 174.8291735, + -36.8785151 + ], + [ + 174.8291478, + -36.8783915 + ], + [ + 174.8291478, + -36.8782886 + ], + [ + 174.8302808, + -36.8777874 + ], + [ + 174.8305018, + -36.8776747 + ], + [ + 174.8309073, + -36.8774235 + ], + [ + 174.8312421, + -36.8772587 + ], + [ + 174.8312077, + -36.8771008 + ], + [ + 174.8313365, + -36.8770253 + ], + [ + 174.8315081, + -36.8771832 + ], + [ + 174.8319464, + -36.8770175 + ], + [ + 174.8320384, + -36.8768042 + ], + [ + 174.8322194, + -36.876702 + ], + [ + 174.8322845, + -36.8767656 + ], + [ + 174.8325172, + -36.8766213 + ], + [ + 174.8329038, + -36.8761774 + ], + [ + 174.8331871, + -36.8758692 + ], + [ + 174.833726, + -36.8754489 + ], + [ + 174.8343983, + -36.8756894 + ], + [ + 174.8345787, + -36.8753974 + ], + [ + 174.8345392, + -36.8752539 + ], + [ + 174.8350149, + -36.875154 + ], + [ + 174.8350022, + -36.8752449 + ], + [ + 174.8350096, + -36.8753835 + ], + [ + 174.8350415, + -36.8755096 + ], + [ + 174.8346298, + -36.8755945 + ], + [ + 174.8347802, + -36.8758729 + ], + [ + 174.8352751, + -36.8760453 + ], + [ + 174.8353743, + -36.8758963 + ], + [ + 174.8356795, + -36.8760915 + ], + [ + 174.8357668, + -36.876134 + ], + [ + 174.8357051, + -36.8761931 + ], + [ + 174.8355399, + -36.8764449 + ], + [ + 174.8353877, + -36.8766751 + ], + [ + 174.8351645, + -36.8767575 + ], + [ + 174.8349499, + -36.8768261 + ], + [ + 174.8347697, + -36.8769085 + ], + [ + 174.8343234, + -36.8772656 + ], + [ + 174.8337539, + -36.8777781 + ], + [ + 174.8333146, + -36.878309 + ], + [ + 174.8331132, + -36.8782542 + ], + [ + 174.8328699, + -36.878784 + ], + [ + 174.8342416, + -36.8795988 + ], + [ + 174.8347595, + -36.8798408 + ], + [ + 174.8349119, + -36.8798696 + ], + [ + 174.8348716, + -36.8800115 + ], + [ + 174.8346495, + -36.8804375 + ], + [ + 174.8346312, + -36.8804379 + ], + [ + 174.8325982, + -36.8803963 + ], + [ + 174.8324179, + -36.8806023 + ], + [ + 174.8321192, + -36.8820636 + ], + [ + 174.8319151, + -36.8828293 + ], + [ + 174.8311496, + -36.8842651 + ], + [ + 174.8315144, + -36.8849051 + ], + [ + 174.8321967, + -36.8848087 + ], + [ + 174.8332402, + -36.8837406 + ], + [ + 174.8340081, + -36.8834433 + ], + [ + 174.836428, + -36.8830636 + ], + [ + 174.8367485, + -36.8829628 + ], + [ + 174.8369142, + -36.88283 + ], + [ + 174.8373394, + -36.8830989 + ], + [ + 174.8373321, + -36.8831729 + ], + [ + 174.8367548, + -36.8837641 + ], + [ + 174.8356801, + -36.8844927 + ], + [ + 174.8354962, + -36.8846273 + ], + [ + 174.834388, + -36.885941 + ], + [ + 174.8341202, + -36.8862468 + ], + [ + 174.8336846, + -36.8865436 + ], + [ + 174.8314673, + -36.8878689 + ], + [ + 174.8311438, + -36.8878347 + ], + [ + 174.8296591, + -36.8874264 + ], + [ + 174.8278042, + -36.8875561 + ], + [ + 174.8276109, + -36.887539 + ], + [ + 174.8257084, + -36.8869965 + ] + ] + ] + }, + "properties": {} }