Skip to content
Closed
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
66 changes: 32 additions & 34 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
// depend on jsts for now https://github.com/bjornharrtell/jsts/blob/master/examples/overlay.html
var jsts = require('jsts');
var gh = require('gh-clipping-algorithm');

/**
* Finds the difference between two {@link Polygon|polygons} by clipping the second
Expand Down Expand Up @@ -57,40 +56,39 @@ var jsts = require('jsts');
* //=differenced
*/

module.exports = function(p1, p2) {
var poly1 = JSON.parse(JSON.stringify(p1));
var poly2 = JSON.parse(JSON.stringify(p2));
if(poly1.type !== 'Feature') {
poly1 = {
type: 'Feature',
properties: {},
geometry: poly1
};
}
if(poly2.type !== 'Feature') {
poly2 = {
type: 'Feature',
properties: {},
geometry: poly2
};
}

var reader = new jsts.io.GeoJSONReader();
var a = reader.read(JSON.stringify(poly1.geometry));
var b = reader.read(JSON.stringify(poly2.geometry));
var differenced = a.difference(b);
var parser = new jsts.io.GeoJSONParser();
differenced = parser.write(differenced);
module.exports = function(poly1, poly2) {
// console.log(poly1);
var a = poly1.coordinates ? poly1.coordinates : poly1.geometry.coordinates;
var b = poly2.coordinates ? poly2.coordinates : poly2.geometry.coordinates;
var u = gh.subtract(a, b);

poly1.geometry = differenced;
var feature = {
"type": "Feature",
"properties": {},
"geometry": {}
};

if (poly1.geometry.type === 'GeometryCollection' && poly1.geometry.geometries.length === 0) {
if (!u || u.length == 0) {
return undefined;
} else {
return {
type: 'Feature',
properties: poly1.properties,
geometry: differenced
};
}

if (gh.utils.isMultiPolygon(u)) {
if (u.length > 1) {
feature.geometry.type = "MultiPolygon";
feature.geometry.coordinates = u;
} else {
feature.geometry.type = "Polygon";
feature.geometry.coordinates = u[0];
}
} else if (gh.utils.isPolygon(u)) {
feature.geometry.type = "Polygon";
feature.geometry.coordinates = u;
}

if (poly1.properties) {
feature.properties = poly1.properties;
}

return feature;
};

21 changes: 0 additions & 21 deletions npm-shrinkwrap.json

This file was deleted.

5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,11 @@
"glob": "~5.0.5",
"tape": "~4.0.0",
"dox": "^0.7.1",
"doxme": "^1.8.2"
"doxme": "^1.8.2",
"geojson-equality": "^0.1.4"
},
"dependencies": {
"jsts": "~0.15.0",
"gh-clipping-algorithm": "^0.*",
"turf-featurecollection": "^1.0.1"
}
}
9 changes: 5 additions & 4 deletions test/test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
var difference = require('../'),
test = require('tape'),
glob = require('glob'),
fs = require('fs');
fs = require('fs')
equal = new(require('geojson-equality'));

var REGEN = process.env.REGEN;

Expand All @@ -12,7 +13,7 @@ test('difference', function(t){
var output = difference(features[0], features[1]);
if (REGEN) fs.writeFileSync(input.replace('/in/', '/out/'), JSON.stringify(output));
t.deepEqual(before, features, 'does not mutate data');
t.deepEqual(output, JSON.parse(fs.readFileSync(input.replace('/in/', '/out/'))), input);
t.ok(equal.compare(output, JSON.parse(fs.readFileSync(input.replace('/in/', '/out/')))), input);
});
t.end();
});
Expand All @@ -24,7 +25,7 @@ test('difference -- geometries', function(t){
var output = difference(fcs[0].geometry, fcs[1].geometry);
if (REGEN) fs.writeFileSync(input.replace('/in/', '/out/'), JSON.stringify(output));
t.deepEqual(before, fcs, 'does not mutate data');
t.deepEqual(output, JSON.parse(fs.readFileSync(input.replace('/in/', '/out/'))), input);
t.ok(equal.compare(output, JSON.parse(fs.readFileSync(input.replace('/in/', '/out/')))), input);
});
t.end();
});
Expand All @@ -36,7 +37,7 @@ test('difference -- geometries', function(t){
var output = difference(fcs[0].geometry, fcs[1].geometry);
if (REGEN) fs.writeFileSync(input.replace('/in/', '/out/'), JSON.stringify(output));
t.deepEqual(before, fcs, 'does not mutate data');
t.deepEqual(output, JSON.parse(fs.readFileSync(input.replace('/in/', '/out/'))), input);
t.ok(equal.compare(output, JSON.parse(fs.readFileSync(input.replace('/in/', '/out/')))), input);
});
t.end();
});
Expand Down