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;
}
Best Answer
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.