Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #820 - @turf/intersect #890

Merged
merged 5 commits into from
Aug 11, 2017
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 7 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,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)));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since truncate does the clone operation, we can drop JSON.stringify from the mix.

var intersection = a.intersection(b);

if (intersection.isEmpty()) {
return undefined;
}
if (intersection.isEmpty()) return null;
Copy link
Member

@DenisCarriere DenisCarriere Aug 4, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Weren't we going to return a Feature<null> instead of null?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry I didn't noticed your suggestion in the issue comment.

Copy link
Member

@DenisCarriere DenisCarriere Aug 5, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No worries 👍

I think we are all in agreement that if we have no results in collection of Features, then we would have an empty FeatureCollection, if the output is a single Feature which would be undefined or null then we would output a Feature<null> that way things don't blow up when you chain TurfJS methods by sending a null as input to another module.


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"
}
}
7 changes: 3 additions & 4 deletions packages/turf-intersect/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

poor lonely var... 😢 he feels old next to all his younger const siblings.

const intersect = require('.');

const directories = {
Expand All @@ -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();
});
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
1 change: 1 addition & 0 deletions packages/turf-intersect/test/out/issue-820.geojson
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
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