From 77d23528a74d0b016dad0894d0cb5a91a3270043 Mon Sep 17 00:00:00 2001 From: Tim Channell Date: Sat, 14 Feb 2015 12:59:31 -0500 Subject: [PATCH 1/9] fixed some tests and stuff --- index.js | 50 ++++++++++++++++++------------- package.json | 4 +-- test/fixtures/out/Intersect1.json | 2 +- test/fixtures/out/armenia.json | 2 +- test/test.js | 4 +-- 5 files changed, 35 insertions(+), 27 deletions(-) diff --git a/index.js b/index.js index fc980e6..2e0c20a 100644 --- a/index.js +++ b/index.js @@ -1,5 +1,5 @@ // depend on jsts for now https://github.com/bjornharrtell/jsts/blob/master/examples/overlay.html -var jsts = require('jsts'); +var gh = require('greiner-hormann'); var featurecollection = require('turf-featurecollection'); /** @@ -58,26 +58,34 @@ var featurecollection = require('turf-featurecollection'); * * //=intersection */ -module.exports = function(poly1, poly2){ - var geom1; - 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(); +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.intersect(a, b); - intersection = parser.write(intersection); - if(intersection.type === 'GeometryCollection' && intersection.geometries.length === 0) { - return; - } else { - return { - type: 'Feature', - properties: {}, - geometry: intersection - }; + 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; } + + return feature; }; diff --git a/package.json b/package.json index 6e2e41b..fb2aed5 100644 --- a/package.json +++ b/package.json @@ -30,7 +30,7 @@ "doxme": "^1.4.3" }, "dependencies": { - "jsts": "~0.15.0", - "turf-featurecollection": "^1.0.0" + "turf-featurecollection": "^1.0.0", + "greiner-hormann": "tchannel/greiner-hormann" } } 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..8a59e83 100644 --- a/test/test.js +++ b/test/test.js @@ -3,7 +3,7 @@ var intersect = require('../'), glob = require('glob'), fs = require('fs'); -var REGEN = true; +var REGEN = false; test('intersect -- features', function(t){ glob.sync(__dirname + '/fixtures/in/*.json').forEach(function(input) { @@ -30,4 +30,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 +}); From a9bb4ef1cfdc59da0416419aaaee478929f075c7 Mon Sep 17 00:00:00 2001 From: Tim Channell Date: Tue, 5 May 2015 15:58:38 -0400 Subject: [PATCH 2/9] updated GH dep --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index fb2aed5..8808985 100644 --- a/package.json +++ b/package.json @@ -31,6 +31,6 @@ }, "dependencies": { "turf-featurecollection": "^1.0.0", - "greiner-hormann": "tchannel/greiner-hormann" + "greiner-hormann": "tchannel/greiner-hormann#master" } } From e8bfc871a0dfa38a8e640d63cf864d4053b18971 Mon Sep 17 00:00:00 2001 From: Tim Channell Date: Thu, 7 May 2015 14:46:53 -0400 Subject: [PATCH 3/9] killed JSTS for real --- index.js | 55 +++++++++++++++++++++++++++++----------------------- package.json | 5 +++-- test/test.js | 11 ++++++----- 3 files changed, 40 insertions(+), 31 deletions(-) diff --git a/index.js b/index.js index d7eebf2..f749b1c 100644 --- a/index.js +++ b/index.js @@ -1,14 +1,13 @@ -// depend on jsts for now https://github.com/bjornharrtell/jsts/blob/master/examples/overlay.html -var jsts = require('jsts'); +var gh = require('greiner-hormann'); /** - * Takes two {@link Polygon|polygons} and finds their intersection. If they share a border, returns the border; if they don't intersect, returns undefined. + * Takes two {@link Polygon} features and finds their intersection. * * @module turf/intersect * @category transformation - * @param {Feature} poly1 the first polygon - * @param {Feature} poly2 the second polygon - * @return {(Feature|undefined|Feature)} if `poly1` and `poly2` overlap, returns a Polygon feature representing the area they overlap; if `poly1` and `poly2` do not overlap, returns `undefined`; if `poly1` and `poly2` share a border, a MultiLineString of the locations where their borders are shared + * @param {Polygon} poly1 the first Polygon + * @param {Polygon} poly2 the second Polygon + * @return {Polygon|undefined|MultiLineString} if `poly1` and `poly2` overlap, returns a Polygon feature representing the area they overlap; if `poly1` and `poly2` do not overlap, returns `undefined`; if `poly1` and `poly2` share a border, a MultiLineString of the locations where their borders are shared * @example * var poly1 = { * "type": "Feature", @@ -58,25 +57,33 @@ 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(); + // console.log(poly1); + 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/package.json b/package.json index 4588c57..9a485d7 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" + "greiner-hormann": "tchannel/greiner-hormann#master" } } 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 +}); From cfbdbaf5adc20355b139b91b5ca6f7f58592b165 Mon Sep 17 00:00:00 2001 From: Tim Channell Date: Thu, 7 May 2015 14:56:24 -0400 Subject: [PATCH 4/9] fixed to finish killing jsts --- index.js | 2 +- npm-shrinkwrap.json | 17 ----------------- package.json | 2 +- 3 files changed, 2 insertions(+), 19 deletions(-) delete mode 100644 npm-shrinkwrap.json diff --git a/index.js b/index.js index f749b1c..afa32e7 100644 --- a/index.js +++ b/index.js @@ -1,4 +1,4 @@ -var gh = require('greiner-hormann'); +var gh = require('gh-clipping-algorithm'); /** * Takes two {@link Polygon} features and finds their intersection. 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 9a485d7..54d6414 100644 --- a/package.json +++ b/package.json @@ -31,6 +31,6 @@ "geojson-equality": "^0.1.4" }, "dependencies": { - "greiner-hormann": "tchannel/greiner-hormann#master" + "gh-clipping-algorithm": "^0.1.0" } } From ccf4938a5d1efb0e1243b56646491c7b22dc76c7 Mon Sep 17 00:00:00 2001 From: Tim Channell Date: Thu, 7 May 2015 18:16:22 -0400 Subject: [PATCH 5/9] updated GH --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 54d6414..9520be8 100644 --- a/package.json +++ b/package.json @@ -31,6 +31,6 @@ "geojson-equality": "^0.1.4" }, "dependencies": { - "gh-clipping-algorithm": "^0.1.0" + "gh-clipping-algorithm": "^0.2.0" } } From 94e0eca56753af2c9e7fcaf6d6beae6f9c6ae32a Mon Sep 17 00:00:00 2001 From: Tim Channell Date: Fri, 8 May 2015 14:30:31 -0400 Subject: [PATCH 6/9] updated gh version --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 9520be8..b0e3f37 100644 --- a/package.json +++ b/package.json @@ -31,6 +31,6 @@ "geojson-equality": "^0.1.4" }, "dependencies": { - "gh-clipping-algorithm": "^0.2.0" + "gh-clipping-algorithm": "^0.3.0" } } From 78972c9d90a817bad80be63344b4c813399ad33d Mon Sep 17 00:00:00 2001 From: Tim Channell Date: Fri, 8 May 2015 14:40:44 -0400 Subject: [PATCH 7/9] fixed docblocks --- index.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/index.js b/index.js index afa32e7..803a29f 100644 --- a/index.js +++ b/index.js @@ -1,13 +1,13 @@ var gh = require('gh-clipping-algorithm'); /** - * Takes two {@link Polygon} features and finds their intersection. + * Takes two {@link Polygon|polygons} and finds their intersection. If they share a border, returns the border; if they don't intersect, returns undefined. * * @module turf/intersect * @category transformation - * @param {Polygon} poly1 the first Polygon - * @param {Polygon} poly2 the second Polygon - * @return {Polygon|undefined|MultiLineString} if `poly1` and `poly2` overlap, returns a Polygon feature representing the area they overlap; if `poly1` and `poly2` do not overlap, returns `undefined`; if `poly1` and `poly2` share a border, a MultiLineString of the locations where their borders are shared + * @param {Feature} poly1 the first polygon + * @param {Feature} poly2 the second polygon + * @return {(Feature|undefined|Feature)} if `poly1` and `poly2` overlap, returns a Polygon feature representing the area they overlap; if `poly1` and `poly2` do not overlap, returns `undefined`; if `poly1` and `poly2` share a border, a MultiLineString of the locations where their borders are shared * @example * var poly1 = { * "type": "Feature", From 813dd482c5479d7d928ca0f6f67ffe2dec3c8da8 Mon Sep 17 00:00:00 2001 From: Tim Channell Date: Tue, 6 Oct 2015 10:17:24 -0400 Subject: [PATCH 8/9] bumped GH version --- index.js | 2 +- package.json | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index 2e0c20a..543fb0f 100644 --- a/index.js +++ b/index.js @@ -1,5 +1,5 @@ // depend on jsts for now https://github.com/bjornharrtell/jsts/blob/master/examples/overlay.html -var gh = require('greiner-hormann'); +var gh = require('gh-clipping-algorithm'); var featurecollection = require('turf-featurecollection'); /** diff --git a/package.json b/package.json index 8808985..aa96d94 100644 --- a/package.json +++ b/package.json @@ -31,6 +31,6 @@ }, "dependencies": { "turf-featurecollection": "^1.0.0", - "greiner-hormann": "tchannel/greiner-hormann#master" + "gh-clipping-algorithm": "^0.4" } } From 07fa5b819309b87e4a62ad9408a11c2b335a3163 Mon Sep 17 00:00:00 2001 From: Tim Channell Date: Wed, 7 Oct 2015 16:43:19 -0400 Subject: [PATCH 9/9] bumped GH --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index d617b20..9baa089 100644 --- a/package.json +++ b/package.json @@ -31,6 +31,6 @@ "geojson-equality": "^0.1.4" }, "dependencies": { - "gh-clipping-algorithm": "^0.4" + "gh-clipping-algorithm": "^0.*" } }