-
Notifications
You must be signed in to change notification settings - Fork 943
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
Changes from 2 commits
ea9551d
9297684
fd592c0
2b02211
3a30d12
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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. | ||
|
@@ -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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Weren't we going to return a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sorry I didn't noticed your suggestion in the issue comment. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
|
||
var writer = new jsts.io.GeoJSONWriter(); | ||
|
||
var geojsonGeometry = writer.write(intersection); | ||
return { | ||
type: 'Feature', | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -34,6 +34,7 @@ | |
"write-json-file": "^2.0.0" | ||
}, | ||
"dependencies": { | ||
"@turf/truncate": "^4.6.0", | ||
"jsts": "1.3.0" | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. poor lonely |
||
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(); | ||
}); |
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 | ||
] | ||
] | ||
] | ||
} | ||
} | ||
] | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
null |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since
truncate
does theclone
operation, we can dropJSON.stringify
from the mix.