From ea9551d2bbe746f01754335998238b8b6dc27bb8 Mon Sep 17 00:00:00 2001 From: stebogit Date: Thu, 3 Aug 2017 08:50:35 -0700 Subject: [PATCH 1/3] return null, truncated input poly --- packages/turf-intersect/index.js | 18 +-- packages/turf-intersect/package.json | 1 + packages/turf-intersect/test.js | 7 +- .../turf-intersect/test/in/issue-820.geojson | 123 ++++++++++++++++++ .../test/out/Intersect1.geojson | 36 ++--- .../test/out/Intersect2.geojson | 28 ++-- .../turf-intersect/test/out/issue-412.geojson | 24 ++-- .../turf-intersect/test/out/issue-820.geojson | 1 + .../test/out/linestring.geojson | 8 +- .../test/out/multilinestring.geojson | 16 +-- .../test/out/multipoint.geojson | 8 +- .../test/out/no-overlap.geojson | 1 + .../turf-intersect/test/out/point.geojson | 4 +- packages/turf-intersect/yarn.lock | 10 ++ 14 files changed, 208 insertions(+), 77 deletions(-) create mode 100644 packages/turf-intersect/test/in/issue-820.geojson create mode 100644 packages/turf-intersect/test/out/issue-820.geojson create mode 100644 packages/turf-intersect/test/out/no-overlap.geojson diff --git a/packages/turf-intersect/index.js b/packages/turf-intersect/index.js index 9a53b94663..280f409dd3 100644 --- a/packages/turf-intersect/index.js +++ b/packages/turf-intersect/index.js @@ -1,5 +1,6 @@ // depend on jsts for now http://bjornharrtell.github.io/jsts/ var jsts = require('jsts'); +var truncate = require('@turf/truncate'); /** * Takes two {@link Polygon|polygons} and finds their intersection. If they share a border, returns the border; if they don't intersect, returns undefined. @@ -34,22 +35,17 @@ var jsts = require('jsts'); * var addToMap = [poly1, poly2, 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 geom1 = (poly1.type === 'Feature') ? poly1.geometry : poly1; + var geom2 = (poly2.type === 'Feature') ? poly2.geometry : poly2; + var reader = new jsts.io.GeoJSONReader(); - var a = reader.read(JSON.stringify(geom1)); - var b = reader.read(JSON.stringify(geom2)); + var a = reader.read(JSON.stringify(truncate(geom1))); + var b = reader.read(JSON.stringify(truncate(geom2))); var intersection = a.intersection(b); - if (intersection.isEmpty()) { - return undefined; - } + if (intersection.isEmpty()) return null; var writer = new jsts.io.GeoJSONWriter(); - var geojsonGeometry = writer.write(intersection); return { type: 'Feature', diff --git a/packages/turf-intersect/package.json b/packages/turf-intersect/package.json index b9fc0fc2f6..f2aa84c298 100644 --- a/packages/turf-intersect/package.json +++ b/packages/turf-intersect/package.json @@ -34,6 +34,7 @@ "write-json-file": "^2.0.0" }, "dependencies": { + "@turf/truncate": "^4.6.0", "jsts": "1.3.0" } } diff --git a/packages/turf-intersect/test.js b/packages/turf-intersect/test.js index 5cc0eb3d9a..f5a0ddeced 100644 --- a/packages/turf-intersect/test.js +++ b/packages/turf-intersect/test.js @@ -3,6 +3,7 @@ const fs = require('fs'); const test = require('tape'); const load = require('load-json-file'); const write = require('write-json-file'); +var truncate = require('@turf/truncate'); const intersect = require('.'); const directories = { @@ -23,10 +24,8 @@ test('intersect', t => { const features = geojson.features; const result = intersect(features[0], features[1]); - if (result) { - if (process.env.REGEN) write.sync(directories.out + filename, result); - t.deepEqual(result, load.sync(directories.out + filename), name); - } + if (process.env.REGEN) write.sync(directories.out + filename, result); + t.deepEqual(result, load.sync(directories.out + filename), name); } t.end(); }); diff --git a/packages/turf-intersect/test/in/issue-820.geojson b/packages/turf-intersect/test/in/issue-820.geojson new file mode 100644 index 0000000000..17aad6cc26 --- /dev/null +++ b/packages/turf-intersect/test/in/issue-820.geojson @@ -0,0 +1,123 @@ +{ + "type": "FeatureCollection", + "features": [ + { + "type": "Feature", + "properties": {}, + "geometry": { + "type": "Polygon", + "coordinates": [ + [ + [ + 43.741513133300735, + 56.20222135938059 + ], + [ + 43.74237144018551, + 56.18856400988486 + ], + [ + 43.77756202246089, + 56.187608768581725 + ], + [ + 43.77927863623042, + 56.19639609165836 + ], + [ + 43.763142466796836, + 56.20212587032091 + ], + [ + 43.741513133300735, + 56.20222135938059 + ] + ] + ] + } + }, + { + "type": "Feature", + "properties": {}, + "geometry": { + "type": "Polygon", + "coordinates": [ + [ + [ + 43.970066430113974, + 56.332893840607994 + ], + [ + 43.970066430115764, + 56.33289386305967 + ], + [ + 43.96907396330773, + 56.33216050512385 + ], + [ + 43.96712784882449, + 56.3329676824026 + ], + [ + 43.9675997442062, + 56.33331243182769 + ], + [ + 43.96955706305079, + 56.33351088302814 + ], + [ + 43.96958194836136, + 56.33343379993835 + ], + [ + 43.969613521760365, + 56.3333626110674 + ], + [ + 43.96964739754773, + 56.3333044427381 + ], + [ + 43.96968537953426, + 56.3332457040905 + ], + [ + 43.96973157401614, + 56.333186395086614 + ], + [ + 43.969791873109784, + 56.333117882577056 + ], + [ + 43.96983217547703, + 56.3330786121781 + ], + [ + 43.969883502771644, + 56.3330312788475 + ], + [ + 43.969928670817815, + 56.332993640236595 + ], + [ + 43.96997589197615, + 56.33295771241299 + ], + [ + 43.97002822123187, + 56.332919492368774 + ], + [ + 43.970066430113974, + 56.332893840607994 + ] + ] + ] + } + } + ] +} \ No newline at end of file diff --git a/packages/turf-intersect/test/out/Intersect1.geojson b/packages/turf-intersect/test/out/Intersect1.geojson index aa44fa0f60..c3b6bdc9bd 100644 --- a/packages/turf-intersect/test/out/Intersect1.geojson +++ b/packages/turf-intersect/test/out/Intersect1.geojson @@ -6,40 +6,40 @@ "coordinates": [ [ [ - -79.94623496447946, - 32.89900638172028 + -79.94623485028362, + 32.89900648336843 ], [ - -79.88571166992188, - 32.887659962078956 + -79.885712, + 32.88766 ], [ - -79.89395141601562, - 32.75551989829049 + -79.893951, + 32.75552 ], [ - -79.92322780260464, - 32.73910022106017 + -79.92322798892725, + 32.739099979427955 ], [ - -79.93789672851562, - 32.74108223150125 + -79.937897, + 32.741082 ], [ - -79.93034362792969, - 32.76475877693074 + -79.930344, + 32.764759 ], [ - -79.97360229492188, - 32.76071688548088 + -79.973602, + 32.760717 ], [ - -79.97428894042969, - 32.83690450361482 + -79.974289, + 32.836905 ], [ - -79.94623496447946, - 32.89900638172028 + -79.94623485028362, + 32.89900648336843 ] ] ] diff --git a/packages/turf-intersect/test/out/Intersect2.geojson b/packages/turf-intersect/test/out/Intersect2.geojson index cc13840a35..a679305063 100644 --- a/packages/turf-intersect/test/out/Intersect2.geojson +++ b/packages/turf-intersect/test/out/Intersect2.geojson @@ -6,32 +6,32 @@ "coordinates": [ [ [ - -80.0066252126598, - 32.84617770697059 + -80.00662505026214, + 32.84617737765197 ], [ - -80.01686096191406, - 32.87266705436184 + -80.016861, + 32.872667 ], [ - -80.068359375, - 32.88189375925038 + -80.068359, + 32.881894 ], [ - -80.0050160405751, - 32.91295307720083 + -80.0050161842375, + 32.91295285230695 ], [ - -79.98184204101562, - 32.90495631913751 + -79.981842, + 32.904956 ], [ - -79.99351501464844, - 32.84440429734253 + -79.993515, + 32.844404 ], [ - -80.0066252126598, - 32.84617770697059 + -80.00662505026214, + 32.84617737765197 ] ] ] diff --git a/packages/turf-intersect/test/out/issue-412.geojson b/packages/turf-intersect/test/out/issue-412.geojson index 9965d97db6..f7bb5c0178 100644 --- a/packages/turf-intersect/test/out/issue-412.geojson +++ b/packages/turf-intersect/test/out/issue-412.geojson @@ -6,28 +6,28 @@ "coordinates": [ [ [ - 11.076136797048882, - 9.856269774244707 + 11.076137, + 9.85627 ], [ - 11.1687361088318, - 9.855073034114678 + 11.16873572210611, + 9.855073127840765 ], [ - 11.167525294018606, - 9.85386305739084 + 11.16752475470916, + 9.853863 ], [ - 11.102599853580063, - 9.85386305739084 + 11.102599666666666, + 9.853863 ], [ - 11.076136797048884, - 9.856269774244707 + 11.076138743097555, + 9.856269454758179 ], [ - 11.076136797048882, - 9.856269774244707 + 11.076137, + 9.85627 ] ] ] diff --git a/packages/turf-intersect/test/out/issue-820.geojson b/packages/turf-intersect/test/out/issue-820.geojson new file mode 100644 index 0000000000..ec747fa47d --- /dev/null +++ b/packages/turf-intersect/test/out/issue-820.geojson @@ -0,0 +1 @@ +null \ No newline at end of file diff --git a/packages/turf-intersect/test/out/linestring.geojson b/packages/turf-intersect/test/out/linestring.geojson index b1a93b137b..da4c25c7d1 100644 --- a/packages/turf-intersect/test/out/linestring.geojson +++ b/packages/turf-intersect/test/out/linestring.geojson @@ -5,12 +5,12 @@ "type": "LineString", "coordinates": [ [ - 4.86544132232666, - 45.78434474634739 + 4.865441, + 45.784345 ], [ - 4.88217830657959, - 45.78745711798122 + 4.882178, + 45.787457 ] ] } diff --git a/packages/turf-intersect/test/out/multilinestring.geojson b/packages/turf-intersect/test/out/multilinestring.geojson index 26277ced2d..c53540a84d 100644 --- a/packages/turf-intersect/test/out/multilinestring.geojson +++ b/packages/turf-intersect/test/out/multilinestring.geojson @@ -6,22 +6,22 @@ "coordinates": [ [ [ - 4.86544132232666, - 45.78434474634739 + 4.865441, + 45.784345 ], [ - 4.88217830657959, - 45.78745711798122 + 4.882178, + 45.787457 ] ], [ [ - 4.860033988952637, - 45.78213006841216 + 4.860034, + 45.78213 ], [ - 4.8618364334106445, - 45.784404601286774 + 4.861836, + 45.784405 ] ] ] diff --git a/packages/turf-intersect/test/out/multipoint.geojson b/packages/turf-intersect/test/out/multipoint.geojson index 6901c49514..f2c6612cd0 100644 --- a/packages/turf-intersect/test/out/multipoint.geojson +++ b/packages/turf-intersect/test/out/multipoint.geojson @@ -5,12 +5,12 @@ "type": "MultiPoint", "coordinates": [ [ - 4.845099449157715, - 45.77793989651294 + 4.845099, + 45.77794 ], [ - 4.854154586791992, - 45.77249220227275 + 4.854155, + 45.772492 ] ] } diff --git a/packages/turf-intersect/test/out/no-overlap.geojson b/packages/turf-intersect/test/out/no-overlap.geojson new file mode 100644 index 0000000000..ec747fa47d --- /dev/null +++ b/packages/turf-intersect/test/out/no-overlap.geojson @@ -0,0 +1 @@ +null \ No newline at end of file diff --git a/packages/turf-intersect/test/out/point.geojson b/packages/turf-intersect/test/out/point.geojson index a228f8a6dd..01f92ae67c 100644 --- a/packages/turf-intersect/test/out/point.geojson +++ b/packages/turf-intersect/test/out/point.geojson @@ -4,8 +4,8 @@ "geometry": { "type": "Point", "coordinates": [ - 4.859991073608398, - 45.784614093068555 + 4.859991, + 45.784614 ] } } diff --git a/packages/turf-intersect/yarn.lock b/packages/turf-intersect/yarn.lock index 39bbf716c0..506251e4e6 100644 --- a/packages/turf-intersect/yarn.lock +++ b/packages/turf-intersect/yarn.lock @@ -2,6 +2,16 @@ # yarn lockfile v1 +"@turf/meta@^4.6.0": + version "4.6.0" + resolved "https://registry.yarnpkg.com/@turf/meta/-/meta-4.6.0.tgz#0d3f9a218e58d1c5e5deedf467c3321dd61203f3" + +"@turf/truncate@^4.6.0": + version "4.6.0" + resolved "https://registry.yarnpkg.com/@turf/truncate/-/truncate-4.6.0.tgz#903ad5ed34c0b851b93529975eb8e2eb769a561f" + dependencies: + "@turf/meta" "^4.6.0" + balanced-match@^1.0.0: version "1.0.0" resolved "https://registry.yarnpkg.com/balanced-match/-/balanced-match-1.0.0.tgz#89b4d199ab2bee49de164ea02b89ce462d71b767" From fd592c0473e68c490e77056f08783b5419ab1e48 Mon Sep 17 00:00:00 2001 From: Denis Date: Fri, 4 Aug 2017 15:41:37 -0400 Subject: [PATCH 2/3] Add Feature & drop extra JSON.stringify --- packages/turf-intersect/index.js | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/packages/turf-intersect/index.js b/packages/turf-intersect/index.js index 280f409dd3..d8d8da86d3 100644 --- a/packages/turf-intersect/index.js +++ b/packages/turf-intersect/index.js @@ -39,11 +39,15 @@ module.exports = function (poly1, poly2) { var geom2 = (poly2.type === 'Feature') ? poly2.geometry : poly2; var reader = new jsts.io.GeoJSONReader(); - var a = reader.read(JSON.stringify(truncate(geom1))); - var b = reader.read(JSON.stringify(truncate(geom2))); + var a = reader.read(truncate(geom1)); + var b = reader.read(truncate(geom2)); var intersection = a.intersection(b); - if (intersection.isEmpty()) return null; + if (intersection.isEmpty()) return { + type: 'Feature', + properties: {}, + geometry: null + }; var writer = new jsts.io.GeoJSONWriter(); var geojsonGeometry = writer.write(intersection); From 2b02211d9dd8bc0064fec94d2f110f29717ac5d6 Mon Sep 17 00:00:00 2001 From: Denis Date: Sat, 5 Aug 2017 17:39:50 -0400 Subject: [PATCH 3/3] Output Feature to tests --- packages/turf-intersect/test.js | 1 - packages/turf-intersect/test/out/issue-820.geojson | 6 +++++- packages/turf-intersect/test/out/no-overlap.geojson | 6 +++++- 3 files changed, 10 insertions(+), 3 deletions(-) diff --git a/packages/turf-intersect/test.js b/packages/turf-intersect/test.js index f5a0ddeced..42a89b6a4c 100644 --- a/packages/turf-intersect/test.js +++ b/packages/turf-intersect/test.js @@ -3,7 +3,6 @@ const fs = require('fs'); const test = require('tape'); const load = require('load-json-file'); const write = require('write-json-file'); -var truncate = require('@turf/truncate'); const intersect = require('.'); const directories = { diff --git a/packages/turf-intersect/test/out/issue-820.geojson b/packages/turf-intersect/test/out/issue-820.geojson index ec747fa47d..d18182872c 100644 --- a/packages/turf-intersect/test/out/issue-820.geojson +++ b/packages/turf-intersect/test/out/issue-820.geojson @@ -1 +1,5 @@ -null \ No newline at end of file +{ + "type": "Feature", + "properties": {}, + "geometry": null +} diff --git a/packages/turf-intersect/test/out/no-overlap.geojson b/packages/turf-intersect/test/out/no-overlap.geojson index ec747fa47d..d18182872c 100644 --- a/packages/turf-intersect/test/out/no-overlap.geojson +++ b/packages/turf-intersect/test/out/no-overlap.geojson @@ -1 +1,5 @@ -null \ No newline at end of file +{ + "type": "Feature", + "properties": {}, + "geometry": null +}