-
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
turf-union, turf-difference, turf-intersect: Swap out martinez-polygon-clipping for polygon-clipping used in v7. #1916
Merged
Merged
Changes from 7 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
4b29fbb
swap out martinez-polygon-clipping for polygon-clipping
mbullington 9e3cbe7
reinstate single polygon return on turf-difference
ngottlieb 48fffab
reinstate single polygon return for turf-intersect
ngottlieb 54f1bf4
remove @ts-ignore from intersect and union
ngottlieb a64a24a
reinstate single polygon return for turf-union
ngottlieb eac392b
remove leftover comment on turf-intersect type error
ngottlieb bce1a54
Merge pull request #1 from ngottlieb/ngottlieb/requested-changes
mbullington 25c97ca
Merge branch 'master' into master
mfedderly File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -118,4 +118,4 @@ | |
} | ||
} | ||
] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
import { Feature, multiPolygon, MultiPolygon, polygon, Polygon, Properties } from "@turf/helpers"; | ||
import { getGeom } from "@turf/invariant"; | ||
import * as martinez from "martinez-polygon-clipping"; | ||
import polygonClipping from 'polygon-clipping'; | ||
|
||
/** | ||
* Takes two {@link Polygon|polygon} or {@link MultiPolygon|multi-polygon} geometries and | ||
|
@@ -47,48 +47,9 @@ export default function intersect<P = Properties>( | |
): Feature<Polygon | MultiPolygon, P> | null { | ||
const geom1 = getGeom(poly1); | ||
const geom2 = getGeom(poly2); | ||
|
||
if (geom1.type === "Polygon" && geom2.type === "Polygon") { | ||
const intersection: any = martinez.intersection(geom1.coordinates, geom2.coordinates); | ||
|
||
if (intersection === null || intersection.length === 0) { return null; } | ||
if (intersection.length === 1) { | ||
const start = intersection[0][0][0]; | ||
const end = intersection[0][0][intersection[0][0].length - 1]; | ||
if (start[0] === end[0] && start[1] === end[1]) { return polygon(intersection[0], options.properties); } | ||
return null; | ||
} | ||
return multiPolygon(intersection, options.properties); | ||
|
||
} else if (geom1.type === "MultiPolygon") { | ||
let resultCoords: any[] = []; | ||
|
||
// iterate through the polygon and run intersect with each part, adding to the resultCoords. | ||
for (const coords of geom1.coordinates) { | ||
const subGeom = getGeom(polygon(coords)); | ||
const subIntersection = intersect(subGeom, geom2); | ||
|
||
if (subIntersection) { | ||
const subIntGeom = getGeom(subIntersection); | ||
|
||
if (subIntGeom.type === "Polygon") { resultCoords.push(subIntGeom.coordinates); | ||
} else if (subIntGeom.type === "MultiPolygon") { resultCoords = resultCoords.concat(subIntGeom.coordinates); | ||
} else { throw new Error("intersection is invalid"); } | ||
} | ||
} | ||
|
||
// Make a polygon with the result | ||
if (resultCoords.length === 0) { return null; } | ||
if (resultCoords.length === 1) { return polygon(resultCoords[0], options.properties); | ||
} else { return multiPolygon(resultCoords, options.properties); } | ||
|
||
} else if (geom2.type === "MultiPolygon") { | ||
// geom1 is a polygon and geom2 a multiPolygon, | ||
// put the multiPolygon first and fallback to the previous case. | ||
return intersect(geom2, geom1); | ||
|
||
} else { | ||
// handle invalid geometry types | ||
throw new Error("poly1 and poly2 must be either polygons or multiPolygons"); | ||
} | ||
|
||
const intersection = polygonClipping.intersection(geom1.coordinates as any, geom2.coordinates as any); | ||
if (intersection.length === 0) 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. Same comment here about trying to return a |
||
if (intersection.length === 1) return polygon(intersection[0], options.properties); | ||
return multiPolygon(intersection, options.properties); | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Can we keep this
Polygon
return? Seems like quite a bit of the tests changed just because of that. Otherwise we should change the types and jsdoc to match the new implementation.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.
In inclined to agree with @mfedderly - it's kind of annoying when things get returned as a
multipolygon
when they aren't