Basically, you need to create a new Polygon
from the ExteriorRing
of the input Polygon
.
gf.createPolygon(p.getExteriorRing().getCoordinateSequence());
There is a little more work with MultiPolygon
s as you have to handle each subpolygon in turn. The whole method becomes something like:
static GeometryFactory gf = new GeometryFactory();
static public Geometry removeHoles(Geometry g) {
if (g.getGeometryType().equalsIgnoreCase("Polygon")) {
Polygon p = (Polygon) g;
return gf.createPolygon(p.getExteriorRing().getCoordinateSequence());
} else if (g.getGeometryType().equalsIgnoreCase("MultiPolygon")) {
MultiPolygon mp = (MultiPolygon) g;
List<Polygon> polys = new ArrayList<>();
for (int i = 0; i < mp.getNumGeometries(); i++) {
Polygon poly = gf.createPolygon(((Polygon) mp.getGeometryN(i)).getExteriorRing().getCoordinateSequence());
polys.add(poly);
}
return gf.createMultiPolygon(polys.toArray(new Polygon[] {}));
}
return g;
}