Index: GeometryConverter.java =================================================================== --- GeometryConverter.java (revision 10959) +++ GeometryConverter.java (working copy) @@ -16,14 +16,8 @@ */ package org.geotools.data.oracle.sdo; -import java.sql.ResultSetMetaData; -import java.sql.SQLException; - -//import oracle.sql.ARRAY; -//import oracle.sql.Datum; -//import oracle.sql.NUMBER; -//import oracle.sql.STRUCT; - +import com.vividsolutions.jts.geom.Coordinate; +import com.vividsolutions.jts.geom.CoordinateList; import com.vividsolutions.jts.geom.Geometry; import com.vividsolutions.jts.geom.GeometryCollection; import com.vividsolutions.jts.geom.GeometryFactory; @@ -33,21 +27,21 @@ import com.vividsolutions.jts.geom.MultiPolygon; import com.vividsolutions.jts.geom.Point; import com.vividsolutions.jts.geom.Polygon; - +import java.sql.ResultSetMetaData; +import java.sql.SQLException; import java.util.Iterator; - -import com.vividsolutions.jts.geom.Coordinate; -import com.vividsolutions.jts.geom.CoordinateList; - import oracle.jdbc.OracleConnection; -import oracle.sql.STRUCT; import oracle.sql.ARRAY; import oracle.sql.ArrayDescriptor; import oracle.sql.CHAR; import oracle.sql.CharacterSet; import oracle.sql.Datum; import oracle.sql.NUMBER; +import oracle.sql.STRUCT; import oracle.sql.StructDescriptor; +import org.geotools.data.ConversionException; + + /** * Sample use of SDO class for simple JTS Geometry. *
@@ -56,7 +50,7 @@ *
* @author jgarnett */ -public class GeometryConverter { +public class GeometryConverter implements org.geotools.data.GeometryConverter { protected OracleConnection connection; GeometryFactory geometryFactory; @@ -68,7 +62,39 @@ this.connection = connection; } public static final String DATATYPE = "MDSYS.SDO_GEOMETRY"; + /** + * Implement the toDbSpatialType method in the GeometryConverter interface. + */ + public Object convertFromGeometry(Geometry geom) throws ConversionException { + try { + return this.toSDO(geom); + } catch (SQLException e) { + throw new ConversionException("Database error attempting to create com.oracle.STRUCT.", e, geom); + } + } + + /** + * Convert provided STRUCT to JTS Geometry. + *
+ * Will return null as null.
+ *
Geometry representing the provided
+ * STRUCT
+ * @see net.refractions.jspatial.Converter#toObject(oracle.sql.STRUCT)
+ * @param sdoGeom Oracle STRUCT to convert to a JTS Geometry
+ * @throws SQLException Error if the DB doesn't support conversion
+ */
+ public Geometry convertToGeometry(Object sdoGeom) throws ConversionException {
+ if (!(sdoGeom instanceof STRUCT)) throw new IllegalArgumentException("Object of class " + sdoGeom.getClass() + " is not convertible using this GeometryConverter.");
+ try {
+ return asGeometry((STRUCT) sdoGeom);
+ } catch (SQLException e) {
+ throw new ConversionException("Database error attempting to create com.vividsolutions.geom.Geometry.", e, sdoGeom);
+ }
+ }
+
+ /**
* Used to handle MDSYS.SDO_GEOMETRY.
*
* @return MDSYS.SDO_GEOMETRY
@@ -447,4 +473,12 @@
}
return array;
}
+
+ /**
+ * Returns whether or not this GeometryConverter can convert an object of the specified
+ * class.
+ */
+ public boolean isCapable(Class cls) {
+ return STRUCT.class.isAssignableFrom(cls);
+ }
}