001: /*
002: * The Unified Mapping Platform (JUMP) is an extensible, interactive GUI
003: * for visualizing and manipulating spatial features with geometry and attributes.
004: *
005: * Copyright (C) 2003 Vivid Solutions
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License
009: * as published by the Free Software Foundation; either version 2
010: * of the License, or (at your option) any later version.
011: *
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
015: * GNU General Public License for more details.
016: *
017: * You should have received a copy of the GNU General Public License
018: * along with this program; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
020: *
021: * For more information, contact:
022: *
023: * Vivid Solutions
024: * Suite #1A
025: * 2328 Government Street
026: * Victoria BC V8T 5G5
027: * Canada
028: *
029: * (250)385-6040
030: * www.vividsolutions.com
031: */
032:
033: package com.vividsolutions.jump.geom;
034:
035: import java.util.*;
036:
037: import com.vividsolutions.jts.geom.*;
038:
039: /**
040: * Some utility functions not present in the JTS version of this class
041: */
042: public class GeometryFactoryUtil {
043: public GeometryFactoryUtil() {
044: }
045:
046: /**
047: * Builds a geometry containing only the element geometries from the
048: * input list which have the requested dimension.
049: * The result is thus guaranteed to have the dimension requested.
050: *
051: * @param geom
052: * @param dimension
053: */
054: public static Geometry buildGeometry(Geometry geom, int dimension) {
055: GeometryFactory factory = new GeometryFactory(geom
056: .getPrecisionModel(), geom.getSRID());
057:
058: if (geom instanceof GeometryCollection) {
059: List geomList = dimensionFilter((GeometryCollection) geom,
060: dimension);
061:
062: if (geomList.isEmpty()) {
063: return getEmptyDimensionalGeometry(factory, dimension);
064: }
065:
066: return factory.buildGeometry(geomList);
067: } else if (geom.getDimension() == dimension) {
068: return geom;
069: } else {
070: return getEmptyDimensionalGeometry(factory, dimension);
071: }
072: }
073:
074: public static List dimensionFilter(GeometryCollection gc,
075: int dimension) {
076: List geomList = new ArrayList();
077:
078: for (Iterator i = new GeometryCollectionIterator(gc); i
079: .hasNext();) {
080: Geometry g = (Geometry) i.next();
081:
082: if (!(g instanceof GeometryCollection)) {
083: if (g.getDimension() == dimension) {
084: geomList.add(g.clone());
085: }
086: }
087: }
088:
089: return geomList;
090: }
091:
092: public static Geometry getEmptyDimensionalGeometry(
093: GeometryFactory factory, int dimension) {
094: switch (dimension) {
095: case 0:
096: return factory.createMultiPoint(new Coordinate[0]);
097:
098: case 1:
099: return factory.createMultiLineString(new LineString[0]);
100:
101: case 2:
102: return factory.createMultiPolygon(new Polygon[0]);
103: }
104:
105: return factory.createGeometryCollection(new Geometry[0]);
106: }
107: }
|