Geometry[] holes = null;
LinearRing[] holesRing = null;
List<Coordinate> shellCoordinates = null;
List<LinearRing> holesList = null;
GeometryFactory fc = polygonToAddVertex.getFactory();
// cast to polygon and get the exterior boundary.
Polygon polygonGeom = (Polygon) polygonToAddVertex;
Geometry boundary = polygonGeom.getExteriorRing();
Coordinate[] shellClosed = null;
Coordinate[] boundaryCoordinates = boundary.getCoordinates();
// for each neighbor
for (Geometry neighbor : geomNeighborList) {
if (boundary.touches(neighbor)) {
// store the modified boundaryCoordinates for add more vertex if
// there are.
shellCoordinates = addIntersection(boundaryCoordinates, line, fc, neighbor);
boundaryCoordinates = shellCoordinates.toArray(new Coordinate[shellCoordinates.size()]);
shellClosed = boundaryCoordinates;
} else {
shellClosed = boundaryCoordinates;
}
}
holes = getInteriorHoles(polygonGeom);
// if not null, the geometry has holes.
if (holes.length > 0) {
holesList = addIntersectionToHoles(holes, line, fc, geomNeighborList);
holesRing = holesList.toArray(new LinearRing[holesList.size()]);
}
// form a closed linearRing.
shellClosed = GeometryUtil.closeGeometry(shellClosed);
LinearRing shellRing = fc.createLinearRing(shellClosed);
result = fc.createPolygon(shellRing, holesRing);
return result;
}