From ee3e849cb4ec17ac0050330025adc0d1a55ccd3f Mon Sep 17 00:00:00 2001 From: Tim Channell Date: Wed, 11 Feb 2015 20:10:51 -0500 Subject: [PATCH 01/10] updated to use greiner-hormann --- .gitignore | 2 ++ index.js | 31 +++++++++++++++++----------- package.json | 3 ++- test/fixtures/out/Intersect1.geojson | 1 - test/fixtures/out/Intersect2.geojson | 1 - 5 files changed, 23 insertions(+), 15 deletions(-) create mode 100644 .gitignore delete mode 100644 test/fixtures/out/Intersect1.geojson delete mode 100644 test/fixtures/out/Intersect2.geojson diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..4712098 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +node_modules/* +test/fixtures/out/* diff --git a/index.js b/index.js index c8d9a85..c15c4f2 100644 --- a/index.js +++ b/index.js @@ -4,6 +4,7 @@ // 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} features and returnes a combined {@link Polygon} feature. If the input Polygon features are not contiguous, this function returns a {@link MultiPolygon} feature. @@ -57,17 +58,23 @@ 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(); +module.exports = function (poly1, poly2) { + var a = poly1.geometry.coordinates; + var b = 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": poly1.properties, + "geometry": {} + } + if (gh.utils.isMultiPolygon(u)) { + feature.geometry.type = "MultiPolygon"; + feature.geometry.coordinates = u; + } 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 070af3b..d66eb91 100644 --- a/package.json +++ b/package.json @@ -29,6 +29,7 @@ "doxme": "^1.4.3" }, "dependencies": { - "jsts": "~0.15.0" + "jsts": "~0.15.0", + "greiner-hormann": "tchannel/greiner-hormann" } } diff --git a/test/fixtures/out/Intersect1.geojson b/test/fixtures/out/Intersect1.geojson deleted file mode 100644 index 201eedf..0000000 --- a/test/fixtures/out/Intersect1.geojson +++ /dev/null @@ -1 +0,0 @@ -{"type":"Feature","geometry":{"type":"Polygon","coordinates":[[[-79.92322780260464,32.73910022106017],[-80.00312805175781,32.69428812316933],[-80.15350341796875,32.82825010814964],[-80.09788513183594,32.927436533285565],[-79.94623496447946,32.89900638172028],[-79.92141723632812,32.953944317478246],[-79.81773376464844,32.923402043498875],[-79.80537414550781,32.7231762754146],[-79.92322780260464,32.73910022106017]]]},"properties":{}} \ No newline at end of file diff --git a/test/fixtures/out/Intersect2.geojson b/test/fixtures/out/Intersect2.geojson deleted file mode 100644 index ec9a1fe..0000000 --- a/test/fixtures/out/Intersect2.geojson +++ /dev/null @@ -1 +0,0 @@ -{"type":"Feature","geometry":{"type":"MultiPolygon","coordinates":[[[[-79.92141723632812,32.953944317478246],[-79.81773376464844,32.923402043498875],[-79.80537414550781,32.7231762754146],[-79.93789672851562,32.74108223150125],[-79.93034362792969,32.76475877693074],[-79.97360229492188,32.76071688548088],[-79.97428894042969,32.83690450361482],[-79.92141723632812,32.953944317478246]]],[[[-80.10543823242188,32.94760622243483],[-79.98184204101562,32.90495631913751],[-79.99351501464844,32.84440429734253],[-80.07453918457031,32.85536439443039],[-80.14389038085938,32.8149783969858],[-80.10543823242188,32.94760622243483]]]]},"properties":{}} \ No newline at end of file From 706bfcf7f573d856d48e14e1d55f3035ba1af2cc Mon Sep 17 00:00:00 2001 From: Tim Channell Date: Wed, 11 Feb 2015 20:12:59 -0500 Subject: [PATCH 02/10] removed jsts --- index.js | 6 ------ package.json | 1 - 2 files changed, 7 deletions(-) diff --git a/index.js b/index.js index c15c4f2..d67a132 100644 --- a/index.js +++ b/index.js @@ -1,9 +1,3 @@ -// 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('greiner-hormann'); /** diff --git a/package.json b/package.json index d66eb91..2d005c8 100644 --- a/package.json +++ b/package.json @@ -29,7 +29,6 @@ "doxme": "^1.4.3" }, "dependencies": { - "jsts": "~0.15.0", "greiner-hormann": "tchannel/greiner-hormann" } } From b3a1c805a87c35034ca02196203642dff52eae19 Mon Sep 17 00:00:00 2001 From: Tim Channell Date: Thu, 12 Feb 2015 09:10:46 -0500 Subject: [PATCH 03/10] fixed dumb gitignore --- .gitignore | 1 - 1 file changed, 1 deletion(-) diff --git a/.gitignore b/.gitignore index 4712098..8d87b1d 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1 @@ node_modules/* -test/fixtures/out/* From b4fc6a1ac4c1538a5322a5688c3e2d9595390e06 Mon Sep 17 00:00:00 2001 From: Tim Channell Date: Thu, 12 Feb 2015 09:13:44 -0500 Subject: [PATCH 04/10] readded output fixtures --- test/fixtures/out/Intersect1.geojson | 1 + test/fixtures/out/Intersect2.geojson | 1 + 2 files changed, 2 insertions(+) create mode 100644 test/fixtures/out/Intersect1.geojson create mode 100644 test/fixtures/out/Intersect2.geojson diff --git a/test/fixtures/out/Intersect1.geojson b/test/fixtures/out/Intersect1.geojson new file mode 100644 index 0000000..201eedf --- /dev/null +++ b/test/fixtures/out/Intersect1.geojson @@ -0,0 +1 @@ +{"type":"Feature","geometry":{"type":"Polygon","coordinates":[[[-79.92322780260464,32.73910022106017],[-80.00312805175781,32.69428812316933],[-80.15350341796875,32.82825010814964],[-80.09788513183594,32.927436533285565],[-79.94623496447946,32.89900638172028],[-79.92141723632812,32.953944317478246],[-79.81773376464844,32.923402043498875],[-79.80537414550781,32.7231762754146],[-79.92322780260464,32.73910022106017]]]},"properties":{}} \ No newline at end of file diff --git a/test/fixtures/out/Intersect2.geojson b/test/fixtures/out/Intersect2.geojson new file mode 100644 index 0000000..ec9a1fe --- /dev/null +++ b/test/fixtures/out/Intersect2.geojson @@ -0,0 +1 @@ +{"type":"Feature","geometry":{"type":"MultiPolygon","coordinates":[[[[-79.92141723632812,32.953944317478246],[-79.81773376464844,32.923402043498875],[-79.80537414550781,32.7231762754146],[-79.93789672851562,32.74108223150125],[-79.93034362792969,32.76475877693074],[-79.97360229492188,32.76071688548088],[-79.97428894042969,32.83690450361482],[-79.92141723632812,32.953944317478246]]],[[[-80.10543823242188,32.94760622243483],[-79.98184204101562,32.90495631913751],[-79.99351501464844,32.84440429734253],[-80.07453918457031,32.85536439443039],[-80.14389038085938,32.8149783969858],[-80.10543823242188,32.94760622243483]]]]},"properties":{}} \ No newline at end of file From ed081c8b7ff51411f655c10fc1febfa0a0b65642 Mon Sep 17 00:00:00 2001 From: Tim Channell Date: Thu, 12 Feb 2015 09:31:31 -0500 Subject: [PATCH 05/10] fixed poly/multipoly handling --- index.js | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index d67a132..6b89d2a 100644 --- a/index.js +++ b/index.js @@ -63,8 +63,13 @@ module.exports = function (poly1, poly2) { "geometry": {} } if (gh.utils.isMultiPolygon(u)) { - feature.geometry.type = "MultiPolygon"; - feature.geometry.coordinates = 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; From dff5846868d248de7bd7563cfa84bcba2d6222a3 Mon Sep 17 00:00:00 2001 From: Tim Channell Date: Thu, 7 May 2015 15:04:22 -0400 Subject: [PATCH 06/10] killed jsts --- index.js | 48 ++++++++++++++++++++++++++++++--------------- npm-shrinkwrap.json | 17 ---------------- package.json | 2 +- test/union.js | 5 +++-- 4 files changed, 36 insertions(+), 36 deletions(-) delete mode 100644 npm-shrinkwrap.json 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..ca35b64 100644 --- a/package.json +++ b/package.json @@ -29,6 +29,6 @@ "doxme": "^1.4.3" }, "dependencies": { - "jsts": "~0.15.0" + "gh-clipping-algorithm": "^0.1.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(); }); From 3f09400900df2c20bab1dcfc4fb538d6ffbed791 Mon Sep 17 00:00:00 2001 From: Tim Channell Date: Thu, 7 May 2015 15:08:56 -0400 Subject: [PATCH 07/10] added devdep --- package.json | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/package.json b/package.json index ca35b64..9a1b2dc 100644 --- a/package.json +++ b/package.json @@ -26,7 +26,8 @@ "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": { "gh-clipping-algorithm": "^0.1.0" From fff47d3cc1a55ae52df10bcd4895bf4ab53d4410 Mon Sep 17 00:00:00 2001 From: Tim Channell Date: Thu, 7 May 2015 18:15:48 -0400 Subject: [PATCH 08/10] updated GH --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 9a1b2dc..b29a379 100644 --- a/package.json +++ b/package.json @@ -30,6 +30,6 @@ "geojson-equality": "^0.1.4" }, "dependencies": { - "gh-clipping-algorithm": "^0.1.0" + "gh-clipping-algorithm": "^0.2.0" } } From 5b83321f209f9a51a7b5d1408e7776f0d5f27f54 Mon Sep 17 00:00:00 2001 From: Tim Channell Date: Fri, 8 May 2015 14:30:59 -0400 Subject: [PATCH 09/10] updated gh version --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index b29a379..6e2716d 100644 --- a/package.json +++ b/package.json @@ -30,6 +30,6 @@ "geojson-equality": "^0.1.4" }, "dependencies": { - "gh-clipping-algorithm": "^0.2.0" + "gh-clipping-algorithm": "^0.3.0" } } From 7ef346ffadfcaff866a732bceffb22f266b6c5ff Mon Sep 17 00:00:00 2001 From: Tim Channell Date: Wed, 7 Oct 2015 16:44:01 -0400 Subject: [PATCH 10/10] bumped GH --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 8371508..b93c211 100644 --- a/package.json +++ b/package.json @@ -30,6 +30,6 @@ "geojson-equality": "^0.1.4" }, "dependencies": { - "gh-clipping-algorithm": "^0.4" + "gh-clipping-algorithm": "^0.*" } }