Skip to content

Commit

Permalink
🐛 Fix #837 - @turf-mask (#865)
Browse files Browse the repository at this point in the history
* Update Typescript definition

* Handle MultiPolygon properly when building a mask

Change from featureEach to flattenEach will output simple Polygons.
Fix #837

* Remove unused code
  • Loading branch information
thiagoxvo authored and DenisCarriere committed Jul 26, 2017
1 parent a05c109 commit 99ea6b1
Show file tree
Hide file tree
Showing 4 changed files with 1,034 additions and 155 deletions.
7 changes: 5 additions & 2 deletions packages/turf-mask/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
/// <reference types="geojson" />

type Polygon = GeoJSON.Feature<GeoJSON.Polygon> | GeoJSON.Polygon;
type Geom = GeoJSON.Polygon | GeoJSON.MultiPolygon;
type Poly = GeoJSON.FeatureCollection<Geom> | GeoJSON.Feature<Geom> | Geom;
type Polygon = GeoJSON.Feature<GeoJSON.Polygon>;
type Mask = GeoJSON.Feature<GeoJSON.Polygon> | 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;
44 changes: 12 additions & 32 deletions packages/turf-mask/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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);
Expand All @@ -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>} multiPolygon GeoJSON Feature
* @returns {FeatureCollection<Polygon>} 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
*
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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],
Expand Down
Loading

0 comments on commit 99ea6b1

Please sign in to comment.