Created
May 21, 2014 18:25
-
-
Save anonymous/d9b9552df056a6773ad5 to your computer and use it in GitHub Desktop.
Fast polygon self-intersection in javascript
This file contains hidden or 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
/** | |
* findSelfIntersects | |
* | |
* Detect self-intersections in a polygon. | |
* | |
* @param {object} GeoJSON polygon co-ordinates. | |
* @return {array} array of points of intersections. | |
*/ | |
var findSelfIntersects = function(geoJsonPolygon) { | |
var coordinates = geoJSON2JTS(geoJsonPolygon); | |
var geometryFactory = new jsts.geom.GeometryFactory(); | |
var shell = geometryFactory.createLinearRing(coordinates); | |
var jstsPolygon = geometryFactory.createPolygon(shell); | |
// if the geometry is aleady a simple linear ring, do not | |
// try to find self intersection points. | |
var validator = new jsts.operation.IsSimpleOp(jstsPolygon); | |
if (validator.isSimpleLinearGeometry(jstsPolygon)) { | |
return; | |
} | |
var res = []; | |
var graph = new jsts.geomgraph.GeometryGraph(0, jstsPolygon); | |
var cat = new jsts.operation.valid.ConsistentAreaTester(graph); | |
var r = cat.isNodeConsistentArea(); | |
if (!r) { | |
var pt = cat.getInvalidPoint(); | |
res.push([pt.x, pt.y]); | |
} | |
return res; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment