diff --git a/index.js b/index.js index d7eebf2..49d2a30 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'); /** * Takes two {@link Polygon|polygons} and finds their intersection. If they share a border, returns the border; if they don't intersect, returns undefined. @@ -58,25 +57,32 @@ var jsts = require('jsts'); * //=intersection */ module.exports = function(poly1, poly2) { - var geom1, geom2; - if(poly1.type === 'Feature') geom1 = poly1.geometry; - else geom1 = poly1; - if(poly2.type === 'Feature') geom2 = poly2.geometry; - else geom2 = poly2; - var reader = new jsts.io.GeoJSONReader(); - var a = reader.read(JSON.stringify(geom1)); - var b = reader.read(JSON.stringify(geom2)); - var intersection = a.intersection(b); - var parser = new jsts.io.GeoJSONParser(); + var a = poly1.coordinates ? poly1.coordinates : poly1.geometry.coordinates; + var b = poly2.coordinates ? poly2.coordinates : poly2.geometry.coordinates; + var u = gh.intersect(a, b); - intersection = parser.write(intersection); - if(intersection.type === 'GeometryCollection' && intersection.geometries.length === 0) { + var feature = { + "type": "Feature", + "properties": {}, + "geometry": {} + }; + + if (!u || u.length == 0) { return undefined; - } else { - return { - type: 'Feature', - properties: {}, - geometry: intersection - }; } + + 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; + } + + return feature; }; diff --git a/npm-shrinkwrap.json b/npm-shrinkwrap.json deleted file mode 100644 index 67e4f5e..0000000 --- a/npm-shrinkwrap.json +++ /dev/null @@ -1,17 +0,0 @@ -{ - "name": "turf-intersect", - "version": "1.4.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 4588c57..9baa089 100644 --- a/package.json +++ b/package.json @@ -27,9 +27,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/fixtures/out/Intersect1.json b/test/fixtures/out/Intersect1.json index 75c2fcf..cb6dfd1 100644 --- a/test/fixtures/out/Intersect1.json +++ b/test/fixtures/out/Intersect1.json @@ -1 +1 @@ -{"type":"Feature","properties":{},"geometry":{"type":"Polygon","coordinates":[[[-79.94623496447946,32.89900638172028],[-79.88571166992188,32.887659962078956],[-79.89395141601562,32.75551989829049],[-79.92322780260464,32.73910022106017],[-79.93789672851562,32.74108223150125],[-79.93034362792969,32.76475877693074],[-79.97360229492188,32.76071688548088],[-79.97428894042969,32.83690450361482],[-79.94623496447946,32.89900638172028]]]}} \ No newline at end of file +{"type":"Feature","properties":{},"geometry":{"type":"Polygon","coordinates":[[[-79.92322780260464,32.73910022106017],[-79.89395141601562,32.75551989829049],[-79.88571166992188,32.887659962078956],[-79.94623496447946,32.89900638172028],[-79.97428894042969,32.83690450361482],[-79.97360229492188,32.76071688548088],[-79.93034362792969,32.76475877693074],[-79.93789672851562,32.74108223150125],[-79.92322780260464,32.73910022106017]]]}} \ No newline at end of file diff --git a/test/fixtures/out/armenia.json b/test/fixtures/out/armenia.json index 3f64830..6df7c7c 100644 --- a/test/fixtures/out/armenia.json +++ b/test/fixtures/out/armenia.json @@ -1 +1 @@ -{"type":"Feature","properties":{},"geometry":{"type":"Polygon","coordinates":[[[43.582746,41.092143],[44.97248,41.248129],[45.179496,40.985354],[45.560351,40.81229],[45.359175,40.561504],[45.891907,40.218476],[45.610012,39.899994],[46.034534,39.628021],[46.483499,39.464155],[46.50572,38.770605],[46.143623,38.741201],[45.735379,39.319719],[45.739978,39.473999],[45.298145,39.471751],[45.001987,39.740004],[44.79399,39.713003],[44.400009,40.005],[43.656436,40.253564],[43.752658,40.740201],[43.582746,41.092143]]]}} \ No newline at end of file +{"type":"Feature","properties":{},"geometry":{"type":"Polygon","coordinates":[[[43.582746,41.092143],[44.97248,41.248129],[45.179496,40.985354],[45.560351,40.81229],[45.359175,40.561504],[45.891907,40.218476],[45.610012,39.899994],[46.034534,39.628021],[46.483499,39.464155],[46.50572,38.770605],[46.143623,38.741201],[45.735379,39.319719],[45.739978,39.473999],[45.298145,39.471751],[45.001987,39.740004],[44.79399,39.713003],[44.400009,40.005],[43.656436,40.253564],[43.752658,40.740201],[43.582746,41.092143]]]}} diff --git a/test/test.js b/test/test.js index 7ed24a8..a37f11a 100644 --- a/test/test.js +++ b/test/test.js @@ -1,16 +1,17 @@ var intersect = require('../'), test = require('tape'), glob = require('glob'), - fs = require('fs'); + fs = require('fs') + equal = new(require('geojson-equality')); -var REGEN = true; +var REGEN = false; test('intersect -- features', function(t){ glob.sync(__dirname + '/fixtures/in/*.json').forEach(function(input) { var features = JSON.parse(fs.readFileSync(input)); var output = intersect(features[0], features[1]); 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(); }); @@ -20,7 +21,7 @@ test('intersect -- geometries', function(t){ var features = JSON.parse(fs.readFileSync(input)); var output = intersect(features[0].geometry, features[1].geometry); 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(); }); @@ -30,4 +31,4 @@ test('intersect -- no overlap', function(t){ var output = intersect(noOverlap[0].geometry, noOverlap[1].geometry); t.deepEqual(output, undefined); t.end(); -}); \ No newline at end of file +});