Source:
private Geometry intersect(Geometry geometry, Geometry mask) { if (geometry instanceof GeometryCollection) { if (geometry.getNumGeometries() == 1) { geometry = geometry.getGeometryN(0); } else { List<Geometry> elements = new ArrayList<>(); String geometryType = geometry.getGeometryN(0).getGeometryType(); for (int i = 0; i < geometry.getNumGeometries(); i++) { Geometry g = geometry.getGeometryN(i); if (g.getEnvelopeInternal().intersects(mask.getEnvelopeInternal())) { Geometry intersected = intersect(g, mask); if (intersected != null) { if(intersected.getGeometryType().equals(geometryType)){ elements.add(intersected); } if(intersected instanceof GeometryCollection){ addGeometries(elements, (GeometryCollection) intersected, geometryType); } } } } if (elements.size() == 0) { return null; } if(geometry instanceof MultiPoint) { Point[] array = elements.toArray(new Point[elements.size()]); return geometry.getFactory().createMultiPoint(array); } else if (geometry instanceof MultiLineString) { LineString[] array = elements.toArray(new LineString[elements.size()]); return geometry.getFactory().createMultiLineString(array); } else if (geometry instanceof MultiPolygon) { Polygon[] array = elements.toArray(new Polygon[elements.size()]); return geometry.getFactory().createMultiPolygon(array); } else { Geometry[] array = elements.toArray(new Geometry[elements.size()]); return geometry.getFactory().createGeometryCollection(array); } } } Geometry result; try { result = geometry.intersection(mask); } catch(Exception e1) { try { result = EnhancedPrecisionOp.intersection(geometry, mask); } catch (Exception e2) { result = geometry; } } if (result.isEmpty() && geometry.intersects(mask)) { try { result = EnhancedPrecisionOp.intersection(geometry, mask); } catch (Exception e2) { result = geometry; } } if (result.isEmpty()) { return null; } else { return result; } }

Target:
private Geometry intersect(Geometry geometry, Geometry mask) { if (geometry instanceof GeometryCollection) { if (geometry.getNumGeometries() == 1) { geometry = geometry.getGeometryN(0); } else { List<Geometry> elements = new ArrayList<>(); String geometryType = geometry.getGeometryN(0).getGeometryType(); for (int i = 0; i < geometry.getNumGeometries(); i++) { Geometry g = geometry.getGeometryN(i); if (g.getEnvelopeInternal().intersects(mask.getEnvelopeInternal())) { Geometry intersected = intersect(g, mask); if (intersected != null) { if (intersected.getGeometryType().equals(geometryType)) { elements.add(intersected); } else if (intersected instanceof GeometryCollection) { addGeometries(elements, (GeometryCollection) intersected, geometryType); } } } } if (elements.size() == 0) { return null; } if(geometry instanceof MultiPoint) { Point[] array = elements.toArray(new Point[elements.size()]); return geometry.getFactory().createMultiPoint(array); } else if (geometry instanceof MultiLineString) { LineString[] array = elements.toArray(new LineString[elements.size()]); return geometry.getFactory().createMultiLineString(array); } else if (geometry instanceof MultiPolygon) { Polygon[] array = elements.toArray(new Polygon[elements.size()]); return geometry.getFactory().createMultiPolygon(array); } else { Geometry[] array = elements.toArray(new Geometry[elements.size()]); return geometry.getFactory().createGeometryCollection(array); } } } Geometry result; try { result = geometry.intersection(mask); } catch(Exception e1) { try { result = EnhancedPrecisionOp.intersection(geometry, mask); } catch (Exception e2) { result = geometry; } } if (result.isEmpty() && geometry.intersects(mask)) { try { result = EnhancedPrecisionOp.intersection(geometry, mask); } catch (Exception e2) { result = geometry; } } if (result.isEmpty()) { return null; } else { return result; } }