Skip to content

Commit

Permalink
Merge pull request #890 from Turfjs/intersect-issue-820
Browse files Browse the repository at this point in the history
Fixes #820 - `@turf/intersect`
  • Loading branch information
DenisCarriere authored Aug 11, 2017
2 parents f853637 + 3a30d12 commit dd4d365
Show file tree
Hide file tree
Showing 14 changed files with 219 additions and 77 deletions.
22 changes: 11 additions & 11 deletions packages/turf-intersect/index.js
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -34,22 +35,21 @@ 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(truncate(geom1));
var b = reader.read(truncate(geom2));
var intersection = a.intersection(b);

if (intersection.isEmpty()) {
return undefined;
}
if (intersection.isEmpty()) return {
type: 'Feature',
properties: {},
geometry: null
};

var writer = new jsts.io.GeoJSONWriter();

var geojsonGeometry = writer.write(intersection);
return {
type: 'Feature',
Expand Down
1 change: 1 addition & 0 deletions packages/turf-intersect/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"write-json-file": "^2.0.0"
},
"dependencies": {
"@turf/truncate": "^4.6.0",
"jsts": "1.3.0"
}
}
6 changes: 2 additions & 4 deletions packages/turf-intersect/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,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();
});
123 changes: 123 additions & 0 deletions packages/turf-intersect/test/in/issue-820.geojson
Original file line number Diff line number Diff line change
@@ -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
]
]
]
}
}
]
}
36 changes: 18 additions & 18 deletions packages/turf-intersect/test/out/Intersect1.geojson
Original file line number Diff line number Diff line change
Expand Up @@ -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
]
]
]
Expand Down
28 changes: 14 additions & 14 deletions packages/turf-intersect/test/out/Intersect2.geojson
Original file line number Diff line number Diff line change
Expand Up @@ -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
]
]
]
Expand Down
24 changes: 12 additions & 12 deletions packages/turf-intersect/test/out/issue-412.geojson
Original file line number Diff line number Diff line change
Expand Up @@ -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
]
]
]
Expand Down
5 changes: 5 additions & 0 deletions packages/turf-intersect/test/out/issue-820.geojson
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"type": "Feature",
"properties": {},
"geometry": null
}
8 changes: 4 additions & 4 deletions packages/turf-intersect/test/out/linestring.geojson
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
"type": "LineString",
"coordinates": [
[
4.86544132232666,
45.78434474634739
4.865441,
45.784345
],
[
4.88217830657959,
45.78745711798122
4.882178,
45.787457
]
]
}
Expand Down
16 changes: 8 additions & 8 deletions packages/turf-intersect/test/out/multilinestring.geojson
Original file line number Diff line number Diff line change
Expand Up @@ -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
]
]
]
Expand Down
Loading

0 comments on commit dd4d365

Please sign in to comment.