gt:         Object[] coords = null;
context: //$HeadURL$
  /*----------------------------------------------------------------------------
   This file is part of deegree, http://deegree.org/
   Copyright (C) 2001-2009 by:
   Department of Geography, University of Bonn
   and
   lat/lon GmbH
  
   This library is free software; you can redistribute it and/or modify it under
   the terms of the GNU Lesser General Public License as published by the Free
   Software Foundation; either version 2.1 of the License, or (at your option)
   any later version.
   This library is distributed in the hope that it will be useful, but WITHOUT
   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
   FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
   details.
   You should have received a copy of the GNU Lesser General Public License
   along with this library; if not, write to the Free Software Foundation, Inc.,
   59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  
   Contact information:
  
   lat/lon GmbH
   Aennchenstr. 19, 53177 Bonn
   Germany
   http://lat-lon.de/
  
   Department of Geography, University of Bonn
   Prof. Dr. Klaus Greve
   Postfach 1147, 53001 Bonn
   Germany
   http://www.geographie.uni-bonn.de/deegree/
  
   e-mail: info@deegree.org
   ----------------------------------------------------------------------------*/
  
  package org.deegree.io.datastore.sql.oracle;
  
  import java.util.ArrayList;
  import java.util.List;
  
  import oracle.spatial.geometry.JGeometry;
  
  import org.deegree.model.crs.CoordinateSystem;
  import org.deegree.model.spatialschema.Curve;
  import org.deegree.model.spatialschema.Geometry;
  import org.deegree.model.spatialschema.GeometryException;
  import org.deegree.model.spatialschema.GeometryFactory;
  import org.deegree.model.spatialschema.MultiCurve;
  import org.deegree.model.spatialschema.MultiPoint;
  import org.deegree.model.spatialschema.MultiSurface;
  import org.deegree.model.spatialschema.Point;
  import org.deegree.model.spatialschema.Position;
  import org.deegree.model.spatialschema.Ring;
  import org.deegree.model.spatialschema.Surface;
  
  /**
   * Adapter between deegree <code>Geometry</code> objects and Oracle <code>JGeometry</code> objects.
   * 
   * @author <a href="mailto:schneider@lat-lon.de">Markus Schneider </A>
   * @author <a href="mailto:poth@lat-lon.de">Andreas Poth </a>
   * @author last edited by: $Author$
   * 
   * @version $Revision$, $Date$
   */
  public class JGeometryAdapter {
  
      private JGeometryAdapter() {
          // avoid instantiation
      }
  
      /**
       * Converts a deegree <code>Geometry</code> instance to a corresponding Oracle <code>JGeometry</code> object.
       * 
       * @param geometry
       *            deegree <code>Geometry</code> to be converted
       * @param srid
       *            coordinate system for created Oracle <code>JGeometry</code> objects
       * @return corresponding Oracle <code>JGeometry</code>
       * @throws GeometryException
       */
      public static JGeometry export( Geometry geometry, int srid )
                              throws GeometryException {
          JGeometry jGeometry = null;
  
          if ( geometry instanceof Point ) {
              jGeometry = exportPoint( (Point) geometry, srid );
          } else if ( geometry instanceof MultiPoint ) {
              jGeometry = exportMultiPoint( (MultiPoint) geometry, srid );
          } else if ( geometry instanceof Curve ) {
              jGeometry = exportCurve( (Curve) geometry, srid );
          } else if ( geometry instanceof MultiCurve ) {
              jGeometry = exportMultiCurve( (MultiCurve) geometry, srid );
          } else if ( geometry instanceof Surface ) {
              jGeometry = exportSurface( (Surface) geometry, srid );
          } else if ( geometry instanceof MultiSurface ) {
              jGeometry = exportMultiSurface( (MultiSurface) geometry, srid );
          } else {
              throw new GeometryException( "Cannot export geometry of type '" + geometry.getClass()
                                           + "' to Oracle JGeometry: Unsupported type." );
          }
          return jGeometry;
      }
  
      /**
       * Converts an Oracle <code>JGeometry</code> instance to a corresponding deegree <code>Geometry</code> object.
       * 
       * @param jGeometry
       *            Oracle <code>JGeometry</code> to be converted
       * @param crs
       *            coordinate system of the created deegree <code>Geometry</code> object
       * @return corresponding deegree <code>Geometry</code>
       * @throws GeometryException
       */
      public static Geometry wrap( JGeometry jGeometry, CoordinateSystem crs )
                              throws GeometryException {
          Geometry geo = null;
  
          switch ( jGeometry.getType() ) {
          case JGeometry.GTYPE_POINT:
              geo = wrapPoint( jGeometry, crs );
              break;
          case JGeometry.GTYPE_CURVE:
              geo = wrapCurve( jGeometry, crs );
              break;
          case JGeometry.GTYPE_POLYGON:
              geo = wrapSurface( jGeometry, crs );
              break;
          case JGeometry.GTYPE_MULTIPOINT:
              geo = wrapMultiPoint( jGeometry, crs );
              break;
          case JGeometry.GTYPE_MULTICURVE:
              geo = wrapMultiCurve( jGeometry, crs );
              break;
          case JGeometry.GTYPE_MULTIPOLYGON:
              geo = wrapMultiSurface( jGeometry, crs );
              break;
          case JGeometry.GTYPE_COLLECTION:
          default: {
              throw new GeometryException( "Cannot export Oracle JGeometry of type '" + jGeometry.getType()
                                           + "' to deegree geometry: Unsupported type." );
          }
          }
          return geo;
      }
  
      /**
       * Creates an Oracle <code>JGeometry</code> with type <code>GTYPE_POINT</code> from a <code>Point</code>.
       * 
       * @param point
       * @param srid
       *            coordinate system for created Oracle <code>JGeometry</code> objects
       */
      private static JGeometry exportPoint( Point point, int srid ) {
          int dimension = point.getCoordinateDimension();
          double[] coords = point.getAsArray();
          if ( dimension == 2 ) {
              coords = new double[] { coords[0], coords[1] };
          } else {
              coords = new double[] { coords[0], coords[1], coords[2] };
          }
          return JGeometry.createPoint( coords, point.getCoordinateDimension(), srid );
      }
  
      /**
       * Creates an Oracle <code>JGeometry</code> with type <code>GTYPE_MULTIPOINT</code> from a <code>MultiPoint</code>.
       * 
       * @param multiPoint
       * @param srid
       *            coordinate system for created Oracle <code>JGeometry</code> objects
       */
      private static JGeometry exportMultiPoint( MultiPoint multiPoint, int srid ) {
          Point[] points = multiPoint.getAllPoints();
          int dimension = multiPoint.getCoordinateDimension();
          Object[] coords = new Object[points.length];
          for ( int i = 0; i < coords.length; i++ ) {
              double[] d = points[i].getAsArray();
              if ( dimension == 2 ) {
                  coords[i] = new double[] { d[0], d[1] };
              } else {
                  coords[i] = new double[] { d[0], d[1], d[2] };
              }
          }
          return JGeometry.createMultiPoint( coords, multiPoint.getCoordinateDimension(), srid );
      }
  
      /**
       * Creates an Oracle <code>JGeometry</code> with type <code>GTYPE_CURVE</code> from a <code>Curve</code>.
       * 
       * @param curve
       * @param srid
       *            coordinate system for created Oracle <code>JGeometry</code> objects
       */
      private static JGeometry exportCurve( Curve curve, int srid )
                              throws GeometryException {
          int dimension = curve.getCoordinateDimension();
          Position[] positions = curve.getAsLineString().getPositions();
          double[] ordinates = new double[positions.length * dimension];
          int ordinateIndex = 0;
          for ( int i = 0; i < positions.length; i++ ) {
              double[] position = positions[i].getAsArray();
              for ( int j = 0; j < dimension; j++ ) {
                  ordinates[ordinateIndex++] = position[j];
              }
          }
          return JGeometry.createLinearLineString( ordinates, dimension, srid );
      }
  
      /**
       * Creates an Oracle <code>JGeometry</code> with type <code>GTYPE_MULTICURVE</code> from a <code>MultiCurve</code>.
       * 
       * @param multiCurve
       * @param srid
       *            coordinate system for created Oracle <code>JGeometry</code> objects
       * @throws GeometryException
       */
      private static JGeometry exportMultiCurve( MultiCurve multiCurve, int srid )
                              throws GeometryException {
          int dimension = multiCurve.getCoordinateDimension();
          Curve[] curves = multiCurve.getAllCurves();
          Object[] coords = new Object[curves.length];
          for ( int i = 0; i < curves.length; i++ ) {
              Position[] positions = curves[i].getAsLineString().getPositions();
              double[] ordinates = new double[positions.length * dimension];
              int ordinateIndex = 0;
              for ( int j = 0; j < positions.length; j++ ) {
                  double[] position = positions[j].getAsArray();
                  for ( int k = 0; k < dimension; k++ ) {
                      ordinates[ordinateIndex++] = position[k];
                  }
              }
              coords[i] = ordinates;
          }
          return JGeometry.createLinearMultiLineString( coords, dimension, srid );
      }
  
      /**
       * Creates an Oracle <code>JGeometry</code> with type <code>GTYPE_POLYGON</code> from a <code>Surface</code>.
       * 
       * @param surface
       * @param srid
       *            coordinate system for created Oracle <code>JGeometry</code> objects
       */
      private static JGeometry exportSurface( Surface surface, int srid ) {
          int dimension = surface.getCoordinateDimension();
          Ring exteriorRing = surface.getSurfaceBoundary().getExteriorRing();
          Ring[] interiorRings = surface.getSurfaceBoundary().getInteriorRings();
  