001: /* uDig - User Friendly Desktop Internet GIS client
002: * http://udig.refractions.net
003: * (C) 2004, Refractions Research Inc.
004: *
005: * This library is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU Lesser General Public
007: * License as published by the Free Software Foundation;
008: * version 2.1 of the License.
009: *
010: * This library is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * Lesser General Public License for more details.
014: */
015: package net.refractions.udig.tools.edit.support;
016:
017: import java.util.ArrayList;
018: import java.util.HashMap;
019: import java.util.List;
020: import java.util.Map;
021:
022: import net.refractions.udig.core.internal.GeometryBuilder;
023: import net.refractions.udig.tools.edit.EditPlugin;
024:
025: import org.geotools.data.shapefile.shp.JTSUtilities;
026: import org.geotools.feature.GeometryAttributeType;
027:
028: import com.vividsolutions.jts.algorithm.CGAlgorithms;
029: import com.vividsolutions.jts.algorithm.RobustCGAlgorithms;
030: import com.vividsolutions.jts.geom.Coordinate;
031: import com.vividsolutions.jts.geom.Geometry;
032: import com.vividsolutions.jts.geom.GeometryCollection;
033: import com.vividsolutions.jts.geom.GeometryFactory;
034: import com.vividsolutions.jts.geom.LineString;
035: import com.vividsolutions.jts.geom.LinearRing;
036: import com.vividsolutions.jts.geom.Point;
037: import com.vividsolutions.jts.geom.Polygon;
038:
039: /**
040: * Some helper methods for creating geometries.
041: *
042: * @author jones
043: * @since 1.1.0
044: */
045: public class GeometryCreationUtil {
046:
047: public static class Bag {
048: public EditGeom geom;
049: public List<Geometry> jts = new ArrayList<Geometry>();
050:
051: public Bag(EditGeom geom) {
052: this .geom = geom;
053: }
054: }
055:
056: /**
057: * Searches an EditBlackboard and creates 1 geometry for each EditGeom in the
058: * Blackboard. Only simple geoms are created. IE no MultiPolygons. That processing
059: * must come later.
060: *
061: * The way the type of geometry is determined is as follows:
062: * <ul>
063: * <li>If the geometry has the same FID as the currentGeom then its type will be of type geomToCreate.</li>
064: * <li>Otherwise the ShapeType of the EditGeom will be compared to the geomAttribute and if that type is legal for that attribute then that is the type of geometry that will be created</li>
065: * <li>If ShapeType is UNKOWN and it does not have the same FID as the curentGeom then the geomAttribute is used to determine the geometry type.</li>
066: * <li>Finally if the geomType is geometry then a Polygon will be created... unless the endpoints are not the same. In that case a Line will be created. Unless it is a single point.</li>
067: *</ul>
068: *
069: * @param currentGeom the shape that will be created as a geomToCreate type.
070: * @param geomToCreate the type of geometry that will be created for the currentShape in the handler. Must be one of Point, LineString, LinearRing or Polygon.
071: * @param geomAttribute The AttributeType of the geometry attribute that will be assist in determining the type of geometry created.
072: * @return a mapping between the FeatureID of a bag of geoms that map to that id. This mapping
073: * can be used to later create complex geometries such as multigeoms.
074: */
075: @SuppressWarnings("unchecked")
076: public static Map<String, GeometryCreationUtil.Bag> createAllGeoms(
077: EditGeom currentGeom, Class geomToCreate,
078: GeometryAttributeType geomAttribute) {
079: EditBlackboard blackboard = currentGeom.getEditBlackboard();
080: List<EditGeom> editGeoms = blackboard.getGeoms();
081: Map<String, GeometryCreationUtil.Bag> idToGeom = new HashMap<String, GeometryCreationUtil.Bag>();
082: for (EditGeom editGeom : editGeoms) {
083: if (editGeom.isChanged()) {
084: if (!idToGeom.containsKey(editGeom.getFeatureIDRef()
085: .get()))
086: idToGeom.put(editGeom.getFeatureIDRef().get(),
087: new GeometryCreationUtil.Bag(editGeom));
088: }
089: }
090: for (EditGeom editGeom : editGeoms) {
091: if (!idToGeom.containsKey(editGeom.getFeatureIDRef().get()))
092: continue;
093:
094: if (idToGeom.containsKey(editGeom.getFeatureIDRef().get())) {
095: if (editGeom.getShell().getNumPoints() > 0)
096: idToGeom.get(editGeom.getFeatureIDRef().get()).jts
097: .add(GeometryCreationUtil.createGeom(
098: determineGeometryType(currentGeom,
099: editGeom, geomToCreate,
100: geomAttribute), editGeom
101: .getShell()));
102:
103: }
104: }
105: return idToGeom;
106: }
107:
108: /**
109: * The way the type of geometry is determined is as follows:
110: * <ul>
111: * <li>If the geometry has the same FID as the currentGeom then its type will be of type geomToCreate.</li>
112: * <li>Otherwise the ShapeType of the EditGeom will be compared to the geomAttribute and if that type is legal for that attribute then that is the type of geometry that will be created</li>
113: * <li>If ShapeType is UNKOWN and it does not have the same FID as the curentGeom then the geomAttribute is used to determine the geometry type.</li>
114: * <li>Finally if the geomType is geometry then a Polygon will be created... unless the endpoints are not the same. In that case a Line will be created. Unless it is a single point.</li>
115: *</ul>
116: *
117: * @param currentGeom the handler's current Geom. If == editGeom then geomToCreate will be returned
118: * @param editGeom the editGeom that will be used to create a geometry.
119: * @param geomToCreate A default value to use if nothing else can be decided.
120: * @param geomAttribute The geometry attribute type of the feature that will be set using the created geometry.
121: * @return
122: */
123: public static Class determineGeometryType(EditGeom currentGeom,
124: EditGeom editGeom, Class geomToCreate,
125: GeometryAttributeType geomAttribute) {
126: if ((editGeom.getFeatureIDRef().get() == null && currentGeom
127: .getFeatureIDRef().get() == null)
128: || editGeom.getFeatureIDRef().get().equals(
129: currentGeom.getFeatureIDRef().get()))
130: return geomToCreate;
131: ShapeType attributeDefinedType = ShapeType
132: .valueOf(geomAttribute.getType());
133: ShapeType typeToSwitchOn;
134: if (attributeDefinedType != editGeom.getShapeType()
135: && attributeDefinedType != ShapeType.UNKNOWN
136: || editGeom.getShapeType() == ShapeType.UNKNOWN) {
137: typeToSwitchOn = attributeDefinedType;
138: } else {
139: typeToSwitchOn = editGeom.getShapeType();
140: }
141:
142: switch (typeToSwitchOn) {
143: case LINE:
144: return LineString.class;
145: case POINT:
146: return Point.class;
147: case POLYGON:
148: return Polygon.class;
149: case UNKNOWN:
150: if (geomAttribute.getType() == Geometry.class
151: || geomAttribute.getType() == GeometryCollection.class) {
152: PrimitiveShape shell = editGeom.getShell();
153: if (shell.getNumPoints() == 1)
154: return Point.class;
155: else if (shell.getPoint(0).equals(
156: shell.getPoint(shell.getNumPoints() - 1))
157: && shell.getNumCoords() != 2)
158: return Polygon.class;
159: else
160: return LineString.class;
161: }
162: }
163: return null;
164: }
165:
166: /**
167: * Creates a geometry for a primitive shape.
168: *
169: * @param geomToCreate the type of geometry to create. Must be one of Point, LineString, LinearRing or Polygon.
170: * @param shape the shape to use. If type is Polygon the shapes EditGeom is used.
171: *
172: * @return A geometry of type geomToCreate
173: */
174: public static <T extends Geometry> T createGeom(
175: Class<T> geomToCreate, PrimitiveShape shape) {
176: Geometry geom;
177: if (geomToCreate == Polygon.class) {
178: geom = createPolygon(shape.getEditGeom());
179: } else if (geomToCreate == LinearRing.class) {
180: geom = GeometryBuilder.create().safeCreateGeometry(
181: LinearRing.class, shape.coordArray());
182: } else if (geomToCreate == LineString.class) {
183: geom = GeometryBuilder.create().safeCreateGeometry(
184: LineString.class, shape.coordArray());
185: } else {
186: geom = GeometryBuilder.create().safeCreateGeometry(
187: Point.class, shape.coordArray());
188: }
189: return geomToCreate.cast(geom);
190: }
191:
192: public final static RobustCGAlgorithms cga = new RobustCGAlgorithms();
193:
194: public static Polygon createPolygon(EditGeom currentGeom) {
195: Coordinate[] shellCoords = currentGeom.getShell().coordArray();
196: LinearRing shell = GeometryBuilder.create().safeCreateGeometry(
197: LinearRing.class, shellCoords);
198:
199: try {
200: if (CGAlgorithms.isCCW(shellCoords))
201: shell = JTSUtilities.reverseRing(shell);
202: } catch (Exception e) {
203: EditPlugin.log("Not a critical problem, just an FYI", e); //$NON-NLS-1$
204: }
205: LinearRing[] holes = new LinearRing[currentGeom.getHoles()
206: .size()];
207: int i = 0;
208: for (PrimitiveShape shape : currentGeom.getHoles()) {
209: Coordinate[] coordArray = shape.coordArray();
210: if (coordArray.length == 0)
211: continue;
212: holes[i] = GeometryBuilder.create().safeCreateGeometry(
213: LinearRing.class, coordArray);
214: if (!CGAlgorithms.isCCW(coordArray)) {
215: holes[i] = JTSUtilities
216: .reverseRing((LinearRing) holes[i]);
217: }
218: i++;
219: }
220: GeometryFactory factory = new GeometryFactory();
221: return factory.createPolygon(shell, holes);
222: }
223:
224: /**
225: * Creates a GeometryCollection if the schemaDeclaredType is a subclass of GeometryCollection. Otherwise
226: * it will just return the first of the list.
227: *
228: * @param geoms list of goemtries that will be added to the GeometryCollection.
229: * @param schemaDeclaredType The type that the resulting geometry has to be compatible with
230: * @return geometry that is compatible with schemaDeclaredType (IE can be assigned to).
231: */
232: public static Geometry ceateGeometryCollection(
233: List<Geometry> geoms, Class<Geometry> schemaDeclaredType) {
234:
235: if (geoms.isEmpty()) {
236: return null;
237: }
238:
239: if (schemaDeclaredType
240: .isAssignableFrom(geoms.get(0).getClass())
241: && geoms.size() == 1)
242: return geoms.get(0);
243:
244: if (!GeometryCollection.class
245: .isAssignableFrom(schemaDeclaredType)
246: && !schemaDeclaredType
247: .isAssignableFrom(GeometryCollection.class))
248: return geoms.get(0);
249:
250: Geometry geom;
251: GeometryFactory factory = new GeometryFactory();
252: Class<? extends Geometry> geomType = geoms.get(0).getClass();
253: if (geomType == Polygon.class) {
254: geom = factory.createMultiPolygon(geoms
255: .toArray(new Polygon[geoms.size()]));
256: } else if (geomType == LinearRing.class) {
257: geom = factory.createMultiLineString(geoms
258: .toArray(new LineString[geoms.size()]));
259: } else if (geomType == LineString.class) {
260: geom = factory.createMultiLineString(geoms
261: .toArray(new LineString[geoms.size()]));
262: } else {
263: geom = factory.createMultiPoint(geoms
264: .toArray(new Point[geoms.size()]));
265: }
266: return geom;
267: }
268:
269: }
|