Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🐛 Fix #837 - @turf-mask #865

Merged
merged 3 commits into from
Jul 26, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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