diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..8d87b1d --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +node_modules/* diff --git a/index.js b/index.js index d02c6d9..b7e4f17 100644 --- a/index.js +++ b/index.js @@ -1,9 +1,4 @@ -// look here for help http://svn.osgeo.org/grass/grass/branches/releasebranch_6_4/vector/v.overlay/main.c -//must be array of polygons - -// 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'); /** * Takes two {@link Polygon|polygons} and returns a combined polygon. If the input polygons are not contiguous, this function returns a {@link MultiPolygon} feature. @@ -58,16 +53,37 @@ var jsts = require('jsts'); * //=union */ module.exports = function(poly1, poly2) { - var reader = new jsts.io.GeoJSONReader(); - var a = reader.read(JSON.stringify(poly1.geometry)); - var b = reader.read(JSON.stringify(poly2.geometry)); - var union = a.union(b); - var parser = new jsts.io.GeoJSONParser(); + // console.log(poly1); + var a = poly1.coordinates ? poly1.coordinates : poly1.geometry.coordinates; + var b = poly2.coordinates ? poly2.coordinates : poly2.geometry.coordinates; + var u = gh.union(a, b); - union = parser.write(union); - return { - type: 'Feature', - geometry: union, - properties: poly1.properties + var feature = { + "type": "Feature", + "properties": {}, + "geometry": {} }; + + if (!u || u.length == 0) { + return undefined; + } + + 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 b334988..0000000 --- a/npm-shrinkwrap.json +++ /dev/null @@ -1,17 +0,0 @@ -{ - "name": "turf-union", - "version": "1.0.2", - "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" - } - } - } - } -} diff --git a/package.json b/package.json index 3c9a2be..b93c211 100644 --- a/package.json +++ b/package.json @@ -26,9 +26,10 @@ "glob": "~4.3.5", "tape": "~3.5.0", "dox": "^0.6.1", - "doxme": "^1.4.3" + "doxme": "^1.4.3", + "geojson-equality": "^0.1.4" }, "dependencies": { - "jsts": "~0.15.0" + "gh-clipping-algorithm": "^0.*" } } diff --git a/test/union.js b/test/union.js index dd636de..6a92e0c 100644 --- a/test/union.js +++ b/test/union.js @@ -1,7 +1,8 @@ var union = require('../'), test = require('tape'), glob = require('glob'), - fs = require('fs'); + fs = require('fs'), + equal = new(require('geojson-equality')); var REGEN = false; @@ -10,7 +11,7 @@ test('union', function(t){ var fcs = JSON.parse(fs.readFileSync(input)); var output = union(fcs[0].features[0], fcs[1].features[0]); if (REGEN) fs.writeFileSync(input.replace('/in/', '/out/'), JSON.stringify(output)); - 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(); });