Skip to content

Instantly share code, notes, and snippets.

@tschaub
Created May 4, 2012 14:21
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tschaub/2595076 to your computer and use it in GitHub Desktop.
Save tschaub/2595076 to your computer and use it in GitHub Desktop.
quick implementation of polygon splitting process
// Execute method for js:split process expects polygon and line geometries as input
run: function(inputs) {
var merger = new LineMerger();
merger.add(inputs.poly._geometry);
merger.add(inputs.line._geometry);
var collection = merger.getMergedLineStrings();
var union = new UnaryUnionOp(collection).union();
var polygonizer = new Polygonizer();
polygonizer.add(union);
var parts = polygonizer.getPolygons();
var candidate, intersection, polys = [];
for (var i=0, ii=parts.size(); i<ii; ++i) {
candidate = geom.Geometry.from_(parts.get(i));
// TODO: ask MD if inputs.poly.overlaps(candidate) is expected to work here
try {
intersection = candidate.intersection(inputs.poly);
// TODO: unhack this
if (intersection.area / candidate.area > 0.01) {
polys.push(candidate);
}
} catch (err) {
// pass
}
}
return {result: new geom.MultiPolygon(polys)};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment