}
public final Geometry decimateTransformGeneralize(Geometry geometry,
MathTransform transform) throws TransformException {
if (geometry instanceof GeometryCollection) {
GeometryCollection collection = (GeometryCollection) geometry;
final int length = collection.getNumGeometries();
boolean cloned = false;
Class elementType = null;
Geometry[] elements = null;
for (int i = 0; i < length; i++) {
Geometry source = collection.getGeometryN(i);
Geometry generalized = decimateTransformGeneralize(source,
transform);
// lazily handle the case where we need to deep clone
if(generalized != source) {
cloned = true;
if(elements == null) {
elements = new Geometry[collection.getNumGeometries()];
for (int j = 0; j < i; j++) {
Geometry element = source.getGeometryN(j);
elements[j] = element;
accumulateGeometryType(elementType, element);
}
}
}
if(cloned) {
elements[i] = generalized;
elementType = accumulateGeometryType(elementType, generalized);
}
}
if(cloned) {
if(elementType == Point.class) {
Point[] points = new Point[elements.length];
System.arraycopy(elements, 0, points, 0, elements.length);
return collection.getFactory().createMultiPoint(points);
} else if (elementType == LineString.class) {
LineString[] lines = new LineString[elements.length];
System.arraycopy(elements, 0, lines, 0, elements.length);
return collection.getFactory().createMultiLineString(lines);
} else if (elementType == Polygon.class) {
Polygon[] polys = new Polygon[elements.length];
System.arraycopy(elements, 0, polys, 0, elements.length);
return collection.getFactory().createMultiPolygon(polys);
} else {
return collection.getFactory().createGeometryCollection(elements);
}
} else {
return collection;
}
} else if (geometry instanceof Point) {