Skip to content

Commit

Permalink
Handle MultiPolygon properly when building a mask
Browse files Browse the repository at this point in the history
Change from featureEach to flattenEach will output simple Polygons.
Fix Turfjs#837
  • Loading branch information
thiagoxvo committed Jul 26, 2017
1 parent 8b8d912 commit 95a5d37
Show file tree
Hide file tree
Showing 3 changed files with 1,024 additions and 128 deletions.
14 changes: 7 additions & 7 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 @@ -64,11 +64,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 @@ -84,11 +84,11 @@ function buildMask(maskPolygon, polygonOuters, polygonInners) {
function separatePolygons(polygon) {
var outers = [];
var inners = [];
featureEach(polygon, function (multiFeature) {
flattenEach(polygon, function (multiFeature) {
if (multiFeature.geometry.type === 'MultiPolygon') {
multiFeature = flattenMultiPolygon(multiFeature);
}
featureEach(multiFeature, function (feature) {
flattenEach(multiFeature, function (feature) {
var coordinates = feature.geometry.coordinates;
var featureOuter = coordinates[0];
var featureInner = coordinates.slice(1);
Expand Down Expand Up @@ -143,7 +143,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 @@ -200,7 +200,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 95a5d37

Please sign in to comment.