diff --git a/index.js b/index.js index 2d9bd42..ff39374 100644 --- a/index.js +++ b/index.js @@ -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 @@ -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; }; + diff --git a/npm-shrinkwrap.json b/npm-shrinkwrap.json deleted file mode 100644 index c1e22ac..0000000 --- a/npm-shrinkwrap.json +++ /dev/null @@ -1,21 +0,0 @@ -{ - "name": "turf-difference", - "version": "2.0.0", - "dependencies": { - "jsts": { - "version": "0.15.0", - "from": "jsts@>=0.15.0 <0.16.0", - "resolved": "https://registry.npmjs.org/jsts/-/jsts-0.15.0.tgz", - "dependencies": { - "javascript.util": { - "version": "0.12.5", - "resolved": "https://registry.npmjs.org/javascript.util/-/javascript.util-0.12.5.tgz" - } - } - }, - "turf-featurecollection": { - "version": "1.0.1", - "from": "turf-featurecollection@>=1.0.0 <2.0.0" - } - } -} diff --git a/package.json b/package.json index cc6a749..3ed7542 100644 --- a/package.json +++ b/package.json @@ -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" } } diff --git a/test/test.js b/test/test.js index 772b92f..806b45a 100644 --- a/test/test.js +++ b/test/test.js @@ -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; @@ -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(); }); @@ -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(); }); @@ -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(); });