1

I have a set of polygons without holes. I wish to merge any intersecting polygons. I don't want to have any holes in my simplified polygons. Is there a recommended way to achieve this using JTS?

The (naive) algorithm I am using now is:

private List<com.vividsolutions.jts.geom.Polygon> simplify(List<com.vividsolutions.jts.geom.Polygon> polygons) {
boolean done;
do {
done = true;
for (int i = 0; i < polygons.size(); i++) {
    Polygon a = polygons.get(i);
    if (0 != a.getNumInteriorRing()) {
        throw new RuntimeException();
    }
    for (int j = i + 1; j < polygons.size();) {
        final Polygon b = polygons.get(j);
        if (a.intersects(b)) {
            polygons.set(i, (Polygon) a.union(b));
            a = polygons.get(i);
            polygons.remove(j);
            done = false;
        }
        else {
            j++;
        }
    }
}
} while (!done);
return polygons;
}
2

Get your all intersecting polygons into a geometry collection (ArrayList) and use JTS UnaryUnion method to merge all polygons at one go. Then convert your geometry into JTS Polygon and use its getExteriorRing() method to remove holes.

  • Note that you may need to iterate over the components of a MultiPolygon to get all the exterior rings, depending on the output. – dbaston Apr 17 '14 at 13:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.