/** * Build an appropriate <code>Geometry</code>, <code>MultiGeometry</code>, or <code> * GeometryCollection</code> to contain the <code>Geometry</code>s in it. For example:<br> * * <ul> * <li>If <code>geomList</code> contains a single <code>Polygon</code>, the <code>Polygon</code> * is returned. * <li>If <code>geomList</code> contains several <code>Polygon</code>s, a <code>MultiPolygon * </code> is returned. * <li>If <code>geomList</code> contains some <code>Polygon</code>s and some <code>LineString * </code>s, a <code>GeometryCollection</code> is returned. * <li>If <code>geomList</code> is empty, an empty <code>GeometryCollection</code> is returned * </ul> * * Note that this method does not "flatten" Geometries in the input, and hence if any * MultiGeometries are contained in the input a GeometryCollection containing them will be * returned. * * @param geomList the <code>Geometry</code>s to combine * @return a <code>Geometry</code> of the "smallest", "most type-specific" class that can contain * the elements of <code>geomList</code> . */ public Geometry buildGeometry(Collection geomList) { /** Determine some facts about the geometries in the list */ Class geomClass = null; boolean isHeterogeneous = false; boolean hasGeometryCollection = false; for (Iterator i = geomList.iterator(); i.hasNext(); ) { Geometry geom = (Geometry) i.next(); Class partClass = geom.getClass(); if (geomClass == null) { geomClass = partClass; } if (partClass != geomClass) { isHeterogeneous = true; } if (geom instanceof GeometryCollection) hasGeometryCollection = true; } /** Now construct an appropriate geometry to return */ // for the empty geometry, return an empty GeometryCollection if (geomClass == null) { return createGeometryCollection(null); } if (isHeterogeneous || hasGeometryCollection) { return createGeometryCollection(toGeometryArray(geomList)); } // at this point we know the collection is hetereogenous. // Determine the type of the result from the first Geometry in the list // this should always return a geometry, since otherwise an empty collection would have already // been returned Geometry geom0 = (Geometry) geomList.iterator().next(); boolean isCollection = geomList.size() > 1; if (isCollection) { if (geom0 instanceof Polygon) { return createMultiPolygon(toPolygonArray(geomList)); } else if (geom0 instanceof LineString) { return createMultiLineString(toLineStringArray(geomList)); } else if (geom0 instanceof Point) { return createMultiPoint(toPointArray(geomList)); } Assert.shouldNeverReachHere("Unhandled class: " + geom0.getClass().getName()); } return geom0; }
public final Geometry transform(Geometry inputGeom) { this.inputGeom = inputGeom; this.factory = inputGeom.getFactory(); if (inputGeom instanceof Point) return transformPoint((Point) inputGeom, null); if (inputGeom instanceof MultiPoint) return transformMultiPoint((MultiPoint) inputGeom, null); if (inputGeom instanceof LinearRing) return transformLinearRing((LinearRing) inputGeom, null); if (inputGeom instanceof LineString) return transformLineString((LineString) inputGeom, null); if (inputGeom instanceof MultiLineString) return transformMultiLineString((MultiLineString) inputGeom, null); if (inputGeom instanceof Polygon) return transformPolygon((Polygon) inputGeom, null); if (inputGeom instanceof MultiPolygon) return transformMultiPolygon((MultiPolygon) inputGeom, null); if (inputGeom instanceof GeometryCollection) return transformGeometryCollection((GeometryCollection) inputGeom, null); throw new IllegalArgumentException( "Unknown Geometry subtype: " + inputGeom.getClass().getName()); }