001: /*
002: * GeoTools - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2004-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;
009: * version 2.1 of the License.
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.referencing;
017:
018: import java.io.IOException;
019: import java.sql.Connection;
020: import java.sql.ResultSet;
021: import java.sql.SQLException;
022: import java.sql.Statement;
023:
024: import javax.sql.DataSource;
025:
026: import org.geotools.data.DataSourceException;
027: import org.geotools.data.Transaction;
028: import org.geotools.data.jdbc.ConnectionPool;
029: import org.geotools.data.jdbc.JDBCUtils;
030: import org.geotools.data.jdbc.referencing.JDBCAuthorityFactory;
031: import org.geotools.referencing.CRS;
032: import org.opengis.referencing.FactoryException;
033: import org.opengis.referencing.crs.CoordinateReferenceSystem;
034:
035: /**
036: * Access CRS information from the Oracle MD_SYS.CS_SRS view.
037: *
038: * @author Andrea Aime - TOPP
039: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/unsupported/oracle-spatial/src/main/java/org/geotools/data/oracle/referencing/OracleAuthorityFactory.java $
040: */
041: public class OracleAuthorityFactory extends JDBCAuthorityFactory {
042:
043: private String TABLE_NAME = "MDSYS.CS_SRS";
044: private String WKT_COLUMN = "WKTEXT";
045: private String SRID_COLUMN = "SRID";
046: private String AUTH_SRID = "SRID";
047:
048: /**
049: * Construct <code>PostgisAuthorityFactory</code>.
050: *
051: * @param pool
052: */
053: public OracleAuthorityFactory(DataSource dataSource) {
054: super (dataSource);
055: }
056:
057: public CoordinateReferenceSystem createCRS(int srid)
058: throws FactoryException, IOException {
059: Connection dbConnection = null;
060:
061: try {
062: String sqlStatement = "SELECT * FROM " + TABLE_NAME
063: + " WHERE " + SRID_COLUMN + " = " + srid;
064: dbConnection = dataSource.getConnection();
065:
066: Statement statement = dbConnection.createStatement();
067: ResultSet result = statement.executeQuery(sqlStatement);
068:
069: if (result.next()) {
070: CoordinateReferenceSystem crs = null;
071: try {
072: crs = createFromAuthority(result);
073: } catch (Exception e) {
074: // do nothing
075: }
076: if (crs == null) {
077: crs = createFromWKT(result);
078: }
079: JDBCUtils.close(statement);
080:
081: return crs;
082: } else {
083: String mesg = "No row found for " + srid
084: + " in table: " + TABLE_NAME;
085: throw new FactoryException(mesg);
086: }
087: } catch (SQLException sqle) {
088: String message = sqle.getMessage();
089:
090: throw new DataSourceException(message, sqle);
091: } finally {
092: JDBCUtils
093: .close(dbConnection, Transaction.AUTO_COMMIT, null);
094: }
095: }
096:
097: protected CoordinateReferenceSystem createFromWKT(ResultSet result)
098: throws DataSourceException, FactoryException {
099: try {
100: String wkt = result.getString(WKT_COLUMN);
101:
102: return factory.createFromWKT(wkt);
103:
104: } catch (SQLException sqle) {
105: String message = sqle.getMessage();
106:
107: throw new DataSourceException(message, sqle);
108: }
109: }
110:
111: protected CoordinateReferenceSystem createFromAuthority(
112: ResultSet result) throws DataSourceException,
113: FactoryException {
114: try {
115: int id = result.getInt(AUTH_SRID);
116:
117: return CRS.decode("EPSG" + ":" + id);
118: } catch (SQLException sqle) {
119: String message = sqle.getMessage();
120:
121: throw new DataSourceException(message, sqle);
122: }
123: }
124:
125: }
|