001: //$HeadURL: https://svn.wald.intevation.org/svn/deegree/base/trunk/src/org/deegree/model/spatialschema/JTSAdapter.java $
002: /*---------------- FILE HEADER ------------------------------------------
003:
004: This file is part of deegree.
005: Copyright (C) 2001-2008 by:
006: EXSE, Department of Geography, University of Bonn
007: http://www.giub.uni-bonn.de/deegree/
008: lat/lon GmbH
009: http://www.lat-lon.de
010:
011: This library is free software; you can redistribute it and/or
012: modify it under the terms of the GNU Lesser General Public
013: License as published by the Free Software Foundation; either
014: version 2.1 of the License, or (at your option) any later version.
015:
016: This library is distributed in the hope that it will be useful,
017: but WITHOUT ANY WARRANTY; without even the implied warranty of
018: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
019: Lesser General Public License for more details.
020:
021: You should have received a copy of the GNU Lesser General Public
022: License along with this library; if not, write to the Free Software
023: Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
024:
025: Contact:
026:
027: Andreas Poth
028: lat/lon GmbH
029: Aennchenstr. 19
030: 53177 Bonn
031: Germany
032: E-Mail: poth@lat-lon.de
033:
034: Prof. Dr. Klaus Greve
035: Department of Geography
036: University of Bonn
037: Meckenheimer Allee 166
038: 53115 Bonn
039: Germany
040: E-Mail: greve@giub.uni-bonn.de
041:
042: ---------------------------------------------------------------------------*/
043: package org.deegree.model.spatialschema;
044:
045: import org.deegree.framework.log.ILogger;
046: import org.deegree.framework.log.LoggerFactory;
047:
048: import com.vividsolutions.jts.geom.PrecisionModel;
049:
050: /**
051: * Adapter between deegree-<tt>Geometry</tt>s and JTS-<tt>Geometry<tt> objects.
052: * <p>
053: * Please note that the generated deegree-objects use null as
054: * <tt>CS_CoordinateSystem</tt>!
055: * <p>
056: * @author <a href="mailto:mschneider@lat-lon.de">Markus Schneider</a>
057: * @version $Revision: 9343 $ $Date: 2007-12-27 05:30:32 -0800 (Thu, 27 Dec 2007) $
058: */
059: public class JTSAdapter {
060:
061: private static final ILogger LOG = LoggerFactory
062: .getLogger(JTSAdapter.class);
063:
064: // precision model that is used for all JTS-Geometries
065: private static PrecisionModel pm = new PrecisionModel();
066:
067: // factory for creating JTS-Geometries
068: private static com.vividsolutions.jts.geom.GeometryFactory jtsFactory = new com.vividsolutions.jts.geom.GeometryFactory(
069: pm, 0);
070:
071: /**
072: * Converts a <tt>Geometry</tt> to a corresponding JTS-<tt>Geometry</tt> object.
073: * <p>
074: * Currently, the following conversions are supported:
075: * <ul>
076: * <li>Point -> Point
077: * <li>MultiPoint -> MultiPoint
078: * <li>Curve -> LineString
079: * <li>MultiCurve -> MultiLineString
080: * <li>Surface -> Polygon
081: * <li>MultiSurface -> MultiPolygon
082: * <li>MultiPrimitive -> GeometryCollection
083: * </ul>
084: * <p>
085: *
086: * @param gmObject
087: * the object to be converted
088: * @return the corresponding JTS-<tt>Geometry</tt> object
089: * @throws GeometryException
090: * if type unsupported or conversion failed
091: */
092: public static com.vividsolutions.jts.geom.Geometry export(
093: Geometry gmObject) throws GeometryException {
094:
095: com.vividsolutions.jts.geom.Geometry geometry = null;
096: if (gmObject instanceof Point) {
097: geometry = export((Point) gmObject);
098: } else if (gmObject instanceof MultiPoint) {
099: geometry = export((MultiPoint) gmObject);
100: } else if (gmObject instanceof Curve) {
101: geometry = export((Curve) gmObject);
102: } else if (gmObject instanceof MultiCurve) {
103: geometry = export((MultiCurve) gmObject);
104: } else if (gmObject instanceof Surface) {
105: geometry = export((Surface) gmObject);
106: } else if (gmObject instanceof MultiSurface) {
107: geometry = export((MultiSurface) gmObject);
108: } else if (gmObject instanceof MultiPrimitive) {
109: geometry = export((MultiPrimitive) gmObject);
110: } else {
111: throw new GeometryException(
112: "JTSAdapter.export does not support type '"
113: + gmObject.getClass().getName() + "'!");
114: }
115: return geometry;
116: }
117:
118: /**
119: * Converts a JTS-<tt>Geometry</tt> object to a corresponding <tt>Geometry</tt>.
120: * <p>
121: * Currently, the following conversions are supported:
122: * <ul>
123: * <li>Point -> Point
124: * <li>MultiPoint -> MultiPoint
125: * <li>LineString -> Curve
126: * <li>MultiLineString -> MultiCurve
127: * <li>Polygon -> Surface
128: * <li>MultiPolygon -> MultiSurface
129: * <li>GeometryCollection -> MultiPrimitive
130: * </ul>
131: * <p>
132: *
133: * @param geometry
134: * the JTS-<tt>Geometry</tt> to be converted
135: * @return the corresponding <tt>Geometry</tt>
136: * @throws GeometryException
137: * if type unsupported or conversion failed
138: */
139: public static Geometry wrap(
140: com.vividsolutions.jts.geom.Geometry geometry)
141: throws GeometryException {
142:
143: Geometry gmObject = null;
144: if (geometry instanceof com.vividsolutions.jts.geom.Point) {
145: gmObject = wrap((com.vividsolutions.jts.geom.Point) geometry);
146: } else if (geometry instanceof com.vividsolutions.jts.geom.MultiPoint) {
147: gmObject = wrap((com.vividsolutions.jts.geom.MultiPoint) geometry);
148: } else if (geometry instanceof com.vividsolutions.jts.geom.LineString) {
149: gmObject = wrap((com.vividsolutions.jts.geom.LineString) geometry);
150: } else if (geometry instanceof com.vividsolutions.jts.geom.MultiLineString) {
151: gmObject = wrap((com.vividsolutions.jts.geom.MultiLineString) geometry);
152: } else if (geometry instanceof com.vividsolutions.jts.geom.Polygon) {
153: gmObject = wrap((com.vividsolutions.jts.geom.Polygon) geometry);
154: } else if (geometry instanceof com.vividsolutions.jts.geom.MultiPolygon) {
155: gmObject = wrap((com.vividsolutions.jts.geom.MultiPolygon) geometry);
156: } else if (geometry instanceof com.vividsolutions.jts.geom.GeometryCollection) {
157: gmObject = wrap((com.vividsolutions.jts.geom.GeometryCollection) geometry);
158: } else {
159: throw new GeometryException(
160: "JTSAdapter.wrap does not support type '"
161: + geometry.getClass().getName() + "'!");
162: }
163: return gmObject;
164: }
165:
166: /**
167: * Converts a <tt>Point</tt> to a <tt>Point</tt>.
168: * <p>
169: *
170: * @param gmPoint
171: * point to be converted
172: * @return the corresponding <tt>Point</tt> object
173: */
174: private static com.vividsolutions.jts.geom.Point export(
175: Point gmPoint) {
176:
177: com.vividsolutions.jts.geom.Coordinate coord = new com.vividsolutions.jts.geom.Coordinate(
178: gmPoint.getX(), gmPoint.getY());
179:
180: return jtsFactory.createPoint(coord);
181: }
182:
183: /**
184: * Converts a <tt>MultiPoint</tt> to a <tt>MultiPoint</tt>.
185: * <p>
186: *
187: * @param gmMultiPoint
188: * multipoint to be converted
189: * @return the corresponding <tt>MultiPoint</tt> object
190: */
191: private static com.vividsolutions.jts.geom.MultiPoint export(
192: MultiPoint gmMultiPoint) {
193: Point[] gmPoints = gmMultiPoint.getAllPoints();
194: com.vividsolutions.jts.geom.Point[] points = new com.vividsolutions.jts.geom.Point[gmPoints.length];
195: for (int i = 0; i < points.length; i++) {
196: points[i] = export(gmPoints[i]);
197: }
198: return jtsFactory.createMultiPoint(points);
199: }
200:
201: /**
202: * Converts a <tt>Curve</tt> to a <tt>LineString</tt>.
203: * <p>
204: *
205: * @param curve
206: * <tt>Curve</tt> to be converted
207: * @return the corresponding <tt>LineString</tt> object
208: * @throws GeometryException
209: */
210: private static com.vividsolutions.jts.geom.LineString export(
211: Curve curve) throws GeometryException {
212:
213: LineString lineString = curve.getAsLineString();
214: com.vividsolutions.jts.geom.Coordinate[] coords = new com.vividsolutions.jts.geom.Coordinate[lineString
215: .getNumberOfPoints()];
216: for (int i = 0; i < coords.length; i++) {
217: Position position = lineString.getPositionAt(i);
218: coords[i] = new com.vividsolutions.jts.geom.Coordinate(
219: position.getX(), position.getY());
220: }
221: return jtsFactory.createLineString(coords);
222: }
223:
224: /**
225: * Converts a <tt>MultiCurve</tt> to a <tt>MultiLineString</tt>.
226: * <p>
227: *
228: * @param multi
229: * <tt>MultiCurve</tt> to be converted
230: * @return the corresponding <tt>MultiLineString</tt> object
231: * @throws GeometryException
232: */
233: private static com.vividsolutions.jts.geom.MultiLineString export(
234: MultiCurve multi) throws GeometryException {
235:
236: Curve[] curves = multi.getAllCurves();
237: com.vividsolutions.jts.geom.LineString[] lineStrings = new com.vividsolutions.jts.geom.LineString[curves.length];
238: for (int i = 0; i < curves.length; i++) {
239: lineStrings[i] = export(curves[i]);
240: }
241: return jtsFactory.createMultiLineString(lineStrings);
242: }
243:
244: /**
245: * Converts an array of <tt>Position</tt>s to a <tt>LinearRing</tt>.
246: * <p>
247: *
248: * @param positions
249: * an array of <tt>Position</tt>s
250: * @return the corresponding <tt>LinearRing</tt> object
251: */
252: public static com.vividsolutions.jts.geom.LinearRing export(
253: Position[] positions) {
254: com.vividsolutions.jts.geom.Coordinate[] coords = new com.vividsolutions.jts.geom.Coordinate[positions.length];
255: for (int i = 0; i < positions.length; i++) {
256: coords[i] = new com.vividsolutions.jts.geom.Coordinate(
257: positions[i].getX(), positions[i].getY());
258: }
259: return jtsFactory.createLinearRing(coords);
260: }
261:
262: /**
263: * Converts a <tt>Surface</tt> to a <tt>Polygon</tt>.
264: * <p>
265: * Currently, the <tt>Surface</tt> _must_ contain exactly one patch!
266: * <p>
267: *
268: * @param surface
269: * a <tt>Surface</tt>
270: * @return the corresponding <tt>Polygon</tt> object
271: */
272: private static com.vividsolutions.jts.geom.Polygon export(
273: Surface surface) {
274: SurfacePatch patch = null;
275: try {
276: patch = surface.getSurfacePatchAt(0);
277: } catch (GeometryException e) {
278: LOG.logError("", e);
279: }
280: Position[] exteriorRing = patch.getExteriorRing();
281: Position[][] interiorRings = patch.getInteriorRings();
282:
283: com.vividsolutions.jts.geom.LinearRing shell = export(exteriorRing);
284: com.vividsolutions.jts.geom.LinearRing[] holes = new com.vividsolutions.jts.geom.LinearRing[0];
285: if (interiorRings != null)
286: holes = new com.vividsolutions.jts.geom.LinearRing[interiorRings.length];
287: for (int i = 0; i < holes.length; i++) {
288: holes[i] = export(interiorRings[i]);
289: }
290: return jtsFactory.createPolygon(shell, holes);
291: }
292:
293: /**
294: * Converts a <tt>MultiSurface</tt> to a <tt>MultiPolygon</tt>.
295: * <p>
296: * Currently, the contained <tt>Surface</tt> _must_ have exactly one patch!
297: * <p>
298: *
299: * @param msurface
300: * a <tt>MultiSurface</tt>
301: * @return the corresponding <tt>MultiPolygon</tt> object
302: */
303: private static com.vividsolutions.jts.geom.MultiPolygon export(
304: MultiSurface msurface) {
305:
306: Surface[] surfaces = msurface.getAllSurfaces();
307: com.vividsolutions.jts.geom.Polygon[] polygons = new com.vividsolutions.jts.geom.Polygon[surfaces.length];
308:
309: for (int i = 0; i < surfaces.length; i++) {
310: polygons[i] = export(surfaces[i]);
311: }
312: return jtsFactory.createMultiPolygon(polygons);
313: }
314:
315: /**
316: * Converts a <tt>MultiPrimitive</tt> to a <tt>GeometryCollection</tt>.
317: * <p>
318: *
319: * @param multi
320: * a <tt>MultiPrimtive</tt>
321: * @return the corresponding <tt>GeometryCollection</tt> object
322: * @throws GeometryException
323: */
324: private static com.vividsolutions.jts.geom.GeometryCollection export(
325: MultiPrimitive multi) throws GeometryException {
326:
327: Geometry[] primitives = multi.getAllPrimitives();
328: com.vividsolutions.jts.geom.Geometry[] geometries = new com.vividsolutions.jts.geom.Geometry[primitives.length];
329:
330: for (int i = 0; i < primitives.length; i++) {
331: geometries[i] = export(primitives[i]);
332: }
333: return jtsFactory.createGeometryCollection(geometries);
334: }
335:
336: /**
337: * Converts a <tt>Point</tt> to a <tt>Point</tt>s.
338: * <p>
339: *
340: * @param point
341: * a <tt>Point</tt> object
342: * @return the corresponding <tt>Point</tt>
343: */
344: private static Point wrap(com.vividsolutions.jts.geom.Point point) {
345: com.vividsolutions.jts.geom.Coordinate coord = point
346: .getCoordinate();
347: return Double.isNaN(coord.z) ? new PointImpl(coord.x, coord.y,
348: null) : new PointImpl(coord.x, coord.y, coord.z, null);
349: }
350:
351: /**
352: * Converts a <tt>MultiPoint</tt> to a <tt>MultiPoint</tt>.
353: * <p>
354: *
355: * @param multi
356: * a <tt>MultiPoint</tt> object
357: * @return the corresponding <tt>MultiPoint</tt>
358: */
359: private static MultiPoint wrap(
360: com.vividsolutions.jts.geom.MultiPoint multi) {
361: Point[] gmPoints = new Point[multi.getNumGeometries()];
362: for (int i = 0; i < gmPoints.length; i++) {
363: gmPoints[i] = wrap((com.vividsolutions.jts.geom.Point) multi
364: .getGeometryN(i));
365: }
366: return new MultiPointImpl(gmPoints, null);
367: }
368:
369: /**
370: * Converts a <tt>LineString</tt> to a <tt>Curve</tt>.
371: * <p>
372: *
373: * @param line
374: * a <tt>LineString</tt> object
375: * @return the corresponding <tt>Curve</tt>
376: * @throws GeometryException
377: */
378: private static Curve wrap(
379: com.vividsolutions.jts.geom.LineString line)
380: throws GeometryException {
381: com.vividsolutions.jts.geom.Coordinate[] coords = line
382: .getCoordinates();
383: Position[] positions = new Position[coords.length];
384: for (int i = 0; i < coords.length; i++) {
385: positions[i] = new PositionImpl(coords[i].x, coords[i].y);
386: }
387: return GeometryFactory.createCurve(positions, null);
388: }
389:
390: /**
391: * Converts a <tt>MultiLineString</tt> to a <tt>MultiCurve</tt>.
392: * <p>
393: *
394: * @param multi
395: * a <tt>MultiLineString</tt> object
396: * @return the corresponding <tt>MultiCurve</tt>
397: * @throws GeometryException
398: */
399: private static MultiCurve wrap(
400: com.vividsolutions.jts.geom.MultiLineString multi)
401: throws GeometryException {
402: Curve[] curves = new Curve[multi.getNumGeometries()];
403: for (int i = 0; i < curves.length; i++) {
404: curves[i] = wrap((com.vividsolutions.jts.geom.LineString) multi
405: .getGeometryN(i));
406: }
407: return GeometryFactory.createMultiCurve(curves);
408: }
409:
410: /**
411: *
412: * Converts a <tt>Polygon</tt> to a <tt>Surface</tt>.
413: * <p>
414: *
415: * @param polygon
416: * a <tt>Polygon</tt>
417: * @return the corresponding <tt>Surface</tt> object
418: * @throws GeometryException
419: */
420: private static Surface wrap(
421: com.vividsolutions.jts.geom.Polygon polygon)
422: throws GeometryException {
423:
424: Position[] exteriorRing = createGMPositions(polygon
425: .getExteriorRing());
426: Position[][] interiorRings = new Position[polygon
427: .getNumInteriorRing()][];
428:
429: for (int i = 0; i < interiorRings.length; i++) {
430: interiorRings[i] = createGMPositions(polygon
431: .getInteriorRingN(i));
432: }
433: SurfacePatch patch = new PolygonImpl(
434: new SurfaceInterpolationImpl(), exteriorRing,
435: interiorRings, null);
436:
437: return new SurfaceImpl(patch);
438: }
439:
440: /**
441: * Converts a <tt>MultiPolygon</tt> to a <tt>MultiSurface</tt>.
442: * <p>
443: *
444: * @param multiPolygon
445: * a <tt>MultiPolygon</tt>
446: * @return the corresponding <tt>MultiSurface</tt> object
447: * @throws GeometryException
448: */
449: private static MultiSurface wrap(
450: com.vividsolutions.jts.geom.MultiPolygon multiPolygon)
451: throws GeometryException {
452:
453: Surface[] surfaces = new Surface[multiPolygon
454: .getNumGeometries()];
455: for (int i = 0; i < surfaces.length; i++) {
456: surfaces[i] = wrap((com.vividsolutions.jts.geom.Polygon) multiPolygon
457: .getGeometryN(i));
458: }
459: return new MultiSurfaceImpl(surfaces);
460: }
461:
462: /**
463: * Converts a <tt>GeometryCollection</tt> to a <tt>MultiPrimitve</tt>.
464: * <p>
465: *
466: * @param collection
467: * a <tt>GeometryCollection</tt>
468: * @return the corresponding <tt>MultiPrimitive</tt> object
469: * @throws GeometryException
470: */
471: private static MultiPrimitive wrap(
472: com.vividsolutions.jts.geom.GeometryCollection collection)
473: throws GeometryException {
474:
475: MultiPrimitive multi = new MultiPrimitiveImpl(null);
476: for (int i = 0; i < collection.getNumGeometries(); i++) {
477: multi.add(wrap(collection.getGeometryN(i)));
478: }
479: return multi;
480: }
481:
482: /**
483: * Converts a <tt>LineString</tt> to an array of <tt>Position</tt>s.
484: * <p>
485: *
486: * @param line
487: * a <tt>LineString</tt> object
488: * @return the corresponding array of <tt>Position</tt>s
489: */
490: private static Position[] createGMPositions(
491: com.vividsolutions.jts.geom.LineString line) {
492: com.vividsolutions.jts.geom.Coordinate[] coords = line
493: .getCoordinates();
494: Position[] positions = new Position[coords.length];
495: for (int i = 0; i < coords.length; i++) {
496: positions[i] = new PositionImpl(coords[i].x, coords[i].y);
497: }
498: return positions;
499: }
500: }
|