/** * 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; }
/** * Converts the <code>List</code> to an array. * * @param multiLineStrings the <code>List</code> of MultiLineStrings to convert * @return the <code>List</code> in array format */ public static MultiLineString[] toMultiLineStringArray(Collection multiLineStrings) { MultiLineString[] multiLineStringArray = new MultiLineString[multiLineStrings.size()]; return (MultiLineString[]) multiLineStrings.toArray(multiLineStringArray); }
/** * Converts the <code>List</code> to an array. * * @param multiPoints the <code>List</code> of MultiPoints to convert * @return the <code>List</code> in array format */ public static MultiPoint[] toMultiPointArray(Collection multiPoints) { MultiPoint[] multiPointArray = new MultiPoint[multiPoints.size()]; return (MultiPoint[]) multiPoints.toArray(multiPointArray); }
/** * Converts the <code>List</code> to an array. * * @param multiPolygons the <code>List</code> of MultiPolygons to convert * @return the <code>List</code> in array format */ public static MultiPolygon[] toMultiPolygonArray(Collection multiPolygons) { MultiPolygon[] multiPolygonArray = new MultiPolygon[multiPolygons.size()]; return (MultiPolygon[]) multiPolygons.toArray(multiPolygonArray); }
/** * Converts the <code>List</code> to an array. * * @param polygons the <code>List</code> of Polygons to convert * @return the <code>List</code> in array format */ public static Polygon[] toPolygonArray(Collection polygons) { Polygon[] polygonArray = new Polygon[polygons.size()]; return (Polygon[]) polygons.toArray(polygonArray); }
/** * Converts the <code>List</code> to an array. * * @param lineStrings the <code>List</code> of LineStrings to convert * @return the <code>List</code> in array format */ public static LineString[] toLineStringArray(Collection lineStrings) { LineString[] lineStringArray = new LineString[lineStrings.size()]; return (LineString[]) lineStrings.toArray(lineStringArray); }
/** * Converts the <code>List</code> to an array. * * @param geometries the list of <code>Geometry's</code> to convert * @return the <code>List</code> in array format */ public static Geometry[] toGeometryArray(Collection geometries) { if (geometries == null) return null; Geometry[] geometryArray = new Geometry[geometries.size()]; return (Geometry[]) geometries.toArray(geometryArray); }
/** * Converts the <code>List</code> to an array. * * @param points the <code>List</code> of Points to convert * @return the <code>List</code> in array format */ public static Point[] toPointArray(Collection points) { Point[] pointArray = new Point[points.size()]; return (Point[]) points.toArray(pointArray); }