Skip to content

Commit

Permalink
Update unkink-polygon testing/refactoring (#889)
Browse files Browse the repository at this point in the history
* Update unkink-polygon testing/refactoring

* Fix linting issue
  • Loading branch information
DenisCarriere authored Aug 4, 2017
1 parent 4ae9c80 commit 51c513d
Show file tree
Hide file tree
Showing 12 changed files with 99 additions and 94 deletions.
32 changes: 12 additions & 20 deletions packages/turf-unkink-polygon/index.js
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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);
};
3 changes: 1 addition & 2 deletions packages/turf-unkink-polygon/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
10 changes: 5 additions & 5 deletions packages/turf-unkink-polygon/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand Down
4 changes: 3 additions & 1 deletion packages/turf-unkink-polygon/test/in/complex.geojson
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
{
"type": "Feature",
"properties": {},
"properties": {
"foo": "bar"
},
"geometry": {
"type": "Polygon",
"coordinates": [
Expand Down
20 changes: 12 additions & 8 deletions packages/turf-unkink-polygon/test/out/complex.geojson
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
"stroke": "#F00",
"fill": "#F00",
"stroke-width": 6,
"fill-opacity": 0.1
"fill-opacity": 0.5,
"foo": "bar"
},
"geometry": {
"type": "Polygon",
Expand Down Expand Up @@ -43,7 +44,8 @@
"stroke": "#00F",
"fill": "#00F",
"stroke-width": 6,
"fill-opacity": 0.1
"fill-opacity": 0.5,
"foo": "bar"
},
"geometry": {
"type": "Polygon",
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down
8 changes: 4 additions & 4 deletions packages/turf-unkink-polygon/test/out/hourglass.geojson
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down
20 changes: 10 additions & 10 deletions packages/turf-unkink-polygon/test/out/hourglassMultiPolygon.geojson
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down
Loading

0 comments on commit 51c513d

Please sign in to comment.