001: /*
002: * uDig - User Friendly Desktop Internet GIS client
003: * http://udig.refractions.net
004: * (C) 2004, Refractions Research Inc.
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation;
009: * version 2.1 of the License.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: */
017: package net.refractions.udig.core.internal;
018:
019: import com.vividsolutions.jts.geom.Coordinate;
020: import com.vividsolutions.jts.geom.Geometry;
021: import com.vividsolutions.jts.geom.GeometryFactory;
022: import com.vividsolutions.jts.geom.LineString;
023: import com.vividsolutions.jts.geom.LinearRing;
024: import com.vividsolutions.jts.geom.MultiLineString;
025: import com.vividsolutions.jts.geom.MultiPoint;
026: import com.vividsolutions.jts.geom.MultiPolygon;
027: import com.vividsolutions.jts.geom.Point;
028: import com.vividsolutions.jts.geom.Polygon;
029:
030: /**
031: * Provides methods for conveniently recreating JTS geometries.
032: *
033: * @author jEICHAR
034: * @since 0.3
035: */
036: public class GeometryBuilder {
037: /**
038: *
039: */
040: public GeometryFactory factory = new GeometryFactory();
041:
042: private GeometryBuilder() {
043: // do nothing
044: }
045:
046: /**
047: * create a geometry builder object.
048: *
049: * @return GeometryBuilder
050: */
051: public static GeometryBuilder create() {
052: return new GeometryBuilder();
053: }
054:
055: /**
056: * Creates a Geometry of type type from the coordinates in the coordinate array.
057: *
058: * @param type
059: * @param coords
060: * @return Geometry
061: */
062: public Geometry createGeometry(Class type, Coordinate[] coords) {
063: if (LinearRing.class.isAssignableFrom(type)) {
064: return factory.createLinearRing(coords);
065: }
066: if (LineString.class.isAssignableFrom(type)) {
067: return factory.createLineString(coords);
068: }
069: return null;
070: }
071:
072: public <T extends Geometry> T safeCreateGeometry(Class<T> type,
073: Coordinate[] coords) {
074: if (LinearRing.class.isAssignableFrom(type)) {
075: if (coords.length < 4) {
076: Coordinate[] tmp = new Coordinate[4];
077: System.arraycopy(coords, 0, tmp, 0, coords.length);
078: for (int i = coords.length; i < tmp.length; i++) {
079: tmp[i] = new Coordinate(coords[0]);
080: }
081: coords = tmp;
082: }
083: if (!coords[0].equals(coords[coords.length - 1])) {
084: Coordinate[] tmp = new Coordinate[coords.length + 1];
085: System.arraycopy(coords, 0, tmp, 0, coords.length);
086: tmp[tmp.length - 1] = tmp[0];
087: coords = tmp;
088: }
089:
090: return type.cast(factory.createLinearRing(coords));
091: }
092: if (LineString.class.isAssignableFrom(type)) {
093: if (coords.length < 2) {
094: Coordinate[] tmp = new Coordinate[2];
095: System.arraycopy(coords, 0, tmp, 0, coords.length);
096: for (int i = coords.length; i < tmp.length; i++) {
097: tmp[i] = new Coordinate(coords[coords.length - 1]);
098: }
099: coords = tmp;
100: }
101: return type.cast(factory.createLineString(coords));
102: }
103: if (Coordinate.class.isAssignableFrom(type)) {
104: return type.cast(factory.createPoint(coords[0]));
105: }
106:
107: if (Point.class.isAssignableFrom(type)) {
108: return type.cast(factory.createPoint(coords[0]));
109: }
110: if (MultiPoint.class.isAssignableFrom(type)) {
111: return type.cast(factory.createMultiPoint(coords));
112: }
113:
114: if (Polygon.class.isAssignableFrom(type)) {
115: return type.cast(factory.createPolygon(
116: (LinearRing) safeCreateGeometry(LinearRing.class,
117: coords), new LinearRing[] {}));
118: }
119:
120: if (MultiPolygon.class.isAssignableFrom(type)) {
121: return type
122: .cast(factory
123: .createMultiPolygon(new Polygon[] { (Polygon) safeCreateGeometry(
124: Polygon.class, coords) }));
125: }
126:
127: if (MultiLineString.class.isAssignableFrom(type)) {
128: return type
129: .cast(factory
130: .createMultiLineString(new LineString[] { (LineString) safeCreateGeometry(
131: LineString.class, coords) }));
132: }
133:
134: return null;
135: }
136:
137: }
|