001: /*
002: * GeoTools - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2003-2006, Geotools Project Managment Committee (PMC)
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; either
009: * version 2.1 of the License, or (at your option) any later version.
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: package org.geotools.data.oracle.attributeio;
017:
018: import java.io.IOException;
019: import java.sql.Connection;
020: import java.sql.PreparedStatement;
021: import java.sql.ResultSet;
022: import java.sql.SQLException;
023: import java.util.logging.Level;
024: import java.util.logging.Logger;
025:
026: import oracle.jdbc.OracleConnection;
027: import oracle.sql.STRUCT;
028:
029: import org.geotools.data.DataSourceException;
030: import org.geotools.data.jdbc.QueryData;
031: import org.geotools.data.jdbc.attributeio.AttributeIO;
032: import org.geotools.data.jdbc.datasource.DataSourceFinder;
033: import org.geotools.data.jdbc.datasource.UnWrapper;
034: import org.geotools.data.oracle.sdo.GeometryConverter;
035: import org.geotools.feature.AttributeType;
036:
037: import com.vividsolutions.jts.geom.Geometry;
038: import com.vividsolutions.jts.geom.GeometryCollection;
039: import com.vividsolutions.jts.geom.GeometryFactory;
040: import com.vividsolutions.jts.geom.LineString;
041: import com.vividsolutions.jts.geom.MultiLineString;
042: import com.vividsolutions.jts.geom.MultiPoint;
043: import com.vividsolutions.jts.geom.MultiPolygon;
044: import com.vividsolutions.jts.geom.Point;
045: import com.vividsolutions.jts.geom.Polygon;
046: import com.vividsolutions.jts.geom.PrecisionModel;
047:
048: /**
049: * An attribute IO that uses the Oracle SDO API to read/write geometries
050: *
051: * @author Andrea Aime
052: * @author Sean Geoghegan, Defence Science and Technology Organisation.
053: *
054: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/unsupported/oracle-spatial/src/main/java/org/geotools/data/oracle/attributeio/SDOAttributeIO.java $
055: */
056: public class SDOAttributeIO implements AttributeIO {
057:
058: private static final Logger LOGGER = org.geotools.util.logging.Logging
059: .getLogger("org.geotools.data.oracle");
060:
061: // geometry adpaters
062: // private AdapterJTS adapterJTS;
063: // private AdapterSDO adapterSDO;
064: GeometryConverter converter;
065: private QueryData queryData;
066: private Class targetClazz;
067: private GeometryFactory geometryFactory;
068:
069: public SDOAttributeIO(AttributeType attributeType,
070: QueryData queryData) throws DataSourceException {
071: this .queryData = queryData;
072: geometryFactory = null;
073: try {
074: String tableName = queryData.getFeatureTypeInfo()
075: .getFeatureTypeName();
076: String columnName = attributeType.getName();
077: targetClazz = attributeType.getType();
078: LOGGER.fine("About to create Geometry convertor for "
079: + tableName + "." + columnName);
080:
081: // TODO should check that it is an OracleConnection
082: Connection conn = queryData.getConnection();
083: if (!(conn instanceof OracleConnection)) {
084: UnWrapper uw = DataSourceFinder.getUnWrapper(conn);
085: if (uw != null)
086: conn = uw.unwrap(conn);
087: }
088: OracleConnection oracleConnection = (OracleConnection) conn;
089: //GeometryFactory gFact = null;
090:
091: int srid = queryData.getFeatureTypeInfo().getSRID(
092: columnName);
093:
094: if (srid != -1) {
095: //SRManager srManager = OraSpatialManager.getSpatialReferenceManager(conn);
096: //SpatialReference sr = srManager.retrieve(srid);
097: //gFact = OraSpatialManager.getGeometryFactory(sr);
098: PrecisionModel pm = new PrecisionModel();
099: geometryFactory = new GeometryFactory(pm, srid);
100:
101: } else {
102: //gFact = OraSpatialManager.getGeometryFactory();
103: geometryFactory = new GeometryFactory();
104: }
105: //adapterSDO = new AdapterSDO(gFact, conn);
106: //adapterJTS = new AdapterJTS(gFact);
107: converter = new GeometryConverter(oracleConnection,
108: geometryFactory);
109:
110: // catch (SQLException e) {
111: // String msg = "Error setting up SDO Geometry convertor";
112: // LOGGER.log(Level.SEVERE, msg, e);
113: // throw new DataSourceException(msg + ":" + e.getMessage(), e);
114: // }
115: // catch (SRException e) {
116: // throw new DataSourceException(
117: // "Error setting up SDO Geometry convertor", e);
118: // }
119: } catch (IOException e) {
120: throw new DataSourceException(e);
121: } finally {
122: // hold try statement in place
123: }
124: }
125:
126: /**
127: * @see org.geotools.data.jdbc.attributeio.AttributeIO#read(java.sql.ResultSet,
128: * int)
129: */
130: public Object read(ResultSet rs, int position) throws IOException {
131: try {
132: Geometry geom = null;
133: Object struct = rs.getObject(position);
134: // oracle.sdoapi.geom.Geometry sdoGeom = adapterSDO.importGeometry(struct);
135: // geom = adapterJTS.exportGeometry(Geometry.class, sdoGeom);
136:
137: geom = converter.asGeometry((STRUCT) struct);
138: // in Oracle you can have polygons in a column declared to be multipolygon, and so on...
139: // so we better convert geometries, since our feature model is not so lenient
140: if (targetClazz.equals(MultiPolygon.class)
141: && geom instanceof Polygon)
142: return geometryFactory
143: .createMultiPolygon(new Polygon[] { (Polygon) geom });
144: else if (targetClazz.equals(MultiPoint.class)
145: && geom instanceof Point)
146: return geometryFactory
147: .createMultiPoint(new Point[] { (Point) geom });
148: else if (targetClazz.equals(MultiLineString.class)
149: && geom instanceof LineString)
150: return geometryFactory
151: .createMultiLineString(new LineString[] { (LineString) geom });
152: else if (targetClazz.equals(GeometryCollection.class))
153: return geometryFactory
154: .createGeometryCollection(new Geometry[] { geom });
155: return geom;
156: } catch (SQLException e) {
157: String msg = "SQL Exception reading geometry column";
158: LOGGER.log(Level.SEVERE, msg, e);
159: throw new DataSourceException(msg, e);
160: }
161: // catch (InvalidGeometryException e) {
162: // String msg = "Problem with the geometry";
163: // LOGGER.log(Level.SEVERE, msg, e);
164: // throw new DataSourceException(msg, e);
165: // } catch (GeometryInputTypeNotSupportedException e) {
166: // String msg = "Geometry Conversion type error";
167: // LOGGER.log(Level.SEVERE, msg, e);
168: // throw new DataSourceException(msg, e);
169: // } catch (GeometryOutputTypeNotSupportedException e) {
170: // String msg = "Geometry Conversion type error";
171: // LOGGER.log(Level.SEVERE, msg, e);
172: // throw new DataSourceException(msg, e);
173: // }
174: }
175:
176: /**
177: *
178: * @see org.geotools.data.jdbc.attributeio.AttributeIO#write(java.sql.ResultSet,
179: * int, java.lang.Object)
180: */
181: public void write(ResultSet rs, int position, Object value)
182: throws IOException {
183: try {
184: //oracle.sdoapi.geom.Geometry sdoGeom = adapterJTS.importGeometry(value);
185: //Object o = adapterSDO.exportGeometry(STRUCT.class, sdoGeom);
186: Geometry geom = (Geometry) value;
187: STRUCT struct = converter.toSDO(geom);
188: rs.updateObject(position, struct);
189: } catch (SQLException sqlException) {
190: String msg = "SQL Exception writing geometry column";
191: LOGGER.log(Level.SEVERE, msg, sqlException);
192: throw new DataSourceException(msg, sqlException);
193: }
194: }
195:
196: /**
197: * @see org.geotools.data.jdbc.attributeio.AttributeIO#write(java.sql.PreparedStatement, int, java.lang.Object)
198: */
199: public void write(PreparedStatement ps, int position, Object value)
200: throws IOException {
201: try {
202: Geometry geom = (Geometry) value;
203: STRUCT struct = converter.toSDO(geom);
204: ps.setObject(position, struct);
205: } catch (SQLException sqlException) {
206: String msg = "SQL Exception writing geometry column";
207: LOGGER.log(Level.SEVERE, msg, sqlException);
208: throw new DataSourceException(msg, sqlException);
209: }
210:
211: }
212:
213: }
|